5822N/A * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2362N/A * or visit www.oracle.com if you need additional information or have any 0N/A * Basic UI implementation for JComboBox. 0N/A * The combo box is a compound component which means that it is an agregate of 0N/A * many simpler components. This class creates and manages the listeners 0N/A * on the combo box and the combo box model. These listeners update the user 0N/A * interface in response to changes in the properties and state of the combo box. 0N/A * All event handling is handled by listener classes created with the 0N/A * <code>createxxxListener()</code> methods and internal classes. 0N/A * You can change the behavior of this class by overriding the 0N/A * <code>createxxxListener()</code> methods and supplying your own 0N/A * event listeners or subclassing from the ones supplied in this class. 0N/A * For adding specific actions, 0N/A * overide <code>installKeyboardActions</code> to add actions in response to 0N/A * @author Arnaud Weber 0N/A * @author Tom Santos 0N/A * @author Mark Davidson 0N/A * This protected field is implementation specific. Do not access directly 0N/A // Control the selection behavior of the JComboBox when it is used 0N/A // in the JTable DefaultCellEditor. 0N/A // This list is for drawing the current item in the combo box. 0N/A // Used to render the currently selected item in the combo box. 0N/A // It doesn't have anything to do with the popup's rendering. 0N/A // The implementation of ComboPopup that is used to show the popup. 0N/A // The Component that the ComboBoxEditor uses for editing 0N/A // The arrow button that invokes the popup. 0N/A // Listeners that are attached to the JComboBox 0N/A * This protected field is implementation specific. Do not access directly 0N/A * or override. Override the listener construction method instead. 0N/A * @see #createKeyListener 0N/A * This protected field is implementation specific. Do not access directly 0N/A * or override. Override the listener construction method instead. 0N/A * @see #createFocusListener 0N/A * This protected field is implementation specific. Do not access directly 0N/A * or override. Override the listener construction method instead. 0N/A * @see #createPropertyChangeListener 0N/A * This protected field is implementation specific. Do not access directly 0N/A * or override. Override the listener construction method instead. 0N/A * @see #createItemListener 0N/A // Listeners that the ComboPopup produces. 0N/A // This is used for knowing when to cache the minimum preferred size. 0N/A // If the data in the list changes, the cached value get marked for recalc. 0N/A // Added to the current JComboBox model 0N/A * This protected field is implementation specific. Do not access directly 0N/A * or override. Override the listener construction method instead. 0N/A * @see #createListDataListener 0N/A * Implements all the Listeners needed by this class, all existing 0N/A * listeners redirect to it. 0N/A * The time factor to treate the series of typed alphanumeric key 0N/A * as prefix for first letter navigation. 0N/A * This is tricky, this variables is needed for DefaultKeySelectionManager 0N/A * to take into account time factor. 0N/A * The default key selection manager 0N/A // Flag for recalculating the minimum preferred size. 0N/A // Cached minimum preferred size. 0N/A // Flag for calculating the display size 0N/A // Cached the size that the display needs to render the largest item 0N/A // Key used for lookup of the DefaultListCellRenderer in the AppContext. 0N/A * Whether or not all cells have the same baseline. 1173N/A * Indicates whether or not the combo box button should be square. 1173N/A * If square, then the width and height are equal, and are both set to 1999N/A * the height of the combo minus appropriate insets. 1999N/A * If specified, these insets act as padding around the cell renderer when 1999N/A * laying out and painting the "selected" item in the combo box. These 1999N/A * insets add to those specified by the cell renderer. 0N/A // Used for calculating the default size. 0N/A * Populates ComboBox's actions. 0N/A //======================== 0N/A // begin UI Initialization 0N/A // Is this combo box a cell editor? 0N/A // Leave focus in JComboBox. 0N/A * Installs the default colors, default font, default renderer, and default 0N/A * editor into the JComboBox. 0N/A "ComboBox.background",
0N/A "ComboBox.foreground",
1173N/A //NOTE: this needs to default to true if not specified 1999N/A * Creates and installs listeners for the combo box and its model. 0N/A * This method is called when the UI is installed. 1999N/A * Uninstalls the default colors, default font, default renderer, 1999N/A * and default editor from the combo box. 0N/A "ComboBox.background",
0N/A "ComboBox.foreground",
1999N/A * Removes the installed listeners from the combo box and its model. 0N/A * The number and types of listeners removed and in this method should be 0N/A * the same that was added in <code>installListeners</code> 0N/A * Creates the popup portion of the combo box. 0N/A * @return an instance of <code>ComboPopup</code> 0N/A * Creates a <code>KeyListener</code> which will be added to the 0N/A * combo box. If this method returns null then it will not be added 0N/A * @return an instance <code>KeyListener</code> or null 0N/A * Creates a <code>FocusListener</code> which will be added to the combo box. 0N/A * If this method returns null then it will not be added to the combo box. 0N/A * @return an instance of a <code>FocusListener</code> or null 0N/A * Creates a list data listener which will be added to the 0N/A * <code>ComboBoxModel</code>. If this method returns null then 0N/A * it will not be added to the combo box model. 0N/A * @return an instance of a <code>ListDataListener</code> or null 0N/A * Creates an <code>ItemListener</code> which will be added to the 0N/A * combo box. If this method returns null then it will not 0N/A * be added to the combo box. 0N/A * Subclasses may override this method to return instances of their own 0N/A * ItemEvent handlers. 0N/A * @return an instance of an <code>ItemListener</code> or null 0N/A * Creates a <code>PropertyChangeListener</code> which will be added to 0N/A * the combo box. If this method returns null then it will not 0N/A * be added to the combo box. 0N/A * @return an instance of a <code>PropertyChangeListener</code> or null 0N/A * Creates a layout manager for managing the components which make up the 0N/A * @return an instance of a layout manager 0N/A * Creates the default renderer that will be used in a non-editiable combo 0N/A * box. A default renderer will used only if a renderer has not been 0N/A * explicitly set with <code>setRenderer</code>. 0N/A * @return a <code>ListCellRender</code> used for the combo box 0N/A * @see javax.swing.JComboBox#setRenderer 0N/A * Creates the default editor that will be used in editable combo boxes. 0N/A * A default editor will be used only if an editor has not been 0N/A * explicitly set with <code>setEditor</code>. 0N/A * @return a <code>ComboBoxEditor</code> used for the combo box 0N/A * @see javax.swing.JComboBox#setEditor 0N/A * Returns the shared listener. 0N/A // end UI Initialization 0N/A //====================== 0N/A //====================== 0N/A // begin Inner classes 0N/A * This listener checks to see if the key event isn't a navigation key. If 0N/A * it finds a key event that wasn't a navigation key it dispatches it to 0N/A * JComboBox.selectWithKeyChar() so that it can do type-ahead. 0N/A * This public inner class should be treated as protected. 0N/A * Instantiate it only within subclasses of 0N/A * <code>BasicComboBoxUI</code>. 0N/A * This listener hides the popup when the focus is lost. It also repaints 0N/A * when focus is gained or lost. 0N/A * This public inner class should be treated as protected. 0N/A * Instantiate it only within subclasses of 0N/A * <code>BasicComboBoxUI</code>. 0N/A * This listener watches for changes in the 0N/A * <code>ComboBoxModel</code>. 0N/A * This public inner class should be treated as protected. 0N/A * Instantiate it only within subclasses of 0N/A * <code>BasicComboBoxUI</code>. 0N/A * @see #createListDataListener 0N/A * This listener watches for changes to the selection in the 0N/A * This public inner class should be treated as protected. 0N/A * Instantiate it only within subclasses of 0N/A * <code>BasicComboBoxUI</code>. 0N/A * @see #createItemListener 0N/A // This class used to implement behavior which is now redundant. 0N/A * This listener watches for bound properties that have changed in the 0N/A * Subclasses which wish to listen to combo box property changes should 0N/A * call the superclass methods to ensure that the combo box ui correctly 0N/A * handles property changes. 0N/A * This public inner class should be treated as protected. 0N/A * Instantiate it only within subclasses of 0N/A * <code>BasicComboBoxUI</code>. 0N/A * @see #createPropertyChangeListener 0N/A // Syncronizes the ToolTip text for the components within the combo box to be the 0N/A // same value as the combo box ToolTip text. 0N/A * This layout manager handles the 'standard' layout of combo boxes. It puts 0N/A * the arrow button to the right and the editor to the left. If there is no 0N/A * editor it still keeps the arrow button to the right. 0N/A * This public inner class should be treated as protected. 0N/A * Instantiate it only within subclasses of 0N/A * <code>BasicComboBoxUI</code>. 0N/A // end Inner classes 0N/A //==================== 0N/A //=============================== 0N/A // begin Sub-Component Management 0N/A * Creates and initializes the components which make up the 0N/A * aggregate combo box. This method is called as part of the UI 0N/A * installation process. 0N/A * The aggregate components which compise the combo box are 0N/A * unregistered and uninitialized. This method is called as part of the 0N/A * UI uninstallation process. 0N/A * This public method is implementation specific and should be private. 0N/A * do not call or override. To implement a specific editor create a 0N/A * custom <code>ComboBoxEditor</code> 0N/A * @see #createEditor 0N/A * @see javax.swing.JComboBox#setEditor 0N/A * @see javax.swing.ComboBoxEditor 0N/A // Switch focus to the editor component 0N/A * This public method is implementation specific and should be private. 0N/A * do not call or override. 0N/A * This protected method is implementation specific and should be private. 0N/A * do not call or override. 0N/A // Should be in the same state as the combobox 0N/A * This protected method is implementation specific and should be private. 0N/A * Do not call or override. 0N/A * This public method is implementation specific and should be private. Do 0N/A * not call or override. 0N/A * @see #createArrowButton 0N/A * This public method is implementation specific and should be private. Do 0N/A * not call or override. 0N/A * @see #createArrowButton 1999N/A * Creates a button which will be used as the control to show or hide 0N/A * the popup portion of the combo box. 0N/A * @return a button which represents the popup control 0N/A // end Sub-Component Management 0N/A //=============================== 0N/A //================================ 0N/A // begin ComboBoxUI Implementation 0N/A * Tells if the popup is visible or not. 0N/A * Determines if the JComboBox is focus traversable. If the JComboBox is editable 0N/A * this returns false, otherwise it returns true. 0N/A // end ComboBoxUI Implementation 0N/A //============================== 0N/A //================================= 0N/A // begin ComponentUI Implementation 0N/A * The minumum size is the size of the display area plus insets plus the button. 1173N/A //calculate the width and height of the button 1173N/A //adjust the size based on the button width 0N/A * Returns the baseline. 0N/A * @throws NullPointerException {@inheritDoc} 0N/A * @throws IllegalArgumentException {@inheritDoc} 0N/A * @see javax.swing.JComponent#getBaseline(int, int) 0N/A // force sameBaseline to be updated. 0N/A // Note, we're assuming the baseline is the same for all 0N/A // cells, if not, this needs to loop through all. 0N/A * Returns an enum indicating how the baseline of the component 0N/A * changes as the size changes. 0N/A * @throws NullPointerException {@inheritDoc} 0N/A * @see javax.swing.JComponent#getBaseline(int, int) 0N/A // Force sameBaseline to be updated. 0N/A // Note, we're assuming the baseline is the same for all 0N/A // cells, if not, this needs to loop through all. 0N/A // This is currently hacky... 0N/A // This is currently hacky... 0N/A // end ComponentUI Implementation 0N/A //=============================== 0N/A //====================== 0N/A // begin Utility Methods 0N/A * Returns whether or not the supplied keyCode maps to a key that is used for 0N/A * navigation. This is used for optimizing key input by only passing non- 0N/A * navigation keys to the type-ahead mechanism. Subclasses should override this 0N/A * if they change the navigation keys. 0N/A * Selects the next item in the list. It won't change the selection if the 0N/A * currently selected item is already the last item. 0N/A * Selects the previous item in the list. It won't change the selection if the 0N/A * currently selected item is already the first item. 0N/A * Hides the popup if it is showing and shows the popup if it is hidden. 0N/A * Returns the area that is reserved for drawing the currently selected item. 0N/A * Gets the insets from the JComboBox. 0N/A // end Utility Methods 0N/A //==================== 0N/A //=============================== 0N/A // begin Painting Utility Methods 0N/A * Paints the currently selected item. 0N/A // Fix for 4238829: should lay out the JPanel. 0N/A * Paints the background of the currently selected item. 0N/A "ComboBox.disabledBackground",
null));
0N/A * Repaint the currently selected item. 0N/A // end Painting Utility Methods 0N/A //============================= 0N/A //=============================== 0N/A // begin Size Utility Methods 0N/A * Return the default size of an empty display area of the combo box using 0N/A * the current renderer and font. 0N/A * @return the size of an empty display area 0N/A * @see #getDisplaySize 0N/A // Calculates the height and width using the default text renderer 0N/A * Returns the calculated size of the display area. The display area is the 0N/A * portion of the combo box in which the selected item is displayed. This 0N/A * method will use the prototype display value if it has been set. 0N/A * For combo boxes with a non trivial number of items, it is recommended to 0N/A * use a prototype display value to significantly speed up the display 0N/A * @return the size of the display area calculated from the combo box items 0N/A * @see javax.swing.JComboBox#setPrototypeDisplayValue 0N/A // Calculates the dimension based on the prototype value 0N/A // Calculate the dimension by iterating over all the elements in the combo 0N/A // Calculates the maximum height and width based on the largest 1173N/A // calculate in the padding 0N/A // Set the cached value 1999N/A * Returns the size a component would have if used as a cell renderer. 1999N/A * @param comp a {@code Component} to check 1999N/A * @return size of the component 1999N/A // This has been refactored out in hopes that it may be investigated and 1999N/A // the component to the currentValuePane and changing the font may be 0N/A // end Size Utility Methods 0N/A //============================= 0N/A //================================= 0N/A // begin Keyboard Action Management 0N/A * Adds keyboard actions to the JComboBox. Actions on enter and esc are already 0N/A * supplied. Add more actions as you need them. 0N/A "ComboBox.actionMap");
0N/A "ComboBox.ancestorInputMap");
0N/A * Removes the focus InputMap and ActionMap. 0N/A // Special case in which pressing the arrow keys will not 0N/A // make the popup appear - except for editable combo boxes 0N/A // and combo boxes inside a table. 0N/A // Forces the selection of the list item if the 0N/A // combo box is in a JTable. 0N/A "ComboBox.showPopupOnNavigation",
false)) {
0N/A // Special case in which pressing the arrow keys will not 0N/A // make the popup appear - except for editable combo boxes. 5822N/A // If ComboBox.noActionOnKeyNavigation is set, 5822N/A // forse selection of list item 5822N/A // Forces the selection of the list item 5822N/A // Use the selected value from popup 5822N/A // to set the selected item in combo box, 5822N/A // but ensure before that JComboBox.actionPerformed() 5822N/A // won't use editor's value to set the selected item 608N/A // Hide combo box if it is a table cell editor 0N/A // Call the default button binding. 0N/A // This is a pretty messy way of passing an event through 0N/A // to the root pane. 0N/A // end Keyboard Action Management 0N/A //=============================== 0N/A // Shared Handler, implements all listeners 0N/A // PropertyChangeListener 1173N/A // If the border of the editor changes then this can effect 1173N/A // the size of the editor which can cause the combo's size to 1173N/A // become invalid so we need to clear size caches 0N/A // This listener checks to see if the key event isn't a navigation 0N/A // key. If it finds a key event that wasn't a navigation key it 0N/A // dispatches it to JComboBox.selectWithKeyChar() so that it can do 0N/A // NOTE: The class is added to both the Editor and ComboBox. 0N/A // The combo box listener hides the popup when the focus is lost. 0N/A // It also repaints when focus is gained or lost. 0N/A // This listener watches for changes in the ComboBoxModel 0N/A // set the editor with the selected item since this 0N/A // is the event handler for a selected item change. 0N/A // This layout manager handles the 'standard' layout of combo boxes. 0N/A // It puts the arrow button to the right and the editor to the left. 0N/A // If there is no editor it still keeps the arrow button to the right. 0N/A // Fix for 4515752: Forward the Enter pressed on the 0N/A // editable combo box to the default button 0N/A // Note: This could depend on event ordering. The first ActionEvent 0N/A // from the editor may be handled by the JComboBox in which case, the 0N/A // enterPressed action will always be invoked. 0N/A // Subsequent same key presses move the keyboard focus to the next 0N/A // object that starts with the same letter.