KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > experimental > chart > swt > editor > SWTNumberAxisEditor


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * ------------------------
28  * SWTNumberAxisEditor.java
29  * ------------------------
30  * (C) Copyright 2006, by Henry Proudhon and Contributors.
31  *
32  * Original Author: Henry Proudhon (henry.proudhon AT insa-lyon.fr);
33  * Contributor(s): David Gilbert (for Object Refinery Limited);
34  *
35  * Changes
36  * -------
37  * 01-Aug-2006 : New class (HP);
38  *
39  */

40
41 package org.jfree.experimental.chart.swt.editor;
42
43 import org.eclipse.swt.SWT;
44 import org.eclipse.swt.events.FocusEvent;
45 import org.eclipse.swt.events.FocusListener;
46 import org.eclipse.swt.events.SelectionAdapter;
47 import org.eclipse.swt.events.SelectionEvent;
48 import org.eclipse.swt.layout.GridData;
49 import org.eclipse.swt.layout.GridLayout;
50 import org.eclipse.swt.widgets.Button;
51 import org.eclipse.swt.widgets.Composite;
52 import org.eclipse.swt.widgets.Label;
53 import org.eclipse.swt.widgets.TabItem;
54 import org.eclipse.swt.widgets.Text;
55 import org.jfree.chart.axis.Axis;
56 import org.jfree.chart.axis.NumberAxis;
57
58 /**
59  * An editor for {@link NumberAxis} properties.
60  */

61 class SWTNumberAxisEditor extends SWTAxisEditor implements FocusListener {
62     
63     /** A flag that indicates whether or not the axis range is determined
64      * automatically.
65      */

66     private boolean autoRange;
67
68     /** The lowest value in the axis range. */
69     private double minimumValue;
70
71     /** The highest value in the axis range. */
72     private double maximumValue;
73
74     /** A checkbox that indicates whether or not the axis range is determined
75      * automatically.
76      */

77     private Button autoRangeCheckBox;
78
79     /** A text field for entering the minimum value in the axis range. */
80     private Text minimumRangeValue;
81
82     /** A text field for entering the maximum value in the axis range. */
83     private Text maximumRangeValue;
84
85     /**
86      * Creates a new editor.
87      *
88      * @param parent the parent.
89      * @param style the style.
90      * @param axis the axis.
91      */

92     public SWTNumberAxisEditor(Composite parent, int style, NumberAxis axis) {
93         super(parent, style, axis);
94         this.autoRange = axis.isAutoRange();
95         this.minimumValue = axis.getLowerBound();
96         this.maximumValue = axis.getUpperBound();
97
98         TabItem item2 = new TabItem(getOtherTabs(), SWT.NONE);
99         item2.setText(" " + localizationResources.getString("Range") + " ");
100         Composite range = new Composite(getOtherTabs(), SWT.NONE);
101         range.setLayout(new GridLayout(2, true));
102         item2.setControl(range);
103         
104         autoRangeCheckBox = new Button(range, SWT.CHECK);
105         autoRangeCheckBox.setText(localizationResources.getString(
106                 "Auto-adjust_range"));
107         autoRangeCheckBox.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
108                 true, false, 2, 1));
109         autoRangeCheckBox.setSelection(this.autoRange);
110         autoRangeCheckBox.addSelectionListener(
111                 new SelectionAdapter() {
112                     public void widgetSelected(SelectionEvent e) {
113                         toggleAutoRange();
114                     }
115                 });
116         new Label(range, SWT.NONE).setText(localizationResources.getString(
117                 "Minimum_range_value"));
118         this.minimumRangeValue = new Text(range, SWT.BORDER);
119         this.minimumRangeValue.setText(String.valueOf(this.minimumValue));
120         this.minimumRangeValue.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
121                 true, false));
122         this.minimumRangeValue.setEnabled(!this.autoRange);
123         //this.minimumRangeValue.addModifyListener(this);
124
//this.minimumRangeValue.addVerifyListener(this);
125
this.minimumRangeValue.addFocusListener(this);
126         new Label(range, SWT.NONE).setText(localizationResources.getString(
127                 "Maximum_range_value"));
128         this.maximumRangeValue = new Text(range, SWT.BORDER);
129         this.maximumRangeValue.setText(String.valueOf(this.maximumValue));
130         this.maximumRangeValue.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
131                 true, false));
132         this.maximumRangeValue.setEnabled(!this.autoRange);
133         //this.maximumRangeValue.addModifyListener(this);
134
//this.maximumRangeValue.addVerifyListener(this);
135
this.maximumRangeValue.addFocusListener(this);
136     }
137
138     /**
139      * Toggle the auto range setting.
140      */

