net.jakubholy.jedit.autocomplete
Class AutoComplete

java.lang.Object
  extended by net.jakubholy.jedit.autocomplete.AutoComplete
All Implemented Interfaces:
java.util.Observer

public final class AutoComplete
extends java.lang.Object
implements java.util.Observer

Try to complete a word typed in the associated buffer. Automatically try to find any completions for the current word being typed and if there're any, present them to the user in a pop-up list. A list of possible completions is constructed from words typed so far in the buffer that satisfy some conditions. The class observers an WordTypedListener to get notified of word events such as a word started/finished/invalidated (when the user jumps to another location etc.).

See Also:
PreferencesManager, WordTypedListener, Based on jEdit's CompleteWord feature.

Method Summary
 void attach(org.gjt.sp.jedit.Buffer buffer)
          Attach the AutoCompletion to the buffer == start completing there.
static AutoComplete attachAction(org.gjt.sp.jedit.Buffer buffer)
          Attach an AutoComplete to the buffer.
static void attachToAllBuffersAction()
          Attach AutoComplete to all opened buffers.
static AutoComplete createAutoCompleteAction(org.gjt.sp.jedit.Buffer buffer)
          Create a new AutoComplete that starts working for the given buffer if the buffer has none.
static void destroyAllAutoCompletes()
          Detach and destroy (forget) all instance of AutoComplete.
 void detach()
          Detach the AutoCompletion from the buffer it's bound to == stop working.
static void detachAction(org.gjt.sp.jedit.Buffer buffer)
          Detach the AutoCompletion from the given buffer if it has one.
protected  void displayCompletionPopup()
          Display a pop-up window with possible completions for the word being typed.
 void exportWordList()
          Save the list of remembered words to the user supplied file.
static void forgetAllWordsAction(org.gjt.sp.jedit.Buffer buffer)
          Forget all words in the completions list including those from the buffer and those from other sources such as the default word list.
 boolean forgetWord(java.lang.String word2remove)
          Remove the given word from the list of remembered words.
static AutoComplete getAutoCompleteOfBuffer(org.gjt.sp.jedit.Buffer buffer)
          Return the AutoComplete associated with the buffer or null if no AutoComplete has been attached to the buffer so far.
 org.gjt.sp.jedit.Buffer getBuffer()
          Return the buffer this AutoComplete is attached to.
protected  net.jakubholy.jedit.autocomplete.Completion[] getCompletions(java.lang.String prefix)
          Get all possible completion for the given prefix.
 java.lang.String getThePrefix()
          The prefix to complete/typed so far.
 WordList getWordList()
          Set the list of words used to find completions.
 java.lang.String getWords()
          A concatenation of all rememberd words.
 int importWordList()
          Read words from a file supplied by the user and add them to those remembered for this buffer.
 int importWordList(java.io.Reader wordListReader)
          Read words via the provided Reader and add them to those remembered for this buffer.
static void parseAction(org.gjt.sp.jedit.Buffer buffer)
          Parse the current buffer and add all words in it to the list.
 void parseBuffer()
          Parse the current buffer and add all words in it to the list.
 boolean rememberWord(java.lang.String word2remember)
          As rememberWord(String, boolean) but silent == false.
 boolean rememberWord(java.lang.String word2remember, boolean silent)
          Add the given word to the list of remembered words.
 boolean rememberWordSilent(java.lang.String word2remember)
          As rememberWord(String, boolean) but silent == true.
 void setThePrefix(java.lang.String thePrefix)
          The prefix to complete/typed so far.
 void setWordList(WordList wordList)
          Get the list of words used to find completions.
static void showWordsAction(org.gjt.sp.jedit.Buffer buffer)
          Show the WordList Editor user interface to edit words remembered for the current buffer.
 void update(java.util.Observable observable, java.lang.Object arg)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

createAutoCompleteAction

public static AutoComplete createAutoCompleteAction(org.gjt.sp.jedit.Buffer buffer)
Create a new AutoComplete that starts working for the given buffer if the buffer has none. If the buffer already has an AutoComplete, it's only returned.


attachAction

public static AutoComplete attachAction(org.gjt.sp.jedit.Buffer buffer)
Attach an AutoComplete to the buffer. It either re-attaches an existing detached one ore creates a new one. If the buffer already has an AutoComplete attached to it, it's detached and re-attached.

Returns:
The [new] buffer's AutoComplete

detachAction

public static void detachAction(org.gjt.sp.jedit.Buffer buffer)
Detach the AutoCompletion from the given buffer if it has one. But we keep the AutoComplete for the buffer and it can be later re-attached. Word list is cleared.


parseAction

public static void parseAction(org.gjt.sp.jedit.Buffer buffer)
                        throws ActionException
Parse the current buffer and add all words in it to the list.

Throws:
ActionException - When the action cannot be performed for some reason

forgetAllWordsAction

public static void forgetAllWordsAction(org.gjt.sp.jedit.Buffer buffer)
                                 throws ActionException
Forget all words in the completions list including those from the buffer and those from other sources such as the default word list.

