com.gremwell.history
Class HistoryDocument

java.lang.Object
  extended by javax.swing.text.AbstractDocument
      extended by javax.swing.text.PlainDocument
          extended by com.gremwell.history.HistoryDocument
All Implemented Interfaces:
java.io.Serializable, javax.swing.text.Document

public class HistoryDocument
extends javax.swing.text.PlainDocument

A Document with history and autocomplete. Can be used with various text components to implement autocomplete

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.text.AbstractDocument
javax.swing.text.AbstractDocument.AbstractElement, javax.swing.text.AbstractDocument.AttributeContext, javax.swing.text.AbstractDocument.BranchElement, javax.swing.text.AbstractDocument.Content, javax.swing.text.AbstractDocument.DefaultDocumentEvent, javax.swing.text.AbstractDocument.ElementEdit, javax.swing.text.AbstractDocument.LeafElement
 
Field Summary
 
Fields inherited from class javax.swing.text.PlainDocument
lineLimitAttribute, tabSizeAttribute
 
Fields inherited from class javax.swing.text.AbstractDocument
BAD_LOCATION, BidiElementName, ContentElementName, ElementNameAttribute, listenerList, ParagraphElementName, SectionElementName
 
Fields inherited from interface javax.swing.text.Document
StreamDescriptionProperty, TitleProperty
 
Constructor Summary
HistoryDocument(javax.swing.text.JTextComponent component)
          Constructor for document with default history.
HistoryDocument(javax.swing.text.JTextComponent component, History history)
          Constructor using existing history object
HistoryDocument(javax.swing.text.JTextComponent component, java.lang.String historyKey)
          Constructor
 
Method Summary
 void addToHistory(java.lang.String str)
          This method should be called by the text component that uses this document, or by anything else that handles the input into the text component.
 History getHistory()
          Returns the History object for this document.
 void insertString(int offs, java.lang.String str, javax.swing.text.AttributeSet a)
          Overrides insertString of the PlainDocument to provide autocompletion Whenever characters are inserted into the document, the typed text is looked up in history and the matching history item is inserted into the document.
 boolean isInserting()
          This function can be used by complex components to tell if the document is currently inserting autocompleted text.
 void setHistory(History history)
          Sets the History object for this document
 
Methods inherited from class javax.swing.text.PlainDocument
createDefaultRoot, getDefaultRootElement, getParagraphElement, insertUpdate, removeUpdate
 
Methods inherited from class javax.swing.text.AbstractDocument
addDocumentListener, addUndoableEditListener, createBranchElement, createLeafElement, createPosition, dump, fireChangedUpdate, fireInsertUpdate, fireRemoveUpdate, fireUndoableEditUpdate, getAsynchronousLoadPriority, getAttributeContext, getBidiRootElement, getContent, getCurrentWriter, getDocumentFilter, getDocumentListeners, getDocumentProperties, getEndPosition, getLength, getListeners, getProperty, getRootElements, getStartPosition, getText, getText, getUndoableEditListeners, postRemoveUpdate, putProperty, readLock, readUnlock, remove, removeDocumentListener, removeUndoableEditListener, render, replace, setAsynchronousLoadPriority, setDocumentFilter, setDocumentProperties, writeLock, writeUnlock
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HistoryDocument

public HistoryDocument(javax.swing.text.JTextComponent component,
                       java.lang.String historyKey)
Constructor

Parameters:
component - - the text component that uses this document.
key - - History key, may be null. An application can have multiple autocomplete components each with its own history, or sharing a history. If separate histories are desired, each component should have a document with a different key

HistoryDocument

public HistoryDocument(javax.swing.text.JTextComponent component,
                       History history)
Constructor using existing history object

Parameters:
component - JTextComponent that will be using this document
history - History object that will store the input history and provide autocomplete suggestions
See Also:
History

HistoryDocument

public HistoryDocument(javax.swing.text.JTextComponent component)
Constructor for document with default history.

Parameters:
component - JTextComponent that will use this document
Method Detail

insertString

public void insertString(int offs,
                         java.lang.String str,
                         javax.swing.text.AttributeSet a)
                  throws javax.swing.text.BadLocationException
Overrides insertString of the PlainDocument to provide autocompletion Whenever characters are inserted into the document, the typed text is looked up in history and the matching history item is inserted into the document. The part that was autocompleted and not typed is selected.

Specified by:
insertString in interface javax.swing.text.Document
Overrides:
insertString in class javax.swing.text.PlainDocument
Parameters:
offs - offset
str - Inserted string
a - Atrribute set
Throws:
javax.swing.text.BadLocationException

addToHistory

public void addToHistory(java.lang.String str)
This method should be called by the text component that uses this document, or by anything else that handles the input into the text component. When user has finished editing the text and types Enter or clicks on some button to use the entered text, this method should be called to add the entered text to the history

Parameters:
str -

getHistory

public History getHistory()
Returns the History object for this document.

Returns:
History object that is used by this document

setHistory

public void setHistory(History history)
Sets the History object for this document

Parameters:
history - History object

isInserting

public boolean isInserting()
This function can be used by complex components to tell if the document is currently inserting autocompleted text. It is provided to avoid infinite recursion.

Returns:
returns true if the document is currently inserting autocompleted text, that is we are somewhere inside the autocomplete() call