Package ca.cgjennings.ui
Class AutocompletionDocument
- java.lang.Object
-
- ca.cgjennings.ui.AutocompletionDocument
-
- All Implemented Interfaces:
NestingDocument
,javax.swing.text.Document
public class AutocompletionDocument extends java.lang.Object implements NestingDocument
An adapter for the document in theJTextComponent
of aJComboBox
that extends theJComboBox
to automatically complete the field text using entries from the combo box list.- Author:
- Chris Jennings
-
-
Constructor Summary
Constructors Constructor Description AutocompletionDocument(javax.swing.JComboBox<?> comboBox)
Create a newAutocompletionDocument
that is wrapped around the existing document within a combo box.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addDocumentListener(javax.swing.event.DocumentListener listener)
void
addUndoableEditListener(javax.swing.event.UndoableEditListener listener)
javax.swing.text.Position
createPosition(int offs)
boolean
getAutocompletion()
java.text.Collator
getCollator()
Return the list orderCollator
, ornull
if none is set.javax.swing.JComboBox<?>
getComboBox()
Return theJComboBox
that this document is autocompleting.javax.swing.text.Element
getDefaultRootElement()
javax.swing.text.Document
getDocument()
Return the original document that is being wrapped by this adapter.javax.swing.text.Position
getEndPosition()
int
getLength()
java.lang.Object
getProperty(java.lang.Object key)
javax.swing.text.Element[]
getRootElements()
javax.swing.text.Position
getStartPosition()
java.lang.String
getText(int offset, int length)
void
getText(int offset, int length, javax.swing.text.Segment txt)
void
insertString(int offset, java.lang.String str, javax.swing.text.AttributeSet a)
static AutocompletionDocument
install(javax.swing.JComboBox<?> comboBox)
Create and install anAutocompletionDocument
on an existingJComboBox
, causing it to begin autocompleting the text field content.static AutocompletionDocument
install(javax.swing.JComboBox<?> comboBox, boolean casesensitive)
Create and install anAutocompletionDocument
on an existingJComboBox
, causing it to begin autocompleting the text field content.boolean
isCaseSensitive()
void
putProperty(java.lang.Object key, java.lang.Object value)
void
remove(int offs, int len)
void
removeDocumentListener(javax.swing.event.DocumentListener listener)
void
removeUndoableEditListener(javax.swing.event.UndoableEditListener listener)
void
render(java.lang.Runnable r)
void
setAutocompletion(boolean autocompletion)
void
setCaseSensitive(boolean casesensitive)
void
setCollator(java.text.Collator collator)
If set to a non-null
object, it is assumed that list elements are sorted according tocollator
.
-
-
-
Constructor Detail
-
AutocompletionDocument
public AutocompletionDocument(javax.swing.JComboBox<?> comboBox)
Create a newAutocompletionDocument
that is wrapped around the existing document within a combo box. The combo box's editor must be a subclass ofJTextComponent
.The new document is not installed in the combo box, so autocompletion is not started automatically. To easily create and install an adapter when possible, use
install(JComboBox)
.- Parameters:
comboBox
-- Throws:
java.lang.IllegalArgumentException
- is the combo box's editor is not aJTextComponent
-
-
Method Detail
-
getDocument
public javax.swing.text.Document getDocument()
Return the original document that is being wrapped by this adapter.- Specified by:
getDocument
in interfaceNestingDocument
- Returns:
- the wrapped document
-
getComboBox
public javax.swing.JComboBox<?> getComboBox()
Return theJComboBox
that this document is autocompleting.- Returns:
- the autocompleted combo box
-
install
public static AutocompletionDocument install(javax.swing.JComboBox<?> comboBox)
Create and install anAutocompletionDocument
on an existingJComboBox
, causing it to begin autocompleting the text field content.- Parameters:
comboBox
- theJComboBox
to modify- Returns:
- the newly installed document if installation was successful,
otherwise
null
if it failed because theJComboBox
is not using aJTextComponent
-
install
public static AutocompletionDocument install(javax.swing.JComboBox<?> comboBox, boolean casesensitive)
Create and install anAutocompletionDocument
on an existingJComboBox
, causing it to begin autocompleting the text field content.- Parameters:
comboBox
- theJComboBox
to modifycasesensitive
- whether autocompletion matching should be case sensitive- Returns:
- the newly installed document if installation was successful,
otherwise
null
if it failed because theJComboBox
is not using aJTextComponent
-
remove
public void remove(int offs, int len) throws javax.swing.text.BadLocationException
- Specified by:
remove
in interfacejavax.swing.text.Document
- Throws:
javax.swing.text.BadLocationException
-
insertString
public void insertString(int offset, java.lang.String str, javax.swing.text.AttributeSet a) throws javax.swing.text.BadLocationException
- Specified by:
insertString
in interfacejavax.swing.text.Document
- Throws:
javax.swing.text.BadLocationException
-
getCollator
public java.text.Collator getCollator()
Return the list orderCollator
, ornull
if none is set.- Returns:
- the
Collator
by which elements are ordered
-
setCollator
public void setCollator(java.text.Collator collator)
If set to a non-null
object, it is assumed that list elements are sorted according tocollator
. A faster search algorithm can be employed when looking for an autocomplete match.- Parameters:
collator
- theCollator
by which elements are ordered
-
getLength
public int getLength()
- Specified by:
getLength
in interfacejavax.swing.text.Document
-
addDocumentListener
public void addDocumentListener(javax.swing.event.DocumentListener listener)
- Specified by:
addDocumentListener
in interfacejavax.swing.text.Document
-
removeDocumentListener
public void removeDocumentListener(javax.swing.event.DocumentListener listener)
- Specified by:
removeDocumentListener
in interfacejavax.swing.text.Document
-
addUndoableEditListener
public void addUndoableEditListener(javax.swing.event.UndoableEditListener listener)
- Specified by:
addUndoableEditListener
in interfacejavax.swing.text.Document
-
removeUndoableEditListener
public void removeUndoableEditListener(javax.swing.event.UndoableEditListener listener)
- Specified by:
removeUndoableEditListener
in interfacejavax.swing.text.Document
-
getProperty
public java.lang.Object getProperty(java.lang.Object key)
- Specified by:
getProperty
in interfacejavax.swing.text.Document
-
putProperty
public void putProperty(java.lang.Object key, java.lang.Object value)
- Specified by:
putProperty
in interfacejavax.swing.text.Document
-
getText
public java.lang.String getText(int offset, int length) throws javax.swing.text.BadLocationException
- Specified by:
getText
in 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:
getText
in interfacejavax.swing.text.Document
- Throws:
javax.swing.text.BadLocationException
-
getStartPosition
public javax.swing.text.Position getStartPosition()
- Specified by:
getStartPosition
in interfacejavax.swing.text.Document
-
getEndPosition
public javax.swing.text.Position getEndPosition()
- Specified by:
getEndPosition
in interfacejavax.swing.text.Document
-
createPosition
public javax.swing.text.Position createPosition(int offs) throws javax.swing.text.BadLocationException
- Specified by:
createPosition
in interfacejavax.swing.text.Document
- Throws:
javax.swing.text.BadLocationException
-
getRootElements
public javax.swing.text.Element[] getRootElements()
- Specified by:
getRootElements
in interfacejavax.swing.text.Document
-
getDefaultRootElement
public javax.swing.text.Element getDefaultRootElement()
- Specified by:
getDefaultRootElement
in interfacejavax.swing.text.Document
-
render
public void render(java.lang.Runnable r)
- Specified by:
render
in interfacejavax.swing.text.Document
-
getAutocompletion
public boolean getAutocompletion()
-
setAutocompletion
public void setAutocompletion(boolean autocompletion)
-
isCaseSensitive
public boolean isCaseSensitive()
-
setCaseSensitive
public void setCaseSensitive(boolean casesensitive)
-
-