KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > monitorenter > gui > chart > layout > controls > RangeChooserPanel


1 /*
2  * RangePanel.java of project jchart2d
3  * Copyright 2006 (C) Achim Westermann, created on 09:50:20.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * If you modify or optimize the code in a useful way please let me know.
20  * Achim.Westermann@gmx.de
21  *
22  */

23 package info.monitorenter.gui.chart.layout.controls;
24
25 import info.monitorenter.util.Range;
26 import infovis.panel.DoubleBoundedRangeModel;
27 import infovis.panel.dqinter.DoubleRangeSlider;
28
29 import java.awt.Dimension JavaDoc;
30 import java.awt.event.ActionEvent JavaDoc;
31 import java.awt.event.ActionListener JavaDoc;
32 import java.text.NumberFormat JavaDoc;
33 import java.text.ParseException JavaDoc;
34 import java.util.Locale JavaDoc;
35
36 import javax.swing.Box JavaDoc;
37 import javax.swing.BoxLayout JavaDoc;
38 import javax.swing.JPanel JavaDoc;
39 import javax.swing.JTextField JavaDoc;
40 import javax.swing.event.ChangeEvent JavaDoc;
41 import javax.swing.event.ChangeListener JavaDoc;
42
43
44 /**
45  * A panel that allows to choose a range from a special
46  * {@link javax.swing.JSlider} with two sliders (dual Slider).
47  * <p>
48  * Credits go to the infovis project of Jean Daniel Fekete for the dual slider
49  * comopenent: <a HREF="http://ivtk.sourceforge.net/"
50  * target="_blank">http://ivtk.sourceforge.net/</a>.
51  * <p>
52  *
53  * @author <a HREF="mailto:Achim.Westermann@gmx.de">Achim Westermann </a>
54  * @version $Revision: 1.1 $
55  */

