📄 basiccomboboxui.java
字号:
* @see #addEditor */ public void removeEditor() { if ( editor != null ) { unconfigureEditor(); comboBox.remove( editor ); editor = null; } } /** * This protected method is implementation specific and should be private. * do not call or override. * * @see #addEditor */ protected void configureEditor() { // Should be in the same state as the combobox editor.setEnabled(comboBox.isEnabled()); editor.setFont( comboBox.getFont() ); if ( editor instanceof Accessible ) { AccessibleContext ac = ((Accessible) editor).getAccessibleContext(); if ( ac != null ) { ac.setAccessibleParent(comboBox); } } if (focusListener != null) { editor.addFocusListener(focusListener); } if (editorFocusListener == null) { editorFocusListener = new EditorFocusListener(); } editor.addFocusListener( editorFocusListener ); if (editorActionListener == null) { editorActionListener = new EditorActionListener(); } 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.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 BasicArrowButton(BasicArrowButton.SOUTH, UIManager.getColor("ComboBox.buttonBackground"), UIManager.getColor("ComboBox.buttonShadow"), UIManager.getColor("ComboBox.buttonDarkShadow"), UIManager.getColor("ComboBox.buttonHighlight")); } // // 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 paint( Graphics g, JComponent c ) { hasFocus = comboBox.hasFocus(); if ( !comboBox.isEditable() ) { Rectangle r = rectangleForCurrentValue(); paintCurrentValueBackground(g,r,hasFocus); 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(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(UIManager.getColor("ComboBox.disabledForeground")); c.setBackground(UIManager.getColor("ComboBox.disabledBackground")); } } // 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() )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -