KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > data > query > SearchQueryBinding


1 package prefuse.data.query;
2
3 import javax.swing.JComponent JavaDoc;
4
5 import prefuse.data.Tuple;
6 import prefuse.data.event.TupleSetListener;
7 import prefuse.data.expression.AbstractPredicate;
8 import prefuse.data.search.PrefixSearchTupleSet;
9 import prefuse.data.search.SearchTupleSet;
10 import prefuse.data.tuple.TupleSet;
11 import prefuse.util.ui.JSearchPanel;
12 import prefuse.visual.VisualTupleSet;
13
14 /**
15  * DynamicQueryBinding supporting text search over data values. Implementations
16  * of the {@link prefuse.data.search.SearchTupleSet} class from the
17  * {@link prefuse.data.search} package can be used to control the type of
18  * search index used.
19  *
20  * @author <a HREF="http://jheer.org">jeffrey heer</a>
21  * @see prefuse.data.search.SearchTupleSet
22  */

23 public class SearchQueryBinding extends DynamicQueryBinding {
24
25     private SearchTupleSet m_set;
26     private Listener m_lstnr;
27     private Object JavaDoc m_lock;
28     
29     /**
30      * Create a new SearchQueryBinding over the given set and data field.
31      * @param ts the TupleSet to query
32      * @param field the data field (Table column) to query
33      */

34     public SearchQueryBinding(TupleSet ts, String JavaDoc field) {
35         this(ts, field, new PrefixSearchTupleSet());
36     }
37     
38     /**
39      * Create a new SearchQueryBinding over the given set and data field,
40      * using the specified SearchTupleSet instance. Use this constructor to
41      * choose the type of search engine used, and to potentially reuse the
42      * same search set over multiple dynamic query bindings.
43      * @param ts the TupleSet to query
44      * @param field the data field (Table column) to query
45      * @param set the {@link prefuse.data.search.SearchTupleSet} to use.
46      */

47     public SearchQueryBinding(TupleSet ts, String JavaDoc field, SearchTupleSet set) {
48         super(ts, field);
49         m_lstnr = new Listener();
50         setPredicate(new SearchBindingPredicate());
51         
52         m_set = set;
53         m_set.index(ts.tuples(), field);
54         m_set.addTupleSetListener(m_lstnr);
55         
56         if ( ts instanceof VisualTupleSet )
57             m_lock = ((VisualTupleSet)ts).getVisualization();
58     }
59     
60     /**
61      * Return the SearchTupleSet used for conducting searches.
62      * @return the {@link prefuse.data.search.SearchTupleSet} used by this
63      * dynamic query binding.
64      */

65     public SearchTupleSet getSearchSet() {
66         return m_set;
67     }
68     
69     // ------------------------------------------------------------------------
70

71     /**
72      * Create a new search text panel for searching over the data.
73      * @return a {@link prefuse.util.ui.JSearchPanel} bound to this
74      * dynamic query.
75      * @see prefuse.data.query.DynamicQueryBinding#createComponent()
76      */

77     public JComponent JavaDoc createComponent() {
78         return createSearchPanel();
79     }
80
81     /**
82      * Create a new search text panel for searching over the data.
83      * @return a {@link prefuse.util.ui.JSearchPanel} bound to this
84      * dynamic query.
85      */

86     public JSearchPanel createSearchPanel() {
87         return createSearchPanel(m_set instanceof PrefixSearchTupleSet);
88     }
89     
90     /**
91      * Create a new search text panel for searching over the data.
92      * @param monitorKeystrokes if true, each keystroke will cause the
93      * search to be re-run (this is the default for prefix searches);
94      * if false, searches will only re-run when the enter key is typed
95      * (this is the default for the other search engine types).
96      * @return a {@link prefuse.util.ui.JSearchPanel} bound to this
97      * dynamic query.
98      */

99     public JSearchPanel createSearchPanel(boolean monitorKeystrokes) {
100         JSearchPanel jsp = new JSearchPanel(m_set, m_field, monitorKeystrokes);
101         if ( m_lock != null ) { jsp.setLock(m_lock); }
102         return jsp;
103     }
104     
105     // ------------------------------------------------------------------------
106

107     private class SearchBindingPredicate extends AbstractPredicate {
108         public boolean getBoolean(Tuple t) {
109             String JavaDoc q = m_set.getQuery();
110             return (q==null || q.length()==0 || m_set.containsTuple(t));
111         }
112         public void touch() {
113             this.fireExpressionChange();
114         }
115     }
116     
117     private class Listener implements TupleSetListener {
118         public void tupleSetChanged(TupleSet tset, Tuple[] added, Tuple[] removed) {
119             ((SearchBindingPredicate)getPredicate()).touch();
120         }
121     }
122
123 } // end of class SearchQueryBinding
124
Popular Tags