001: /*
002: * @(#)SearchableUtils.java
003: *
004: * Copyright 2002 - 2004 JIDE Software Inc. All rights reserved.
005: */
006: package com.jidesoft.swing;
007:
008: import javax.swing.*;
009: import javax.swing.text.JTextComponent;
010:
011: /**
012: * Utility class to make component searchable. It's very easy to use this class.
013: * In order to make a component, all you need to do is to call
014: * <code><pre>
015: * SearchableUtils.installSearchable(component);
016: * </pre></code>
017: * The component could be a JList, JTree or JTable. If you need to further customize some
018: * attributes of Searchable, you can assign a variable that returns from installSearchable().
019: * <code><pre>
020: * Searchable searchable = SearchableUtils.installSearchable(component);
021: * // further configure it
022: * searchable.setCaseSensitive(true);
023: * // ...
024: * </pre></code>
025: * Usually you don't need to uninstall the searchable from the component. But if for some reason, you need
026: * to disable the searchable feature of the component, you can call uninstallSearchable().
027: * <code><pre>
028: * Searchable searchable = SearchableUtils.installSearchable(component);
029: * // ...
030: * // Now disable it
031: * SearchableUtils.uninstallSearchable(searchable);
032: * </pre></code>
033: * <p/>
034: * There is a small trick that you should know. JTree and JList implemented partially the
035: * quick search feature so that when you type in the first charactor, it will jump to the first
036: * occurence. This feature sometimes conflicts with the Searchable we provided. So it'd better
037: * if you disable the JTree or JList default feature by creating JTree
038: * and JList with getNextMatch method overridden. See below
039: * <code><pre>
040: * JTree tree = new JTree(...) {
041: * public TreePath getNextMatch(String prefix, int startingRow, Position.Bias bias) {
042: * return null;
043: * }
044: * };
045: * <p/>
046: * JList list = new JList(...){
047: * public int getNextMatch(String prefix, int startIndex, Position.Bias bias) {
048: * return -1;
049: * }
050: * };
051: * </pre></code>
052: */
053: public class SearchableUtils {
054: /**
055: * Installs the searchable function onto a JTree.
056: *
057: * @param tree
058: * @return A TreeSearchable
059: */
060: public static TreeSearchable installSearchable(JTree tree) {
061: return new TreeSearchable(tree);
062: }
063:
064: /**
065: * Installs the searchable function onto a JTable.
066: *
067: * @param table
068: * @return A TableSearchable
069: */
070: public static TableSearchable installSearchable(JTable table) {
071: return new TableSearchable(table);
072: }
073:
074: /**
075: * Installs the searchable function onto a JList.
076: *
077: * @param list
078: * @return A ListSearchable
079: */
080: public static ListSearchable installSearchable(JList list) {
081: return new ListSearchable(list);
082: }
083:
084: /**
085: * Installs the searchable function onto a JComboBox.
086: *
087: * @param combobox
088: * @return A ComboBoxSearchable
089: */
090: public static ComboBoxSearchable installSearchable(
091: JComboBox combobox) {
092: return new ComboBoxSearchable(combobox);
093: }
094:
095: /**
096: * Installs the searchable function onto a JTextComponent.
097: *
098: * @param textComponent
099: * @return A TextComponentSearchable
100: */
101: public static TextComponentSearchable installSearchable(
102: JTextComponent textComponent) {
103: return new TextComponentSearchable(textComponent);
104: }
105:
106: public static void uninstallSearchable(Searchable searchable) {
107: searchable.uninstallListeners();
108: }
109: }
|