Class NamedBeanComboBox<B extends NamedBean>
- java.lang.Object
-
- java.awt.Component
-
- java.awt.Container
-
- javax.swing.JComponent
-
- javax.swing.JComboBox<B>
-
- jmri.swing.NamedBeanComboBox<B>
-
- Type Parameters:
B
- the supported type of NamedBean
- All Implemented Interfaces:
java.awt.event.ActionListener
,java.awt.image.ImageObserver
,java.awt.ItemSelectable
,java.awt.MenuContainer
,java.io.Serializable
,java.util.EventListener
,javax.accessibility.Accessible
,javax.swing.event.ListDataListener
- Direct Known Subclasses:
TurnoutTableJTable.BeanBoxRenderer
public class NamedBeanComboBox<B extends NamedBean> extends javax.swing.JComboBox<B>
AJComboBox
forNamedBean
s.When editable, this will create a new NamedBean if backed by a
ProvidingManager
ifgetSelectedItem()
is called and the current text is neither the system name nor user name of an existing NamedBean. This will also validate input when editable, showing an Information (blue I in circle) icon to indicate a name will be used to create a new Named Bean, an Error (red X in circle) icon to indicate a typed in name cannot be used (either because it would not be valid as a user name or system name or because the name of an existing NamedBean not usable in the current context has been entered, or no icon to indicate the name of an existing Named Bean has been entered.When not editable, this will allow (but may not actively show) continual typing of a system name or a user name by a user to match a NamedBean even if only the system name or user name or both are displayed (e.g. if a list of turnouts is shown by user name only, a user may type in the system name of the turnout and the turnout will be selected correctly). If the typing speed is slower than the
UIManager
'sComboBox.timeFactor
setting, keyboard input acts like a normal JComboBox, with only the first character displayed matching the user input.Note: It is recommended that implementations that exclude some NamedBeans from the combo box call
JComponent.setToolTipText(String)
to provide a context specific reason for excluding those items. The default tool tip reads (example for Turnouts) "Turnouts not shown cannot be used in this context.", but a better tool tip (example for Signal Heads when creating a Signal Mast) may be "Signal Heads not shown are assigned to another Signal Mast."To change the tool tip text shown when an existing bean is not selected, this class should be subclassed and the methods
getBeanInUseMessage(java.lang.String, java.lang.String)
,getInvalidNameFormatMessage(java.lang.String, java.lang.String, java.lang.String)
,getNoMatchingBeanMessage(java.lang.String, java.lang.String)
, andgetWillCreateBeanMessage(java.lang.String, java.lang.String)
should be overridden.- 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 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 NamedBeanComboBox(Manager<B> manager)
Create a ComboBox without a selection using theNamedBean.DisplayOptions.DISPLAYNAME
to sort NamedBeans.NamedBeanComboBox(Manager<B> manager, B selection)
Create a ComboBox with an existing selection using theNamedBean.DisplayOptions.DISPLAYNAME
to sort NamedBeans.NamedBeanComboBox(Manager<B> manager, B selection, NamedBean.DisplayOptions displayOrder)
Create a ComboBox with an existing selection using the specified display order to sort NamedBeans.NamedBeanComboBox(Manager<B> manager, B selection, NamedBean.DisplayOptions displayOrder, java.util.function.Predicate<B> filter)
Create a ComboBox with an existing selection using the specified display order to sort NamedBeans.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
dispose()
java.lang.String
getBeanInUseMessage(java.lang.String beanType, java.lang.String displayName)
Get the localized message to display in a tooltip when a typed in bean name matches a named bean has been included in a call tosetExcludedItems(java.util.Set)
andisValidatingInput()
istrue
.NamedBean.DisplayOptions
getDisplayOrder()
java.util.Set<B>
getExcludedItems()
java.lang.String
getInvalidNameFormatMessage(java.lang.String beanType, java.lang.String text, java.lang.String exception)
Get the localized message to display in a tooltip when a typed in bean name is not a valid name format for creating a bean.Manager<B>
getManager()
java.lang.String
getNoMatchingBeanMessage(java.lang.String beanType, java.lang.String text)
Get the localized message to display when a typed in bean name does not match a named bean,isValidatingInput()
istrue
andisProviding()
isfalse
.B
getSelectedItem()
java.lang.String
getSelectedItemDisplayName()
Get the display name of the selected item.java.lang.String
getSelectedItemSystemName()
Get the system name of the selected item.java.lang.String
getSelectedItemUserName()
Get the user name of the selected item.java.lang.String
getWillCreateBeanMessage(java.lang.String beanType, java.lang.String text)
Get the localized message to display when a typed in bean name does not match a named bean,isValidatingInput()
istrue
andisProviding()
istrue
.boolean
isAllowNull()
Is this JComboBox allowing a null object to be selected?boolean
isProviding()
Check if new NamedBeans can be provided by aProvidingManager
whenJComboBox.isEditable
returnstrue
.boolean
isValidatingInput()
Is this JComboBox validating typed input?void
setAllowNull(boolean allowNull)
Set if this JComboBox allows a null object to be selected.void
setDisplayOrder(NamedBean.DisplayOptions displayOrder)
void
setEditable(boolean editable)
void
setExcludedItems(java.util.Set<B> excludedItems)
Collection of named beans managed by the manager for this combo box that should not be included in the combo box.void
setProviding(boolean providing)
Set if new NamedBeans can be provided by aProvidingManager
whenJComboBox.isEditable()
returnstrue
.void
setSelectedItem(java.lang.Object item)
void
setSelectedItemByName(java.lang.String name)
Set the selected item by either its user name or system name.void
setValidatingInput(boolean validatingInput)
Set if this JComboBox validates typed input.-
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, getModel, getPopupMenuListeners, getPrototypeDisplayValue, getRenderer, getSelectedIndex, getSelectedObjects, getUI, getUIClassID, hidePopup, insertItemAt, installAncestorListener, intervalAdded, intervalRemoved, isEditable, isLightWeightPopupEnabled, isPopupVisible, paramString, processKeyBinding, processKeyEvent, removeActionListener, removeAllItems, removeItem, removeItemAt, removeItemListener, removePopupMenuListener, selectedItemChanged, selectWithKeyChar, setAction, setActionCommand, setEditor, setEnabled, setKeySelectionManager, setLightWeightPopupEnabled, setMaximumRowCount, setModel, setPopupVisible, setPrototypeDisplayValue, setRenderer, setSelectedIndex, 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, getPreferredSize, 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, paintComponent, 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, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMixingCutoutShape, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
-
-
-
-
Constructor Detail
-
NamedBeanComboBox
public NamedBeanComboBox(Manager<B> manager)
Create a ComboBox without a selection using theNamedBean.DisplayOptions.DISPLAYNAME
to sort NamedBeans.- Parameters:
manager
- the Manager backing the ComboBox
-
NamedBeanComboBox
public NamedBeanComboBox(Manager<B> manager, B selection)
Create a ComboBox with an existing selection using theNamedBean.DisplayOptions.DISPLAYNAME
to sort NamedBeans.- Parameters:
manager
- the Manager backing the ComboBoxselection
- the NamedBean that is selected or null to specify no selection
-
NamedBeanComboBox
public NamedBeanComboBox(Manager<B> manager, B selection, NamedBean.DisplayOptions displayOrder)
Create a ComboBox with an existing selection using the specified display order to sort NamedBeans.- Parameters:
manager
- the Manager backing the ComboBoxselection
- the NamedBean that is selected or null to specify no selectiondisplayOrder
- the sorting scheme for NamedBeans
-
NamedBeanComboBox
public NamedBeanComboBox(Manager<B> manager, B selection, NamedBean.DisplayOptions displayOrder, java.util.function.Predicate<B> filter)
Create a ComboBox with an existing selection using the specified display order to sort NamedBeans.- Parameters:
manager
- the Manager backing the ComboBoxselection
- the NamedBean that is selected or null to specify no selectiondisplayOrder
- the sorting scheme for NamedBeansfilter
- the filter or null if no filter
-
-
Method Detail
-
getManager
public Manager<B> getManager()
-
getDisplayOrder
public NamedBean.DisplayOptions getDisplayOrder()
-
setDisplayOrder
public final void setDisplayOrder(NamedBean.DisplayOptions displayOrder)
-
isValidatingInput
public boolean isValidatingInput()
Is this JComboBox validating typed input?- Returns:
- true if validating input; false otherwise
-
setValidatingInput
public void setValidatingInput(boolean validatingInput)
Set if this JComboBox validates typed input.- Parameters:
validatingInput
- true to validate; false to prevent validation
-
isAllowNull
public boolean isAllowNull()
Is this JComboBox allowing a null object to be selected?- Returns:
- true if allowing a null selection; false otherwise
-
setAllowNull
public void setAllowNull(boolean allowNull)
Set if this JComboBox allows a null object to be selected. If so, the null object is placed first in the displayed list of NamedBeans.- Parameters:
allowNull
- true if allowing a null selection; false otherwise
-
getSelectedItem
public B getSelectedItem()
To get the current selection without potentially creating a NamedBean call
JComboBox.getItemAt(int)
withJComboBox.getSelectedIndex()
as the index instead (as ingetItemAt(getSelectedIndex())
).- Overrides:
getSelectedItem
in classjavax.swing.JComboBox<B extends NamedBean>
- Returns:
- the selected item as the supported type of NamedBean, creating a
new NamedBean as needed if
JComboBox.isEditable()
andisProviding()
are true, or null if there is no selection, orisAllowNull()
is true and the null object is selected
-
isProviding
public boolean isProviding()
Check if new NamedBeans can be provided by aProvidingManager
whenJComboBox.isEditable
returnstrue
.- Returns:
true
is allowing new NamedBeans to be provided;false
otherwise
-
setProviding
public void setProviding(boolean providing)
Set if new NamedBeans can be provided by aProvidingManager
whenJComboBox.isEditable()
returnstrue
.- Parameters:
providing
-true
to allow new NamedBeans to be provided;false
otherwise
-
setEditable
public void setEditable(boolean editable)
-
getSelectedItemDisplayName
public java.lang.String getSelectedItemDisplayName()
Get the display name of the selected item.- Returns:
- the display name of the selected item or null if the selected item is null or there is no selection
-
getSelectedItemSystemName
public java.lang.String getSelectedItemSystemName()
Get the system name of the selected item.- Returns:
- the system name of the selected item or null if the selected item is null or there is no selection
-
getSelectedItemUserName
public java.lang.String getSelectedItemUserName()
Get the user name of the selected item.- Returns:
- the user name of the selected item or null if the selected item is null or there is no selection
-
setSelectedItem
public void setSelectedItem(java.lang.Object item)
-
setSelectedItemByName
public void setSelectedItemByName(java.lang.String name)
Set the selected item by either its user name or system name.- Parameters:
name
- the name of the item to select- Throws:
java.lang.IllegalArgumentException
- ifisAllowNull()
is false and no bean exists by name or name is null
-
dispose
public void dispose()
-
getBeanInUseMessage
public java.lang.String getBeanInUseMessage(java.lang.String beanType, java.lang.String displayName)
Get the localized message to display in a tooltip when a typed in bean name matches a named bean has been included in a call tosetExcludedItems(java.util.Set)
andisValidatingInput()
istrue
.- Parameters:
beanType
- the type of bean as provided byManager.getBeanTypeHandled()
displayName
- the bean name as provided byNamedBean.getDisplayName(jmri.NamedBean.DisplayOptions)
with the options ingetDisplayOrder()
- Returns:
- the localized message
-
getInvalidNameFormatMessage
public java.lang.String getInvalidNameFormatMessage(java.lang.String beanType, java.lang.String text, java.lang.String exception)
Get the localized message to display in a tooltip when a typed in bean name is not a valid name format for creating a bean.- Parameters:
beanType
- the type of bean as provided byManager.getBeanTypeHandled()
text
- the typed in nameexception
- the localized message text from the exception thrown byManager.validateSystemNameFormat(java.lang.String, java.util.Locale)
- Returns:
- the localized message
-
getNoMatchingBeanMessage
public java.lang.String getNoMatchingBeanMessage(java.lang.String beanType, java.lang.String text)
Get the localized message to display when a typed in bean name does not match a named bean,isValidatingInput()
istrue
andisProviding()
isfalse
.- Parameters:
beanType
- the type of bean as provided byManager.getBeanTypeHandled()
text
- the typed in name- Returns:
- the localized message
-
getWillCreateBeanMessage
public java.lang.String getWillCreateBeanMessage(java.lang.String beanType, java.lang.String text)
Get the localized message to display when a typed in bean name does not match a named bean,isValidatingInput()
istrue
andisProviding()
istrue
.- Parameters:
beanType
- the type of bean as provided byManager.getBeanTypeHandled()
text
- the typed in name- Returns:
- the localized message
-
getExcludedItems
public java.util.Set<B> getExcludedItems()
-
setExcludedItems
public void setExcludedItems(java.util.Set<B> excludedItems)
Collection of named beans managed by the manager for this combo box that should not be included in the combo box. This may be, for example, a list of SignalHeads already in use, and therefor not available to be added to a SignalMast.- Parameters:
excludedItems
- items to be excluded from this combo box
-
-