org.xnap.commons.gui.completion
Class Completion

java.lang.Object
  extended by org.xnap.commons.gui.completion.Completion

public class Completion
extends Object

The Completion class follows the mediator pattern.

It manages the text component and the completion model and decouples them from the CompletionMode which decides when and how to offer completion to the user.

If you want to provide your own completion modes, install them in the CompletionModeFactory.

During its lifetime the completion object is coupled to only one JTextComponent, it's a one-to-one mapping. To disable it, use setEnabled(false).

A completion object can be used as follows:

 JTextField jtf = new JTextField();
 Completion comp = new Completion(jtf, new FileCompletionModel());
 comp.setMode(new AutomaticDropDownCompletionMode());
 
When typing into the text field above the text is matched against existing files on the filesystem and possible completions are presented according to the AutomaticDropDownCompletionMode.

Author:
Felix Berger

Constructor Summary
Completion(JTextComponent textComponent)
          Convenience wrapper for Completion(JTextComponent, boolean).
Completion(JTextComponent textComponent, boolean wholeText)
          Convenience wrapper for Completion(JTextComponent, CompletionModel, boolean).
Completion(JTextComponent textComponent, CompletionModel model)
          Convenience wrapper for Completion(JTextComponent, CompletionModel, boolean).
Completion(JTextComponent textComponent, CompletionModel model, boolean wholeText)
          Constructs a new Completion object.
 
Method Summary
 void addCompletionModeListener(CompletionModeListener l)
          Adds a CompletionModeListener to the listener list.
protected  void fireModeChanged(Class oldMode, Class newMode)
          Can be used by subclasses to fire a completion mode change in case they provide their own implementation of setMode(CompletionMode).
 CompletionMode getMode()
          Returns the currently active completion mode.
 CompletionModel getModel()
          Returns the currently used model.
 String getText()
          Returns the text which should be completed.
 JTextComponent getTextComponent()
          Returns the text component this completion mode is responsible for
 boolean isEnabled()
          Returns whether the currently set completion mode is enabled.
 boolean isWholeTextCompletion()
          Returns whether this completion mode is supposed to complete the whole text of its text component made availabe through JTextComponent.getText() or just the last word before the cursor.
 void removeCompletionModeListener(CompletionModeListener l)
          Removes a CompletionModeListener from the listener list.
 void setEnabled(boolean enabled)
          Enables or disables the currently set completion mode.
 void setMode(CompletionMode newMode)
          Sets a new completion mode for the managed text component and enables it if the completion is currently enabled.
 void setModel(CompletionModel model)
          Sets the completion model.
 void setText(String text)
          Convenience wrapper for setText(String, int, int).
protected  void setText(String text, int selectionStart, int selectionEnd)
          Sets the given text honoring the whole text mode.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Completion

public Completion(JTextComponent textComponent,
                  CompletionModel model,
                  boolean wholeText)
Constructs a new Completion object. GlobalDefaultCompletionMode is set as the default completion mode.

Parameters:
textComponent - the text component completion will be done for
model - the completion model that provides the possible completions
wholeText - if true the whole text of the text component is used for completion otherwise only the last word before the cursor is completed.
Throws:
NullPointerException - if textComponent is null.

Completion

public Completion(JTextComponent textComponent,
                  boolean wholeText)
Convenience wrapper for Completion(JTextComponent, CompletionModel, boolean). The missing completion model defaults to the DefaultCompletionModel.


Completion

public Completion(JTextComponent textComponent,
                  CompletionModel model)
Convenience wrapper for Completion(JTextComponent, CompletionModel, boolean). The missing boolean defaults to true.


Completion

public Completion(JTextComponent textComponent)
Convenience wrapper for Completion(JTextComponent, boolean). The missing boolean defaults to true.

Method Detail

setMode

public void setMode(CompletionMode newMode)
Sets a new completion mode for the managed text component and enables it if the completion is currently enabled. The old mode is disabled and CompletionModeListeners are notified of the mode change.

Parameters:
newMode - the new completion mode
Throws:
NullPointerException - if newMode is null

setModel

public void setModel(CompletionModel model)
Sets the completion model. The current completion mode is disabled and enabled in order to be notified of this change.

Parameters:
model - the new completion model which will be used henceforth

setEnabled

public void setEnabled(boolean enabled)
Enables or disables the currently set completion mode.


isEnabled

public boolean isEnabled()
Returns whether the currently set completion mode is enabled.


getModel

public CompletionModel getModel()
Returns the currently used model.


getMode

public CompletionMode getMode()
Returns the currently active completion mode.


getTextComponent

public JTextComponent getTextComponent()
Returns the text component this completion mode is responsible for


isWholeTextCompletion

public final boolean isWholeTextCompletion()
Returns whether this completion mode is supposed to complete the whole text of its text component made availabe through JTextComponent.getText() or just the last word before the cursor.


setText

public void setText(String text)
Convenience wrapper for setText(String, int, int). Sets the text without any selection and setts the cursor to the end of the set text.

Parameters:
text - the text to set

setText

protected void setText(String text,
                       int selectionStart,
                       int selectionEnd)
Sets the given text honoring the whole text mode.

Parameters:
text - the text to set
selectionStart - the offset of the selection start relative to the beginning of the set text. Can be greater than selectionEnd.
selectionEnd - the offset of the selection end relative to the beginning of the set text. This is where the cursor is afterwards. TODO why is this not public?

getText

public String getText()
Returns the text which should be completed.

Returns:
Returns JTextComponent.getText()if wholeText is true otherwise the word before the cursor.

addCompletionModeListener

public void addCompletionModeListener(CompletionModeListener l)
Adds a CompletionModeListener to the listener list.

Parameters:
l - the CompletionModeListener to be added

removeCompletionModeListener

public void removeCompletionModeListener(CompletionModeListener l)
Removes a CompletionModeListener from the listener list.

Parameters:
l - the CompletionModeListener to be removed

fireModeChanged

protected void fireModeChanged(Class oldMode,
                               Class newMode)
Can be used by subclasses to fire a completion mode change in case they provide their own implementation of setMode(CompletionMode).

Parameters:
oldMode - the class of the old completion mode
newMode - the class of the new completion mode


Copyright © 2004-2007 XNap Commons Team. All Rights Reserved.