synthcomboboxui.java

来自「java jdk 1.4的源码」· Java 代码 · 共 1,736 行 · 第 1/4 页

JAVA
1,736
字号
	}	comboBox.getEditor().addActionListener(editorActionListener);        comboBox.configureEditor(comboBox.getEditor(),comboBox.getSelectedItem());    }    /**     * This protected method is implementation specific and should be private.     * Do not call or override.     *      * @see #addEditor     */    protected void unconfigureEditor() {        if (focusListener != null) {            editor.removeFocusListener(focusListener);        }        if ( editorFocusListener != null )  {            editor.removeFocusListener( editorFocusListener );        }	if ( editorActionListener != null) {	    comboBox.getEditor().removeActionListener(editorActionListener);	}    }    /**     * This public method is implementation specific and should be private. Do     * not call or override.     *     * @see #createArrowButton     */    public void configureArrowButton() {        if ( arrowButton != null ) {            arrowButton.setName("ComboBox.arrowButton");            arrowButton.setEnabled( comboBox.isEnabled() );            arrowButton.setRequestFocusEnabled(false);	    arrowButton.addMouseListener( popup.getMouseListener() );	    arrowButton.addMouseMotionListener( popup.getMouseMotionListener() );            arrowButton.resetKeyboardActions();        }    }    /**     * This public method is implementation specific and should be private. Do     * not call or override.     *     * @see #createArrowButton     */    public void unconfigureArrowButton() {        if ( arrowButton != null ) {	    arrowButton.removeMouseListener( popup.getMouseListener() );	    arrowButton.removeMouseMotionListener( popup.getMouseMotionListener() );        }    }    /**     * 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() {        return new SynthArrowButton(SwingConstants.SOUTH);    }    //    // end Sub-Component Management    //===============================    //================================    // begin ComboBoxUI Implementation    //    /**     * Tells if the popup is visible or not.     */    public boolean isPopupVisible( JComboBox c ) {        return popup.isVisible();    }    /**     * Hides the popup.     */    public void setPopupVisible( JComboBox c, boolean v ) {        if ( v ) {            popup.show();        } else {            popup.hide();        }    }    /**     * Determines if the JComboBox is focus traversable.  If the JComboBox is editable     * this returns false, otherwise it returns true.     */    public boolean isFocusTraversable( JComboBox c ) {        return !comboBox.isEditable();    }    //    // end ComboBoxUI Implementation    //==============================    //=================================    // begin ComponentUI Implementation    public void update(Graphics g, JComponent c) {        SynthContext context = getContext(c);        SynthLookAndFeel.update(context, g);        paint(context, g);        context.dispose();    }    public void paint(Graphics g, JComponent c) {        SynthContext context = getContext(c);        paint(context, g);        context.dispose();    }    protected void paint(SynthContext context, Graphics g) {        hasFocus = comboBox.hasFocus();        if ( !comboBox.isEditable() ) {            Rectangle r = rectangleForCurrentValue();            paintCurrentValue(g,r,hasFocus);        }    }    public Dimension getPreferredSize( JComponent c ) {	return getMinimumSize(c);    }    /**      * The minumum size is the size of the display area plus insets plus the button.     */    public Dimension getMinimumSize( JComponent c ) {        if ( !isMinimumSizeDirty ) {            return new Dimension(cachedMinimumSize);        }        Dimension size = getDisplaySize();        Insets insets = getInsets();        size.height += insets.top + insets.bottom;        int buttonSize = size.height - (insets.top + insets.bottom);        size.width +=  insets.left + insets.right + buttonSize;        cachedMinimumSize.setSize( size.width, size.height );         isMinimumSizeDirty = false;        return new Dimension(size);    }    public Dimension getMaximumSize( JComponent c ) {	return new Dimension(Short.MAX_VALUE, Short.MAX_VALUE);    }    // This is currently hacky...    public int getAccessibleChildrenCount(JComponent c) {        if ( comboBox.isEditable() ) {            return 2;        }        else {            return 1;        }    }    // This is currently hacky...    public Accessible getAccessibleChild(JComponent c, int i) {        // 0 = the popup        // 1 = the editor        switch ( i ) {        case 0:            if ( popup instanceof Accessible ) {                AccessibleContext ac = ((Accessible) popup).getAccessibleContext();                ac.setAccessibleParent(comboBox);                return(Accessible) popup;            }            break;        case 1:            if ( comboBox.isEditable()                  && (editor instanceof Accessible) ) {                AccessibleContext ac = ((Accessible) editor).getAccessibleContext();                ac.setAccessibleParent(comboBox);                return(Accessible) editor;            }            break;        }        return null;    }    //    // end ComponentUI Implementation    //===============================    //======================    // begin Utility Methods    //    /**     * Returns whether or not the supplied keyCode maps to a key that is used for     * navigation.  This is used for optimizing key input by only passing non-     * navigation keys to the type-ahead mechanism.  Subclasses should override this     * if they change the navigation keys.     */    protected boolean isNavigationKey( int keyCode ) {        return keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_DOWN ||        // This is horrible, but necessary since these aren't         // supported until JDK 1.2        keyCode == KeyStroke.getKeyStroke("KP_UP").getKeyCode() ||         keyCode == KeyStroke.getKeyStroke("KP_DOWN").getKeyCode();    }      /**     * Selects the next item in the list.  It won't change the selection if the     * currently selected item is already the last item.     */    protected void selectNextPossibleValue() {        int si;        if ( isTableCellEditor ) {            si = listBox.getSelectedIndex();        }        else {            si = comboBox.getSelectedIndex();        }        if ( si < comboBox.getModel().getSize() - 1 ) {            if ( isTableCellEditor ) {                listBox.setSelectedIndex( si + 1 );                listBox.ensureIndexIsVisible( si + 1 );            }            else {                comboBox.setSelectedIndex(si+1);            }            comboBox.repaint();        }    }    /**     * Selects the previous item in the list.  It won't change the selection if the     * currently selected item is already the first item.     */    protected void selectPreviousPossibleValue() {        int si;        if ( isTableCellEditor ) {            si = listBox.getSelectedIndex();        }        else {            si = comboBox.getSelectedIndex();        }        if ( si > 0 ) {            if ( isTableCellEditor ) {                listBox.setSelectedIndex( si - 1 );                listBox.ensureIndexIsVisible( si - 1 );            }            else {                comboBox.setSelectedIndex(si-1);            }            comboBox.repaint();        }    }    /**     * Hides the popup if it is showing and shows the popup if it is hidden.     */    protected void toggleOpenClose() {        setPopupVisible(comboBox, !isPopupVisible(comboBox));    }    /**     * Returns the area that is reserved for drawing the currently selected item.     */    protected Rectangle rectangleForCurrentValue() {        int width = comboBox.getWidth();        int height = comboBox.getHeight();        Insets insets = getInsets();        int buttonSize = height - (insets.top + insets.bottom);	if ( arrowButton != null ) {            buttonSize = arrowButton.getWidth();	}	if(SynthLookAndFeel.isLeftToRight(comboBox)) {	    return new Rectangle(insets.left, insets.top,			     width - (insets.left + insets.right + buttonSize),                             height - (insets.top + insets.bottom));	}	else {	    return new Rectangle(insets.left + buttonSize, insets.top,			     width - (insets.left + insets.right + buttonSize),                             height - (insets.top + insets.bottom));	}    }    /**     * Gets the insets from the JComboBox.     */    protected Insets getInsets() {        return comboBox.getInsets();    }    //    // end Utility Methods    //====================    //===============================    // begin Painting Utility Methods    //    /**     * Paints the currently selected item.     */    public void paintCurrentValue(Graphics g,Rectangle bounds,boolean hasFocus) {        ListCellRenderer renderer = comboBox.getRenderer();        Component c;        if ( hasFocus && !isPopupVisible(comboBox) ) {            c = renderer.getListCellRendererComponent( listBox,                                                       comboBox.getSelectedItem(),                                                       -1,                                                       true,                                                       false );        }        else {            c = renderer.getListCellRendererComponent( listBox,                                                       comboBox.getSelectedItem(),                                                       -1,                                                       false,                                                       false );        }        // Fix for 4238829: should lay out the JPanel.        boolean shouldValidate = false;        if (c instanceof JPanel)  {            shouldValidate = true;        }        if (c instanceof UIResource) {            c.setName("ComboBox.renderer");            currentValuePane.paintComponent(g,c,comboBox,bounds.x,bounds.y,                                        bounds.width,bounds.height, shouldValidate);        }        else {            currentValuePane.paintComponent(g,c,comboBox,bounds.x,bounds.y,                                        bounds.width,bounds.height, shouldValidate);        }    }    /**     * Repaint the currently selected item.     */    void repaintCurrentValue() {        Rectangle r = rectangleForCurrentValue();        comboBox.repaint(r.x,r.y,r.width,r.height);    }    //    // end Painting Utility Methods    //=============================    //===============================    // begin Size Utility Methods    //    /**      * Return the default size of an empty display area of the combo box using     * the current renderer and font.     *     * @return the size of an empty display area     * @see #getDisplaySize     */    protected Dimension getDefaultSize() {	// Calculates the height and width using the default text renderer	Dimension d = getSizeForComponent(getDefaultListCellRenderer().getListCellRendererComponent(listBox, " ", -1, false, false));        return new Dimension(d.width, d.height);    }    /**      * Returns the calculated size of the display area. The display area is the     * portion of the combo box in which the selected item is displayed. This      * method will use the prototype display value if it has been set.      * <p>     * For combo boxes with a non trivial number of items, it is recommended to     * use a prototype display value to significantly speed up the display      * size calculation.     *      * @return the size of the display area calculated from the combo box items     * @see javax.swing.JComboBox#setPrototypeDisplayValue     */    protected Dimension getDisplaySize() {        if (!isDisplaySizeDirty)  {            return new Dimension(cachedDisplaySize);        }	Dimension result = new Dimension();                ListCellRenderer renderer = comboBox.getRenderer();        if (renderer == null)  {            renderer = new DefaultListCellRenderer();        }        Object prototypeValue = comboBox.getPrototypeDisplayValue();        if (prototypeValue != null)  {            // Calculates the dimension based on the prototype value            result = getSizeForComponent(renderer.getListCellRendererComponent(listBox, 									       prototypeValue,									       -1, false, false));        } else {            // Calculate the dimension by iterating over all the elements in the combo            // box list.            ComboBoxModel model = comboBox.getModel();            int modelSize = model.getSize();	    Dimension d;            Component cpn;                        if (modelSize > 0 ) {                for (int i = 0; i < modelSize ; i++ ) {                    // Calculates the maximum height and width based on the largest                    // element                    d = getSizeForComponent(renderer.getListCellRendererComponent(listBox, 										  model.getElementAt(i),										  -1, false, false));                    result.width = Math.max(result.width,d.width);                    result.height = Math.max(result.height,d.height);                }            } else {

⌨️ 快捷键说明

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