net.sourceforge.olduvai.treejuxtaposer
Class IncrementalSearch

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by java.awt.Window
              extended by java.awt.Frame
                  extended by javax.swing.JFrame
                      extended by net.sourceforge.olduvai.treejuxtaposer.IncrementalSearch
All Implemented Interfaces:
java.awt.event.ActionListener, java.awt.event.KeyListener, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, java.util.EventListener, javax.accessibility.Accessible, javax.swing.event.ListSelectionListener, javax.swing.RootPaneContainer, javax.swing.WindowConstants

public class IncrementalSearch
extends javax.swing.JFrame
implements java.awt.event.KeyListener, javax.swing.event.ListSelectionListener, java.awt.event.ActionListener

Incremental search functionality and GUI class. Typing letter by letter will search a cache of results and display matches. The cache grows over time without bound.

Author:
jslack
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JFrame
javax.swing.JFrame.AccessibleJFrame
 
Nested classes/interfaces inherited from class java.awt.Frame
java.awt.Frame.AccessibleAWTFrame
 
Nested classes/interfaces inherited from class java.awt.Window
java.awt.Window.AccessibleAWTWindow
 
Nested classes/interfaces inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BaselineResizeBehavior, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
private static java.lang.String initialText
          Initial text in comment box searchStatus.
private static boolean matchPrefixOnly
          Set to true for just matching prefixes, false for matching anywhere in the string.
private  int numFound
          The number of items in the list of results results.
private  java.util.Hashtable prefix
          Cache of results, indexed by substrings of previously found.
private  javax.swing.JButton resetButton
          Reset button
private  javax.swing.JTextField results
          Text that is put into the results pane resultsPane.
private  javax.swing.JScrollPane resultsPane
          Pane for picking results that can be scrolled.
private static int returnThreshold
          Highlight search results found if under (or equal to) this threshold
private  javax.swing.JList searchResults
          List of results that can be picked that is put into the text field results.
private  javax.swing.JTextArea searchStatus
          Comment box that displays the number of things that match a user selection.
private  javax.swing.JScrollPane statusPane
          Pane for the search status searchStatus.
private  TreeJuxtaposer tj
          The TJ for this set of found results, for calling updates to the drawers.
 
Fields inherited from class javax.swing.JFrame
accessibleContext, EXIT_ON_CLOSE, rootPane, rootPaneCheckingEnabled
 
Fields inherited from class java.awt.Frame
CROSSHAIR_CURSOR, DEFAULT_CURSOR, E_RESIZE_CURSOR, HAND_CURSOR, ICONIFIED, MAXIMIZED_BOTH, MAXIMIZED_HORIZ, MAXIMIZED_VERT, MOVE_CURSOR, N_RESIZE_CURSOR, NE_RESIZE_CURSOR, NORMAL, NW_RESIZE_CURSOR, S_RESIZE_CURSOR, SE_RESIZE_CURSOR, SW_RESIZE_CURSOR, TEXT_CURSOR, W_RESIZE_CURSOR, WAIT_CURSOR
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface javax.swing.WindowConstants
DISPOSE_ON_CLOSE, DO_NOTHING_ON_CLOSE, HIDE_ON_CLOSE
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
IncrementalSearch(TreeJuxtaposer tj)
          Initialize the search box with default values.
IncrementalSearch(TreeJuxtaposer tj, java.util.ArrayList initialList)
          Initialize the search box with a list of names.
 
Method Summary
 void actionPerformed(java.awt.event.ActionEvent arg0)
          Action Listener for the reset button.
private  void doUI()
          Set up the user interface.
 void initializeList(java.util.ArrayList nameList)
          Initialize nodes in the search list if the input is not null.
 void keyPressed(java.awt.event.KeyEvent e)
          Stub function, use keyReleased(KeyEvent) events to fully support copy/paste/delete/typing.
 void keyReleased(java.awt.event.KeyEvent evt)
          Triggers the search function after a key is released.
 void keyTyped(java.awt.event.KeyEvent e)
          Stub function, use keyReleased(KeyEvent) events to fully support copy/paste/delete/typing.
