basiccomboboxui.java
来自「Mac OS X 10.4.9 for x86 Source Code gcc」· Java 代码 · 共 1,239 行 · 第 1/3 页
JAVA
1,239 行
* * @return render for the combo box */ protected ListCellRenderer createRenderer() { return new BasicComboBoxRenderer(); } /** * Creates component that will be responsible for displaying/editting * selected item in the combo box. This editor is used only when combo box * is editable. * * @return component that will be responsible for displaying/editting * selected item in the combo box. */ protected ComboBoxEditor createEditor() { return new BasicComboBoxEditor(); } /** * This method installs 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 and install arrow button arrowButton = createArrowButton(); comboBox.add(arrowButton); // Set list that will be used by BasicComboBoxRender // in order to determine the right colors when rendering listBox = new JList(); Color background = arrowButton.getBackground(); listBox.setBackground(background); listBox.setSelectionBackground(background.darker()); Color foreground = arrowButton.getForeground(); listBox.setForeground(foreground); listBox.setSelectionForeground(foreground); // set editor and renderer for the combo box. Editor is used // only if combo box becomes editable, otherwise renderer is used // to paint the selected item; combobox is not editable by default. comboBox.setRenderer(createRenderer()); comboBox.setEditor(createEditor()); editor = comboBox.getEditor().getEditorComponent(); // create drop down list of items popup = createPopup(); comboBox.revalidate(); } /** * This method uninstalls components from this JComboBox */ protected void uninstallComponents() { // uninstall arrow button unconfigureArrowButton(); comboBox.remove(arrowButton); arrowButton = null; listBox = null; popup = null; comboBox.setRenderer(null); comboBox.setEditor(null); editor = null; } /** * This method adds editor to the combo box */ public void addEditor() { comboBox.add(editor); } /** * This method removes editor from the combo box */ public void removeEditor() { comboBox.remove(editor); } /** * This method configures editor for this combo box. */ protected void configureEditor() { // FIXME: Need to implement. Set font and add listeners. } /** * This method removes all the listeners for the editor. */ protected void unconfigureEditor() { // FIXME: Need to implement } /** * This method adds listeners to the arrow button part of the combo box. */ public void configureArrowButton() { arrowButton.addMouseListener(mouseListener); } /** * This method removes listeners from the arrow button part of the combo * box. */ public void unconfigureArrowButton() { arrowButton.removeMouseListener(mouseListener); } /** * This method create arrow button for this JComboBox. Arrow button is * responsible for displaying / hiding drop down list of items when it is * clicked. * * @return JButton arrow button for this JComboBox. */ protected JButton createArrowButton() { return new BasicArrowButton(BasicArrowButton.SOUTH); } /** * This method checks if popup part of the combo box is visible on the * screen * * @param c The JComboBox to check * * @return true if popup part of the JComboBox is visible and false * otherwise. */ public boolean isPopupVisible(JComboBox c) { return popup.isVisible(); } /** * Displays/Hides 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) { if (c instanceof JComboBox) { JComboBox cb = (JComboBox) c; paintBorder(g, comboBox.getBounds(), hasFocus); Rectangle rect = rectangleForCurrentValue(); paintCurrentValueBackground(g, rect, hasFocus); paintCurrentValue(g, rect, hasFocus); } } private void paintBorder(Graphics g, Rectangle bounds, boolean hasFocus) { int x = 0; int y = 0; int width = bounds.width; int height = bounds.height; Color oldColor = g.getColor(); if (! arrowButton.getModel().isPressed()) BasicGraphicsUtils.drawEtchedRect(g, x, y, width, height, Color.gray, Color.white, Color.gray, Color.white); else { g.setColor(darkShadow); g.drawRect(x, y, width, height); g.setColor(shadow); g.drawRect(x + 1, y + 1, width - 3, height - 3); } g.setColor(oldColor); } /** * Returns preferred size for the given menu item. * * @param c comboBox for which to get preferred size * * @return $Dimension$ preferred size for the given combo box */ public Dimension getPreferredSize(JComponent c) { // return null to indicate that combo box's layout will determin its // preferred size return null; } /** * This method returns the minimum size for this {@link JComboBox} for this * look and feel. * * @param c The {@link JComponent} to find the minimum size for. * * @return The dimensions of the minimum size. */ public Dimension getMinimumSize(JComponent c) { return null; } /** * This method 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 dimensions of the minimum size. */ public Dimension getMaximumSize(JComponent c) { return null; } 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; } /** * This method 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); } /** * This method 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); } /** * This method 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)); } /** * This method returns 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() { Rectangle cbBounds = comboBox.getBounds(); // Subtract width or the arrow button and border insets Rectangle rectForCurrentValue = new Rectangle(cbBounds.x + borderInsets.left, cbBounds.y + borderInsets.top, cbBounds.width - arrowButtonWidth - borderInsets.left - borderInsets.right, cbBounds.height - borderInsets.top - borderInsets.bottom); return rectForCurrentValue; } /** * This method returns insets of the current border. * * @return Insets representing space between combo box and its border */ protected Insets getInsets() { return new Insets(0, 0, 0, 0); } /** * This method 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) { if (! comboBox.isEditable()) { 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. */ Component comp = comboBox.getRenderer() .getListCellRendererComponent(listBox, (currentValue != null ? currentValue : ""), -1, isPressed, hasFocus); if (! comboBox.isEnabled()) comp.setEnabled(false); g.translate(borderInsets.left, borderInsets.top); comp.setBounds(0, 0, bounds.width, bounds.height); comp.paint(g); g.translate(-borderInsets.left, -borderInsets.top); comboBox.revalidate(); } else comboBox.getEditor().setItem(comboBox.getSelectedItem()); } /** * This method paints 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) { // background is painted by renderer, so it seems that nothing // should be done here.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?