KickJava   Java API By Example, From Geeks To Geeks.

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


1 package prefuse.data.query;
2
3 import javax.swing.JComboBox JavaDoc;
4 import javax.swing.JComponent JavaDoc;
5 import javax.swing.JList JavaDoc;
6 import javax.swing.event.ListSelectionEvent JavaDoc;
7 import javax.swing.event.ListSelectionListener JavaDoc;
8
9 import prefuse.data.Table;
10 import prefuse.data.column.ColumnMetadata;
11 import prefuse.data.expression.BooleanLiteral;
12 import prefuse.data.expression.ColumnExpression;
13 import prefuse.data.expression.ComparisonPredicate;
14 import prefuse.data.expression.Expression;
15 import prefuse.data.expression.Literal;
16 import prefuse.data.expression.OrPredicate;
17 import prefuse.data.expression.Predicate;
18 import prefuse.data.tuple.TupleSet;
19 import prefuse.util.DataLib;
20 import prefuse.util.ui.JToggleGroup;
21
22 /**
23  * DynamicQueryBinding supporting queries based on a list of included
24  * data values.
25  * @author <a HREF="http://jheer.org">jeffrey heer</a>
26  */

27 public class ListQueryBinding extends DynamicQueryBinding {
28
29     /** String used to indicate inclusion of all data values. */
30     private static final String JavaDoc ALL = "All";
31     
32     private Class JavaDoc m_type;
33     private ListModel m_model;
34     private Listener JavaDoc m_lstnr;
35     private boolean m_includeAll;
36     
37     /**
38      * Create a new ListQueryBinding over the given set and data field.
39      * @param ts the TupleSet to query
40      * @param field the data field (Table column) to query
41      */

42     public ListQueryBinding(TupleSet ts, String JavaDoc field) {
43         this(ts, field, true);
44     }
45     
46     /**
47      * Create a new ListQueryBinding over the given set and data field.
48      * @param ts the TupleSet to query
49      * @param field the data field (Table column) to query
50      * @param includeAllOption indicates if the dynamic queries should
51      * include an "All" option for including all data values
52      */

53     public ListQueryBinding(TupleSet ts, String JavaDoc field, boolean includeAllOption) {
54         super(ts, field);
55         m_type = DataLib.inferType(ts, field);
56         m_lstnr = new Listener JavaDoc();
57         m_includeAll = includeAllOption;
58         initPredicate();
59         initModel();
60     }
61     
62     private void initPredicate() {
63         // set up predicate
64
OrPredicate orP = new OrPredicate();
65         orP.add(BooleanLiteral.TRUE);
66         setPredicate(orP);
67     }
68     
69     private void initModel() {
70         if ( m_model != null )
71             m_model.removeListSelectionListener(m_lstnr);
72         
73         // set up data / selection model
74
Object JavaDoc[] o = null;
75         if ( m_tuples instanceof Table ) {
76             ColumnMetadata md = ((Table)m_tuples).getMetadata(m_field);
77             o = md.getOrdinalArray();
78         } else {
79             o = DataLib.ordinalArray(m_tuples.tuples(), m_field);
80         }
81         m_model = new ListModel(o);
82         m_model.addListSelectionListener(m_lstnr);
83         if ( m_includeAll ) {
84             m_model.insertElementAt(ALL, 0);
85             m_model.setSelectedItem(ALL);
86         }
87     }
88
89     // ------------------------------------------------------------------------
90

91     /**
92      * Returns a list model for creating custom dynamic query widgets.
93      * This list model acts both as a data model and a selection model,
94      * and so must be registered as both with any custom widgets.
95      * @return the dynamic query list model
96      */

97     public ListModel getListModel() {
98         return m_model;
99     }
100     
101     /**
102      * Creates a new group of check boxes for interacting with the query.
103      * @return a {@link prefuse.util.ui.JToggleGroup} of check boxes bound to
104      * this dynamic query.
105      * @see prefuse.data.query.DynamicQueryBinding#createComponent()
106      */

107     public JComponent JavaDoc createComponent() {
108         return createCheckboxGroup();
109     }
110     
111     /**
112      * Create a new interactive list for interacting with the query.
113      * @return a {@link javax.swing.JList} bound to this dynamic query.
114      */

115     public JList JavaDoc createList() {
116         JList JavaDoc list = new JList JavaDoc(m_model);
117         list.setSelectionModel(m_model);
118         return list;
119     }
120     
121     /**
122      * Create a new drop-down combo box for interacting with the query.
123      * @return a {@link javax.swing.JComboBox} bound to this dynamic query.
124      */

125     public JComboBox JavaDoc createComboBox() {
126         return new JComboBox JavaDoc(m_model);
127     }
128
129     /**
130      * Creates a new group of check boxes for interacting with the query.
131      * @return a {@link prefuse.util.ui.JToggleGroup} of check boxes bound to
132      * this dynamic query.
133      */

134     public JToggleGroup createCheckboxGroup() {
135         return createToggleGroup(JToggleGroup.CHECKBOX);
136     }
137     
138     /**
139      * Creates a new group of radio buttons for interacting with the query.
140      * @return a {@link prefuse.util.ui.JToggleGroup} of radio buttons bound to
141      * this dynamic query.
142      */

143     public JToggleGroup createRadioGroup() {
144         return createToggleGroup(JToggleGroup.RADIO);
145     }
146     
147     private JToggleGroup createToggleGroup(int type) {
148         return new JToggleGroup(type, m_model, m_model);
149     }
150     
151     // ------------------------------------------------------------------------
152

153     /**
154      * Create a comparison predicate fof the given data value
155      */

156     private ComparisonPredicate getComparison(Object JavaDoc o) {
157         Expression left = new ColumnExpression(m_field);
158         Expression right = Literal.getLiteral(o, m_type);
159         return new ComparisonPredicate(ComparisonPredicate.EQ, left, right);
160     }
161     
162     private class Listener implements ListSelectionListener JavaDoc {
163
164         public void valueChanged(ListSelectionEvent JavaDoc e) {
165             ListModel model = (ListModel)e.getSource();
166             OrPredicate orP = (OrPredicate)m_query;
167             
168             if ( model.isSelectionEmpty() )
169             {
170                 orP.clear();
171             }
172             else if ( m_includeAll && model.isSelectedIndex(0) )
173             {
174                 orP.set(BooleanLiteral.TRUE);
175             }
176             else
177             {
178                 int min = model.getMinSelectionIndex();
179                 int max = model.getMaxSelectionIndex();
180                 int count = 0;
181                 for ( int i=min; i<=max; ++i ) {
182                     if ( model.isSelectedIndex(i) )
183                         ++count;
184                 }
185             
186                 if ( count == model.getSize() ) {
187                     orP.set(BooleanLiteral.TRUE);
188                 } else if ( count == 1 ) {
189                     orP.set(getComparison(model.getElementAt(min)));
190                 } else {
191                     Predicate[] p = new Predicate[count];
192                     for ( int i=min, j=0; i<=max; ++i ) {
193                         if ( model.isSelectedIndex(i) )
194                             p[j++] = getComparison(model.getElementAt(i));
195                     }
196                     orP.set(p);
197                 }
198             }
199         }
200     }
201     
202 } // end of class ListQueryBinding
203
Popular Tags