KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > editor > DefaultNumberAxisEditor


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, 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  * DefaultNumberAxisEditor.java
29  * ----------------------------
30  * (C) Copyright 2005, Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): Arnaud Lelievre;
34  *
35  * $Id: DefaultNumberAxisEditor.java,v 1.1.2.1 2005/11/24 16:11:48 mungady Exp $
36  *
37  * Changes:
38  * --------
39  * 24-Nov-2005 : Version 1, based on NumberAxisPropertyEditor (DG);
40  */

41
42 package org.jfree.chart.editor;
43
44 import java.awt.BasicStroke JavaDoc;
45 import java.awt.Color JavaDoc;
46 import java.awt.event.ActionEvent JavaDoc;
47 import java.awt.event.FocusEvent JavaDoc;
48 import java.awt.event.FocusListener JavaDoc;
49 import java.util.ResourceBundle JavaDoc;
50
51 import javax.swing.BorderFactory JavaDoc;
52 import javax.swing.JCheckBox JavaDoc;
53 import javax.swing.JColorChooser JavaDoc;
54 import javax.swing.JLabel JavaDoc;
55 import javax.swing.JOptionPane JavaDoc;
56 import javax.swing.JPanel JavaDoc;
57 import javax.swing.JTabbedPane JavaDoc;
58 import javax.swing.JTextField JavaDoc;
59
60 import org.jfree.chart.axis.Axis;
61 import org.jfree.chart.axis.NumberAxis;
62 import org.jfree.layout.LCBLayout;
63 import org.jfree.ui.PaintSample;
64 import org.jfree.ui.StrokeChooserPanel;
65 import org.jfree.ui.StrokeSample;
66
67 /**
68  * A panel for editing the properties of a value axis.
69  */