static void main(java.lang.String[] args)
          Static test function, standalone testing on this class.
private  java.util.ArrayList partialResult()
          Check cache for previous results that match text input.
private  java.util.ArrayList recursiveSearch(java.lang.String text)
          Search hashtable (prefix) for the given text.
 void resetSearch()
          Reset the search results to empty, redraws in all drawers.
 void valueChanged(javax.swing.event.ListSelectionEvent evt)
          List change detection function.
 
Methods inherited from class javax.swing.JFrame
addImpl, createRootPane, frameInit, getAccessibleContext, getContentPane, getDefaultCloseOperation, getGlassPane, getGraphics, getJMenuBar, getLayeredPane, getRootPane, getTransferHandler, isDefaultLookAndFeelDecorated, isRootPaneCheckingEnabled, paramString, processWindowEvent, remove, repaint, setContentPane, setDefaultCloseOperation, setDefaultLookAndFeelDecorated, setGlassPane, setIconImage, setJMenuBar, setLayeredPane, setLayout, setRootPane, setRootPaneCheckingEnabled, setTransferHandler, update
 
Methods inherited from class java.awt.Frame
addNotify, getCursorType, getExtendedState, getFrames, getIconImage, getMaximizedBounds, getMenuBar, getState, getTitle, isResizable, isUndecorated, remove, removeNotify, setCursor, setExtendedState, setMaximizedBounds, setMenuBar, setResizable, setState, setTitle, setUndecorated
 
Methods inherited from class java.awt.Window
addPropertyChangeListener, addPropertyChangeListener, addWindowFocusListener, addWindowListener, addWindowStateListener, applyResourceBundle, applyResourceBundle, createBufferStrategy, createBufferStrategy, dispose, getBufferStrategy, getFocusableWindowState, getFocusCycleRootAncestor, getFocusOwner, getFocusTraversalKeys, getGraphicsConfiguration, getIconImages, getInputContext, getListeners, getLocale, getModalExclusionType, getMostRecentFocusOwner, getOwnedWindows, getOwner, getOwnerlessWindows, getToolkit, getWarningString, getWindowFocusListeners, getWindowListeners, getWindows, getWindowStateListeners, hide, isActive, isAlwaysOnTop, isAlwaysOnTopSupported, isFocusableWindow, isFocusCycleRoot, isFocused, isLocationByPlatform, isShowing, pack, postEvent, processEvent, processWindowFocusEvent, processWindowStateEvent, removeWindowFocusListener, removeWindowListener, removeWindowStateListener, reshape, setAlwaysOnTop, setBounds, setBounds, setCursor, setFocusableWindowState, setFocusCycleRoot, setIconImages, setLocationByPlatform, setLocationRelativeTo, setMinimumSize, setModalExclusionType, setSize, setSize, setVisible, show, toBack, toFront
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalPolicy, getInsets, getLayout, getMaximumSize, getMinimumSize, getMousePosition, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paint, paintComponents, preferredSize, print, printComponents, processContainerEvent, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, prepareImage, prepareImage, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resize, resize, setBackground, setComponentOrientation, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeysEnabled, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setName, setPreferredSize, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.awt.MenuContainer
getFont, postEvent
 

Field Detail

initialText

private static java.lang.String initialText
Initial text in comment box searchStatus.


resultsPane

private javax.swing.JScrollPane resultsPane
Pane for picking results that can be scrolled.


searchResults

private javax.swing.JList searchResults
List of results that can be picked that is put into the text field results.


statusPane

private javax.swing.JScrollPane statusPane
Pane for the search status searchStatus.


searchStatus

private javax.swing.JTextArea searchStatus
Comment box that displays the number of things that match a user selection.


results

