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
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
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.Integer
configMaximumRowCount
the configured maximum row count or nullprivate java.lang.String
hint
greyed text to display in the editor when the selected value is emptyprivate 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
-
-
Constructor Summary
Constructors Constructor Description JosmComboBox()
Creates aJosmComboBox
with aJosmComboBoxModel
data model.JosmComboBox(E prototypeDisplayValue)
Deprecated, for removal: This API element is subject to removal in a future version.useJComboBox.setPrototypeDisplayValue(E)
instead.JosmComboBox(E[] items)
Creates aJosmComboBox
that contains the elements in the specified array.JosmComboBox(JosmComboBoxModel<E> aModel)
Creates aJosmComboBox
that takes it items from an existingJosmComboBoxModel
data model.JosmComboBox(JosmComboBoxModel<E> aModel, E prototypeDisplayValue)
Deprecated, for removal: This API element is subject to removal in a future version.useJComboBox.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 componentjava.lang.String
getEditorItemAsString()
Returns the string representation of current edited item, or null.java.lang.String
getHint()
Returns the hint textjavax.swing.JList
getList()
Get the dropdown list componentJosmComboBoxModel<E>
getModel()
Returns theJosmComboBoxModel
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 textvoid
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
-
-
-
-
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 aJosmComboBox
with aJosmComboBoxModel
data model. The default data model is an empty list of objects. UseaddItem
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.useJComboBox.setPrototypeDisplayValue(E)
instead.Creates aJosmComboBox
with aJosmComboBoxModel
data model and the specified prototype display value. The default data model is an empty list of objects. UseaddItem
to add items. By default the first item in the data model becomes selected.- Parameters:
prototypeDisplayValue
- theObject
used to compute the maximum number of elements to be displayed at once before displaying a scroll bar- Since:
- 5450
-
JosmComboBox
public JosmComboBox(JosmComboBoxModel<E> aModel)
Creates aJosmComboBox
that takes it items from an existingJosmComboBoxModel
data model.- Parameters:
aModel
- the model that provides the displayed list of items
-
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.useJComboBox.setPrototypeDisplayValue(E)
instead.Creates aJosmComboBox
that takes it items from an existingJosmComboBoxModel
data model and sets the specified prototype display value.- Parameters:
aModel
- the model that provides the displayed list of itemsprototypeDisplayValue
- use this item to size the combobox (may be null)
-
JosmComboBox
public JosmComboBox(E[] items)
Creates aJosmComboBox
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()
-
getModel
public JosmComboBoxModel<E> getModel()
Returns theJosmComboBoxModel
currently used.- Overrides:
getModel
in classjavax.swing.JComboBox<E>
- Returns:
- the model or null
-
setEditor
public void setEditor(javax.swing.ComboBoxEditor newEditor)
- Overrides:
setEditor
in 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:
setComponentOrientation
in 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:
getPreferredSize
in 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 theJLabel
returned by the list cell renderer -- unfortunately does not work out well because many UIs change the foreground color or the enabled state of theJLabel
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 callmodelToView
andgetBaseline
on it, thus obtaining the text coordinates.- Overrides:
paintComponent
in 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:
popupMenuWillBecomeVisible
in interfacejavax.swing.event.PopupMenuListener
- See Also:
BasicComboBoxUI.getAccessibleChild(javax.swing.JComponent, int)
-
popupMenuWillBecomeInvisible
public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent e)
- Specified by:
popupMenuWillBecomeInvisible
in interfacejavax.swing.event.PopupMenuListener
-
popupMenuCanceled
public void popupMenuCanceled(javax.swing.event.PopupMenuEvent e)
- Specified by:
popupMenuCanceled
in interfacejavax.swing.event.PopupMenuListener
-
propertyChange
public void propertyChange(java.beans.PropertyChangeEvent evt)
- Specified by:
propertyChange
in interfacejava.beans.PropertyChangeListener
-
-