org.ceryle.ui
Class PredictiveComboBoxEditor

java.lang.Object
  extended by javax.swing.plaf.basic.BasicComboBoxEditor
      extended by org.ceryle.ui.PredictiveComboBoxEditor
All Implemented Interfaces:
FocusListener, EventListener, ComboBoxEditor

public class PredictiveComboBoxEditor
extends BasicComboBoxEditor

Extends the BasicComboBoxEditor to provide "predictive" behaviour, where a subset of a backing list (a SetModel) is displayed by typing in the initial characters of a word or phrase. The list is dynamically modified to list all items from the backing list that begin with the current characters in the editor.

Notes

Backing Model
The constructor for the superclass does not require a reference to its parent JComboBox, whereas this class does. The constructor copies the contents of the JComboBox's ListModel into an immutable backing list. Because of this, any changes in the JComboBox's model after that will not be reflected in the backing list unless it is explicitly re-set using this class' method setModel(ListModel).
Editability
The parent JComboBox will be set editable by the constructor of this class, since the functionality of this editor relies on editability of the editor field. If the JComboBox is re-set uneditable, it will revert back to its default behaviour (i.e., this class' features will cease to function).
Editor Keys
The editor field will respond to letters (including spaces), digits, the Underscore ('_'), Period ('.'), and Dash ('-') keys as comparable characters. The Left and Right cursor keys will navigate within the editor; the Up and Down navigate the JComboBox's list. The Delete key will clear the editor field, whereas the Asterisk ('*') key displays the entire source list.
Sorting and Comparisons
The backing list is copied into a SetModel and sorted using its sort() method. This assumes all items comply with the Comparable interface. If any item in the backing list is not a String, its Object.toString() method is used for comparison.

Since:
JDK1.4
Version:
$Id: PredictiveComboBoxEditor.java,v 3.2 2007-06-15 12:09:39 altheim Exp $
Author:
Murray Altheim
See Also:
SetModel

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.plaf.basic.BasicComboBoxEditor
BasicComboBoxEditor.UIResource
 
Field Summary
 
Fields inherited from class javax.swing.plaf.basic.BasicComboBoxEditor
editor
 
Constructor Summary
PredictiveComboBoxEditor(JComboBox box)
          Constructor with the parent JComboBox.
 
Method Summary
 boolean isValidCharacter(char c)
          Returns true if char c is a letter, digit, the Underscore ('_'), Period ('.'), or Dash ('-') character.
 void setItem(Object anObject)
          Set the item that should be edited.
 void setModel(ListModel model)
          Sets the backing list to a copy of the contents of the provided ListModel.
static boolean setPrototypeDisplayValue(JComboBox combobox)
          A static utility method that sets the prototype display value used to calculate the size of the display for the provided JComboBox.
 
Methods inherited from class javax.swing.plaf.basic.BasicComboBoxEditor
addActionListener, focusGained, focusLost, getEditorComponent, getItem, removeActionListener, selectAll
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PredictiveComboBoxEditor

public PredictiveComboBoxEditor(JComboBox box)
Constructor with the parent JComboBox.

Method Detail

setModel

public void setModel(ListModel model)
Sets the backing list to a copy of the contents of the provided ListModel.


setItem

public void setItem(Object anObject)
Set the item that should be edited. Cancel any editing if necessary.

Specified by:
setItem in interface ComboBoxEditor
Overrides:
setItem in class BasicComboBoxEditor

setPrototypeDisplayValue

public static boolean setPrototypeDisplayValue(JComboBox combobox)
A static utility method that sets the prototype display value used to calculate the size of the display for the provided JComboBox. The contents of the backing ListModel of the JComboBox will be scanned to locate the longest String width (using the FontMetric for its editor text field), and this will be used to set the prototype.

If the JComboBox has no items or its FontMetrics can't be obtained (e.g., it is not visible), this method will have no effect.

Returns:
true if able to set a value, false if any failure.

isValidCharacter

public boolean isValidCharacter(char c)
Returns true if char c is a letter, digit, the Underscore ('_'), Period ('.'), or Dash ('-') character.



The Ceryle Project. Copyright ©2001-2007 Murray Altheim, All Rights Reserved. See LICENSE included with distribution.