Throws:
ActionException - When the action cannot be performed for some reason

showWordsAction

public static void showWordsAction(org.gjt.sp.jedit.Buffer buffer)
                            throws ActionException
Show the WordList Editor user interface to edit words remembered for the current buffer.

Throws:
ActionException - When the action cannot be performed for some reason

getAutoCompleteOfBuffer

public static AutoComplete getAutoCompleteOfBuffer(org.gjt.sp.jedit.Buffer buffer)
Return the AutoComplete associated with the buffer or null if no AutoComplete has been attached to the buffer so far. The AutoComplete doesn't need to be currently attached to the buffer if it has been detached recently - check that its method getBuffer() doesn't return null and that it matches the buffer.

Parameters:
buffer - The buffer whose AutoComplete you want to access.
Returns:
AutoComplete associated (not necessarily attached) with the buffer or null.

attachToAllBuffersAction

public static void attachToAllBuffersAction()
                                     throws ActionException
Attach AutoComplete to all opened buffers.

Throws:
ActionException - When the action cannot be performed for some reason

exportWordList

public void exportWordList()
                    throws java.io.IOException
Save the list of remembered words to the user supplied file.

Throws:
java.io.IOException

importWordList

public int importWordList()
                   throws MaxWordsExceededException,
                          java.io.FileNotFoundException,
                          java.io.IOException
Read words from a file supplied by the user and add them to those remembered for this buffer.

See importWordList(Reader) for details on the file format.

Returns:
Count of the imported words.
Throws:
MaxWordsExceededException - When the maximal number of remembered words have already been reached.
java.io.FileNotFoundException - The import file couldn't be found
java.io.IOException - Exception when trying to read the file

importWordList

public int importWordList(java.io.Reader wordListReader)
                   throws java.io.IOException,
                          MaxWordsExceededException
Read words via the provided Reader and add them to those remembered for this buffer.

The file must contain one word per line. We do not check that they're words, remove white spaces or anything like that - we import them as they are. Only rows that contain nothing but whitespaces are ignored.

Note: The wordListReader is closed at the end.

Parameters:
wordListReader - A reader (e.g. FileReader) for reading the word list. Note: it's closed at the end.
Returns:
Count of the imported words.
Throws:
MaxWordsExceededException - When the maximal number of remembered words have already been reached.
java.io.IOException - Exception when trying to read the file

destroyAllAutoCompletes

public static void destroyAllAutoCompletes()
Detach and destroy (forget) all instance of AutoComplete.


detach

public void detach()
Detach the AutoCompletion from the buffer it's bound to == stop working. Word list is emptied.


attach

public void attach(org.gjt.sp.jedit.Buffer buffer)
Attach the AutoCompletion to the buffer == start completing there. The buffer is parsed for words to remember as possible completions.


update

public void update(java.util.Observable observable,
                   java.lang.Object arg)
Specified by:
update in interface java.util.Observer
See Also:
WordTypedListener

displayCompletionPopup

protected void displayCompletionPopup()
Display a pop-up window with possible completions for the word being typed.


getCompletions

protected net.jakubholy.jedit.autocomplete.Completion[] getCompletions(java.lang.String prefix)
Get all possible completion for the given prefix.


getThePrefix

public java.lang.String getThePrefix()
The prefix to complete/typed so far.


setThePrefix

public void setThePrefix(java.lang.String thePrefix)
The prefix to complete/typed so far.


getWordList

public WordList getWordList()
Set the list of words used to find completions.


setWordList

public void setWordList(WordList wordList)
Get the list of words used to find completions.


forgetWord

public boolean forgetWord(java.lang.String word2remove)
Remove the given word from the list of remembered words.

Returns:
True if the word was in the list & is removed.

rememberWord

public boolean rememberWord(java.lang.String word2remember,
                            boolean silent)
                     throws MaxWordsExceededException
Add the given word to the list of remembered words.

Parameters:
word2remember - The word/string to remember
silent - If true don't throw MaxWordsExceededException, only do nothing and return false
Returns:
True if the word has been added to the list, false if it hasn't been added because either it was already there or the max. number of words to remember is already reached and silent is true.
Throws:
MaxWordsExceededException - When the maximal number of remembered words have already been reached - unless silent is true.

rememberWord

public boolean rememberWord(java.lang.String word2remember)
                     throws MaxWordsExceededException
As rememberWord(String, boolean) but silent == false. (Throw an exception when the max. number of words is exceeded.)

Throws:
MaxWordsExceededException

rememberWordSilent

public boolean rememberWordSilent(java.lang.String word2remember)
As rememberWord(String, boolean) but silent == true. (Never throw an exception when the max. number of words is exceeded.)


getWords

public java.lang.String getWords()
A concatenation of all rememberd words.

Returns:
A concatenation of all rememberd words separated by ','.

parseBuffer

public void parseBuffer()
Parse the current buffer and add all words in it to the list.


getBuffer

public org.gjt.sp.jedit.Buffer getBuffer()
Return the buffer this AutoComplete is attached to.

Returns:
The buffer this AutoComplete is attached to or null if not attached.