KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > visual > expression > SearchPredicate


1 package prefuse.visual.expression;
2
3 import prefuse.Visualization;
4 import prefuse.data.Tuple;
5 import prefuse.data.expression.BooleanLiteral;
6 import prefuse.data.expression.Expression;
7 import prefuse.data.search.SearchTupleSet;
8 import prefuse.visual.VisualItem;
9
10 /**
11  * Expression that indicates if an item is currently a member of a data group of the type
12  * {@link prefuse.data.search.SearchTupleSet}, but including a possible special case in
13  * which all items should be pass through the predicate if no search query is specified.
14  * The data group name is provided by a String-valued sub-expression.
15  *
16  * @author <a HREF="http://jheer.org">jeffrey heer</a>
17  */

18 public class SearchPredicate extends InGroupPredicate {
19
20     private Expression m_incEmpty;
21     private int paramCount = 0;
22     
23     /**
24      * Create a new SearchPredicate. By default, looks into the
25      * {@link prefuse.Visualization#ALL_ITEMS} data group and assumes all items
26      * should pass the predicate if no search query has been specified.
27      */

28     public SearchPredicate() {
29         this(Visualization.SEARCH_ITEMS, true);
30         paramCount = 0;
31     }
32     
33     /**
34      * Create a new SearchPredicate. By default, looks into the
35      * {@link prefuse.Visualization#ALL_ITEMS} data group.
36      * @param includeAllByDefault indicates if all items
37      * should pass the predicate if no search query has been specified.
38      */

39     public SearchPredicate(boolean includeAllByDefault) {
40         this(Visualization.SEARCH_ITEMS, includeAllByDefault);
41     }
42     
43     /**
44      * Create a new SearchPredicate.
45      * @param group the data group to look up, should resolve to a
46      * {@link prefuse.data.search.SearchTupleSet} instance.
47      * @param includeAllByDefault indicates if all items
48      * should pass the predicate if no search query has been specified.
49      */

50     public SearchPredicate(String JavaDoc group, boolean includeAllByDefault) {
51         super(group);
52         m_incEmpty = new BooleanLiteral(includeAllByDefault);
53         paramCount = 2;
54     }
55     
56     /**
57      * @see prefuse.data.expression.Expression#getBoolean(prefuse.data.Tuple)
58      */

59     public boolean getBoolean(Tuple t) {
60         String JavaDoc group = getGroup(t);
61         if ( group == null ) return false;
62         boolean incEmpty = m_incEmpty.getBoolean(t);
63         
64         VisualItem item = (VisualItem)t;
65         Visualization vis = item.getVisualization();
66         SearchTupleSet search = (SearchTupleSet)vis.getGroup(group);
67         if ( search == null && incEmpty )
68             return true;
69         
70         String JavaDoc query = search.getQuery();
71         return (incEmpty && (query==null || query.length()==0))
72                 || vis.isInGroup(item, group);
73     }
74
75     /**
76      * @see prefuse.data.expression.Function#addParameter(prefuse.data.expression.Expression)
77      */

78     public void addParameter(Expression e) {
79         if ( paramCount == 0 )
80             super.addParameter(e);
81         else if ( paramCount == 1 )
82             m_incEmpty = e;
83         else
84             throw new IllegalStateException JavaDoc(
85               "This function takes only 2 parameters.");
86     }
87
88     /**
89      * @see prefuse.data.expression.Function#getName()
90      */

91     public String JavaDoc getName() {
92         return "MATCH";
93     }
94
95     /**
96      * @see prefuse.data.expression.Function#getParameterCount()
97      */

98     public int getParameterCount() {
99         return 2;
100     }
101     
102     /**
103      * @see java.lang.Object#toString()
104      */

105     public String JavaDoc toString() {
106         return getName()+"("+m_group+", "+m_incEmpty+")";
107     }
108     
109 } // end of class SearchPredicate
110
Popular Tags