Class JosmComboBox<E>
- java.lang.Object
-
- java.awt.Component
-
- java.awt.Container
-
- javax.swing.JComponent
-
- javax.swing.JComboBox<E>
-
- org.openstreetmap.josm.gui.widgets.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
JosmTextFieldfor 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
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.IntegerconfigMaximumRowCountthe configured maximum row count or nullprivate java.lang.Stringhintgreyed text to display in the editor when the selected value is emptyprivate intpreferredHeightThe preferred height of the combobox when closed.static java.lang.StringPROP_MAXIMUM_ROW_COUNTLimits 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
-
-
Constructor Summary
Constructors Constructor Description JosmComboBox()Creates aJosmComboBoxwith aJosmComboBoxModeldata model.JosmComboBox(E[] items)Creates aJosmComboBoxthat contains the elements in the specified array.JosmComboBox(JosmComboBoxModel<E> aModel)Creates aJosmComboBoxthat takes it items from an existingJosmComboBoxModeldata model.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voiddiscardAllUndoableEdits()Empties the internal undo manager, if any.booleandisplayHint()Return true if the combobox should display the hint text.JosmTextFieldgetEditorComponent()Returns the editor componentjava.lang.StringgetEditorItemAsString()Returns the string representation of current edited item, or null.java.lang.StringgetHint()Returns the hint textjavax.swing.JListgetList()Get the dropdown list componentJosmComboBoxModel<E>getModel()Returns theJosmComboBoxModelcurrently used.java.awt.DimensiongetPreferredSize()Overrides the calculated height.java.lang.StringgetText()Returns the text in the combobox editor.private voidinit()protected voidpaintComponent(java.awt.Graphics g)Draw the hint text for read-only comboboxes.voidpopupMenuCanceled(javax.swing.event.PopupMenuEvent e)voidpopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent e)voidpopupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent ev)Limits the popup height.voidpropertyChange(java.beans.PropertyChangeEvent evt)voidsetComponentOrientation(java.awt.ComponentOrientation o)voidsetEditor(javax.swing.ComboBoxEditor newEditor)java.lang.StringsetHint(java.lang.String newHint)Sets the hint to display when no text has been entered.intsetPreferredHeight(int height)Sets the preferred height of the combobox editor.EsetSelectedItemText(java.lang.String text)Selects an item and/or sets textvoidsetText(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
-
-
-
-
Field Detail
-
PROP_MAXIMUM_ROW_COUNT
public static final java.lang.String PROP_MAXIMUM_ROW_COUNT
Limits the number of rows that this combobox will show.- See Also:
- Constant Field Values
-
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 aJosmComboBoxwith aJosmComboBoxModeldata model. The default data model is an empty list of objects. UseaddItemto add items. By default the first item in the data model becomes selected.
-
JosmComboBox
public JosmComboBox(JosmComboBoxModel<E> aModel)
Creates aJosmComboBoxthat takes it items from an existingJosmComboBoxModeldata model.- Parameters:
aModel- the model that provides the displayed list of items
-
JosmComboBox
public JosmComboBox(E[] items)
Creates aJosmComboBoxthat 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()
-
getModel
public JosmComboBoxModel<E> getModel()
Returns theJosmComboBoxModelcurrently used.- Overrides:
getModelin classjavax.swing.JComboBox<E>- Returns:
- the model or null
-
setEditor
public void setEditor(javax.swing.ComboBoxEditor newEditor)
- Overrides:
setEditorin classjavax.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 textSelects the item whose
toString()equalstext. 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:
setComponentOrientationin classjava.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:
getPreferredSizein classjavax.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)andsetForeground(gray)on theJLabelreturned by the list cell renderer -- unfortunately does not work out well because many UIs change the foreground color or the enabled state of theJLabelafter 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 callmodelToViewandgetBaselineon it, thus obtaining the text coordinates.- Overrides:
paintComponentin classjavax.swing.JComponent- See Also:
BasicComboBoxUI.paintCurrentValue(java.awt.Graphics, java.awt.Rectangle, boolean),SynthLabelUI.paint(java.awt.Graphics, javax.swing.JComponent)
-
discardAllUndoableEdits
public final void discardAllUndoableEdits()
Empties the internal undo manager, if any.Used in the
UploadDialog.- Since:
- 14977
-
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:
popupMenuWillBecomeVisiblein interfacejavax.swing.event.PopupMenuListener- See Also:
BasicComboBoxUI.getAccessibleChild(javax.swing.JComponent, int)
-
popupMenuWillBecomeInvisible
public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent e)
- Specified by:
popupMenuWillBecomeInvisiblein interfacejavax.swing.event.PopupMenuListener
-
popupMenuCanceled
public void popupMenuCanceled(javax.swing.event.PopupMenuEvent e)
- Specified by:
popupMenuCanceledin interfacejavax.swing.event.PopupMenuListener
-
propertyChange
public void propertyChange(java.beans.PropertyChangeEvent evt)
- Specified by:
propertyChangein interfacejava.beans.PropertyChangeListener
-
-