windowscomboboxui.java

来自「JAVA 所有包」· Java 代码 · 共 547 行 · 第 1/2 页

JAVA
547
字号
            c.setFont(comboBox.getFont());            if ( comboBox.isEnabled() ) {                c.setForeground(comboBox.getForeground());                c.setBackground(comboBox.getBackground());            } else {                c.setForeground(DefaultLookup.getColor(                         comboBox, this, "ComboBox.disabledForeground", null));                c.setBackground(DefaultLookup.getColor(                         comboBox, this, "ComboBox.disabledBackground", null));            }            boolean shouldValidate = false;            if (c instanceof JPanel)  {                shouldValidate = true;            }            currentValuePane.paintComponent(g, c, comboBox, bounds.x, bounds.y,                                            bounds.width, bounds.height, shouldValidate);                    } else {            super.paintCurrentValue(g, bounds, hasFocus);        }    }    /**     * {@inheritDoc}     * @since 1.6     */    public void paintCurrentValueBackground(Graphics g, Rectangle bounds,                                            boolean hasFocus) {        if (XPStyle.getXP() == null) {            super.paintCurrentValueBackground(g, bounds, hasFocus);        }    }    public Dimension getPreferredSize( JComponent c ) {        Dimension d = super.getPreferredSize(c);        if (XPStyle.getXP() != null) {            d.width += 5;        } else {            d.width += 4;        }        d.height += 2;        return d;    }    /**     * Creates a layout manager for managing the components which make up the      * combo box.     *      * @return an instance of a layout manager     */    protected LayoutManager createLayoutManager() {        return new BasicComboBoxUI.ComboBoxLayoutManager() {	    public void layoutContainer(Container parent) {		super.layoutContainer(parent);		if (XPStyle.getXP() != null && arrowButton != null) {		    Dimension d = parent.getSize();		    Insets insets = getInsets();		    int buttonWidth = arrowButton.getPreferredSize().width;		    arrowButton.setBounds(WindowsGraphicsUtils.isLeftToRight((JComboBox)parent)					  ? (d.width - insets.right - buttonWidth)					  : insets.left,					  insets.top,					  buttonWidth, d.height - insets.top - insets.bottom);		}	    }	};    }    protected void installKeyboardActions() {        super.installKeyboardActions();    }    protected ComboPopup createPopup() {        return super.createPopup();    }    /**     * Creates the default editor that will be used in editable combo boxes.       * A default editor will be used only if an editor has not been      * explicitly set with <code>setEditor</code>.     *     * @return a <code>ComboBoxEditor</code> used for the combo box     * @see javax.swing.JComboBox#setEditor     */    protected ComboBoxEditor createEditor() {	return new WindowsComboBoxEditor();    }    /**      * {@inheritDoc}     * @since 1.6     */    @Override     protected ListCellRenderer createRenderer() {        XPStyle xp = XPStyle.getXP();        if (xp != null && xp.isSkinDefined(comboBox, Part.CP_READONLY)) {            return new WindowsComboBoxRenderer();        } else {            return super.createRenderer();        }    }     /**     * Creates an button which will be used as the control to show or hide     * the popup portion of the combo box.     *     * @return a button which represents the popup control     */    protected JButton createArrowButton() {	if (XPStyle.getXP() != null) {            return new XPComboBoxButton();	} else {	    return super.createArrowButton();	}    }    private class XPComboBoxButton extends XPStyle.GlyphButton {        public XPComboBoxButton() {            super(null,                   (! XPStyle.getXP().isSkinDefined(comboBox, Part.CP_DROPDOWNBUTTONRIGHT))                    ? Part.CP_DROPDOWNBUTTON                   : (comboBox.getComponentOrientation() == ComponentOrientation.RIGHT_TO_LEFT)                     ? Part.CP_DROPDOWNBUTTONLEFT                     : Part.CP_DROPDOWNBUTTONRIGHT                  );	    setRequestFocusEnabled(false);	}           @Override         protected State getState() {            State rv;            rv = super.getState();            if (rv != State.DISABLED                && ! comboBox.isEditable()                 && XPStyle.getXP().isSkinDefined(comboBox,                                                  Part.CP_DROPDOWNBUTTONRIGHT)) {                /*                 * for non editable ComboBoxes Vista seems to have the                 * same glyph for all non DISABLED states                 */                rv = State.NORMAL;            }             return rv;        }        public Dimension getPreferredSize() {            return new Dimension(17, 21);        }        void setPart(Part part) {            setPart(comboBox, part);        }        WindowsComboBoxUI getWindowsComboBoxUI() {            return WindowsComboBoxUI.this;        }    }    /**      * Subclassed to add Windows specific Key Bindings.     * This class is now obsolete and doesn't do anything.      * Only included for backwards API compatibility.     * Do not call or override.     *      * @deprecated As of Java 2 platform v1.4.     */    @Deprecated    protected class WindowsComboPopup extends BasicComboPopup {        public WindowsComboPopup( JComboBox cBox ) {            super( cBox );        }        protected KeyListener createKeyListener() {            return new InvocationKeyHandler();        }        protected class InvocationKeyHandler extends BasicComboPopup.InvocationKeyHandler {	    protected InvocationKeyHandler() {		WindowsComboPopup.this.super();	    }        }    }    /**      * Subclassed to highlight selected item in an editable combo box.     */    public static class WindowsComboBoxEditor        extends BasicComboBoxEditor.UIResource {        /**         * {@inheritDoc}         * @since 1.6         */        protected JTextField createEditorComponent() {            JTextField editor = super.createEditorComponent();            Border border = (Border)UIManager.get("ComboBox.editorBorder");            if (border != null) {                editor.setBorder(border);            }            editor.setOpaque(false);            return editor;        }        public void setItem(Object item) {            super.setItem(item);            if (editor.hasFocus()) {                editor.selectAll();            }        }    }    /**     * Subclassed to set opacity {@code false} on the renderer     * and to show border for focused cells.     */    private static class WindowsComboBoxRenderer           extends BasicComboBoxRenderer.UIResource {        private static final Object BORDER_KEY = new StringBuilder("BORDER_KEY");        private static final Border NULL_BORDER = new EmptyBorder(0, 0, 0, 0);        /**         * {@inheritDoc}         */        @Override         public Component getListCellRendererComponent(                                                 JList list,                                                  Object value,                                                 int index,                                                  boolean isSelected,                                                  boolean cellHasFocus) {            Component rv =                 super.getListCellRendererComponent(list, value, index,                                                    isSelected, cellHasFocus);            if (rv instanceof JComponent) {                JComponent component = (JComponent) rv;                if (index == -1 && isSelected) {                    Border border = component.getBorder();                    Border dashedBorder =                         new WindowsBorders.DashedBorder(list.getForeground());                    component.setBorder(dashedBorder);                    //store current border in client property if needed                    if (component.getClientProperty(BORDER_KEY) == null) {                        component.putClientProperty(BORDER_KEY,                                        (border == null) ? NULL_BORDER : border);                    }                } else {                    if (component.getBorder() instanceof                           WindowsBorders.DashedBorder) {                        Object storedBorder = component.getClientProperty(BORDER_KEY);                        if (storedBorder instanceof Border) {                            component.setBorder(                                (storedBorder == NULL_BORDER) ? null                                     : (Border) storedBorder);                        }                        component.putClientProperty(BORDER_KEY, null);                    }                }                if (index == -1) {                    component.setOpaque(false);                    component.setForeground(list.getForeground());                } else {                    component.setOpaque(true);                }            }            return rv;        }            }}

⌨️ 快捷键说明

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