56 public class RangeChooserPanel extends JPanel JavaDoc {
57
58   /**
59    * Generated <code>serialVersionUID</code>.
60    */

61   private static final long serialVersionUID = 3258413911148410931L;
62
63   /** Used for formatting the numbers displayed in the text fields. */
64   private NumberFormat JavaDoc m_nf = NumberFormat.getNumberInstance(Locale.getDefault());
65
66   /** The bislider to choose a range. */
67   private DoubleRangeSlider m_slider;
68
69   /**
70    * Creates an instance that works on the given range.
71    * <p>
72    *
73    * @param range
74    * defines the bounds of the current selection and the extension of
75    * these that is choosable.
76    */

77   public RangeChooserPanel(final Range range) {
78
79     super();
80     this.m_nf.setMinimumFractionDigits(2);
81     this.m_nf.setMaximumFractionDigits(2);
82
83     // TODO: refactor calling action class to reuse this instance.
84
double min = range.getMin();
85     double max = range.getMax();
86     double minBound = min;
87     double maxBound = max;
88     if (minBound == Double.NEGATIVE_INFINITY || minBound == Double.NaN
89         || minBound == Double.POSITIVE_INFINITY || minBound == -Double.MAX_VALUE) {
90       min = -100;
91     }
92     minBound = min - (max - min) / 2;
93
94     if (maxBound == Double.NEGATIVE_INFINITY || maxBound == Double.NaN
95         || maxBound == Double.POSITIVE_INFINITY || maxBound == Double.MAX_VALUE) {
96       max = 100;
97     }
98     maxBound = max + (max - min) / 2;
99
100     this.m_slider = new DoubleRangeSlider(minBound, maxBound, min, max);
101
102     this.setLayout(new BoxLayout JavaDoc(this, BoxLayout.Y_AXIS));
103     this.m_slider.setMaximumSize(new Dimension JavaDoc(300, 40));
104     this.m_slider.setEnabled(true);
105     this.add(Box.createVerticalStrut(10));
106     this.add(this.m_slider);
107     // text field views to show the range values;
108
// minimum value view:
109
final JTextField JavaDoc rangeMinView = new JTextField JavaDoc();
110     rangeMinView.setText(this.m_nf.format(new Double JavaDoc(this.m_slider.getLowValue())));
111     DoubleBoundedRangeModel rangeModel = this.m_slider.getModel();
112     rangeMinView.setEditable(true);
113     rangeModel.addChangeListener(new ChangeListener JavaDoc() {
114       public void stateChanged(final ChangeEvent JavaDoc e) {
115         rangeMinView.setText(RangeChooserPanel.this.m_nf.format(new Double JavaDoc(
116             RangeChooserPanel.this.m_slider.getLowValue())));
117       }
118     });
119     rangeMinView.setPreferredSize(new Dimension JavaDoc(120, 20));
120     rangeMinView.addActionListener(new ActionListener JavaDoc() {
121       public void actionPerformed(final ActionEvent JavaDoc ae) {
122         JTextField JavaDoc textField = (JTextField JavaDoc) ae.getSource();
123         try {
124           Number JavaDoc entered = RangeChooserPanel.this.m_nf.parse(textField.getText());
125           double low = entered.doubleValue();
126           double high = RangeChooserPanel.this.m_slider.getHighValue();
127           double minSlider = low - (high - low) / 2;
128           RangeChooserPanel.this.m_slider.setMinimum(minSlider);
129           RangeChooserPanel.this.m_slider.setLowValue(low);
130         } catch (ParseException JavaDoc e) {
131           // TODO: maybe inform user of invalid input.
132
textField.setText(RangeChooserPanel.this.m_nf.format(RangeChooserPanel.this.m_slider
133               .getLowValue()));
134         }
135       }
136     });
137     rangeMinView.setToolTipText("Enter a number and hit Return.");
138
139     // maximum value view:
140
final JTextField JavaDoc rangeMaxView = new JTextField JavaDoc();
141     rangeMaxView.setText(new Double JavaDoc(this.m_slider.getHighValue()).toString());
142     rangeMaxView.setEditable(true);
143     rangeModel.addChangeListener(new ChangeListener JavaDoc() {
144       public void stateChanged(final ChangeEvent JavaDoc e) {
145         rangeMaxView.setText(RangeChooserPanel.this.m_nf.format(new Double JavaDoc(
146             RangeChooserPanel.this.m_slider.getHighValue())));
147       }
148     });
149     rangeMaxView.setPreferredSize(new Dimension JavaDoc(120, 20));
150     rangeMaxView.addActionListener(new ActionListener JavaDoc() {
151       public void actionPerformed(final ActionEvent JavaDoc ae) {
152         JTextField JavaDoc textField = (JTextField JavaDoc) ae.getSource();
153         try {
154           Number JavaDoc entered = RangeChooserPanel.this.m_nf.parse(textField.getText());
155           double low = RangeChooserPanel.this.m_slider.getLowValue();
156           double high = entered.doubleValue();
157           double maxSlider = high + (high - low) / 2;
158           RangeChooserPanel.this.m_slider.setMaximum(maxSlider);
159           RangeChooserPanel.this.m_slider.setHighValue(high);
160         } catch (ParseException JavaDoc e) {
161           // TODO: maybe inform user of invalid input.
162
textField.setText(RangeChooserPanel.this.m_nf.format(RangeChooserPanel.this.m_slider
163               .getHighValue()));
164         }
165       }
166     });
167     rangeMaxView.setToolTipText("Enter a number and hit Return.");
168
169     // add range views to panel:
170
JPanel JavaDoc rangeViewPanel = new JPanel JavaDoc();
171     rangeViewPanel.setMaximumSize(new Dimension JavaDoc(300, 30));
172     rangeViewPanel.setLayout(new BoxLayout JavaDoc(rangeViewPanel, BoxLayout.X_AXIS));
173     rangeViewPanel.add(Box.createHorizontalGlue());
174     rangeViewPanel.add(rangeMinView);
175     rangeViewPanel.add(Box.createHorizontalStrut(10));
176     rangeViewPanel.add(rangeMaxView);
177     rangeViewPanel.add(Box.createHorizontalGlue());
178     this.add(Box.createVerticalStrut(10));
179     this.add(rangeViewPanel);
180     this.add(Box.createVerticalGlue());
181   }
182
183   /**
184    * Returns the current selected range.
185    * <p>
186    *
187    * @return the current selected range.
188    */

189   public Range getRange() {
190     return new Range(this.m_slider.getLowValue(), this.m_slider.getHighValue());
191   }
192 }
193
Popular Tags