70 class DefaultNumberAxisEditor extends DefaultAxisEditor
71                               implements FocusListener JavaDoc {
72
73     /** A flag that indicates whether or not the axis range is determined
74      * automatically.
75      */

76     private boolean autoRange;
77
78     /** The lowest value in the axis range. */
79     private double minimumValue;
80
81     /** The highest value in the axis range. */
82     private double maximumValue;
83
84     /** A checkbox that indicates whether or not the axis range is determined
85      * automatically.
86      */

87     private JCheckBox JavaDoc autoRangeCheckBox;
88
89     /** A text field for entering the minimum value in the axis range. */
90     private JTextField JavaDoc minimumRangeValue;
91
92     /** A text field for entering the maximum value in the axis range. */
93     private JTextField JavaDoc maximumRangeValue;
94
95     /** The paint selected for drawing the gridlines. */
96     private PaintSample gridPaintSample;
97
98     /** The stroke selected for drawing the gridlines. */
99     private StrokeSample gridStrokeSample;
100
101     /** An array of stroke samples to choose from (since I haven't written a
102      * decent StrokeChooser component yet).
103      */

104     private StrokeSample[] availableStrokeSamples;
105     
106     /** The resourceBundle for the localization. */
107     protected static ResourceBundle JavaDoc localizationResources =
108         ResourceBundle.getBundle("org.jfree.chart.editor.LocalizationBundle");
109
110     /**
111      * Standard constructor: builds a property panel for the specified axis.
112      *
113      * @param axis the axis, which should be changed.
114      */

115     public DefaultNumberAxisEditor(NumberAxis axis) {
116
117         super(axis);
118
119         this.autoRange = axis.isAutoRange();
120         this.minimumValue = axis.getLowerBound();
121         this.maximumValue = axis.getUpperBound();
122
123         this.gridPaintSample = new PaintSample(Color.blue);
124         this.gridStrokeSample = new StrokeSample(new BasicStroke JavaDoc(1.0f));
125
126         this.availableStrokeSamples = new StrokeSample[3];
127         this.availableStrokeSamples[0]
128             = new StrokeSample(new BasicStroke JavaDoc(1.0f));
129         this.availableStrokeSamples[1]
130             = new StrokeSample(new BasicStroke JavaDoc(2.0f));
131         this.availableStrokeSamples[2]
132             = new StrokeSample(new BasicStroke JavaDoc(3.0f));
133
134         JTabbedPane JavaDoc other = getOtherTabs();
135
136         JPanel JavaDoc range = new JPanel JavaDoc(new LCBLayout(3));
137         range.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
138
139         range.add(new JPanel JavaDoc());
140         this.autoRangeCheckBox = new JCheckBox JavaDoc(
141             localizationResources.getString("Auto-adjust_range"), this.autoRange
142         );
143         this.autoRangeCheckBox.setActionCommand("AutoRangeOnOff");
144         this.autoRangeCheckBox.addActionListener(this);
145         range.add(this.autoRangeCheckBox);
146         range.add(new JPanel JavaDoc());
147
148         range.add(
149             new JLabel JavaDoc(localizationResources.getString("Minimum_range_value"))
150         );
151         this.minimumRangeValue = new JTextField JavaDoc(
152             Double.toString(this.minimumValue)
153         );
154         this.minimumRangeValue.setEnabled(!this.autoRange);
155         this.minimumRangeValue.setActionCommand("MinimumRange");
156         this.minimumRangeValue.addActionListener(this);
157         this.minimumRangeValue.addFocusListener(this);
158         range.add(this.minimumRangeValue);
159         range.add(new JPanel JavaDoc());
160
161         range.add(
162             new JLabel JavaDoc(localizationResources.getString("Maximum_range_value"))
163         );
164         this.maximumRangeValue = new JTextField JavaDoc(
165             Double.toString(this.maximumValue)
166         );
167         this.maximumRangeValue.setEnabled(!this.autoRange);
168         this.maximumRangeValue.setActionCommand("MaximumRange");
169         this.maximumRangeValue.addActionListener(this);
170         this.maximumRangeValue.addFocusListener(this);
171         range.add(this.maximumRangeValue);
172         range.add(new JPanel JavaDoc());
173
174         other.add(localizationResources.getString("Range"), range);
175
176     }
177
178     /**
179      * Returns the current setting of the auto-range property.
180      *
181      * @return <code>true</code> if auto range is enabled.
182      */

183     public boolean isAutoRange() {
184         return this.autoRange;
185     }
186
187     /**
188      * Returns the current setting of the minimum value in the axis range.
189      *
190      * @return The current setting of the minimum value in the axis range.
191      */

192     public double getMinimumValue() {
193         return this.minimumValue;
194     }
195
196     /**
197      * Returns the current setting of the maximum value in the axis range.
198      *
199      * @return The current setting of the maximum value in the axis range.
200      */

201     public double getMaximumValue() {
202         return this.maximumValue;
203     }
204
205     /**
206      * Handles actions from within the property panel.
207      * @param event an event.
208      */

209     public void actionPerformed(ActionEvent JavaDoc event) {
210         String JavaDoc command = event.getActionCommand();
211         if (command.equals("GridStroke")) {
212             attemptGridStrokeSelection();
213         }
214         else if (command.equals("GridPaint")) {
215             attemptGridPaintSelection();
216         }
217         else if (command.equals("AutoRangeOnOff")) {
218             toggleAutoRange();
219         }
220         else if (command.equals("MinimumRange")) {
221             validateMinimum();
222         }
223         else if (command.equals("MaximumRange")) {
224             validateMaximum();
225         }
226         else {
227             // pass to the super-class for handling
228
super.actionPerformed(event);
229         }
230     }
231
232     /**
233      * Handle a grid stroke selection.
234      */

235     private void attemptGridStrokeSelection() {
236         StrokeChooserPanel panel = new StrokeChooserPanel(
237             null, this.availableStrokeSamples
238         );
239         int result = JOptionPane.showConfirmDialog(
240             this, panel, localizationResources.getString("Stroke_Selection"),
241             JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE
242         );
243
244         if (result == JOptionPane.OK_OPTION) {
245             this.gridStrokeSample.setStroke(panel.getSelectedStroke());
246         }
247     }
248
249     /**
250      * Handle a grid paint selection.
251      */

252     private void attemptGridPaintSelection() {
253         Color JavaDoc c;
254         c = JColorChooser.showDialog(
255             this, localizationResources.getString("Grid_Color"), Color.blue
256         );
257         if (c != null) {
258             this.gridPaintSample.setPaint(c);
259         }
260     }
261
262     /**
263      * Does nothing.
264      *
265      * @param event the event.
266      */

267     public void focusGained(FocusEvent JavaDoc event) {
268         // don't need to do anything
269
}
270
271     /**
272      * Revalidates minimum/maximum range.
273      *
274      * @param event the event.
275      */

276     public void focusLost(FocusEvent JavaDoc event) {
277         if (event.getSource() == this.minimumRangeValue) {
278             validateMinimum();
279         }
280         else if (event.getSource() == this.maximumRangeValue) {
281             validateMaximum();
282         }
283     }
284
285     /**
286      * Toggle the auto range setting.
287      */

288     public void toggleAutoRange() {
289         this.autoRange = this.autoRangeCheckBox.isSelected();
290         if (this.autoRange) {
291             this.minimumRangeValue.setText(Double.toString(this.minimumValue));
292             this.minimumRangeValue.setEnabled(false);
293             this.maximumRangeValue.setText(Double.toString(this.maximumValue));
294             this.maximumRangeValue.setEnabled(false);
295         }
296         else {
297             this.minimumRangeValue.setEnabled(true);
298             this.maximumRangeValue.setEnabled(true);
299         }
300     }
301
302     /**
303      * Revalidate the range minimum.
304      */

305     public void validateMinimum() {
306         double newMin;
307         try {
308             newMin = Double.parseDouble(this.minimumRangeValue.getText());
309             if (newMin >= this.maximumValue) {
310                 newMin = this.minimumValue;
311             }
312         }
313         catch (NumberFormatException JavaDoc e) {
314             newMin = this.minimumValue;
315         }
316
317         this.minimumValue = newMin;
318         this.minimumRangeValue.setText(Double.toString(this.minimumValue));
319     }
320
321     /**
322      * Revalidate the range maximum.
323      */

324     public void validateMaximum() {
325         double newMax;
326         try {
327             newMax = Double.parseDouble(this.maximumRangeValue.getText());
328             if (newMax <= this.minimumValue) {
329                 newMax = this.maximumValue;
330             }
331         }
332         catch (NumberFormatException JavaDoc e) {
333             newMax = this.maximumValue;
334         }
335
336         this.maximumValue = newMax;
337         this.maximumRangeValue.setText(Double.toString(this.maximumValue));
338     }
339
340     /**
341      * Sets the properties of the specified axis to match the properties
342      * defined on this panel.
343      *
344      * @param axis the axis.
345      */

346     public void setAxisProperties(Axis axis) {
347         super.setAxisProperties(axis);
348         NumberAxis numberAxis = (NumberAxis) axis;
349         numberAxis.setAutoRange(this.autoRange);
350         if (!this.autoRange) {
351             numberAxis.setRange(this.minimumValue, this.maximumValue);
352         }
353     }
354
355 }
356
Popular Tags