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 theJTextComponentof aJComboBoxthat extends theJComboBoxto 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 newAutocompletionDocumentthat 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 voidaddDocumentListener(javax.swing.event.DocumentListener listener)voidaddUndoableEditListener(javax.swing.event.UndoableEditListener listener)javax.swing.text.PositioncreatePosition(int offs)booleangetAutocompletion()java.text.CollatorgetCollator()Return the list orderCollator, ornullif none is set.javax.swing.JComboBox<?>getComboBox()Return theJComboBoxthat this document is autocompleting.javax.swing.text.ElementgetDefaultRootElement()javax.swing.text.DocumentgetDocument()Return the original document that is being wrapped by this adapter.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 offset, java.lang.String str, javax.swing.text.AttributeSet a)static AutocompletionDocumentinstall(javax.swing.JComboBox<?> comboBox)Create and install anAutocompletionDocumenton an existingJComboBox, causing it to begin autocompleting the text field content.static AutocompletionDocumentinstall(javax.swing.JComboBox<?> comboBox, boolean casesensitive)Create and install anAutocompletionDocumenton an existingJComboBox, causing it to begin autocompleting the text field content.booleanisCaseSensitive()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)voidsetAutocompletion(boolean autocompletion)voidsetCaseSensitive(boolean casesensitive)voidsetCollator(java.text.Collator collator)If set to a non-nullobject, it is assumed that list elements are sorted according tocollator.
-
-
-
Constructor Detail
-
AutocompletionDocument
public AutocompletionDocument(javax.swing.JComboBox<?> comboBox)
Create a newAutocompletionDocumentthat 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:
getDocumentin interfaceNestingDocument- Returns:
- the wrapped document
-
getComboBox
public javax.swing.JComboBox<?> getComboBox()
Return theJComboBoxthat this document is autocompleting.- Returns:
- the autocompleted combo box
-
install
public static AutocompletionDocument install(javax.swing.JComboBox<?> comboBox)
Create and install anAutocompletionDocumenton an existingJComboBox, causing it to begin autocompleting the text field content.- Parameters:
comboBox- theJComboBoxto modify- Returns:
- the newly installed document if installation was successful,
otherwise
nullif it failed because theJComboBoxis not using aJTextComponent
-
install
public static AutocompletionDocument install(javax.swing.JComboBox<?> comboBox, boolean casesensitive)
Create and install anAutocompletionDocumenton an existingJComboBox, causing it to begin autocompleting the text field content.- Parameters:
comboBox- theJComboBoxto modifycasesensitive- whether autocompletion matching should be case sensitive- Returns:
- the newly installed document if installation was successful,
otherwise
nullif it failed because theJComboBoxis not using aJTextComponent
-
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 offset, 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
-
getCollator
public java.text.Collator getCollator()
Return the list orderCollator, ornullif none is set.- Returns:
- the
Collatorby which elements are ordered
-
setCollator
public void setCollator(java.text.Collator collator)
If set to a non-nullobject, 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- theCollatorby which elements are ordered
-
getLength
public int getLength()
- Specified by:
getLengthin interfacejavax.swing.text.Document
-
addDocumentListener
public void addDocumentListener(javax.swing.event.DocumentListener listener)
- Specified by:
addDocumentListenerin interfacejavax.swing.text.Document
-
removeDocumentListener
public void removeDocumentListener(javax.swing.event.DocumentListener listener)
- Specified by:
removeDocumentListenerin interfacejavax.swing.text.Document
-
addUndoableEditListener
public void addUndoableEditListener(javax.swing.event.UndoableEditListener listener)
- Specified by:
addUndoableEditListenerin interfacejavax.swing.text.Document
-
removeUndoableEditListener
public void removeUndoableEditListener(javax.swing.event.UndoableEditListener listener)
- Specified by:
removeUndoableEditListenerin interfacejavax.swing.text.Document
-
getProperty
public java.lang.Object getProperty(java.lang.Object key)
- Specified by:
getPropertyin interfacejavax.swing.text.Document
-
putProperty
public void putProperty(java.lang.Object key, java.lang.Object value)- Specified by:
putPropertyin 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
-
getStartPosition
public javax.swing.text.Position getStartPosition()
- Specified by:
getStartPositionin interfacejavax.swing.text.Document
-
getEndPosition
public javax.swing.text.Position getEndPosition()
- Specified by:
getEndPositionin 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
-
getRootElements
public javax.swing.text.Element[] getRootElements()
- Specified by:
getRootElementsin interfacejavax.swing.text.Document
-
getDefaultRootElement
public javax.swing.text.Element getDefaultRootElement()
- Specified by:
getDefaultRootElementin interfacejavax.swing.text.Document
-
render
public void render(java.lang.Runnable r)
- Specified by:
renderin interfacejavax.swing.text.Document
-
getAutocompletion
public boolean getAutocompletion()
-
setAutocompletion
public void setAutocompletion(boolean autocompletion)
-
isCaseSensitive
public boolean isCaseSensitive()
-
setCaseSensitive
public void setCaseSensitive(boolean casesensitive)
-
-