Class AutoCompTextField<E>
- java.lang.Object
-
- java.awt.Component
-
- java.awt.Container
-
- javax.swing.JComponent
-
- javax.swing.text.JTextComponent
-
- javax.swing.JTextField
-
- org.openstreetmap.josm.gui.widgets.JosmTextField
-
- org.openstreetmap.josm.gui.tagging.ac.AutoCompTextField<E>
-
- Type Parameters:
E
- the type of items in the model
- All Implemented Interfaces:
ComponentListener
,FocusListener
,KeyListener
,ImageObserver
,MenuContainer
,PropertyChangeListener
,Serializable
,EventListener
,Accessible
,CellEditor
,Scrollable
,SwingConstants
,TableCellEditor
,Destroyable
- Direct Known Subclasses:
AbstractIdTextField
public class AutoCompTextField<E> extends JosmTextField implements TableCellEditor, KeyListener
An auto-completing TextField.When the user starts typing, this textfield will suggest the
best matching item
from its model. The items in the model can be of any type while the items'toString
values are used for autocompletion.- Since:
- 18221
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class javax.swing.JTextField
JTextField.AccessibleJTextField
-
Nested classes/interfaces inherited from class javax.swing.text.JTextComponent
JTextComponent.AccessibleJTextComponent, JTextComponent.DropLocation, JTextComponent.KeyBinding
-
Nested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
-
Nested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainer
-
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
-
-
Field Summary
Fields Modifier and Type Field Description private boolean
AUTOCOMPLETE_NUMBERS
Whether to autocomplete numbersprivate boolean
autocompleteEnabled
true if the combobox should autocompleteprivate MaxLengthDocumentFilter
docFilter
a filter to enforce max.private static Pattern
IS_NUMBER
a regex that matches numbersprotected AutoCompComboBoxModel<E>
model
the modelprivate String
originalValue
private CellEditorSupport
tableCellEditorSupport
-
Fields inherited from class javax.swing.JTextField
notifyAction
-
Fields inherited from class javax.swing.text.JTextComponent
DEFAULT_KEYMAP, FOCUS_ACCELERATOR_KEY
-
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
-
Fields inherited from interface javax.swing.SwingConstants
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST
-
-
Constructor Summary
Constructors Constructor Description AutoCompTextField()
Constructs a newAutoCompTextField
.AutoCompTextField(int columns)
Constructs a newAutoCompTextField
.AutoCompTextField(int columns, boolean undoRedo)
Constructs a newAutoCompTextField
.AutoCompTextField(AutoCompComboBoxModel<E> model)
Constructs a newAutoCompTextField
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addAutoCompListener(AutoCompListener l)
Adds an AutoCompListener.void
addCellEditorListener(CellEditorListener l)
private void
autocomplete(String oldText)
Autocompletes what the user typed in.void
cancelCellEditing()
(package private) void
copyToSysSel(String s)
Copies a String to the UNIX system-wide selection (aka middle-click).protected void
fireAutoCompEvent(int id, Object item)
Notifies all listeners that have registered interest for notification on this event type.AutoCompListener[]
getAutoCompListeners()
Returns an array of all the currentAutoCompListener
s.Object
getCellEditorValue()
AutoCompComboBoxModel<E>
getModel()
Returns theAutoCompComboBoxModel
currently used.Component
getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
protected void
init()
boolean
isAutocompleteEnabled()
Returnstrue
if autocompletion is enabled.boolean
isCellEditable(EventObject anEvent)
void
keyPressed(KeyEvent e)
void
keyReleased(KeyEvent e)
void
keyTyped(KeyEvent e)
Listens to key events and eventually schedules an autocomplete.protected void
rememberOriginalValue(String value)
void
removeAutoCompListener(AutoCompListener l)
Removes the specified AutoCompListener.void
removeCellEditorListener(CellEditorListener l)
protected void
restoreOriginalValue()
boolean
setAutocompleteEnabled(boolean enabled)
Enables or disables the autocompletion.void
setMaxTextLength(int length)
Sets the maximum number of characters allowed.void
setModel(AutoCompComboBoxModel<E> model)
Sets the data model that theAutoCompTextField
uses to obtain the list of items.boolean
shouldSelectCell(EventObject anEvent)
boolean
stopCellEditing()
-
Methods inherited from class org.openstreetmap.josm.gui.widgets.JosmTextField
componentHidden, componentMoved, componentResized, componentShown, destroy, discardAllUndoableEdits, displayHint, drawHint, focusGained, focusLost, getHint, getHintFont, getHintTextColor, getIcon, paintComponent, propertyChange, setComponentOrientation, setHint, setIcon
-
Methods inherited from class javax.swing.JTextField
actionPropertyChanged, addActionListener, configurePropertiesFromAction, createActionPropertyChangeListener, createDefaultModel, fireActionPerformed, getAccessibleContext, getAction, getActionListeners, getActions, getColumns, getColumnWidth, getHorizontalAlignment, getHorizontalVisibility, getPreferredSize, getScrollOffset, getUIClassID, isValidateRoot, paramString, postActionEvent, removeActionListener, scrollRectToVisible, setAction, setActionCommand, setColumns, setDocument, setFont, setHorizontalAlignment, setScrollOffset
-
Methods inherited from class javax.swing.text.JTextComponent
addCaretListener, addInputMethodListener, addKeymap, copy, cut, fireCaretUpdate, getCaret, getCaretColor, getCaretListeners, getCaretPosition, getDisabledTextColor, getDocument, getDragEnabled, getDropLocation, getDropMode, getFocusAccelerator, getHighlighter, getInputMethodRequests, getKeymap, getKeymap, getMargin, getNavigationFilter, getPreferredScrollableViewportSize, getPrintable, getScrollableBlockIncrement, getScrollableTracksViewportHeight, getScrollableTracksViewportWidth, getScrollableUnitIncrement, getSelectedText, getSelectedTextColor, getSelectionColor, getSelectionEnd, getSelectionStart, getText, getText, getToolTipText, getUI, isEditable, loadKeymap, modelToView, modelToView2D, moveCaretPosition, paste, print, print, print, processInputMethodEvent, read, removeCaretListener, removeKeymap, removeNotify, replaceSelection, restoreComposedText, saveComposedText, select, selectAll, setCaret, setCaretColor, setCaretPosition, setDisabledTextColor, setDragEnabled, setDropMode, setEditable, setFocusAccelerator, setHighlighter, setKeymap, setMargin, setNavigationFilter, setSelectedTextColor, setSelectionColor, setSelectionEnd, setSelectionStart, setText, setUI, updateUI, viewToModel, viewToModel2D, write
-
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, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, 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, 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, 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, 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
-
autocompleteEnabled
private boolean autocompleteEnabled
true if the combobox should autocomplete
-
docFilter
private transient MaxLengthDocumentFilter docFilter
a filter to enforce max. text length
-
model
protected AutoCompComboBoxModel<E> model
the model
-
AUTOCOMPLETE_NUMBERS
private final boolean AUTOCOMPLETE_NUMBERS
Whether to autocomplete numbers
-
tableCellEditorSupport
private transient CellEditorSupport tableCellEditorSupport
-
originalValue
private String originalValue
-
-
Constructor Detail
-
AutoCompTextField
public AutoCompTextField()
Constructs a newAutoCompTextField
.
-
AutoCompTextField
public AutoCompTextField(AutoCompComboBoxModel<E> model)
Constructs a newAutoCompTextField
.- Parameters:
model
- the model to use
-
AutoCompTextField
public AutoCompTextField(int columns)
Constructs a newAutoCompTextField
.- Parameters:
columns
- the number of columns to use to calculate the preferred width; if columns is set to zero, the preferred width will be whatever naturally results from the component implementation
-
AutoCompTextField
public AutoCompTextField(int columns, boolean undoRedo)
Constructs a newAutoCompTextField
.- Parameters:
columns
- the number of columns to use to calculate the preferred width; if columns is set to zero, the preferred width will be whatever naturally results from the component implementationundoRedo
- Enables or not Undo/Redo feature. Not recommended for table cell editors, unless each cell provides its own editor
-
-
Method Detail
-
init
protected final void init()
-
getModel
public AutoCompComboBoxModel<E> getModel()
Returns theAutoCompComboBoxModel
currently used.- Returns:
- the model
-
setModel
public void setModel(AutoCompComboBoxModel<E> model)
Sets the data model that theAutoCompTextField
uses to obtain the list of items.- Parameters:
model
- theAutoCompComboBoxModel
that provides the list of items used for autocomplete
-
isAutocompleteEnabled
public final boolean isAutocompleteEnabled()
Returnstrue
if autocompletion is enabled.- Returns:
true
if autocompletion is enabled.
-
setAutocompleteEnabled
public boolean setAutocompleteEnabled(boolean enabled)
Enables or disables the autocompletion.- Parameters:
enabled
-true
to enable autocompletion- Returns:
true
if autocomplete was enabled before calling this
-
setMaxTextLength
public void setMaxTextLength(int length)
Sets the maximum number of characters allowed.- Parameters:
length
- maximum number of characters allowed
-
autocomplete
private void autocomplete(String oldText)
Autocompletes what the user typed in.Gets the user input from the editor, finds the best matching item in the model, sets the editor text to it, and highlights the autocompleted part. If there is no matching item, removes the list selection.
- Parameters:
oldText
- the text before the last keypress was processed
-
copyToSysSel
void copyToSysSel(String s)
Copies a String to the UNIX system-wide selection (aka middle-click).- Parameters:
s
- the string to copy
-
addAutoCompListener
public void addAutoCompListener(AutoCompListener l)
Adds an AutoCompListener.- Parameters:
l
- the AutoComp listener to be added
-
removeAutoCompListener
public void removeAutoCompListener(AutoCompListener l)
Removes the specified AutoCompListener.- Parameters:
l
- the autoComp listener to be removed
-
getAutoCompListeners
public AutoCompListener[] getAutoCompListeners()
Returns an array of all the currentAutoCompListener
s.- Returns:
- all of the
AutoCompListener
s added or an empty array if no listeners have been added
-
fireAutoCompEvent
protected void fireAutoCompEvent(int id, Object item)
Notifies all listeners that have registered interest for notification on this event type. The event instance is lazily created. The listener list is processed in last to first order.- Parameters:
id
- The Autocomp event iditem
- The item selected for autocompletion.- See Also:
EventListenerList
-
keyTyped
public void keyTyped(KeyEvent e)
Listens to key events and eventually schedules an autocomplete.- Specified by:
keyTyped
in interfaceKeyListener
- Parameters:
e
- the key event
-
keyPressed
public void keyPressed(KeyEvent e)
- Specified by:
keyPressed
in interfaceKeyListener
-
keyReleased
public void keyReleased(KeyEvent e)
- Specified by:
keyReleased
in interfaceKeyListener
-
addCellEditorListener
public void addCellEditorListener(CellEditorListener l)
- Specified by:
addCellEditorListener
in interfaceCellEditor
-
rememberOriginalValue
protected void rememberOriginalValue(String value)
-
restoreOriginalValue
protected void restoreOriginalValue()
-
removeCellEditorListener
public void removeCellEditorListener(CellEditorListener l)
- Specified by:
removeCellEditorListener
in interfaceCellEditor
-
cancelCellEditing
public void cancelCellEditing()
- Specified by:
cancelCellEditing
in interfaceCellEditor
-
getCellEditorValue
public Object getCellEditorValue()
- Specified by:
getCellEditorValue
in interfaceCellEditor
-
isCellEditable
public boolean isCellEditable(EventObject anEvent)
- Specified by:
isCellEditable
in interfaceCellEditor
-
shouldSelectCell
public boolean shouldSelectCell(EventObject anEvent)
- Specified by:
shouldSelectCell
in interfaceCellEditor
-
stopCellEditing
public boolean stopCellEditing()
- Specified by:
stopCellEditing
in interfaceCellEditor
-
getTableCellEditorComponent
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
- Specified by:
getTableCellEditorComponent
in interfaceTableCellEditor
-
-