Class JosmComboBox<E>

  • Type Parameters:
    E - the type of the elements of this combo box
    All Implemented Interfaces:
    java.awt.event.ActionListener, java.awt.image.ImageObserver, java.awt.ItemSelectable, java.awt.MenuContainer, java.beans.PropertyChangeListener, java.io.Serializable, java.util.EventListener, javax.accessibility.Accessible, javax.swing.event.ListDataListener, javax.swing.event.PopupMenuListener
    Direct Known Subclasses:
    AutoCompComboBox, OsmPrimitiveTypesComboBox

    public class JosmComboBox<E>
    extends javax.swing.JComboBox<E>
    implements javax.swing.event.PopupMenuListener, java.beans.PropertyChangeListener
    Base class for all comboboxes in JOSM.

    This combobox will show as many rows as possible without covering the combox itself. It makes sure the list will never go outside the screen (see #7917). You may limit the number of rows shown with the configuration: gui.combobox.maximum-row-count.

    This combobox uses a JosmTextField for its editor component.

    Since:
    5429 (creation), 7015 (generics for Java 7)
    See Also:
    Serialized Form
    • Nested Class Summary

      • Nested classes/interfaces inherited from class javax.swing.JComboBox

        javax.swing.JComboBox.AccessibleJComboBox, javax.swing.JComboBox.KeySelectionManager
      • Nested classes/interfaces inherited from class javax.swing.JComponent

        javax.swing.JComponent.AccessibleJComponent
      • Nested classes/interfaces inherited from class java.awt.Container

        java.awt.Container.AccessibleAWTContainer
      • Nested classes/interfaces inherited from class java.awt.Component

        java.awt.Component.AccessibleAWTComponent, java.awt.Component.BaselineResizeBehavior, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.Integer configMaximumRowCount
      the configured maximum row count or null
      private java.lang.String hint
      greyed text to display in the editor when the selected value is empty
      private int preferredHeight
      The preferred height of the combobox when closed.
      static java.lang.String PROP_MAXIMUM_ROW_COUNT
      Limits the number of rows that this combobox will show.
      • Fields inherited from class javax.swing.JComboBox

        actionCommand, dataModel, editor, isEditable, keySelectionManager, lightWeightPopupEnabled, maximumRowCount, renderer, selectedItemReminder
      • Fields inherited from class javax.swing.JComponent

        listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
      • Fields inherited from class java.awt.Component

        accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
      • Fields inherited from interface java.awt.image.ImageObserver

        ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
    • Constructor Summary

      Constructors 
      Constructor Description
      JosmComboBox()
      Creates a JosmComboBox with a JosmComboBoxModel data model.
      JosmComboBox​(E prototypeDisplayValue)
      Deprecated, for removal: This API element is subject to removal in a future version.
      use JComboBox.setPrototypeDisplayValue(E) instead.
      JosmComboBox​(E[] items)
      Creates a JosmComboBox that contains the elements in the specified array.
      JosmComboBox​(JosmComboBoxModel<E> aModel)
      Creates a JosmComboBox that takes it items from an existing JosmComboBoxModel data model.
      JosmComboBox​(JosmComboBoxModel<E> aModel, E prototypeDisplayValue)
      Deprecated, for removal: This API element is subject to removal in a future version.
      use JComboBox.setPrototypeDisplayValue(E) instead.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void discardAllUndoableEdits()
      Empties the internal undo manager, if any.
      boolean displayHint()
      Return true if the combobox should display the hint text.
      JosmTextField getEditorComponent()
      Returns the editor component
      java.lang.String getEditorItemAsString()
      Returns the string representation of current edited item, or null.
      java.lang.String getHint()
      Returns the hint text
      javax.swing.JList getList()
      Get the dropdown list component
      JosmComboBoxModel<E> getModel()
      Returns the JosmComboBoxModel currently used.
      java.awt.Dimension getPreferredSize()
      Overrides the calculated height.
      java.lang.String getText()
      Returns the text in the combobox editor.
      private void init()  
      protected void paintComponent​(java.awt.Graphics g)
      Draw the hint text for read-only comboboxes.
      void popupMenuCanceled​(javax.swing.event.PopupMenuEvent e)  
      void popupMenuWillBecomeInvisible​(javax.swing.event.PopupMenuEvent e)  
      void popupMenuWillBecomeVisible​(javax.swing.event.PopupMenuEvent ev)
      Limits the popup height.
      void propertyChange​(java.beans.PropertyChangeEvent evt)  
      void setComponentOrientation​(java.awt.ComponentOrientation o)  
      void setEditor​(javax.swing.ComboBoxEditor newEditor)  
      java.lang.String setHint​(java.lang.String newHint)
      Sets the hint to display when no text has been entered.
      int setPreferredHeight​(int height)
      Sets the preferred height of the combobox editor.
      E setSelectedItemText​(java.lang.String text)
      Selects an item and/or sets text
      void setText​(java.lang.String value)
      Sets the text in the combobox editor.
      • Methods inherited from class javax.swing.JComboBox

        actionPerformed, actionPropertyChanged, addActionListener, addItem, addItemListener, addPopupMenuListener, configureEditor, configurePropertiesFromAction, contentsChanged, createActionPropertyChangeListener, createDefaultKeySelectionManager, fireActionEvent, fireItemStateChanged, firePopupMenuCanceled, firePopupMenuWillBecomeInvisible, firePopupMenuWillBecomeVisible, getAccessibleContext, getAction, getActionCommand, getActionListeners, getEditor, getItemAt, getItemCount, getItemListeners, getKeySelectionManager, getMaximumRowCount, getPopupMenuListeners, getPrototypeDisplayValue, getRenderer, getSelectedIndex, getSelectedItem, getSelectedObjects, getUI, getUIClassID, hidePopup, insertItemAt, installAncestorListener, intervalAdded, intervalRemoved, isEditable, isLightWeightPopupEnabled, isPopupVisible, paramString, processKeyBinding, processKeyEvent, removeActionListener, removeAllItems, removeItem, removeItemAt, removeItemListener, removePopupMenuListener, selectedItemChanged, selectWithKeyChar, setAction, setActionCommand, setEditable, setEnabled, setKeySelectionManager, setLightWeightPopupEnabled, setMaximumRowCount, setModel, setPopupVisible, setPrototypeDisplayValue, setRenderer, setSelectedIndex, setSelectedItem, setUI, showPopup, updateUI
      • Methods inherited from class javax.swing.JComponent

        addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
      • Methods inherited from class java.awt.Container

        add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree
      • Methods inherited from class java.awt.Component

        action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, resize, resize, setBounds, setBounds, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMixingCutoutShape, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • configMaximumRowCount

        private java.lang.Integer configMaximumRowCount
        the configured maximum row count or null
      • preferredHeight

        private int preferredHeight
        The preferred height of the combobox when closed. Use if the items in the list dropdown are taller than the item in the editor, as in some comboboxes in the preset dialog. -1 to use the height of the tallest item in the list.
      • hint

        private java.lang.String hint
        greyed text to display in the editor when the selected value is empty
    • Constructor Detail

      • JosmComboBox

        public JosmComboBox()
        Creates a JosmComboBox with a JosmComboBoxModel data model. The default data model is an empty list of objects. Use addItem to add items. By default the first item in the data model becomes selected.
      • JosmComboBox

        @Deprecated(since="18221",
                    forRemoval=true)
        public JosmComboBox​(E prototypeDisplayValue)
        Deprecated, for removal: This API element is subject to removal in a future version.
        use JComboBox.setPrototypeDisplayValue(E) instead.
        Creates a JosmComboBox with a JosmComboBoxModel data model and the specified prototype display value. The default data model is an empty list of objects. Use addItem to add items. By default the first item in the data model becomes selected.
        Parameters:
        prototypeDisplayValue - the Object used to compute the maximum number of elements to be displayed at once before displaying a scroll bar
        Since:
        5450
      • JosmComboBox

        @Deprecated(since="18221",
                    forRemoval=true)
        public JosmComboBox​(JosmComboBoxModel<E> aModel,
                            E prototypeDisplayValue)
        Deprecated, for removal: This API element is subject to removal in a future version.
        use JComboBox.setPrototypeDisplayValue(E) instead.
        Creates a JosmComboBox that takes it items from an existing JosmComboBoxModel data model and sets the specified prototype display value.
        Parameters:
        aModel - the model that provides the displayed list of items
        prototypeDisplayValue - use this item to size the combobox (may be null)
      • JosmComboBox

        public JosmComboBox​(E[] items)
        Creates a JosmComboBox that contains the elements in the specified array. By default the first item in the array (and therefore the data model) becomes selected.
        Parameters:
        items - an array of objects to insert into the combo box
    • Method Detail

      • init

        private void init()
      • setEditor

        public void setEditor​(javax.swing.ComboBoxEditor newEditor)
        Overrides:
        setEditor in class javax.swing.JComboBox<E>
      • getEditorComponent

        public JosmTextField getEditorComponent()
        Returns the editor component
        Returns:
        the editor component
        Since:
        9484
        See Also:
        ComboBoxEditor.getEditorComponent()
      • getEditorItemAsString

        public java.lang.String getEditorItemAsString()
        Returns the string representation of current edited item, or null.
        Returns:
        the string representation of current edited item, or null
        Since:
        18313
      • getText

        public java.lang.String getText()
        Returns the text in the combobox editor.
        Returns:
        the text
        Since:
        18173
        See Also:
        JTextComponent.getText(int, int)
      • setText

        public void setText​(java.lang.String value)
        Sets the text in the combobox editor.
        Parameters:
        value - the text to set
        Since:
        18173
        See Also:
        JTextComponent.setText(java.lang.String)
      • setSelectedItemText

        public E setSelectedItemText​(java.lang.String text)
        Selects an item and/or sets text

        Selects the item whose toString() equals text. If an item could not be found, selects nothing and sets the text anyway.

        Parameters:
        text - the text to select and set
        Returns:
        the item or null
      • getHint

        public java.lang.String getHint()
        Returns the hint text
        Returns:
        the hint text
      • setHint

        public java.lang.String setHint​(java.lang.String newHint)
        Sets the hint to display when no text has been entered.
        Parameters:
        newHint - the hint to set
        Returns:
        the old hint
        Since:
        18221
      • setComponentOrientation

        public void setComponentOrientation​(java.awt.ComponentOrientation o)
        Overrides:
        setComponentOrientation in class java.awt.Component
      • displayHint

        public boolean displayHint()
        Return true if the combobox should display the hint text.
        Returns:
        whether to display the hint text
        Since:
        18221
      • getPreferredSize

        public java.awt.Dimension getPreferredSize()
        Overrides the calculated height. See: setPreferredHeight(int).
        Overrides:
        getPreferredSize in class javax.swing.JComponent
        Since:
        18221
      • setPreferredHeight

        public int setPreferredHeight​(int height)
        Sets the preferred height of the combobox editor.

        A combobox editor is automatically sized to accomodate the widest and the tallest items in the list. In the Preset dialogs we show more of an item in the list than in the editor, so the editor becomes too big. With this method we can set the editor height to a fixed value.

        Set this to -1 to get the default behaviour back.

        See also: #6157

        Parameters:
        height - the preferred height or -1
        Returns:
        the old preferred height
        Since:
        18221
        See Also:
        JComponent.setPreferredSize(java.awt.Dimension)
      • getList

        public javax.swing.JList getList()
        Get the dropdown list component
        Returns:
        the list or null
      • paintComponent

        protected void paintComponent​(java.awt.Graphics g)
        Draw the hint text for read-only comboboxes.

        The obvious way -- to call setText(hint) and setForeground(gray) on the JLabel returned by the list cell renderer -- unfortunately does not work out well because many UIs change the foreground color or the enabled state of the JLabel after the list cell renderer has returned (BasicComboBoxUI). Other UIs don't honor the label color at all (SynthLabelUI).

        We use the same approach as in JosmTextField. The only problem we face is to get the coordinates of the text inside the combobox. Fortunately even read-only comboboxes have a (partially configured) editor component, although they don't use it. We configure that editor just enough to call modelToView and getBaseline on it, thus obtaining the text coordinates.

        Overrides:
        paintComponent in class javax.swing.JComponent
        See Also:
        BasicComboBoxUI.paintCurrentValue(java.awt.Graphics, java.awt.Rectangle, boolean), SynthLabelUI.paint(java.awt.Graphics, javax.swing.JComponent)
      • popupMenuWillBecomeVisible

        public void popupMenuWillBecomeVisible​(javax.swing.event.PopupMenuEvent ev)
        Limits the popup height.

        Limits the popup height to the available screen space either below or above the combobox, whichever is bigger. To find the maximum number of rows that fit the screen, it does the reverse of the calculation done in BasicComboPopup.getPopupLocation().

        Specified by:
        popupMenuWillBecomeVisible in interface javax.swing.event.PopupMenuListener
        See Also:
        BasicComboBoxUI.getAccessibleChild(javax.swing.JComponent, int)
      • popupMenuWillBecomeInvisible

        public void popupMenuWillBecomeInvisible​(javax.swing.event.PopupMenuEvent e)
        Specified by:
        popupMenuWillBecomeInvisible in interface javax.swing.event.PopupMenuListener
      • popupMenuCanceled

        public void popupMenuCanceled​(javax.swing.event.PopupMenuEvent e)
        Specified by:
        popupMenuCanceled in interface javax.swing.event.PopupMenuListener
      • propertyChange

        public void propertyChange​(java.beans.PropertyChangeEvent evt)
        Specified by:
        propertyChange in interface java.beans.PropertyChangeListener