datepickercombo.java

来自「一个SWT日期时间选择控件类」· Java 代码 · 共 1,120 行 · 第 1/3 页

JAVA
1,120
字号
            e.time = event.time;
            notifyListeners(SWT.FocusOut, e);

            break;
        }

        case SWT.MouseDown: {
            if (event.button != 1) {
                return;
            }

            dropDown(false);

            Event e = new Event();
            e.time = event.time;
            notifyListeners(SWT.DefaultSelection, e);

            break;
        }

        case SWT.Selection: {
            // sebthom
            if (!isClosePopupWithSingleMouseClick) {
                Date date = dp.getDate();
                text.setText(getFormat().format(date));
                text.selectAll();

                Event e = new Event();
                e.time = event.time;
                e.stateMask = event.stateMask;
                e.doit = event.doit;
                notifyListeners(SWT.Selection, e);
                event.doit = e.doit;

                break;
            }

            // otherwise perform the code of SWT.MouseDoubleClick
        }

        case SWT.MouseDoubleClick: {
            dropDown(false);

            Date date = dp.getDate();

            // sebthom
            if (date == null) {
                text.setText("");
            } else {
                text.setText(getFormat().format(date));
                text.selectAll();
            }

            Event e = new Event();
            e.time = event.time;
            e.stateMask = event.stateMask;
            e.doit = event.doit;
            notifyListeners(SWT.Selection, e);
            event.doit = e.doit;

            break;
        }

        case SWT.Traverse: {
            switch (event.detail) {
            case SWT.TRAVERSE_TAB_NEXT:
            case SWT.TRAVERSE_RETURN:
            case SWT.TRAVERSE_ESCAPE:
            case SWT.TRAVERSE_ARROW_PREVIOUS:
            case SWT.TRAVERSE_ARROW_NEXT:
                event.doit = false;

                break;
            }

            Event e = new Event();
            e.time = event.time;
            e.detail = event.detail;
            e.doit = event.doit;
            e.keyCode = event.keyCode;
            notifyListeners(SWT.Traverse, e);
            event.doit = e.doit;

            break;
        }

        case SWT.KeyUp: {
            Event e = new Event();
            e.time = event.time;
            e.character = event.character;
            e.keyCode = event.keyCode;
            e.stateMask = event.stateMask;
            notifyListeners(SWT.KeyUp, e);

            break;
        }

        case SWT.KeyDown: {
            if (event.character == SWT.ESC) {
                // escape key cancels popup dp
                dropDown(false);
            }

            if ((event.character == SWT.CR) || (event.character == '\t')) {
                // Enter and Tab cause default selection
                dropDown(false);

                Event e = new Event();
                e.time = event.time;
                e.stateMask = event.stateMask;
                notifyListeners(SWT.DefaultSelection, e);
            }

            //At this point the widget may have been disposed.
            // If so, do not continue.
            if (isDisposed()) {
                break;
            }

            Event e = new Event();
            e.time = event.time;
            e.character = event.character;
            e.keyCode = event.keyCode;
            e.stateMask = event.stateMask;
            notifyListeners(SWT.KeyDown, e);

            break;
        }
        }
    }

    private void dropDown(boolean drop) {
        if (drop == isDropped()) {
            return;
        }

        if (!drop) {
            popup.setVisible(false);
            text.setFocus();

            return;
        }

        Rectangle listRect = dp.getBounds();
        Point point = getParent().toDisplay(getLocation());
        Point comboSize = getSize();
        int width = Math.max(comboSize.x, listRect.width + 2);
        popup.setBounds(point.x, point.y + comboSize.y, width,
            listRect.height + 2);
        popup.setVisible(true);
        dp.setFocus();
    }

    public Control[] getChildren() {
        checkWidget();
        return new Control[0];
    }

    public Date getDate() {
        checkWidget();
        return dp.getDate();
    }

    public boolean getEditable() {
        return text.getEditable();
    }

    /**
     * @author sebthom
     */
    public String getText() {
        return text.getText();
    }

    /**
     * Set uu the date in text mode
     * 
     * @param txt text representation of Date 
     * @deprecated use setDate(date) instead
     */
    public void setText(String txt) {
        text.setText(txt);
    }

    /**
     * Returns the height of Text widget
     *
     * @return height
     */
    public int getTextHeight() {
        checkWidget();
        return text.getLineHeight();
    }

    private void initAccessible() {
        getAccessible().addAccessibleListener(new AccessibleAdapter() {
                public void getHelp(AccessibleEvent e) {
                    e.result = getToolTipText();
                }
            });

        getAccessible().addAccessibleControlListener(new AccessibleControlAdapter() {
                public void getChildAtPoint(AccessibleControlEvent e) {
                    Point testPoint = toControl(new Point(e.x, e.y));

                    if (getBounds().contains(testPoint)) {
                        e.childID = ACC.CHILDID_SELF;
                    }
                }

                public void getChildCount(AccessibleControlEvent e) {
                    e.detail = 0;
                }

                public void getLocation(AccessibleControlEvent e) {
                    Rectangle location = getBounds();
                    Point pt = toDisplay(new Point(location.x, location.y));
                    e.x = pt.x;
                    e.y = pt.y;
                    e.width = location.width;
                    e.height = location.height;
                }

                public void getRole(AccessibleControlEvent e) {
                    e.detail = ACC.ROLE_COMBOBOX;
                }

                public void getState(AccessibleControlEvent e) {
                    e.detail = ACC.STATE_NORMAL;
                }

                public void getValue(AccessibleControlEvent e) {
                    e.result = text.getText();
                }
            });
    }

    private void internalLayout() {
        if (isDropped()) {
            dropDown(false);
        }

        Rectangle rect = getClientArea();
        int width = rect.width;
        int height = rect.height;
        Point arrowSize = arrow.computeSize(SWT.DEFAULT, height);
        text.setBounds(0, 0, width - arrowSize.x, height);
        arrow.setBounds(width - arrowSize.x, 0, arrowSize.x, arrowSize.y);

        Point size = getSize();
        int itemHeight = dp.getBounds().height;
        Point listSize = dp.computeSize(SWT.DEFAULT, itemHeight);
        dp.setBounds(1, 1, Math.max(size.x - 2, listSize.x), listSize.y);
    }

    /**
     * determines if you need to double click a date in the expanded calender control to hide it
     * default is false meaning you have to double click a date
     *
     * @author sebthom
     *
     * @param useSingleMouseClickToCommit
     */
    public boolean isClosePopupWithSingleMouseClick() {
        return isClosePopupWithSingleMouseClick;
    }

    private boolean isDropped() {
        return popup.getVisible();
    }

    /**
     * Retirns true if control is in focus 
     *
     * @return focus state
     */
    public boolean isFocusControl() {
        checkWidget();

        if (text.isFocusControl() || arrow.isFocusControl()
                || dp.isFocusControl() || popup.isFocusControl()) {
            return true;
        } else {
            return super.isFocusControl();
        }
    }

    private void popupEvent(Event event) {
        switch (event.type) {
        case SWT.Paint:

            // draw black rectangle around dp
            Rectangle listRect = dp.getBounds();
            Color black = getDisplay().getSystemColor(SWT.COLOR_BLACK);
            event.gc.setForeground(black);
            event.gc.drawRectangle(0, 0, listRect.width + 1, listRect.height
                + 1);

            break;

        case SWT.Close:
            event.doit = false;
            dropDown(false);

            break;

        case SWT.Deactivate:
            dropDown(false);

            break;
        }
    }

    /**
     * Drawing stuff
     *
     * @param x x
     * @param y y
     * @param width width
     * @param height height
     * @param all must we redraw all of controls? 
     */
    public void redraw(int x, int y, int width, int height, boolean all) {
        checkWidget();

        if (!all) {
            return;
        }

        Point location = text.getLocation();
        text.redraw(x - location.x, y - location.y, width, height, all);
        location = dp.getLocation();
        dp.redraw(x - location.x, y - location.y, width, height, all);

        if (arrow != null) {
            location = arrow.getLocation();
            arrow.redraw(x - location.x, y - location.y, width, height, all);
        }
    }

    /**
     * Removes the listener.
     *
     * @param listener
     *            the listener
     *
     * @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
     *                when called from the wrong thread
     * @exception SWTError(ERROR_WIDGET_DISPOSED)
     *                when the widget has been disposed
     * @exception SWTError(ERROR_NULL_ARGUMENT)
     *                when listener is null
     */
    public void removeModifyListener(ModifyListener listener) {
        checkWidget();

        if (listener == null) {
            SWT.error(SWT.ERROR_NULL_ARGUMENT);
        }

        removeListener(SWT.Modify, listener);
    }

    /**
     * Removes the listener.
     *
     * @param listener
     *            the listener
     *
     * @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
     *                when called from the wrong thread
     * @exception SWTError(ERROR_WIDGET_DISPOSED)
     *                when the widget has been disposed
     * @exception SWTError(ERROR_NULL_ARGUMENT)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?