KickJava   Java API By Example, From Geeks To Geeks.

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


1 package prefuse.data.query;
2
3 import java.awt.event.FocusEvent JavaDoc;
4 import java.awt.event.FocusListener JavaDoc;
5
6 import javax.swing.JComponent JavaDoc;
7 import javax.swing.JSlider JavaDoc;
8 import javax.swing.event.ChangeEvent JavaDoc;
9 import javax.swing.event.ChangeListener JavaDoc;
10
11 import prefuse.data.expression.ColumnExpression;
12 import prefuse.data.expression.Literal;
13 import prefuse.data.expression.RangePredicate;
14 import prefuse.data.tuple.TupleSet;
15 import prefuse.util.DataLib;
16 import prefuse.util.TypeLib;
17 import prefuse.util.ui.JRangeSlider;
18 import prefuse.util.ui.ValuedRangeModel;
19
20 /**
21  * DynamicQueryBinding supporting queries based on a range of included
22  * data values.
23  * @author <a HREF="http://jheer.org">jeffrey heer</a>
24  */

25 public class RangeQueryBinding extends DynamicQueryBinding {
26
27     private Class JavaDoc m_type;
28     private Listener JavaDoc m_lstnr;
29     private ValuedRangeModel m_model;
30     private boolean m_ordinal;
31     
32     private static FocusListener JavaDoc s_sliderAdj;
33     
34     /**
35      * Create a new RangeQueryBinding over the given set and data field.
36      * @param ts the TupleSet to query
37      * @param field the data field (Table column) to query
38      */

39     public RangeQueryBinding(TupleSet ts, String JavaDoc field) {
40         this(ts, field, false);
41     }
42     
43     /**
44      * Create a new RangeQueryBinding over the given set and data field,
45      * optionally forcing an ordinal treatment of data.
46      * @param ts the TupleSet to query
47      * @param field the data field (Table column) to query
48      * @param forceOrdinal if true, forces all items in the range to be
49      * treated in strictly ordinal fashion. That means that if the data
50      * is numerical, the quantitative nature of the data will be ignored
51      * and only the relative ordering of the numbers will matter. In terms
52      * of mechanism, this entails that a {@link ObjectRangeModel} and not
53      * a {@link NumberRangeModel} will be used to represent the data. If
54      * the argument is false, default inference mechanisms will be used.
55      */

56     public RangeQueryBinding(TupleSet ts, String JavaDoc field, boolean forceOrdinal) {
57         super(ts, field);
58         m_type = DataLib.inferType(ts, field);
59         m_ordinal = forceOrdinal;
60         m_lstnr = new Listener JavaDoc();
61         initPredicate();
62         initModel();
63     }
64     
65     private void initPredicate() {
66         // determine minimum and maximum values
67
Object JavaDoc min = DataLib.min(m_tuples, m_field).get(m_field);
68         Object JavaDoc max = DataLib.max(m_tuples, m_field).get(m_field);
69         
70         // set up predicate
71
Literal left = Literal.getLiteral(min, m_type);
72         Literal right = Literal.getLiteral(max, m_type);
73         ColumnExpression ce = new ColumnExpression(m_field);
74         RangePredicate rp = new RangePredicate(ce, left, right);
75         setPredicate(rp);
76     }
77     
78     public void initModel() {
79         if ( m_model != null )
80             m_model.removeChangeListener(m_lstnr);
81         
82         // set up data / selection model
83
ValuedRangeModel model = null;
84         if ( TypeLib.isNumericType(m_type) && !m_ordinal ) {
85             Number JavaDoc min = (Number JavaDoc)DataLib.min(m_tuples, m_field).get(m_field);
86             Number JavaDoc max = (Number JavaDoc)DataLib.max(m_tuples, m_field).get(m_field);
87             model = new NumberRangeModel(min, max, min, max);
88         } else {
89             model = new ObjectRangeModel(
90                         DataLib.ordinalArray(m_tuples, m_field));
91         }
92         m_model = model;
93         m_model.addChangeListener(m_lstnr);
94     }
95
96     /**
97      * Return the ValuedRangeModel constructed by this dynamic query binding.
98      * This model backs any user interface components generated by this
99      * instance.
100      * @return the ValuedRangeModel for this range query binding.
101      */

102     public ValuedRangeModel getModel() {
103         return m_model;
104     }
105     
106     /**
107      * Attempts to return the ValuedRangeModel for this binding as a
108      * NumberRangeModel. If the range model is not an instance of
109      * {@link NumberRangeModel}, a null value is returned.
110      * @return the ValuedRangeModel for this binding as a
111      * {@link NumberRangeModel}, or null if the range is not numerical.
112      */

113     public NumberRangeModel getNumberModel() {
114         return (m_model instanceof NumberRangeModel ?
115                 (NumberRangeModel)m_model : null);
116     }
117     
118     /**
119      * Attempts to return the ValuedRangeModel for this binding as an
120      * ObjectRangeModel. If the range model is not an instance of
121      * {@link ObjectRangeModel}, a null value is returned.
122      * @return the ValuedRangeModel for this binding as an
123      * {@link ObjectRangeModel}, or null if the range is numerical.
124      */

125     public ObjectRangeModel getObjectModel() {
126         return (m_model instanceof ObjectRangeModel ?
127                 (ObjectRangeModel)m_model : null);
128     }
129     
130     // ------------------------------------------------------------------------
131

132     /**
133      * Create a new horizontal range slider for interacting with the query.
134      * @return a {@link prefuse.util.ui.JRangeSlider} bound to this dynamic
135      * query.
136      * @see prefuse.data.query.DynamicQueryBinding#createComponent()
137      */

138     public JComponent JavaDoc createComponent() {
139         return createHorizontalRangeSlider();
140     }
141     
142     /**
143      * Create a new horizontal range slider for interacting with the query.
144      * @return a {@link prefuse.util.ui.JRangeSlider} bound to this dynamic
145      * query.
146      */

147     public JRangeSlider createHorizontalRangeSlider() {
148         return createRangeSlider(JRangeSlider.HORIZONTAL,
149                 JRangeSlider.LEFTRIGHT_TOPBOTTOM);
150     }
151
152     /**
153      * Create a new vertical range slider for interacting with the query.
154      * @return a {@link prefuse.util.ui.JRangeSlider} bound to this dynamic
155      * query.
156      */

157     public JRangeSlider createVerticalRangeSlider() {
158         return createRangeSlider(JRangeSlider.VERTICAL,
159                 JRangeSlider.RIGHTLEFT_BOTTOMTOP);
160     }
161     
162     /**
163      * Create a new range slider for interacting with the query, using the
164      * given orientation and direction.
165      * @param orientation the orientation (horizontal or vertical) of the
166      * slider (see {@link prefuse.util.ui.JRangeSlider})
167      * @param direction the direction (direction of data values) of the slider
168      * (see {@link prefuse.util.ui.JRangeSlider})
169      * @return a {@link prefuse.util.ui.JRangeSlider} bound to this dynamic
170      * query.
171      */

172     public JRangeSlider createRangeSlider(int orientation, int direction) {
173         return new JRangeSlider(m_model, orientation, direction);
174     }
175     
176     /**
177      * Create a new regular (non-range) slider for interacting with the query.
178      * This allows you to select a single value at a time.
179      * @return a {@link javax.swing.JSlider} bound to this dynamic query.
180      */

181     public JSlider JavaDoc createSlider() {
182         JSlider JavaDoc slider = new JSlider JavaDoc(m_model);
183         slider.addFocusListener(getSliderAdjuster());
184         return slider;
185     }
186     
187     private synchronized static FocusListener JavaDoc getSliderAdjuster() {
188         if ( s_sliderAdj == null )
189             s_sliderAdj = new SliderAdjuster();
190         return s_sliderAdj;
191     }
192     
193     // ------------------------------------------------------------------------
194

195     private static class SliderAdjuster implements FocusListener JavaDoc {
196         public void focusGained(FocusEvent JavaDoc e) {
197             ((JSlider JavaDoc)e.getSource()).setExtent(0);
198         }
199         public void focusLost(FocusEvent JavaDoc e) {
200             // do nothing
201
}
202     } // end of inner class SliderAdjuster
203

204     private class Listener implements ChangeListener JavaDoc {
205         public void stateChanged(ChangeEvent JavaDoc e) {
206             ValuedRangeModel model = (ValuedRangeModel)e.getSource();
207             Object JavaDoc lo = model.getLowValue();
208             Object JavaDoc hi = model.getHighValue();
209             
210             RangePredicate rp = (RangePredicate)m_query;
211             rp.setLeftExpression(Literal.getLiteral(lo, m_type));
212             rp.setRightExpression(Literal.getLiteral(hi, m_type));
213         }
214     } // end of inner class Listener
215

216 } // end of class RangeQueryBinding
217
Popular Tags