KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > ui > NumberAxisPropertyEditPanel


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 License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * --------------------------------
27  * NumberAxisPropertyEditPanel.java
28  * --------------------------------
29  * (C) Copyright 2000-2004, Object Refinery Limited and Contributors.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): Arnaud Lelievre;
33  *
34  * $Id: NumberAxisPropertyEditPanel.java,v 1.3 2005/03/29 12:56:52 mungady Exp $
35  *
36  * Changes (from 24-Aug-2001)
37  * --------------------------
38  * 24-Aug-2001 : Added standard source header. Fixed DOS encoding problem (DG);
39  * 07-Nov-2001 : Separated the JCommon Class Library classes, JFreeChart now
40  * requires jcommon.jar (DG);
41  * 12-Dec-2001 : minor change due to bug fix elsewhere (DG);
42  * 20-May-2003 : Enabled initialisation of paint and stroke samples to prevent
43  * NullPointers (TM)
44  * 08-Sep-2003 : Added internationalization via use of properties
45  * resourceBundle (RFE 690236) (AL);
46  * 08-Sep-2003 : Changed ValueAxis API (DG);
47  */

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

77 class NumberAxisPropertyEditPanel extends AxisPropertyEditPanel
78                                   implements FocusListener JavaDoc {
79
80     /** A flag that indicates whether or not the axis range is determined
81      * automatically.
82      */

83     private boolean autoRange;
84
85     /** The lowest value in the axis range. */
86     private double minimumValue;
87
88     /** The highest value in the axis range. */
89     private double maximumValue;
90
91     /** A checkbox that indicates whether or not the axis range is determined
92      * automatically.
93      */

94     private JCheckBox JavaDoc autoRangeCheckBox;
95
96     /** A text field for entering the minimum value in the axis range. */
97     private JTextField JavaDoc minimumRangeValue;
98
99     /** A text field for entering the maximum value in the axis range. */
100     private JTextField JavaDoc maximumRangeValue;
101
102     /** A checkbox that controls whether or not gridlines are showing for the
103      * axis.
104      */

105     //private JCheckBox showGridLinesCheckBox;
106

107     /** The paint selected for drawing the gridlines. */
108     private PaintSample gridPaintSample;
109
110     /** The stroke selected for drawing the gridlines. */
111     private StrokeSample gridStrokeSample;
112
113     /** An array of stroke samples to choose from (since I haven't written a
114      * decent StrokeChooser component yet).
115      */

116     private StrokeSample[] availableStrokeSamples;
117     
118     /** The resourceBundle for the localization. */
119     protected static ResourceBundle JavaDoc localizationResources =
120         ResourceBundle.getBundle("org.jfree.chart.ui.LocalizationBundle");
121
122
123     /**
124      * Standard constructor: builds a property panel for the specified axis.
125      *
126      * @param axis the axis, which should be changed.
127      */

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

196     public boolean isAutoRange() {
197         return this.autoRange;
198     }
199
200     /**
201      * Returns the current setting of the minimum value in the axis range.
202      *
203      * @return The current setting of the minimum value in the axis range.
204      */

205     public double getMinimumValue() {
206         return this.minimumValue;
207     }
208
209     /**
210      * Returns the current setting of the maximum value in the axis range.
211      *
212      * @return The current setting of the maximum value in the axis range.
213      */

214     public double getMaximumValue() {
215         return this.maximumValue;
216     }
217
218     /**
219      * Handles actions from within the property panel.
220      * @param event an event.
221      */

222     public void actionPerformed(ActionEvent JavaDoc event) {
223         String JavaDoc command = event.getActionCommand();
224         if (command.equals("GridStroke")) {
225             attemptGridStrokeSelection();
226         }
227         else if (command.equals("GridPaint")) {
228             attemptGridPaintSelection();
229         }
230         else if (command.equals("AutoRangeOnOff")) {
231             toggleAutoRange();
232         }
233         else if (command.equals("MinimumRange")) {
234             validateMinimum();
235         }
236         else if (command.equals("MaximumRange")) {
237             validateMaximum();
238         }
239         else {
240             // pass to the super-class for handling
241
super.actionPerformed(event);
242         }
243     }
244
245     /**
246      * Handle a grid stroke selection.
247      */

248     private void attemptGridStrokeSelection() {
249         StrokeChooserPanel panel = new StrokeChooserPanel(
250             null, this.availableStrokeSamples
251         );
252         int result = JOptionPane.showConfirmDialog(
253             this, panel, localizationResources.getString("Stroke_Selection"),
254             JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE
255         );
256
257         if (result == JOptionPane.OK_OPTION) {
258             this.gridStrokeSample.setStroke(panel.getSelectedStroke());
259         }
260     }
261
262     /**
263      * Handle a grid paint selection.
264      */

265     private void attemptGridPaintSelection() {
266         Color JavaDoc c;
267         c = JColorChooser.showDialog(
268             this, localizationResources.getString("Grid_Color"), Color.blue
269         );
270         if (c != null) {
271             this.gridPaintSample.setPaint(c);
272         }
273     }
274
275     /**
276      * Does nothing.
277      *
278      * @param event the event.
279      */

280     public void focusGained(FocusEvent JavaDoc event) {
281         // don't need to do anything
282
}
283
284     /**
285      * Revalidates minimum/maximum range.
286      *
287      * @param event the event.
288      */

289     public void focusLost(FocusEvent JavaDoc event) {
290         if (event.getSource() == this.minimumRangeValue) {
291             validateMinimum();
292         }
293         else if (event.getSource() == this.maximumRangeValue) {
294             validateMaximum();
295         }
296     }
297
298     /**
299      * Toggle the auto range setting.
300      */

301     public void toggleAutoRange() {
302         this.autoRange = this.autoRangeCheckBox.isSelected();
303         if (this.autoRange) {
304             this.minimumRangeValue.setText(Double.toString(this.minimumValue));
305             this.minimumRangeValue.setEnabled(false);
306             this.maximumRangeValue.setText(Double.toString(this.maximumValue));
307             this.maximumRangeValue.setEnabled(false);
308         }
309         else {
310             this.minimumRangeValue.setEnabled(true);
311             this.maximumRangeValue.setEnabled(true);
312         }
313     }
314
315     /**
316      * Revalidate the range minimum.
317      */

318     public void validateMinimum() {
319         double newMin;
320         try {
321             newMin = Double.parseDouble(this.minimumRangeValue.getText());
322             if (newMin >= this.maximumValue) {
323                 newMin = this.minimumValue;
324             }
325         }
326         catch (NumberFormatException JavaDoc e) {
327             newMin = this.minimumValue;
328         }
329
330         this.minimumValue = newMin;
331         this.minimumRangeValue.setText(Double.toString(this.minimumValue));
332     }
333
334     /**
335      * Revalidate the range maximum.
336      */

337     public void validateMaximum() {
338         double newMax;
339         try {
340             newMax = Double.parseDouble(this.maximumRangeValue.getText());
341             if (newMax <= this.minimumValue) {
342                 newMax = this.maximumValue;
343             }
344         }
345         catch (NumberFormatException JavaDoc e) {
346             newMax = this.maximumValue;
347         }
348
349         this.maximumValue = newMax;
350         this.maximumRangeValue.setText(Double.toString(this.maximumValue));
351     }
352
353     /**
354      * Sets the properties of the specified axis to match the properties
355      * defined on this panel.
356      *
357      * @param axis the axis.
358      */

359     public void setAxisProperties(Axis axis) {
360         super.setAxisProperties(axis);
361         NumberAxis numberAxis = (NumberAxis) axis;
362         numberAxis.setAutoRange(this.autoRange);
363         if (!this.autoRange) {
364             numberAxis.setRange(this.minimumValue, this.maximumValue);
365         }
366     }
367
368 }
369
Popular Tags