KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > util > ui > JValueSlider


1 package prefuse.util.ui;
2
3 import java.awt.Color JavaDoc;
4 import java.awt.Dimension JavaDoc;
5 import java.awt.Font JavaDoc;
6 import java.awt.FontMetrics JavaDoc;
7 import java.awt.Graphics JavaDoc;
8 import java.awt.event.ActionEvent JavaDoc;
9 import java.awt.event.MouseAdapter JavaDoc;
10 import java.awt.event.MouseEvent JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.List JavaDoc;
14
15 import javax.swing.AbstractAction JavaDoc;
16 import javax.swing.BoxLayout JavaDoc;
17 import javax.swing.JComponent JavaDoc;
18 import javax.swing.JLabel JavaDoc;
19 import javax.swing.JSlider JavaDoc;
20 import javax.swing.JTextField JavaDoc;
21 import javax.swing.event.ChangeEvent JavaDoc;
22 import javax.swing.event.ChangeListener JavaDoc;
23
24 import prefuse.util.StringLib;
25
26 /**
27  * Swing component that contains a slider, and title label, and editable
28  * text box displaying the slider value.
29  *
30  * @author <a HREF="http://jheer.org">jeffrey heer</a>
31  */

32 public class JValueSlider extends JComponent JavaDoc {
33
34     private Number JavaDoc m_min, m_max, m_value;
35     private boolean m_ignore = false;
36     
37     private JLabel JavaDoc m_label;
38     private JSlider JavaDoc m_slider;
39     private JTextField JavaDoc m_field;
40     private List JavaDoc m_listeners;
41     
42     private int m_smin = 0;
43     private int m_srange = 100;
44     
45     /**
46      * Create a new JValueSlider.
47      * @param title the title label of the slider component
48      * @param min the value associated with the minimum slider position
49      * @param max the value associated with the maximum slider position
50      * @param value the value associated with the starting slider position
51      */

52     public JValueSlider(String JavaDoc title, double min, double max, double value) {
53         this(title, new Double JavaDoc(min), new Double JavaDoc(max), new Double JavaDoc(value));
54     }
55     
56     /**
57      * Create a new JValueSlider.
58      * @param title the title label of the slider component
59      * @param min the value associated with the minimum slider position
60      * @param max the value associated with the maximum slider position
61      * @param value the value associated with the starting slider position
62      */

63     public JValueSlider(String JavaDoc title, float min, float max, float value) {
64         this(title, new Float JavaDoc(min), new Float JavaDoc(max), new Float JavaDoc(value));
65     }
66     
67     /**
68      * Create a new JValueSlider.
69      * @param title the title label of the slider component
70      * @param min the value associated with the minimum slider position
71      * @param max the value associated with the maximum slider position
72      * @param value the value associated with the starting slider position
73      */

74     public JValueSlider(String JavaDoc title, int min, int max, int value) {
75         this(title, new Integer JavaDoc(min), new Integer JavaDoc(max), new Integer JavaDoc(value));
76         m_smin = min;
77         m_srange = max-min;
78         m_slider.setMinimum(min);
79         m_slider.setMaximum(max);
80         setValue(new Integer JavaDoc(value));
81     }
82     
83     /**
84      * Create a new JValueSlider.
85      * @param title the title label of the slider component
86      * @param min the value associated with the minimum slider position
87      * @param max the value associated with the maximum slider position
88      * @param value the value associated with the starting slider position
89      */

90     public JValueSlider(String JavaDoc title, long min, long max, long value) {
91         this(title, new Long JavaDoc(min), new Long JavaDoc(max), new Long JavaDoc(value));
92     }
93     
94     /**
95      * Create a new JValueSlider.
96      * @param title the title label of the slider component
97      * @param min the value associated with the minimum slider position
98      * @param max the value associated with the maximum slider position
99      * @param value the value associated with the starting slider position
100      */

101     public JValueSlider(String JavaDoc title, Number JavaDoc min, Number JavaDoc max, Number JavaDoc value) {
102         m_min = min;
103         m_max = max;
104         m_value = value;
105         m_slider = new JSlider JavaDoc();
106         m_label = new JLabel JavaDoc(title);
107         m_field = new JTextField JavaDoc();
108         m_listeners = new ArrayList JavaDoc();
109         
110         m_field.setBorder(null);
111         
112         setSliderValue();
113         setFieldValue();
114         
115         initUI();
116     }
117     
118     /**
119      * Initialize the UI
120      */

121     protected void initUI() {
122         m_slider.addChangeListener(new ChangeListener JavaDoc() {
123             public void stateChanged(ChangeEvent JavaDoc e) {
124                 if ( m_ignore ) return;
125                 m_ignore = true;
126                 // update the value
127
m_value = getSliderValue();
128                 // set text field value
129
setFieldValue();
130                 // fire event
131
fireChangeEvent();
132                 m_ignore = false;
133             }
134         });
135         m_field.addActionListener(new AbstractAction JavaDoc() {
136             public void actionPerformed(ActionEvent JavaDoc e) {
137                 if ( m_ignore ) return;
138                 m_ignore = true;
139                 Number JavaDoc v = getFieldValue();
140                 if ( v != m_value ) {
141                     // update the value
142
m_value = v;
143                     // set slider value
144
setSliderValue();
145                 }
146                 // fire event
147
fireChangeEvent();
148                 m_ignore = false;
149             }
150         });
151         m_field.addMouseListener(new MouseAdapter JavaDoc() {
152             public void mouseEntered(MouseEvent JavaDoc e) {
153                 String JavaDoc s = m_field.getText();
154                 if ( isTextObscured(m_field, s) )
155                     m_field.setToolTipText(s);
156             }
157             public void mouseExited(MouseEvent JavaDoc e) {
158                 m_field.setToolTipText(null);
159             }
160         });
161         m_label.addMouseListener(new MouseAdapter JavaDoc() {
162             public void mouseEntered(MouseEvent JavaDoc e) {
163                 String JavaDoc s = m_label.getText();
164                 if ( isTextObscured(m_label, s) )
165                     m_label.setToolTipText(s);
166             }
167             public void mouseExited(MouseEvent JavaDoc e) {
168                 m_label.setToolTipText(null);
169             }
170         });
171         
172         setLayout(new BoxLayout JavaDoc(this, BoxLayout.X_AXIS));
173         add(m_label);
174         add(m_slider);
175         add(m_field);
176     }
177     
178     /**
179      * Check if any label text is obscured.
180      */

181     private static boolean isTextObscured(JComponent JavaDoc c, String JavaDoc s) {
182         Graphics JavaDoc g = c.getGraphics();
183         FontMetrics JavaDoc fm = g.getFontMetrics(c.getFont());
184         int sw = fm.stringWidth(s);
185         return ( sw > c.getWidth() );
186     }
187     
188     // ------------------------------------------------------------------------
189

190     /**
191      * Get the current value ssociated with the slider position.
192      * @return the current value
193      */

194     public Number JavaDoc getValue() {
195         return m_value;
196     }
197
198     /**
199      * Set the current value ssociated with the slider position.
200      * @param value the current value to set
201      */

202     public void setValue(Number JavaDoc value) {
203         m_value = value;
204         setSliderValue();
205         setFieldValue();
206     }
207     
208     /**
209      * Compute the current slider value from the current slider position
210      * @return the current value
211      */

212     private Number JavaDoc getSliderValue() {
213         if ( m_value instanceof Integer JavaDoc ) {
214             int val = m_slider.getValue();
215             int min = m_min.intValue();
216             int max = m_max.intValue();
217             return new Integer JavaDoc(min + (val-m_smin)*(max-min)/m_srange);
218         } else if ( m_value instanceof Long JavaDoc ) {
219             int val = m_slider.getValue();
220             long min = m_min.longValue();
221             long max = m_max.longValue();
222             return new Long JavaDoc(min + (val-m_smin)*(max-min)/m_srange);
223         } else {
224             double f = (m_slider.getValue()-m_smin)/(double)m_srange;
225             double min = m_min.doubleValue();
226             double max = m_max.doubleValue();
227             double val = min + f*(max-min);
228             return (m_value instanceof Double JavaDoc ? (Number JavaDoc)new Double JavaDoc(val)
229                                               : new Float JavaDoc((float)val));
230         }
231     }
232     
233     /**
234      * Private set the slider position based upon the current value
235      */

236     private void setSliderValue() {
237         int val;
238         if ( m_value instanceof Double JavaDoc || m_value instanceof Float JavaDoc ) {
239             double value = m_value.doubleValue();
240             double min = m_min.doubleValue();
241             double max = m_max.doubleValue();
242             val = m_smin + (int)Math.round(m_srange*((value-min)/(max-min)));
243         } else {
244             long value = m_value.longValue();
245             long min = m_min.longValue();
246             long max = m_max.longValue();
247             val = m_smin + (int)((m_srange*(value-min))/(max-min));
248         }
249         m_slider.setValue(val);
250     }
251     
252     /**
253      * Get the value in the text field.
254      * @return the current text field value
255      */

256     private Number JavaDoc getFieldValue() {
257         if ( m_value instanceof Double JavaDoc || m_value instanceof Float JavaDoc ) {
258             double v;
259             try {
260                 v = Double.parseDouble(m_field.getText());
261             } catch ( Exception JavaDoc e ) {
262                 // TODO handle exception
263
return m_value;
264             }
265             if ( v < m_min.doubleValue() || v > m_max.doubleValue() ) {
266                 // TODO handle exception
267
return m_value;
268             }
269             return m_value instanceof Double JavaDoc ? (Number JavaDoc)new Double JavaDoc(v)
270                                              : new Float JavaDoc((float)v);
271         } else {
272             long v;
273             try {
274                 v = Long.parseLong(m_field.getText());
275             } catch ( Exception JavaDoc e ) {
276                 // TODO handle exception
277
return m_value;
278             }
279             if ( v < m_min.longValue() || v > m_max.longValue() ) {
280                 // TODO handle exception
281
return m_value;
282             }
283             return m_value instanceof Long JavaDoc ? (Number JavaDoc)new Long JavaDoc(v)
284                                            : new Integer JavaDoc((int)v);
285         }
286     }
287     
288     /**
289      * Set the text field value based upon the current value.
290      */

291     private void setFieldValue() {
292         String JavaDoc text;
293         if ( m_value instanceof Double JavaDoc || m_value instanceof Float JavaDoc )
294             text = StringLib.formatNumber(m_value.doubleValue(),3);
295         else
296             text = String.valueOf(m_value.longValue());
297         m_field.setText(text);
298     }
299     
300     // ------------------------------------------------------------------------
301

302     /**
303      * Add a change listener to listen to this component.
304      * @param cl the change listener to add
305      */

306     public void addChangeListener(ChangeListener JavaDoc cl) {
307         if ( !m_listeners.contains(cl) )
308             m_listeners.add(cl);
309     }
310
311     /**
312      * Remove a change listener listening to this component.
313      * @param cl the change listener to remove
314      */

315     public void removeChangeListener(ChangeListener JavaDoc cl) {
316         m_listeners.remove(cl);
317     }
318     
319     /**
320      * Fire a change event to listeners.
321      */

322     protected void fireChangeEvent() {
323         Iterator JavaDoc iter = m_listeners.iterator();
324         ChangeEvent JavaDoc evt = new ChangeEvent JavaDoc(this);
325         while ( iter.hasNext() ) {
326             ChangeListener JavaDoc cl = (ChangeListener JavaDoc)iter.next();
327             cl.stateChanged(evt);
328         }
329     }
330     
331     // ------------------------------------------------------------------------
332

333     /**
334      * @see java.awt.Component#setBackground(java.awt.Color)
335      */

336     public void setBackground(Color JavaDoc c) {
337         m_field.setBackground(c);
338         m_label.setBackground(c);
339         m_slider.setBackground(c);
340         super.setBackground(c);
341     }
342     
343     /**
344      * @see java.awt.Component#setForeground(java.awt.Color)
345      */

346     public void setForeground(Color JavaDoc c) {
347         m_field.setForeground(c);
348         m_label.setForeground(c);
349         m_slider.setForeground(c);
350         super.setForeground(c);
351     }
352     
353     /**
354      * @see java.awt.Component#setFont(java.awt.Font)
355      */

356     public void setFont(Font JavaDoc f) {
357         m_field.setFont(f);
358         m_label.setFont(f);
359         m_slider.setFont(f);
360         super.setFont(f);
361     }
362     
363     /**
364      * @see javax.swing.JComponent#setPreferredSize(java.awt.Dimension)
365      */

366     public void setPreferredSize(Dimension JavaDoc d) {
367         int fw = Math.min(40, d.width/5);
368         int lw = Math.min(100, (d.width-fw)/2);
369         int sw = d.width-fw-lw;
370         super.setPreferredSize(d);
371         Dimension JavaDoc dd = new Dimension JavaDoc(lw, d.height);
372         m_label.setPreferredSize(dd);
373         dd = new Dimension JavaDoc(sw, d.height);
374         m_slider.setPreferredSize(dd);
375         dd = new Dimension JavaDoc(fw, d.height);
376         m_field.setPreferredSize(dd);
377     }
378     
379 } // end of class JValueSlider
380
Popular Tags