org.xnap.commons.gui.completion
Class DefaultCompletionModel

java.lang.Object
  extended by javax.swing.AbstractListModel
      extended by javax.swing.DefaultComboBoxModel
          extended by org.xnap.commons.gui.completion.DefaultCompletionModel
All Implemented Interfaces:
Serializable, ComboBoxModel, ListModel, MutableComboBoxModel, CompletionModel

public class DefaultCompletionModel
extends DefaultComboBoxModel
implements CompletionModel

This classl uses a ternary search tree for completion.

You can easily add arrays of objects to it and remove single object if they should not show up as possible completions anymore.

A toArray() method gives access to all objects stored in the model and can be used for serialization.

See also CompletionSettingDirector.

Author:
Felix Berger
See Also:
Serialized Form

Field Summary
 
Fields inherited from class javax.swing.AbstractListModel
listenerList
 
Constructor Summary
DefaultCompletionModel()
          Constructs a new model with no items.
DefaultCompletionModel(Object[] items)
          Constructs a new model using the given items for completion.
DefaultCompletionModel(Object[] items, boolean sorted)
          Constructs a new model using the given items for completion.
 
Method Summary
 void clear()
          Clears the model.
 boolean complete(String prefix)
          Finds possible completions for a prefix.
 String completeUniquePrefix(String prefix)
          Returns largest common prefix of all possible completions for the given prefix.
 void insert(Object object)
          Adds an object to the completion model's ternary search tree.
 void insert(Object[] items)
          Adds an array of objects to the completion model's ternary search tree.
 void insert(Object[] items, boolean sorted)
          Adds an array of objects to the completion model's ternary search tree.
 void remove(Object object)
          Removes the object from the ternary search tree.
 Object[] toArray()
          Returns an array containing all of the elements in this completion model.
 
Methods inherited from class javax.swing.DefaultComboBoxModel
addElement, getElementAt, getIndexOf, getSelectedItem, getSize, insertElementAt, removeAllElements, removeElement, removeElementAt, setSelectedItem
 
Methods inherited from class javax.swing.AbstractListModel
addListDataListener, fireContentsChanged, fireIntervalAdded, fireIntervalRemoved, getListDataListeners, getListeners, removeListDataListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface javax.swing.MutableComboBoxModel
addElement, insertElementAt, removeElement, removeElementAt
 
Methods inherited from interface javax.swing.ComboBoxModel
getSelectedItem, setSelectedItem
 
Methods inherited from interface javax.swing.ListModel
addListDataListener, getElementAt, getSize, removeListDataListener
 

Constructor Detail

DefaultCompletionModel

public DefaultCompletionModel()
Constructs a new model with no items.


DefaultCompletionModel

public DefaultCompletionModel(Object[] items)
Constructs a new model using the given items for completion.

Parameters:
items - the objects used for completion

DefaultCompletionModel

public DefaultCompletionModel(Object[] items,
                              boolean sorted)
Constructs a new model using the given items for completion.

Parameters:
items - the array of objects used for completion
sorted - whether the array of items is sorted or not
Method Detail

complete

public boolean complete(String prefix)
Description copied from interface: CompletionModel
Finds possible completions for a prefix. As a side effect the completions are added to the MutableComboBoxModel. Use ListModel.getSize() to get the number of completions found and retrieve them with ListModel.getElementAt(int).

Specified by:
complete in interface CompletionModel
Parameters:
prefix - the prefix being matched
Returns:
true, if completion was successful, i.e. the prefix matches at least one item in the model

completeUniquePrefix

public String completeUniquePrefix(String prefix)
Description copied from interface: CompletionModel
Returns largest common prefix of all possible completions for the given prefix.

Specified by:
completeUniquePrefix in interface CompletionModel
Parameters:
prefix - the prefix being matched
Returns:
the largest common prefix of all possible matches, the prefix given if no longer prefix can be found

insert

public void insert(Object object)
Adds an object to the completion model's ternary search tree. The object is inserted using its Object.toString() method. If another object with the same name exists it is simply replaced by this one, no equality tests are carried out.

Parameters:
object - the object which is completed using its Object.toString() method

insert

public void insert(Object[] items,
                   boolean sorted)
Adds an array of objects to the completion model's ternary search tree. For large arrays adding the items in one chunk is recommended, this ensures the search tree is more balanced.

Parameters:
items - the array of objects used for completion
sorted - whether the array of items is sorted or not

insert

public void insert(Object[] items)
Adds an array of objects to the completion model's ternary search tree. The array will be sorted before it is added.

Parameters:
items - the array of objects used for completion
See Also:
insert(Object[], boolean)

remove

public void remove(Object object)
Removes the object from the ternary search tree. Equality is tested with the Object.equals(Object) method.

Parameters:
object - the object to be removed from the search tree

toArray

public Object[] toArray()
Returns an array containing all of the elements in this completion model. The array's elements are sorted alphabetically according to the return values of Object.toString().

Returns:
an array containing all of the elements in this completion model

clear

public void clear()
Clears the model.

Subsequent calls to complete(String) will return false.



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