⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basiccomboboxui.java

📁 java jdk 1.4的源码
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
            g.setColor(UIManager.getColor("ComboBox.background"));        else            g.setColor(UIManager.getColor("ComboBox.disabledBackground"));        g.fillRect(bounds.x,bounds.y,bounds.width,bounds.height);        g.setColor(t);    }    /**     * 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 {		result = getDefaultSize();		if (comboBox.isEditable()) {		    result.width = 100;		}            }        }	if ( comboBox.isEditable() ) {	    Dimension d = editor.getPreferredSize();	    result.width = Math.max(result.width,d.width);	    result.height = Math.max(result.height,d.height);	}                // Set the cached value        cachedDisplaySize.setSize(result.width, result.height);        isDisplaySizeDirty = false;        return result;    }    /**     * This has been refactored out in hopes that it may be investigated and     * simplified for the next major release. adding/removing     * the component to the currentValuePane and changing the font may be      * redundant operations.     */    private Dimension getSizeForComponent(Component comp) {	currentValuePane.add(comp);	comp.setFont(comboBox.getFont());	Dimension d = comp.getPreferredSize();	currentValuePane.remove(comp);	return d;    }	    //    // end Size Utility Methods    //=============================    //=================================    // begin Keyboard Action Management    //    /**     * Adds keyboard actions to the JComboBox.  Actions on enter and esc are already     * supplied.  Add more actions as you need them.     */    protected void installKeyboardActions() {	InputMap km = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);	SwingUtilities.replaceUIInputMap(comboBox, JComponent.			     WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, km);	ActionMap am = getActionMap();	if (am != null) {	    SwingUtilities.replaceUIActionMap(comboBox, am);	}    }    InputMap getInputMap(int condition) {	if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) {	    return (InputMap)UIManager.get("ComboBox.ancestorInputMap");	}	return null;    }    ActionMap getActionMap() {	return createActionMap();    }    static Action homeAction = new NavigationalAction(KeyEvent.VK_HOME);    static Action endAction = new NavigationalAction(KeyEvent.VK_END);    static Action pgUpAction = new NavigationalAction(KeyEvent.VK_PAGE_UP);    static Action pgDownAction = new NavigationalAction(KeyEvent.VK_PAGE_DOWN);    ActionMap createActionMap() {	ActionMap map = new ActionMapUIResource();	map.put("hidePopup", new HidePopupAction());	map.put("pageDownPassThrough", pgDownAction);	map.put("pageUpPassThrough", pgUpAction);	map.put("homePassThrough", homeAction);	map.put("endPassThrough", endAction);	map.put("selectNext", new DownAction());	map.put("togglePopup", new AltAction());	map.put("spacePopup", new SpaceAction());	map.put("selectPrevious", new UpAction());	map.put("enterPressed", new EnterAction());	return map;    }    boolean isTableCellEditor() {	return isTableCellEditor;    }    /**     * Removes the focus InputMap and ActionMap.     */    protected void uninstallKeyboardActions() {	SwingUtilities.replaceUIInputMap(comboBox, JComponent.				 WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, null);	SwingUtilities.replaceUIActionMap(comboBox, null);    }    //    // Actions    //     class HidePopupAction extends AbstractAction {	public void actionPerformed( ActionEvent e ) {	    JComboBox comboBox = (JComboBox)e.getSource();	    if ( comboBox.isEnabled() ) {		comboBox.firePopupMenuCanceled();		comboBox.setPopupVisible(false);	    }	}	public boolean isEnabled() {	    return comboBox.isPopupVisible();	}    }    static class NavigationalAction extends AbstractAction {        int keyCode;        NavigationalAction(int keyCode) {	    this.keyCode = keyCode;        }	public void actionPerformed(ActionEvent ev) {	    JComboBox comboBox = (JComboBox)ev.getSource();	    int index = getNextIndex(comboBox);            if (index >= 0 && index < comboBox.getItemCount()) {                comboBox.setSelectedIndex(index);            }        }        int getNextIndex(JComboBox comboBox) {            switch (keyCode) {            case KeyEvent.VK_PAGE_UP:                int listHeight = comboBox.getMaximumRowCount();                int index = comboBox.getSelectedIndex() - listHeight;                return (index < 0 ? 0: index);            case KeyEvent.VK_PAGE_DOWN:                listHeight = comboBox.getMaximumRowCount();                index = comboBox.getSelectedIndex() + listHeight;                int max = comboBox.getItemCount();                return (index < max ? index: max-1);            case KeyEvent.VK_HOME:                return 0;            case KeyEvent.VK_END:                return comboBox.getItemCount() - 1;            default:                return comboBox.getSelectedIndex();            }        }    }    static class DownAction extends AbstractAction {	public void actionPerformed(ActionEvent e) {	    JComboBox comboBox = (JComboBox)e.getSource();	    if ( comboBox.isEnabled() && comboBox.isShowing() ) {		if ( comboBox.isPopupVisible() ) {		    BasicComboBoxUI ui = (BasicComboBoxUI)comboBox.getUI();		    ui.selectNextPossibleValue();		} else {		    comboBox.setPopupVisible(true);		}	    }	}    }    static class EnterAction extends AbstractAction {	public void actionPerformed(ActionEvent e) {	    JComboBox comboBox = (JComboBox)e.getSource();	    if ( !comboBox.isEnabled() ) {		return;	    }	    BasicComboBoxUI ui = (BasicComboBoxUI)comboBox.getUI();	    if ( ui.isTableCellEditor() ) {		// Forces the selection of the list item if the		// combo box is in a JTable.		comboBox.setSelectedIndex(ui.popup.getList().getSelectedIndex());	    } 	    else {		if (comboBox.isPopupVisible()) {		    comboBox.setPopupVisible(false);		} else {		    // Call the default button binding.		    // This is a pretty messy way of passing an event through		    // to the root pane.		    JRootPane root = SwingUtilities.getRootPane(comboBox);		    if (root != null) {			InputMap im = root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);			ActionMap am = root.getActionMap();			if (im != null && am != null) {			    Object obj = im.get(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0));			    if (obj != null) {				Action action = am.get(obj);				if (action != null) {				    action.actionPerformed(e);				}			    }			}		    }		}	    }	}    }    static class AltAction extends AbstractAction {	public void actionPerformed(ActionEvent e) {	    JComboBox comboBox = (JComboBox)e.getSource();	    if ( comboBox.isEnabled() ) {		BasicComboBoxUI ui = (BasicComboBoxUI)comboBox.getUI();		if ( ui.isTableCellEditor() ) {		    // Forces the selection of the list item if the		    // combo box is in a JTable.		    comboBox.setSelectedIndex(ui.popup.getList().getSelectedIndex());		} 		else {		    comboBox.setPopupVisible(!comboBox.isPopupVisible());		}            }	}    }    // Same as the AltAction except that it doesn't invoke if    // the space key is pressed in the editable text portion.    static class SpaceAction extends AltAction {	public void actionPerformed(ActionEvent e) {	    JComboBox comboBox = (JComboBox)e.getSource();	    if ( !comboBox.isEditable() ) {		super.actionPerformed(e);            }	}    }        static class UpAction extends AbstractAction {	public void actionPerformed(ActionEvent e) {	    JComboBox comboBox = (JComboBox)e.getSource();	    if ( comboBox.isEnabled() ) {		BasicComboBoxUI ui = (BasicComboBoxUI)comboBox.getUI();		if (ui.isPopupVisible(comboBox)) {                    ui.selectPreviousPossibleValue();                }            }        }    }    //    // end Keyboard Action Management    //===============================    class EditorFocusListener extends FocusAdapter {	/**	 * This will make the comboBox fire an ActionEvent if the editor	 * value is different from the selected item in the model.  	 * This allows for the entering of data in the combo box editor and	 * sends notification when tabbing or clicking out of focus.	 */         public void focusLost( FocusEvent e ) {	    ComboBoxEditor editor = comboBox.getEditor();            Object item = editor.getItem();            if (!e.isTemporary() && item != null && 		!item.equals( comboBox.getSelectedItem())) {                comboBox.actionPerformed                    (new ActionEvent(editor, 0, "",                                     EventQueue.getMostRecentEventTime(), 0));            }        }    }    class EditorActionListener implements ActionListener {	// Fix for 4515752: Forward the Enter pressed on the	// editable combo box to the default button if the item has	// not changed.	// Note: This could depend on event ordering. The first ActionEvent	// from the editor may be handled by the JComboBox in which case, the	// enterPressed action will always be invoked.	public void actionPerformed(ActionEvent evt) {	    Object item = comboBox.getEditor().getItem();	    if (item != null && item.equals(comboBox.getSelectedItem())) {		ActionMap am = comboBox.getActionMap();		if (am != null) {		    Action action = am.get("enterPressed");		    if (action != null) {			action.actionPerformed(new ActionEvent(comboBox, evt.getID(), 							       evt.getActionCommand(),							       evt.getModifiers()));		    }		}	    }	}    }}

⌨️ 快捷键说明

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