001: package prefuse.data.query;
002:
003: import javax.swing.JComponent;
004:
005: import prefuse.data.Tuple;
006: import prefuse.data.event.TupleSetListener;
007: import prefuse.data.expression.AbstractPredicate;
008: import prefuse.data.search.PrefixSearchTupleSet;
009: import prefuse.data.search.SearchTupleSet;
010: import prefuse.data.tuple.TupleSet;
011: import prefuse.util.ui.JSearchPanel;
012: import prefuse.visual.VisualTupleSet;
013:
014: /**
015: * DynamicQueryBinding supporting text search over data values. Implementations
016: * of the {@link prefuse.data.search.SearchTupleSet} class from the
017: * {@link prefuse.data.search} package can be used to control the type of
018: * search index used.
019: *
020: * @author <a href="http://jheer.org">jeffrey heer</a>
021: * @see prefuse.data.search.SearchTupleSet
022: */
023: public class SearchQueryBinding extends DynamicQueryBinding {
024:
025: private SearchTupleSet m_set;
026: private Listener m_lstnr;
027: private Object m_lock;
028:
029: /**
030: * Create a new SearchQueryBinding over the given set and data field.
031: * @param ts the TupleSet to query
032: * @param field the data field (Table column) to query
033: */
034: public SearchQueryBinding(TupleSet ts, String field) {
035: this (ts, field, new PrefixSearchTupleSet());
036: }
037:
038: /**
039: * Create a new SearchQueryBinding over the given set and data field,
040: * using the specified SearchTupleSet instance. Use this constructor to
041: * choose the type of search engine used, and to potentially reuse the
042: * same search set over multiple dynamic query bindings.
043: * @param ts the TupleSet to query
044: * @param field the data field (Table column) to query
045: * @param set the {@link prefuse.data.search.SearchTupleSet} to use.
046: */
047: public SearchQueryBinding(TupleSet ts, String field,
048: SearchTupleSet set) {
049: super (ts, field);
050: m_lstnr = new Listener();
051: setPredicate(new SearchBindingPredicate());
052:
053: m_set = set;
054: m_set.index(ts.tuples(), field);
055: m_set.addTupleSetListener(m_lstnr);
056:
057: if (ts instanceof VisualTupleSet)
058: m_lock = ((VisualTupleSet) ts).getVisualization();
059: }
060:
061: /**
062: * Return the SearchTupleSet used for conducting searches.
063: * @return the {@link prefuse.data.search.SearchTupleSet} used by this
064: * dynamic query binding.
065: */
066: public SearchTupleSet getSearchSet() {
067: return m_set;
068: }
069:
070: // ------------------------------------------------------------------------
071:
072: /**
073: * Create a new search text panel for searching over the data.
074: * @return a {@link prefuse.util.ui.JSearchPanel} bound to this
075: * dynamic query.
076: * @see prefuse.data.query.DynamicQueryBinding#createComponent()
077: */
078: public JComponent createComponent() {
079: return createSearchPanel();
080: }
081:
082: /**
083: * Create a new search text panel for searching over the data.
084: * @return a {@link prefuse.util.ui.JSearchPanel} bound to this
085: * dynamic query.
086: */
087: public JSearchPanel createSearchPanel() {
088: return createSearchPanel(m_set instanceof PrefixSearchTupleSet);
089: }
090:
091: /**
092: * Create a new search text panel for searching over the data.
093: * @param monitorKeystrokes if true, each keystroke will cause the
094: * search to be re-run (this is the default for prefix searches);
095: * if false, searches will only re-run when the enter key is typed
096: * (this is the default for the other search engine types).
097: * @return a {@link prefuse.util.ui.JSearchPanel} bound to this
098: * dynamic query.
099: */
100: public JSearchPanel createSearchPanel(boolean monitorKeystrokes) {
101: JSearchPanel jsp = new JSearchPanel(m_set, m_field,
102: monitorKeystrokes);
103: if (m_lock != null) {
104: jsp.setLock(m_lock);
105: }
106: return jsp;
107: }
108:
109: // ------------------------------------------------------------------------
110:
111: private class SearchBindingPredicate extends AbstractPredicate {
112: public boolean getBoolean(Tuple t) {
113: String q = m_set.getQuery();
114: return (q == null || q.length() == 0 || m_set
115: .containsTuple(t));
116: }
117:
118: public void touch() {
119: this .fireExpressionChange();
120: }
121: }
122:
123: private class Listener implements TupleSetListener {
124: public void tupleSetChanged(TupleSet tset, Tuple[] added,
125: Tuple[] removed) {
126: ((SearchBindingPredicate) getPredicate()).touch();
127: }
128: }
129:
130: } // end of class SearchQueryBinding
|