private javax.swing.JTextField results
Text that is put into the results pane resultsPane.


prefix

private java.util.Hashtable prefix
Cache of results, indexed by substrings of previously found.


tj

private TreeJuxtaposer tj
The TJ for this set of found results, for calling updates to the drawers.


numFound

private int numFound
The number of items in the list of results results.


resetButton

private javax.swing.JButton resetButton
Reset button


returnThreshold

private static final int returnThreshold
Highlight search results found if under (or equal to) this threshold

See Also:
Constant Field Values

matchPrefixOnly

private static boolean matchPrefixOnly
Set to true for just matching prefixes, false for matching anywhere in the string.

Constructor Detail

IncrementalSearch

public IncrementalSearch(TreeJuxtaposer tj)
Initialize the search box with default values.

Parameters:
tj - The TJ for this search box.

IncrementalSearch

public IncrementalSearch(TreeJuxtaposer tj,
                         java.util.ArrayList initialList)
Initialize the search box with a list of names.

Parameters:
tj - The TJ for this search box.
initialList - A list of names to use with search. Used mostly for testing.
Method Detail

doUI

private void doUI()
Set up the user interface.


initializeList

public void initializeList(java.util.ArrayList nameList)
Initialize nodes in the search list if the input is not null.

Parameters:
nameList - List of names to put into the search dialog. If this is null, do nothing. TJ (TreeJuxtaposer.addNamesToSearchList()) will call this function again and set this list to the list of named nodes after laying out the tree.

recursiveSearch

private java.util.ArrayList recursiveSearch(java.lang.String text)
Search hashtable (prefix) for the given text. If not in the hashtable, use result of string except for last character and reduce that list with the full text input. Recursion is done first, so all prefixes of the text should be in the hash table. Called by

Parameters:
text - Text string to search for in the hash, to recover a list of matches.
Returns:
List of matching strings for the given text input.

partialResult

private java.util.ArrayList partialResult()
Check cache for previous results that match text input. If the text has no match, call recursiveSearch(String), which will fill in the cache to get a correct result. This is called by the keyReleased(KeyEvent) event handler.

Returns:
List of items from the cache (cache is updated with recursiveSearch(String) if input text is not found).

resetSearch

public void resetSearch()
Reset the search results to empty, redraws in all drawers.


main

public static void main(java.lang.String[] args)
Static test function, standalone testing on this class.

Parameters:
args - Input, not used

valueChanged

public void valueChanged(javax.swing.event.ListSelectionEvent evt)
List change detection function. Updates highlighting on tree (changes to selected items reflected in tree), and modifies status line to indicate number of items selected (searchStatus).

Specified by:
valueChanged in interface javax.swing.event.ListSelectionListener
Parameters:
evt - List change event, on the list searchResults. This is ignored in place of re-reading the list content.

keyReleased

public void keyReleased(java.awt.event.KeyEvent evt)
Triggers the search function after a key is released. The caret position (text cursor) is stored and recovered after text is entered to properly support deletes, copy-paste, and normal typing.

Specified by:
keyReleased in interface java.awt.event.KeyListener
Parameters:
evt - Key release event.

keyTyped

public void keyTyped(java.awt.event.KeyEvent e)
Stub function, use keyReleased(KeyEvent) events to fully support copy/paste/delete/typing.

Specified by:
keyTyped in interface java.awt.event.KeyListener
Parameters:
e - Key typing event.

keyPressed

public void keyPressed(java.awt.event.KeyEvent e)
Stub function, use keyReleased(KeyEvent) events to fully support copy/paste/delete/typing.

Specified by:
keyPressed in interface java.awt.event.KeyListener
Parameters:
e - Key press event.

actionPerformed

public void actionPerformed(java.awt.event.ActionEvent arg0)
Action Listener for the reset button.

Specified by:
actionPerformed in interface java.awt.event.ActionListener
Parameters:
arg0 - Event that triggered the reset button.