basiccomboboxui.java
来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 1,343 行 · 第 1/3 页
JAVA
1,343 行
} /** * Creates the component that will be responsible for displaying/editing * the selected item in the combo box. This editor is used only when combo * box is editable. * * @return A new component that will be responsible for displaying/editing * the selected item in the combo box. */ protected ComboBoxEditor createEditor() { return new BasicComboBoxEditor.UIResource(); } /** * Installs the components for this JComboBox. ArrowButton, main * part of combo box (upper part) and popup list of items are created and * configured here. */ protected void installComponents() { // create drop down list of items popup = createPopup(); listBox = popup.getList(); // create and install arrow button arrowButton = createArrowButton(); comboBox.add(arrowButton); if (comboBox.isEditable()) addEditor(); comboBox.add(currentValuePane); } /** * Uninstalls components from this {@link JComboBox}. * * @see #installComponents() */ protected void uninstallComponents() { // uninstall arrow button unconfigureArrowButton(); comboBox.remove(arrowButton); arrowButton = null; popup = null; if (comboBox.getRenderer() instanceof UIResource) comboBox.setRenderer(null); // if the editor is not an instanceof UIResource, it was not set by the // UI delegate, so don't clear it... ComboBoxEditor currentEditor = comboBox.getEditor(); if (currentEditor instanceof UIResource) { comboBox.setEditor(null); editor = null; } } /** * Adds the current editor to the combo box. */ public void addEditor() { removeEditor(); editor = comboBox.getEditor().getEditorComponent(); comboBox.add(editor); } /** * Removes the current editor from the combo box. */ public void removeEditor() { if (editor != null) { unconfigureEditor(); comboBox.remove(editor); } } /** * Configures the editor for this combo box. */ protected void configureEditor() { editor.setFont(comboBox.getFont()); if (popupKeyListener != null) editor.addKeyListener(popupKeyListener); comboBox.configureEditor(comboBox.getEditor(), comboBox.getSelectedItem()); } /** * Unconfigures the editor for this combo nox. This method is not implemented. */ protected void unconfigureEditor() { if (popupKeyListener != null) editor.removeKeyListener(popupKeyListener); } /** * Configures the arrow button. * * @see #configureArrowButton() */ public void configureArrowButton() { if (arrowButton != null) { arrowButton.setEnabled(comboBox.isEnabled()); arrowButton.setFocusable(false); if (popupMouseListener != null) arrowButton.addMouseListener(popupMouseListener); if (popupMouseMotionListener != null) arrowButton.addMouseMotionListener(popupMouseMotionListener); } } /** * Unconfigures the arrow button. * * @see #configureArrowButton() * * @specnote The specification says this method is implementation specific * and should not be used or overridden. */ public void unconfigureArrowButton() { if (arrowButton != null) { if (popupMouseListener != null) arrowButton.removeMouseListener(popupMouseListener); if (popupMouseMotionListener != null) arrowButton.removeMouseMotionListener(popupMouseMotionListener); } } /** * Creates an arrow button for this {@link JComboBox}. The arrow button is * displayed at the right end of the combo box and is used to display/hide * the drop down list of items. * * @return A new button. */ protected JButton createArrowButton() { return new BasicArrowButton(BasicArrowButton.SOUTH); } /** * Returns <code>true</code> if the popup is visible, and <code>false</code> * otherwise. * * @param c The JComboBox to check * * @return <code>true</code> if popup part of the JComboBox is visible and * <code>false</code> otherwise. */ public boolean isPopupVisible(JComboBox c) { return popup.isVisible(); } /** * Displays/hides the {@link JComboBox}'s list of items on the screen. * * @param c The combo box, for which list of items should be * displayed/hidden * @param v true if show popup part of the jcomboBox and false to hide. */ public void setPopupVisible(JComboBox c, boolean v) { if (v) popup.show(); else popup.hide(); } /** * JComboBox is focus traversable if it is editable and not otherwise. * * @param c combo box for which to check whether it is focus traversable * * @return true if focus tranversable and false otherwise */ public boolean isFocusTraversable(JComboBox c) { if (!comboBox.isEditable()) return true; return false; } /** * Paints given menu item using specified graphics context * * @param g The graphics context used to paint this combo box * @param c comboBox which needs to be painted. */ public void paint(Graphics g, JComponent c) { hasFocus = comboBox.hasFocus(); if (! comboBox.isEditable()) { Rectangle rect = rectangleForCurrentValue(); paintCurrentValueBackground(g, rect, hasFocus); paintCurrentValue(g, rect, hasFocus); } } /** * Returns preferred size for the combo box. * * @param c comboBox for which to get preferred size * * @return The preferred size for the given combo box */ public Dimension getPreferredSize(JComponent c) { return getMinimumSize(c); } /** * Returns the minimum size for this {@link JComboBox} for this * look and feel. Also makes sure cachedMinimimSize is setup correctly. * * @param c The {@link JComponent} to find the minimum size for. * * @return The dimensions of the minimum size. */ public Dimension getMinimumSize(JComponent c) { if (isMinimumSizeDirty) { Insets i = getInsets(); Dimension d = getDisplaySize(); d.width += i.left + i.right + d.height; cachedMinimumSize = new Dimension(d.width, d.height + i.top + i.bottom); isMinimumSizeDirty = false; } return new Dimension(cachedMinimumSize); } /** * Returns the maximum size for this {@link JComboBox} for this * look and feel. * * @param c The {@link JComponent} to find the maximum size for * * @return The maximum size (<code>Dimension(32767, 32767)</code>). */ public Dimension getMaximumSize(JComponent c) { return new Dimension(32767, 32767); } public int getAccessibleChildrenCount(JComponent c) { // FIXME: Need to implement return 0; } public Accessible getAccessibleChild(JComponent c, int i) { // FIXME: Need to implement return null; } /** * Returns true if the specified key is a navigation key and false otherwise * * @param keyCode a key for which to check whether it is navigation key or * not. * * @return true if the specified key is a navigation key and false otherwis */ protected boolean isNavigationKey(int keyCode) { return false; } /** * Selects next possible item relative to the current selection * to be next selected item in the combo box. */ protected void selectNextPossibleValue() { int index = comboBox.getSelectedIndex(); if (index != comboBox.getItemCount() - 1) comboBox.setSelectedIndex(index + 1); } /** * Selects previous item relative to current selection to be * next selected item. */ protected void selectPreviousPossibleValue() { int index = comboBox.getSelectedIndex(); if (index != 0) comboBox.setSelectedIndex(index - 1); } /** * Displays combo box popup if the popup is not currently shown * on the screen and hides it if it is currently shown */ protected void toggleOpenClose() { setPopupVisible(comboBox, ! isPopupVisible(comboBox)); } /** * Returns the bounds in which comboBox's selected item will be * displayed. * * @return rectangle bounds in which comboBox's selected Item will be * displayed */ protected Rectangle rectangleForCurrentValue() { int w = comboBox.getWidth(); int h = comboBox.getHeight(); Insets i = comboBox.getInsets(); int arrowSize = h - (i.top + i.bottom); if (arrowButton != null) { arrowSize = arrowButton.getWidth(); } return new Rectangle(i.left, i.top, w - (i.left + i.right + arrowSize), h - (i.top + i.left)); } /** * Returns the insets of the current border. * * @return Insets representing space between combo box and its border */ protected Insets getInsets() { return comboBox.getInsets(); } /** * Paints currently selected value in the main part of the combo * box (part without popup). * * @param g graphics context * @param bounds Rectangle representing the size of the area in which * selected item should be drawn * @param hasFocus true if combo box has focus and false otherwise */ public void paintCurrentValue(Graphics g, Rectangle bounds, boolean hasFocus) { Object currentValue = comboBox.getSelectedItem(); boolean isPressed = arrowButton.getModel().isPressed(); /* Gets the component to be drawn for the current value. * If there is currently no selected item we will take an empty * String as replacement. */ ListCellRenderer renderer = comboBox.getRenderer(); if (comboBox.getSelectedIndex() != -1) { Component comp; if (hasFocus && ! isPopupVisible(comboBox)) { comp = renderer.getListCellRendererComponent(listBox, comboBox.getSelectedItem(), -1, true, false); } else { comp = renderer.getListCellRendererComponent(listBox, comboBox.getSelectedItem(), -1, false, false); Color bg = UIManager.getColor("ComboBox.disabledForeground"); comp.setBackground(bg); } comp.setFont(comboBox.getFont()); if (hasFocus && ! isPopupVisible(comboBox)) { comp.setForeground(listBox.getSelectionForeground()); comp.setBackground(listBox.getSelectionBackground()); } else if (comboBox.isEnabled()) { comp.setForeground(comboBox.getForeground()); comp.setBackground(comboBox.getBackground()); } else { Color fg = UIManager.getColor("ComboBox.disabledForeground"); comp.setForeground(fg); Color bg = UIManager.getColor("ComboBox.disabledBackground"); comp.setBackground(bg); } currentValuePane.paintComponent(g, comp, comboBox, bounds.x, bounds.y, bounds.width, bounds.height); } } /** * Paints the background of part of the combo box, where currently * selected value is displayed. If the combo box has focus this method * should also paint focus rectangle around the combo box. * * @param g graphics context * @param bounds Rectangle representing the size of the largest item in the * comboBox * @param hasFocus true if combo box has fox and false otherwise */ public void paintCurrentValueBackground(Graphics g, Rectangle bounds, boolean hasFocus) { Color saved = g.getColor(); if (comboBox.isEnabled()) { g.setColor(UIManager.getColor("UIManager.background")); } else { g.setColor(UIManager.getColor("UIManager.disabledBackground")); } g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height); g.setColor(saved); } private static final ListCellRenderer DEFAULT_RENDERER = new DefaultListCellRenderer(); /** * Returns the default size for the display area of a combo box that does * not contain any elements. This method returns the width and height of * a single space in the current font, plus a margin of 1 pixel. * * @return The default display size. * * @see #getDisplaySize() */ protected Dimension getDefaultSize() {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?