001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * ------------------------
028: * SWTNumberAxisEditor.java
029: * ------------------------
030: * (C) Copyright 2006, by Henry Proudhon and Contributors.
031: *
032: * Original Author: Henry Proudhon (henry.proudhon AT insa-lyon.fr);
033: * Contributor(s): David Gilbert (for Object Refinery Limited);
034: *
035: * Changes
036: * -------
037: * 01-Aug-2006 : New class (HP);
038: *
039: */
040:
041: package org.jfree.experimental.chart.swt.editor;
042:
043: import org.eclipse.swt.SWT;
044: import org.eclipse.swt.events.FocusEvent;
045: import org.eclipse.swt.events.FocusListener;
046: import org.eclipse.swt.events.SelectionAdapter;
047: import org.eclipse.swt.events.SelectionEvent;
048: import org.eclipse.swt.layout.GridData;
049: import org.eclipse.swt.layout.GridLayout;
050: import org.eclipse.swt.widgets.Button;
051: import org.eclipse.swt.widgets.Composite;
052: import org.eclipse.swt.widgets.Label;
053: import org.eclipse.swt.widgets.TabItem;
054: import org.eclipse.swt.widgets.Text;
055: import org.jfree.chart.axis.Axis;
056: import org.jfree.chart.axis.NumberAxis;
057:
058: /**
059: * An editor for {@link NumberAxis} properties.
060: */
061: class SWTNumberAxisEditor extends SWTAxisEditor implements
062: FocusListener {
063:
064: /** A flag that indicates whether or not the axis range is determined
065: * automatically.
066: */
067: private boolean autoRange;
068:
069: /** The lowest value in the axis range. */
070: private double minimumValue;
071:
072: /** The highest value in the axis range. */
073: private double maximumValue;
074:
075: /** A checkbox that indicates whether or not the axis range is determined
076: * automatically.
077: */
078: private Button autoRangeCheckBox;
079:
080: /** A text field for entering the minimum value in the axis range. */
081: private Text minimumRangeValue;
082:
083: /** A text field for entering the maximum value in the axis range. */
084: private Text maximumRangeValue;
085:
086: /**
087: * Creates a new editor.
088: *
089: * @param parent the parent.
090: * @param style the style.
091: * @param axis the axis.
092: */
093: public SWTNumberAxisEditor(Composite parent, int style,
094: NumberAxis axis) {
095: super (parent, style, axis);
096: this .autoRange = axis.isAutoRange();
097: this .minimumValue = axis.getLowerBound();
098: this .maximumValue = axis.getUpperBound();
099:
100: TabItem item2 = new TabItem(getOtherTabs(), SWT.NONE);
101: item2.setText(" " + localizationResources.getString("Range")
102: + " ");
103: Composite range = new Composite(getOtherTabs(), SWT.NONE);
104: range.setLayout(new GridLayout(2, true));
105: item2.setControl(range);
106:
107: autoRangeCheckBox = new Button(range, SWT.CHECK);
108: autoRangeCheckBox.setText(localizationResources
109: .getString("Auto-adjust_range"));
110: autoRangeCheckBox.setLayoutData(new GridData(SWT.FILL,
111: SWT.CENTER, true, false, 2, 1));
112: autoRangeCheckBox.setSelection(this .autoRange);
113: autoRangeCheckBox.addSelectionListener(new SelectionAdapter() {
114: public void widgetSelected(SelectionEvent e) {
115: toggleAutoRange();
116: }
117: });
118: new Label(range, SWT.NONE).setText(localizationResources
119: .getString("Minimum_range_value"));
120: this .minimumRangeValue = new Text(range, SWT.BORDER);
121: this .minimumRangeValue.setText(String
122: .valueOf(this .minimumValue));
123: this .minimumRangeValue.setLayoutData(new GridData(SWT.FILL,
124: SWT.CENTER, true, false));
125: this .minimumRangeValue.setEnabled(!this .autoRange);
126: //this.minimumRangeValue.addModifyListener(this);
127: //this.minimumRangeValue.addVerifyListener(this);
128: this .minimumRangeValue.addFocusListener(this );
129: new Label(range, SWT.NONE).setText(localizationResources
130: .getString("Maximum_range_value"));
131: this .maximumRangeValue = new Text(range, SWT.BORDER);
132: this .maximumRangeValue.setText(String
133: .valueOf(this .maximumValue));
134: this .maximumRangeValue.setLayoutData(new GridData(SWT.FILL,
135: SWT.CENTER, true, false));
136: this .maximumRangeValue.setEnabled(!this .autoRange);
137: //this.maximumRangeValue.addModifyListener(this);
138: //this.maximumRangeValue.addVerifyListener(this);
139: this .maximumRangeValue.addFocusListener(this );
140: }
141:
142: /**
143: * Toggle the auto range setting.
144: */
145: public void toggleAutoRange() {
146: this .autoRange = this .autoRangeCheckBox.getSelection();
147: if (this .autoRange) {
148: this .minimumRangeValue.setText(Double
149: .toString(this .minimumValue));
150: this .minimumRangeValue.setEnabled(false);
151: this .maximumRangeValue.setText(Double
152: .toString(this .maximumValue));
153: this .maximumRangeValue.setEnabled(false);
154: } else {
155: this .minimumRangeValue.setEnabled(true);
156: this .maximumRangeValue.setEnabled(true);
157: }
158: }
159:
160: /**
161: * Revalidate the range minimum:
162: * it should be less than the current maximum.
163: *
164: * @param candidate the minimum value
165: *
166: * @return A boolean.
167: */
168: public boolean validateMinimum(String candidate) {
169: boolean valid = true;
170: try {
171: if (Double.parseDouble(candidate) >= this .maximumValue) {
172: valid = false;
173: }
174: } catch (NumberFormatException e) {
175: valid = false;
176: }
177: return valid;
178: }
179:
180: /**
181: * Revalidate the range maximum:
182: * it should be greater than the current minimum
183: *
184: * @param candidate the maximum value
185: *
186: * @return A boolean.
187: */
188: public boolean validateMaximum(String candidate) {
189: boolean valid = true;
190: try {
191: if (Double.parseDouble(candidate) <= this .minimumValue) {
192: valid = false;
193: }
194: } catch (NumberFormatException e) {
195: valid = false;
196: }
197: return valid;
198: }
199:
200: /* (non-Javadoc)
201: * @see org.eclipse.swt.events.FocusListener#focusGained(
202: * org.eclipse.swt.events.FocusEvent)
203: */
204: public void focusGained(FocusEvent e) {
205: // don't need to do anything
206: }
207:
208: /* (non-Javadoc)
209: * @see org.eclipse.swt.events.FocusListener#focusLost(
210: * org.eclipse.swt.events.FocusEvent)
211: */
212: public void focusLost(FocusEvent e) {
213: if (e.getSource() == this .minimumRangeValue) {
214: // verify min value
215: if (!validateMinimum(this .minimumRangeValue.getText()))
216: this .minimumRangeValue.setText(String
217: .valueOf(this .minimumValue));
218: else
219: this .minimumValue = Double
220: .parseDouble(this .minimumRangeValue.getText());
221: } else if (e.getSource() == this .maximumRangeValue) {
222: // verify max value
223: if (!validateMaximum(this .maximumRangeValue.getText()))
224: this .maximumRangeValue.setText(String
225: .valueOf(this .maximumValue));
226: else
227: this .maximumValue = Double
228: .parseDouble(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: }
|