141     public void toggleAutoRange() {
142         this.autoRange = this.autoRangeCheckBox.getSelection();
143         if (this.autoRange) {
144             this.minimumRangeValue.setText(Double.toString(this.minimumValue));
145             this.minimumRangeValue.setEnabled(false);
146             this.maximumRangeValue.setText(Double.toString(this.maximumValue));
147             this.maximumRangeValue.setEnabled(false);
148         }
149         else {
150             this.minimumRangeValue.setEnabled(true);
151             this.maximumRangeValue.setEnabled(true);
152         }
153     }
154
155     /**
156      * Revalidate the range minimum:
157      * it should be less than the current maximum.
158      *
159      * @param candidate the minimum value
160      *
161      * @return A boolean.
162      */

163     public boolean validateMinimum(String JavaDoc candidate)
164     {
165         boolean valid = true;
166         try {
167             if (Double.parseDouble(candidate) >= this.maximumValue) {
168                 valid = false;
169             }
170         }
171         catch (NumberFormatException JavaDoc e) {
172             valid = false;
173         }
174         return valid;
175     }
176
177     /**
178      * Revalidate the range maximum:
179      * it should be greater than the current minimum
180      *
181      * @param candidate the maximum value
182      *
183      * @return A boolean.
184      */

185     public boolean validateMaximum(String JavaDoc candidate)
186     {
187         boolean valid = true;
188         try {
189             if (Double.parseDouble(candidate) <= this.minimumValue) {
190                 valid = false;
191             }
192         }
193         catch (NumberFormatException JavaDoc e) {
194             valid = false;
195         }
196         return valid;
197     }
198
199     /* (non-Javadoc)
200      * @see org.eclipse.swt.events.FocusListener#focusGained(
201      * org.eclipse.swt.events.FocusEvent)
202      */

203     public void focusGained(FocusEvent e) {
204         // don't need to do anything
205
}
206
207     /* (non-Javadoc)
208      * @see org.eclipse.swt.events.FocusListener#focusLost(
209      * org.eclipse.swt.events.FocusEvent)
210      */

211     public void focusLost(FocusEvent e) {
212         if (e.getSource() == this.minimumRangeValue) {
213             // verify min value
214
if (! validateMinimum( this.minimumRangeValue.getText()))
215                 this.minimumRangeValue.setText(String.valueOf(
216                         this.minimumValue));
217             else
218                 this.minimumValue = Double.parseDouble(
219                         this.minimumRangeValue.getText());
220         }
221         else if (e.getSource() == this.maximumRangeValue) {
222             // verify max value
223
if (! validateMaximum(this.maximumRangeValue.getText()))
224                 this.maximumRangeValue.setText(String.valueOf(
225                         this.maximumValue));
226             else
227                 this.maximumValue = Double.parseDouble(
228                         this.maximumRangeValue.getText());
229         }
230     }
231
232     /**
233      * Sets the properties of the specified axis to match
234      * the properties defined on this panel.
235      *
236      * @param axis the axis.
237      */

238     public void setAxisProperties(Axis axis) {
239         super.setAxisProperties(axis);
240         NumberAxis numberAxis = (NumberAxis) axis;
241         numberAxis.setAutoRange(this.autoRange);
242         if (! this.autoRange)
243             numberAxis.setRange(this.minimumValue, this.maximumValue);
244     }
245 }
246
Popular Tags