1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2004, 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 under the terms
  10. * of the GNU Lesser General Public License as published by the Free Software Foundation;
  11. * either version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  14. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License along with this
  18. * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. *
  21. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  22. * in the United States and other countries.]
  23. *
  24. * --------------------------------
  25. * NumberAxisPropertyEditPanel.java
  26. * --------------------------------
  27. * (C) Copyright 2000-2004, Object Refinery Limited and Contributors.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): Arnaud Lelievre;
  31. *
  32. * $Id: NumberAxisPropertyEditPanel.java,v 1.1 2004/08/31 14:54:21 mungady Exp $
  33. *
  34. * Changes (from 24-Aug-2001)
  35. * --------------------------
  36. * 24-Aug-2001 : Added standard source header. Fixed DOS encoding problem (DG);
  37. * 07-Nov-2001 : Separated the JCommon Class Library classes, JFreeChart now requires
  38. * jcommon.jar (DG);
  39. * 12-Dec-2001 : minor change due to bug fix elsewhere (DG);
  40. * 20-May-2003 : Enabled initialisation of paint and stroke samples to prevent
  41. * NullPointers (TM)
  42. * 08-Sep-2003 : Added internationalization via use of properties resourceBundle (RFE 690236) (AL);
  43. *
  44. * 08-Sep-2003 : Changed ValueAxis API (DG);
  45. */
  46. package org.jfree.chart.ui;
  47. import java.awt.BasicStroke;
  48. import java.awt.Color;
  49. import java.awt.event.ActionEvent;
  50. import java.awt.event.FocusEvent;
  51. import java.awt.event.FocusListener;
  52. import java.util.ResourceBundle;
  53. import javax.swing.BorderFactory;
  54. import javax.swing.JCheckBox;
  55. import javax.swing.JColorChooser;
  56. import javax.swing.JLabel;
  57. import javax.swing.JOptionPane;
  58. import javax.swing.JPanel;
  59. import javax.swing.JTabbedPane;
  60. import javax.swing.JTextField;
  61. import org.jfree.chart.axis.Axis;
  62. import org.jfree.chart.axis.NumberAxis;
  63. import org.jfree.layout.LCBLayout;
  64. import org.jfree.ui.PaintSample;
  65. import org.jfree.ui.StrokeChooserPanel;
  66. import org.jfree.ui.StrokeSample;
  67. /**
  68. * A panel for editing the properties of a value axis.
  69. *
  70. */
  71. class NumberAxisPropertyEditPanel extends AxisPropertyEditPanel implements FocusListener {
  72. /** A flag that indicates whether or not the axis range is determined
  73. * automatically.
  74. */
  75. private boolean autoRange;
  76. /** The lowest value in the axis range. */
  77. private double minimumValue;
  78. /** The highest value in the axis range. */
  79. private double maximumValue;
  80. /** A checkbox that indicates whether or not the axis range is determined
  81. * automatically.
  82. */
  83. private JCheckBox autoRangeCheckBox;
  84. /** A text field for entering the minimum value in the axis range. */
  85. private JTextField minimumRangeValue;
  86. /** A text field for entering the maximum value in the axis range. */
  87. private JTextField maximumRangeValue;
  88. /** A checkbox that controls whether or not gridlines are showing for the
  89. * axis.
  90. */
  91. //private JCheckBox showGridLinesCheckBox;
  92. /** The paint selected for drawing the gridlines. */
  93. private PaintSample gridPaintSample;
  94. /** The stroke selected for drawing the gridlines. */
  95. private StrokeSample gridStrokeSample;
  96. /** An array of stroke samples to choose from (since I haven't written a
  97. * decent StrokeChooser component yet).
  98. */
  99. private StrokeSample[] availableStrokeSamples;
  100. /** The resourceBundle for the localization. */
  101. protected static ResourceBundle localizationResources =
  102. ResourceBundle.getBundle("org.jfree.chart.ui.LocalizationBundle");
  103. /**
  104. * Standard constructor: builds a property panel for the specified axis.
  105. *
  106. * @param axis the axis, which should be changed.
  107. */
  108. public NumberAxisPropertyEditPanel(NumberAxis axis) {
  109. super(axis);
  110. this.autoRange = axis.isAutoRange();
  111. this.minimumValue = axis.getLowerBound();
  112. this.maximumValue = axis.getUpperBound();
  113. this.gridPaintSample = new PaintSample(Color.blue);
  114. this.gridStrokeSample = new StrokeSample(new BasicStroke(1.0f));
  115. this.availableStrokeSamples = new StrokeSample[3];
  116. this.availableStrokeSamples[0] = new StrokeSample(new BasicStroke(1.0f));
  117. this.availableStrokeSamples[1] = new StrokeSample(new BasicStroke(2.0f));
  118. this.availableStrokeSamples[2] = new StrokeSample(new BasicStroke(3.0f));
  119. JTabbedPane other = getOtherTabs();
  120. JPanel range = new JPanel(new LCBLayout(3));
  121. range.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
  122. range.add(new JPanel());
  123. this.autoRangeCheckBox = new JCheckBox(localizationResources.getString("Auto-adjust_range"),
  124. this.autoRange);
  125. this.autoRangeCheckBox.setActionCommand("AutoRangeOnOff");
  126. this.autoRangeCheckBox.addActionListener(this);
  127. range.add(this.autoRangeCheckBox);
  128. range.add(new JPanel());
  129. range.add(new JLabel(localizationResources.getString("Minimum_range_value")));
  130. this.minimumRangeValue = new JTextField(Double.toString(this.minimumValue));
  131. this.minimumRangeValue.setEnabled(!this.autoRange);
  132. this.minimumRangeValue.setActionCommand("MinimumRange");
  133. this.minimumRangeValue.addActionListener(this);
  134. this.minimumRangeValue.addFocusListener(this);
  135. range.add(this.minimumRangeValue);
  136. range.add(new JPanel());
  137. range.add(new JLabel(localizationResources.getString("Maximum_range_value")));
  138. this.maximumRangeValue = new JTextField(Double.toString(this.maximumValue));
  139. this.maximumRangeValue.setEnabled(!this.autoRange);
  140. this.maximumRangeValue.setActionCommand("MaximumRange");
  141. this.maximumRangeValue.addActionListener(this);
  142. this.maximumRangeValue.addFocusListener(this);
  143. range.add(this.maximumRangeValue);
  144. range.add(new JPanel());
  145. other.add(localizationResources.getString("Range"), range);
  146. // JPanel grid = new JPanel(new LCBLayout(3));
  147. // grid.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
  148. // grid.add(new JPanel());
  149. // showGridLinesCheckBox = new JCheckBox("Show grid lines",
  150. // axis.isGridLinesVisible());
  151. // grid.add(showGridLinesCheckBox);
  152. // grid.add(new JPanel());
  153. // grid.add(new JLabel("Grid stroke:"));
  154. // JButton button = new JButton("Set stroke...");
  155. // button.setActionCommand("GridStroke");
  156. // button.addActionListener(this);
  157. // grid.add(gridStrokeSample);
  158. // grid.add(button);
  159. // grid.add(new JLabel("Grid paint:"));
  160. // button = new JButton("Set paint...");
  161. // button.setActionCommand("GridPaint");
  162. // button.addActionListener(this);
  163. // grid.add(gridPaintSample);
  164. // grid.add(button);
  165. // other.add("Grid", grid);
  166. }
  167. /**
  168. * Returns the current setting of the auto-range property.
  169. *
  170. * @return <code>true</code> if auto range is enabled.
  171. */
  172. public boolean isAutoRange() {
  173. return this.autoRange;
  174. }
  175. /**
  176. * Returns the current setting of the minimum value in the axis range.
  177. *
  178. * @return the current setting of the minimum value in the axis range.
  179. */
  180. public double getMinimumValue() {
  181. return this.minimumValue;
  182. }
  183. /**
  184. * Returns the current setting of the maximum value in the axis range.
  185. *
  186. * @return the current setting of the maximum value in the axis range.
  187. */
  188. public double getMaximumValue() {
  189. return this.maximumValue;
  190. }
  191. /**
  192. * Handles actions from within the property panel.
  193. * @param event an event.
  194. */
  195. public void actionPerformed(ActionEvent event) {
  196. String command = event.getActionCommand();
  197. if (command.equals("GridStroke")) {
  198. attemptGridStrokeSelection();
  199. }
  200. else if (command.equals("GridPaint")) {
  201. attemptGridPaintSelection();
  202. }
  203. else if (command.equals("AutoRangeOnOff")) {
  204. toggleAutoRange();
  205. }
  206. else if (command.equals("MinimumRange")) {
  207. validateMinimum();
  208. }
  209. else if (command.equals("MaximumRange")) {
  210. validateMaximum();
  211. }
  212. else {
  213. // pass to the super-class for handling
  214. super.actionPerformed(event);
  215. }
  216. }
  217. /**
  218. * Handle a grid stroke selection.
  219. */
  220. private void attemptGridStrokeSelection() {
  221. StrokeChooserPanel panel = new StrokeChooserPanel(null, this.availableStrokeSamples);
  222. int result =
  223. JOptionPane.showConfirmDialog(this, panel,
  224. localizationResources.getString("Stroke_Selection"),
  225. JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
  226. if (result == JOptionPane.OK_OPTION) {
  227. this.gridStrokeSample.setStroke(panel.getSelectedStroke());
  228. }
  229. }
  230. /**
  231. * Handle a grid paint selection.
  232. */
  233. private void attemptGridPaintSelection() {
  234. Color c;
  235. c = JColorChooser.showDialog(this, localizationResources.getString("Grid_Color"),
  236. Color.blue);
  237. if (c != null) {
  238. this.gridPaintSample.setPaint(c);
  239. }
  240. }
  241. /**
  242. * Does nothing.
  243. *
  244. * @param event the event.
  245. */
  246. public void focusGained(FocusEvent event) {
  247. // don't need to do anything
  248. }
  249. /**
  250. * Revalidates minimum/maximum range.
  251. *
  252. * @param event the event.
  253. */
  254. public void focusLost(FocusEvent event) {
  255. if (event.getSource() == this.minimumRangeValue) {
  256. validateMinimum();
  257. }
  258. else if (event.getSource() == this.maximumRangeValue) {
  259. validateMaximum();
  260. }
  261. }
  262. /**
  263. * Toggle the auto range setting.
  264. */
  265. public void toggleAutoRange() {
  266. this.autoRange = this.autoRangeCheckBox.isSelected();
  267. if (this.autoRange) {
  268. this.minimumRangeValue.setText(Double.toString(this.minimumValue));
  269. this.minimumRangeValue.setEnabled(false);
  270. this.maximumRangeValue.setText(Double.toString(this.maximumValue));
  271. this.maximumRangeValue.setEnabled(false);
  272. }
  273. else {
  274. this.minimumRangeValue.setEnabled(true);
  275. this.maximumRangeValue.setEnabled(true);
  276. }
  277. }
  278. /**
  279. * Revalidate the range minimum.
  280. */
  281. public void validateMinimum() {
  282. double newMin;
  283. try {
  284. newMin = Double.parseDouble(this.minimumRangeValue.getText());
  285. if (newMin >= this.maximumValue) {
  286. newMin = this.minimumValue;
  287. }
  288. }
  289. catch (NumberFormatException e) {
  290. newMin = this.minimumValue;
  291. }
  292. this.minimumValue = newMin;
  293. this.minimumRangeValue.setText(Double.toString(this.minimumValue));
  294. }
  295. /**
  296. * Revalidate the range maximum.
  297. */
  298. public void validateMaximum() {
  299. double newMax;
  300. try {
  301. newMax = Double.parseDouble(this.maximumRangeValue.getText());
  302. if (newMax <= this.minimumValue) {
  303. newMax = this.maximumValue;
  304. }
  305. }
  306. catch (NumberFormatException e) {
  307. newMax = this.maximumValue;
  308. }
  309. this.maximumValue = newMax;
  310. this.maximumRangeValue.setText(Double.toString(this.maximumValue));
  311. }
  312. /**
  313. * Sets the properties of the specified axis to match the properties
  314. * defined on this panel.
  315. *
  316. * @param axis the axis.
  317. */
  318. public void setAxisProperties(Axis axis) {
  319. super.setAxisProperties(axis);
  320. NumberAxis numberAxis = (NumberAxis) axis;
  321. numberAxis.setAutoRange(this.autoRange);
  322. if (!this.autoRange) {
  323. numberAxis.setRange(this.minimumValue, this.maximumValue);
  324. }
  325. // numberAxis.setGridLinesVisible(this.showGridLinesCheckBox.isSelected());
  326. // numberAxis.setGridPaint(this.gridPaintSample.getPaint());
  327. // numberAxis.setGridStroke(this.gridStrokeSample.getStroke());
  328. }
  329. }