Package org.jdesktop.swingx.autocomplete
Class AutoCompleteDocument
- java.lang.Object
-
- org.jdesktop.swingx.autocomplete.AutoCompleteDocument
-
- All Implemented Interfaces:
javax.swing.text.Document
- Direct Known Subclasses:
AutoCompleteStyledDocument
public class AutoCompleteDocument extends java.lang.Object implements javax.swing.text.DocumentA 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 Classes Modifier and Type Class Description private classAutoCompleteDocument.Handlerprivate static classAutoCompleteDocument.LookupResult
-
Field Summary
Fields Modifier and Type Field Description (package private) AbstractAutoCompleteAdaptoradaptorThe adaptor that is used to find and select items.protected javax.swing.text.Documentdelegateprivate static java.util.Comparator<java.lang.String>EQUALSprivate static java.util.Comparator<java.lang.String>EQUALS_IGNORE_CASEprivate AutoCompleteDocument.Handlerhandler(package private) booleanselectingFlag to indicate if adaptor.setSelectedItem has been called.private static java.util.Comparator<java.lang.String>STARTS_WITHprivate static java.util.Comparator<java.lang.String>STARTS_WITH_IGNORE_CASEprotected booleanstrictMatchingtrue, 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) ObjectToStringConverterstringConverter
-
Constructor Summary
Constructors Constructor Description AutoCompleteDocument(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, javax.swing.text.Document delegate)Creates a new AutoCompleteDocument for the given AbstractAutoCompleteAdaptor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddDocumentListener(javax.swing.event.DocumentListener listener)voidaddUndoableEditListener(javax.swing.event.UndoableEditListener listener)protected javax.swing.text.DocumentcreateDefaultDocument()Creates the default backing document when no delegate is passed to this document.javax.swing.text.PositioncreatePosition(int offs)javax.swing.text.ElementgetDefaultRootElement()javax.swing.text.PositiongetEndPosition()intgetLength()java.lang.ObjectgetProperty(java.lang.Object key)javax.swing.text.Element[]getRootElements()javax.swing.text.PositiongetStartPosition()java.lang.StringgetText(int offset, int length)voidgetText(int offset, int length, javax.swing.text.Segment txt)voidinsertString(int offs, java.lang.String str, javax.swing.text.AttributeSet a)booleanisStrictMatching()Returns if only items from the adaptor's list should be allowed to be entered.private AutoCompleteDocument.LookupResultlookupItem(java.lang.String pattern)Searches for an item that matches the given pattern.private AutoCompleteDocument.LookupResultlookupItem(java.lang.String pattern, java.util.Comparator<java.lang.String> comparator)private AutoCompleteDocument.LookupResultlookupOneItem(java.lang.Object item, java.lang.String pattern, java.util.Comparator<java.lang.String> comparator)voidputProperty(java.lang.Object key, java.lang.Object value)voidremove(int offs, int len)voidremoveDocumentListener(javax.swing.event.DocumentListener listener)voidremoveUndoableEditListener(javax.swing.event.UndoableEditListener listener)voidrender(java.lang.Runnable r)private voidsetSelectedItem(java.lang.Object item, java.lang.String itemAsString)Selects the given item using the AbstractAutoCompleteAdaptor.private voidsetText(java.lang.String text)Sets the text of this AutoCompleteDocument to the given text.
-
-
-
Field Detail
-
strictMatching
protected boolean strictMatching
true, if only items from the adaptors's list can be entered false, otherwise (selected item might not be in the adaptors's list)
-
delegate
protected final javax.swing.text.Document delegate
-
selecting
boolean selecting
Flag 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 adaptor
The adaptor that is used to find and select items.
-
stringConverter
ObjectToStringConverter stringConverter
-
handler
private final AutoCompleteDocument.Handler handler
-
EQUALS_IGNORE_CASE
private static final java.util.Comparator<java.lang.String> EQUALS_IGNORE_CASE
-
STARTS_WITH_IGNORE_CASE
private static final java.util.Comparator<java.lang.String> STARTS_WITH_IGNORE_CASE
-
EQUALS
private static final java.util.Comparator<java.lang.String> EQUALS
-
STARTS_WITH
private static final java.util.Comparator<java.lang.String> STARTS_WITH
-
-
Constructor Detail
-
AutoCompleteDocument
public AutoCompleteDocument(AbstractAutoCompleteAdaptor adaptor, boolean strictMatching, ObjectToStringConverter stringConverter, javax.swing.text.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- theDocumentdelegate 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
public AutoCompleteDocument(AbstractAutoCompleteAdaptor adaptor, boolean strictMatching)
Creates a new AutoCompleteDocument for the given AbstractAutoCompleteAdaptor.- Parameters:
strictMatching- true, if only items from the adaptor's list should be allowed to be enteredadaptor- The adaptor that will be used to find and select matching items.
-
-
Method Detail
-
createDefaultDocument
protected javax.swing.text.Document createDefaultDocument()
Creates the default backing document when no delegate is passed to this document.- Returns:
- the default backing document
-
remove
public void remove(int offs, int len) throws javax.swing.text.BadLocationException- Specified by:
removein interfacejavax.swing.text.Document- Throws:
javax.swing.text.BadLocationException
-
insertString
public void insertString(int offs, java.lang.String str, javax.swing.text.AttributeSet a) throws javax.swing.text.BadLocationException- Specified by:
insertStringin interfacejavax.swing.text.Document- Throws:
javax.swing.text.BadLocationException
-
setText
private void setText(java.lang.String text)
Sets the text of this AutoCompleteDocument to the given text.- Parameters:
text- the text that will be set for this document
-
setSelectedItem
private void setSelectedItem(java.lang.Object item, java.lang.String itemAsString)Selects the given item using the AbstractAutoCompleteAdaptor.- Parameters:
itemAsString- string representation of the item to be selecteditem- the item that is to be selected
-
lookupItem
private AutoCompleteDocument.LookupResult lookupItem(java.lang.String pattern)
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
nullif no item matches
-
lookupOneItem
private AutoCompleteDocument.LookupResult lookupOneItem(java.lang.Object item, java.lang.String pattern, java.util.Comparator<java.lang.String> comparator)
-
lookupItem
private AutoCompleteDocument.LookupResult lookupItem(java.lang.String pattern, java.util.Comparator<java.lang.String> comparator)
-
addDocumentListener
public void addDocumentListener(javax.swing.event.DocumentListener listener)
- Specified by:
addDocumentListenerin interfacejavax.swing.text.Document
-
addUndoableEditListener
public void addUndoableEditListener(javax.swing.event.UndoableEditListener listener)
- Specified by:
addUndoableEditListenerin interfacejavax.swing.text.Document
-
createPosition
public javax.swing.text.Position createPosition(int offs) throws javax.swing.text.BadLocationException- Specified by:
createPositionin interfacejavax.swing.text.Document- Throws:
javax.swing.text.BadLocationException
-
getDefaultRootElement
public javax.swing.text.Element getDefaultRootElement()
- Specified by:
getDefaultRootElementin interfacejavax.swing.text.Document
-
getEndPosition
public javax.swing.text.Position getEndPosition()
- Specified by:
getEndPositionin interfacejavax.swing.text.Document
-
getLength
public int getLength()
- Specified by:
getLengthin interfacejavax.swing.text.Document
-
getProperty
public java.lang.Object getProperty(java.lang.Object key)
- Specified by:
getPropertyin interfacejavax.swing.text.Document
-
getRootElements
public javax.swing.text.Element[] getRootElements()
- Specified by:
getRootElementsin interfacejavax.swing.text.Document
-
getStartPosition
public javax.swing.text.Position getStartPosition()
- Specified by:
getStartPositionin interfacejavax.swing.text.Document
-
getText
public java.lang.String getText(int offset, int length) throws javax.swing.text.BadLocationException- Specified by:
getTextin interfacejavax.swing.text.Document- Throws:
javax.swing.text.BadLocationException
-
getText
public void getText(int offset, int length, javax.swing.text.Segment txt) throws javax.swing.text.BadLocationException- Specified by:
getTextin interfacejavax.swing.text.Document- Throws:
javax.swing.text.BadLocationException
-
putProperty
public void putProperty(java.lang.Object key, java.lang.Object value)- Specified by:
putPropertyin interfacejavax.swing.text.Document
-
removeDocumentListener
public void removeDocumentListener(javax.swing.event.DocumentListener listener)
- Specified by:
removeDocumentListenerin interfacejavax.swing.text.Document
-
removeUndoableEditListener
public void removeUndoableEditListener(javax.swing.event.UndoableEditListener listener)
- Specified by:
removeUndoableEditListenerin interfacejavax.swing.text.Document
-
render
public void render(java.lang.Runnable r)
- Specified by:
renderin interfacejavax.swing.text.Document
-
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
-
-