?? basiccomboboxui.java
字號:
} 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 || keyCode == KeyEvent.VK_KP_UP || keyCode == KeyEvent.VK_KP_DOWN; } private boolean isNavigationKey(int keyCode, int modifiers) { InputMap inputMap = comboBox.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); KeyStroke key = KeyStroke.getKeyStroke(keyCode, modifiers); if (inputMap != null && inputMap.get(key) != null) { return true; } return false; } /** * 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(BasicGraphicsUtils.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 ); c.setBackground(UIManager.getColor("ComboBox.background")); } c.setFont(comboBox.getFont()); if ( hasFocus && !isPopupVisible(comboBox) ) { c.setForeground(listBox.getSelectionForeground()); c.setBackground(listBox.getSelectionBackground()); } else { 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)); } } // Fix for 4238829: should lay out the JPanel. boolean shouldValidate = false; if (c instanceof JPanel) { shouldValidate = true; } currentValuePane.paintComponent(g,c,comboBox,bounds.x,bounds.y, bounds.width,bounds.height, shouldValidate); } /** * Paints the background of the currently selected item. */ public void paintCurrentValueBackground(Graphics g,Rectangle bounds,boolean hasFocus) { Color t = g.getColor(); if ( comboBox.isEnabled() ) g.setColor(DefaultLookup.getColor(comboBox, this, "ComboBox.background", null)); else g.setColor(DefaultLookup.getColor(comboBox, this, "ComboBox.disabledBackground", null)); 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); LazyActionMap.installLazyActionMap(comboBox, BasicComboBoxUI.class, "ComboBox.actionMap"); } InputMap getInputMap(int condition) { if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) { return (InputMap)DefaultLookup.get(comboBox, this, "ComboBox.ancestorInputMap"); } return null; } 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 // private static class Actions extends UIAction { private static final String HIDE = "hidePopup"; private static final String DOWN = "selectNext"; private static final String DOWN_2 = "selectNext2"; private static final String TOGGLE = "togglePopup"; private static final String TOGGLE_2 = "spacePopup"; private static final String UP = "selectPrevious"; private static final String UP_2 = "selectPrevious2"; private static final String ENTER = "enterPressed"; private static final String PAGE_DOWN = "pageDownPassThrough"; private static final String PAGE_UP = "pageUpPassThrough"; private static final String HOME = "homePassThrough"; private static final String END = "endPassThrough"; Actions(String name) { super(name); } public void actionPerformed( ActionEvent e ) { String key = getName(); JComboBox comboBox = (JComboBox)e.getSource(); BasicComboBoxUI ui = (BasicComboBoxUI)BasicLookAndFeel.getUIOfType( comboBox.getUI(), BasicComboBoxUI.class); if (key == HIDE) { comboBox.firePopupMenuCanceled(); comboBox.setPopupVisible(false); } else if (key == PAGE_DOWN || key == PAGE_UP || key == HOME || key == END) { int index = getNextIndex(comboBox, key); if (index >= 0 && index < comboBox.getItemCount()) { comboBox.setSelectedIndex(index); } } else if (key == DOWN) { if (comboBox.isShowing() ) { if ( comboBox.isPopupVisible() ) { if (ui != null) { ui.selectNextPossibleValue();
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -