Package org.jdesktop.swingx.autocomplete
Class AutoCompleteDocument
java.lang.Object
org.jdesktop.swingx.autocomplete.AutoCompleteDocument
- All Implemented Interfaces:
Document
- Direct Known Subclasses:
AutoCompleteStyledDocument
A document that can be plugged into any JTextComponent to enable automatic completion.
It finds and selects matching items using any implementation of the AbstractAutoCompleteAdaptor.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate class
private static class
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) AbstractAutoCompleteAdaptor
The adaptor that is used to find and select items.protected final Document
private static final Comparator
<String> private static final Comparator
<String> private final AutoCompleteDocument.Handler
(package private) boolean
Flag to indicate if adaptor.setSelectedItem has been called.private static final Comparator
<String> private static final Comparator
<String> protected boolean
true, if only items from the adaptors's list can be entered false, otherwise (selected item might not be in the adaptors's list)(package private) ObjectToStringConverter
Fields inherited from interface javax.swing.text.Document
StreamDescriptionProperty, TitleProperty
-
Constructor Summary
ConstructorsConstructorDescriptionAutoCompleteDocument
(AbstractAutoCompleteAdaptor adaptor, boolean strictMatching) Creates a new AutoCompleteDocument for the given AbstractAutoCompleteAdaptor.AutoCompleteDocument
(AbstractAutoCompleteAdaptor adaptor, boolean strictMatching, ObjectToStringConverter stringConverter) Creates a new AutoCompleteDocument for the given AbstractAutoCompleteAdaptor.AutoCompleteDocument
(AbstractAutoCompleteAdaptor adaptor, boolean strictMatching, ObjectToStringConverter stringConverter, Document delegate) Creates a new AutoCompleteDocument for the given AbstractAutoCompleteAdaptor. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addDocumentListener
(DocumentListener listener) void
addUndoableEditListener
(UndoableEditListener listener) protected Document
Creates the default backing document when no delegate is passed to this document.createPosition
(int offs) int
getProperty
(Object key) Element[]
getText
(int offset, int length) void
void
insertString
(int offs, String str, AttributeSet a) boolean
Returns if only items from the adaptor's list should be allowed to be entered.lookupItem
(String pattern) Searches for an item that matches the given pattern.lookupItem
(String pattern, Comparator<String> comparator) lookupOneItem
(Object item, String pattern, Comparator<String> comparator) void
putProperty
(Object key, Object value) void
remove
(int offs, int len) void
removeDocumentListener
(DocumentListener listener) void
void
private void
setSelectedItem
(Object item, String itemAsString) Selects the given item using the AbstractAutoCompleteAdaptor.private void
Sets the text of this AutoCompleteDocument to the given text.
-
Field Details
-
strictMatching
protected boolean strictMatchingtrue, if only items from the adaptors's list can be entered false, otherwise (selected item might not be in the adaptors's list) -
delegate
-
selecting
boolean selectingFlag to indicate if adaptor.setSelectedItem has been called. Subsequent calls to remove/insertString should be ignored as they are likely have been caused by the adapted Component that is trying to set the text for the selected component. -
adaptor
AbstractAutoCompleteAdaptor adaptorThe adaptor that is used to find and select items. -
stringConverter
ObjectToStringConverter stringConverter -
handler
-
EQUALS_IGNORE_CASE
-
STARTS_WITH_IGNORE_CASE
-
EQUALS
-
STARTS_WITH
-
-
Constructor Details
-
AutoCompleteDocument
public AutoCompleteDocument(AbstractAutoCompleteAdaptor adaptor, boolean strictMatching, ObjectToStringConverter stringConverter, Document delegate) Creates a new AutoCompleteDocument for the given AbstractAutoCompleteAdaptor.- Parameters:
adaptor
- The adaptor that will be used to find and select matching items.strictMatching
- true, if only items from the adaptor's list should be allowed to be enteredstringConverter
- the converter used to transform items to stringsdelegate
- theDocument
delegate backing this document
-
AutoCompleteDocument
public AutoCompleteDocument(AbstractAutoCompleteAdaptor adaptor, boolean strictMatching, ObjectToStringConverter stringConverter) Creates a new AutoCompleteDocument for the given AbstractAutoCompleteAdaptor.- Parameters:
adaptor
- The adaptor that will be used to find and select matching items.strictMatching
- true, if only items from the adaptor's list should be allowed to be enteredstringConverter
- the converter used to transform items to strings
-
AutoCompleteDocument
Creates a new AutoCompleteDocument for the given AbstractAutoCompleteAdaptor.- Parameters:
adaptor
- The adaptor that will be used to find and select matching items.strictMatching
- true, if only items from the adaptor's list should be allowed to be entered
-
-
Method Details
-
createDefaultDocument
Creates the default backing document when no delegate is passed to this document.- Returns:
- the default backing document
-
remove
- Specified by:
remove
in interfaceDocument
- Throws:
BadLocationException
-
insertString
- Specified by:
insertString
in interfaceDocument
- Throws:
BadLocationException
-
setText
Sets the text of this AutoCompleteDocument to the given text.- Parameters:
text
- the text that will be set for this document
-
setSelectedItem
Selects the given item using the AbstractAutoCompleteAdaptor.- Parameters:
item
- the item that is to be selecteditemAsString
- string representation of the item to be selected
-
lookupItem
Searches for an item that matches the given pattern. The AbstractAutoCompleteAdaptor is used to access the candidate items. The match is not case-sensitive and will only match at the beginning of each item's string representation.- Parameters:
pattern
- the pattern that should be matched- Returns:
- the first item that matches the pattern or
null
if no item matches
-
lookupOneItem
private AutoCompleteDocument.LookupResult lookupOneItem(Object item, String pattern, Comparator<String> comparator) -
lookupItem
-
addDocumentListener
- Specified by:
addDocumentListener
in interfaceDocument
-
addUndoableEditListener
- Specified by:
addUndoableEditListener
in interfaceDocument
-
createPosition
- Specified by:
createPosition
in interfaceDocument
- Throws:
BadLocationException
-
getDefaultRootElement
- Specified by:
getDefaultRootElement
in interfaceDocument
-
getEndPosition
- Specified by:
getEndPosition
in interfaceDocument
-
getLength
public int getLength() -
getProperty
- Specified by:
getProperty
in interfaceDocument
-
getRootElements
- Specified by:
getRootElements
in interfaceDocument
-
getStartPosition
- Specified by:
getStartPosition
in interfaceDocument
-
getText
- Specified by:
getText
in interfaceDocument
- Throws:
BadLocationException
-
getText
- Specified by:
getText
in interfaceDocument
- Throws:
BadLocationException
-
putProperty
- Specified by:
putProperty
in interfaceDocument
-
removeDocumentListener
- Specified by:
removeDocumentListener
in interfaceDocument
-
removeUndoableEditListener
- Specified by:
removeUndoableEditListener
in interfaceDocument
-
render
-
isStrictMatching
public boolean isStrictMatching()Returns if only items from the adaptor's list should be allowed to be entered.- Returns:
- if only items from the adaptor's list should be allowed to be entered
-