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. * AxisPropertyEditPanel.java
  26. * --------------------------
  27. * (C) Copyright 2000-2004, by Object Refinery Limited and Contributors.
  28. *
  29. * Original Author: David Gilbert;
  30. * Contributor(s): Andrzej Porebski;
  31. * Arnaud Lelievre;
  32. *
  33. * $Id: AxisPropertyEditPanel.java,v 1.1 2004/08/31 14:54:21 mungady Exp $
  34. *
  35. * Changes (from 24-Aug-2001)
  36. * --------------------------
  37. * 24-Aug-2001 : Added standard source header. Fixed DOS encoding problem (DG);
  38. * 07-Nov-2001 : Separated the JCommon Class Library classes, JFreeChart now requires
  39. * jcommon.jar (DG);
  40. * 21-Nov-2001 : Allowed for null axes (DG);
  41. * 09-Apr-2002 : Minor change to import statement to fix Javadoc error (DG);
  42. * 15-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  43. * 08-Sep-2003 : Added internationalization via use of properties resourceBundle (RFE 690236) (AL);
  44. *
  45. */
  46. package org.jfree.chart.ui;
  47. import java.awt.BorderLayout;
  48. import java.awt.Color;
  49. import java.awt.Font;
  50. import java.awt.Insets;
  51. import java.awt.Paint;
  52. import java.awt.event.ActionEvent;
  53. import java.awt.event.ActionListener;
  54. import java.util.ResourceBundle;
  55. import javax.swing.BorderFactory;
  56. import javax.swing.JButton;
  57. import javax.swing.JCheckBox;
  58. import javax.swing.JColorChooser;
  59. import javax.swing.JLabel;
  60. import javax.swing.JOptionPane;
  61. import javax.swing.JPanel;
  62. import javax.swing.JTabbedPane;
  63. import javax.swing.JTextField;
  64. import org.jfree.chart.axis.Axis;
  65. import org.jfree.chart.axis.NumberAxis;
  66. import org.jfree.layout.LCBLayout;
  67. import org.jfree.ui.FontChooserPanel;
  68. import org.jfree.ui.FontDisplayField;
  69. import org.jfree.ui.InsetsChooserPanel;
  70. import org.jfree.ui.InsetsTextField;
  71. import org.jfree.ui.PaintSample;
  72. /**
  73. * A panel for editing the properties of an axis.
  74. *
  75. */
  76. public class AxisPropertyEditPanel extends JPanel implements ActionListener {
  77. /** The axis label. */
  78. private JTextField label;
  79. /** The label font. */
  80. private Font labelFont;
  81. /** The label paint. */
  82. private PaintSample labelPaintSample;
  83. /** A field showing a description of the label font. */
  84. private JTextField labelFontField;
  85. /** The font for displaying tick labels on the axis. */
  86. private Font tickLabelFont;
  87. /** A field containing a description of the font for displaying tick labels on the axis. */
  88. private JTextField tickLabelFontField;
  89. /** The paint (color) for the tick labels. */
  90. private PaintSample tickLabelPaintSample;
  91. /** An empty sub-panel for extending the user interface to handle more complex axes. */
  92. private JPanel slot1;
  93. /** An empty sub-panel for extending the user interface to handle more complex axes. */
  94. private JPanel slot2;
  95. /** A flag that indicates whether or not the tick labels are visible. */
  96. private JCheckBox showTickLabelsCheckBox;
  97. /** A flag that indicates whether or not the tick marks are visible. */
  98. private JCheckBox showTickMarksCheckBox;
  99. /** Insets text field. */
  100. private InsetsTextField tickLabelInsetsTextField;
  101. /** Label insets text field. */
  102. private InsetsTextField labelInsetsTextField;
  103. /** The tick label insets. */
  104. private Insets tickLabelInsets;
  105. /** The label insets. */
  106. private Insets labelInsets;
  107. /** A tabbed pane for... */
  108. private JTabbedPane otherTabs;
  109. /** The resourceBundle for the localization. */
  110. protected static ResourceBundle localizationResources =
  111. ResourceBundle.getBundle("org.jfree.chart.ui.LocalizationBundle");
  112. /**
  113. * A static method that returns a panel that is appropriate for the axis
  114. * type.
  115. *
  116. * @param axis the axis whose properties are to be displayed/edited in the panel.
  117. *
  118. * @return a panel or <code>null</code< if axis is <code>null</code>.
  119. */
  120. public static AxisPropertyEditPanel getInstance(Axis axis) {
  121. if (axis != null) {
  122. // figure out what type of axis we have and instantiate the
  123. // appropriate panel
  124. if (axis instanceof NumberAxis) {
  125. return new NumberAxisPropertyEditPanel((NumberAxis) axis);
  126. }
  127. else {
  128. return new AxisPropertyEditPanel(axis);
  129. }
  130. }
  131. else {
  132. return null;
  133. }
  134. }
  135. /**
  136. * Standard constructor: builds a panel for displaying/editing the
  137. * properties of the specified axis.
  138. *
  139. * @param axis the axis whose properties are to be displayed/edited in the panel.
  140. */
  141. public AxisPropertyEditPanel(Axis axis) {
  142. this.labelFont = axis.getLabelFont();
  143. this.labelPaintSample = new PaintSample(axis.getLabelPaint());
  144. this.tickLabelFont = axis.getTickLabelFont();
  145. this.tickLabelPaintSample = new PaintSample(axis.getTickLabelPaint());
  146. // Insets values
  147. this.tickLabelInsets = axis.getTickLabelInsets();
  148. this.labelInsets = axis.getLabelInsets();
  149. setLayout(new BorderLayout());
  150. JPanel general = new JPanel(new BorderLayout());
  151. general.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),
  152. localizationResources.getString("General")));
  153. JPanel interior = new JPanel(new LCBLayout(5));
  154. interior.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
  155. interior.add(new JLabel(localizationResources.getString("Label")));
  156. this.label = new JTextField(axis.getLabel());
  157. interior.add(this.label);
  158. interior.add(new JPanel());
  159. interior.add(new JLabel(localizationResources.getString("Font")));
  160. this.labelFontField = new FontDisplayField(this.labelFont);
  161. interior.add(this.labelFontField);
  162. JButton b = new JButton(localizationResources.getString("Select..."));
  163. b.setActionCommand("SelectLabelFont");
  164. b.addActionListener(this);
  165. interior.add(b);
  166. interior.add(new JLabel(localizationResources.getString("Paint")));
  167. interior.add(this.labelPaintSample);
  168. b = new JButton(localizationResources.getString("Select..."));
  169. b.setActionCommand("SelectLabelPaint");
  170. b.addActionListener(this);
  171. interior.add(b);
  172. interior.add(new JLabel(localizationResources.getString("Label_Insets")));
  173. b = new JButton(localizationResources.getString("Edit..."));
  174. b.setActionCommand("LabelInsets");
  175. b.addActionListener(this);
  176. this.labelInsetsTextField = new InsetsTextField(this.labelInsets);
  177. interior.add(this.labelInsetsTextField);
  178. interior.add(b);
  179. interior.add(new JLabel(localizationResources.getString("Tick_Label_Insets")));
  180. b = new JButton(localizationResources.getString("Edit..."));
  181. b.setActionCommand("TickLabelInsets");
  182. b.addActionListener(this);
  183. this.tickLabelInsetsTextField = new InsetsTextField(this.tickLabelInsets);
  184. interior.add(this.tickLabelInsetsTextField);
  185. interior.add(b);
  186. general.add(interior);
  187. add(general, BorderLayout.NORTH);
  188. this.slot1 = new JPanel(new BorderLayout());
  189. JPanel other = new JPanel(new BorderLayout());
  190. other.setBorder(BorderFactory.createTitledBorder(
  191. BorderFactory.createEtchedBorder(),
  192. localizationResources.getString("Other")));
  193. this.otherTabs = new JTabbedPane();
  194. this.otherTabs.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
  195. JPanel ticks = new JPanel(new LCBLayout(3));
  196. ticks.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
  197. this.showTickLabelsCheckBox = new JCheckBox(
  198. localizationResources.getString("Show_tick_labels"), axis.isTickLabelsVisible()
  199. );
  200. ticks.add(this.showTickLabelsCheckBox);
  201. ticks.add(new JPanel());
  202. ticks.add(new JPanel());
  203. ticks.add(new JLabel(localizationResources.getString("Tick_label_font")));
  204. this.tickLabelFontField = new FontDisplayField(this.tickLabelFont);
  205. ticks.add(this.tickLabelFontField);
  206. b = new JButton(localizationResources.getString("Select..."));
  207. b.setActionCommand("SelectTickLabelFont");
  208. b.addActionListener(this);
  209. ticks.add(b);
  210. this.showTickMarksCheckBox = new JCheckBox(
  211. localizationResources.getString("Show_tick_marks"), axis.isTickMarksVisible()
  212. );
  213. ticks.add(this.showTickMarksCheckBox);
  214. ticks.add(new JPanel());
  215. ticks.add(new JPanel());
  216. this.otherTabs.add(localizationResources.getString("Ticks"), ticks);
  217. other.add(this.otherTabs);
  218. this.slot1.add(other);
  219. this.slot2 = new JPanel(new BorderLayout());
  220. this.slot2.add(this.slot1, BorderLayout.NORTH);
  221. add(this.slot2);
  222. }
  223. /**
  224. * Returns the current axis label.
  225. *
  226. * @return the current axis label.
  227. */
  228. public String getLabel() {
  229. return this.label.getText();
  230. }
  231. /**
  232. * Returns the current label font.
  233. *
  234. * @return the current label font.
  235. */
  236. public Font getLabelFont() {
  237. return this.labelFont;
  238. }
  239. /**
  240. * Returns the current label paint.
  241. *
  242. * @return the current label paint.
  243. */
  244. public Paint getLabelPaint() {
  245. return this.labelPaintSample.getPaint();
  246. }
  247. /**
  248. * Returns a flag that indicates whether or not the tick labels are visible.
  249. *
  250. * @return <code>true</code> if ick mark labels are visible.
  251. */
  252. public boolean isTickLabelsVisible() {
  253. return this.showTickLabelsCheckBox.isSelected();
  254. }
  255. /**
  256. * Returns the font used to draw the tick labels (if they are showing).
  257. *
  258. * @return the font used to draw the tick labels.
  259. */
  260. public Font getTickLabelFont() {
  261. return this.tickLabelFont;
  262. }
  263. /**
  264. * Returns the current tick label paint.
  265. *
  266. * @return the current tick label paint.
  267. */
  268. public Paint getTickLabelPaint() {
  269. return this.tickLabelPaintSample.getPaint();
  270. }
  271. /**
  272. * Returns the current value of the flag that determines whether or not
  273. * tick marks are visible.
  274. *
  275. * @return <code>true</code> if tick marks are visible.
  276. */
  277. public boolean isTickMarksVisible() {
  278. return this.showTickMarksCheckBox.isSelected();
  279. }
  280. /**
  281. * Returns the current tick label insets value
  282. *
  283. * @return the current tick label insets value.
  284. */
  285. public Insets getTickLabelInsets() {
  286. return (this.tickLabelInsets == null)
  287. ? new Insets(0, 0, 0, 0)
  288. : this.tickLabelInsets;
  289. }
  290. /**
  291. * Returns the current label insets value
  292. *
  293. * @return the current label insets value.
  294. */
  295. public Insets getLabelInsets() {
  296. return (this.labelInsets == null) ? new Insets(0, 0, 0, 0) : this.labelInsets;
  297. }
  298. /**
  299. * Returns a reference to the tabbed pane.
  300. *
  301. * @return a reference to the tabbed pane.
  302. */
  303. public JTabbedPane getOtherTabs() {
  304. return this.otherTabs;
  305. }
  306. /**
  307. * Handles user interaction with the property panel.
  308. * @param event Information about the event that triggered the call to
  309. * this method.
  310. */
  311. public void actionPerformed(ActionEvent event) {
  312. String command = event.getActionCommand();
  313. if (command.equals("SelectLabelFont")) {
  314. attemptLabelFontSelection();
  315. }
  316. else if (command.equals("SelectLabelPaint")) {
  317. attemptModifyLabelPaint();
  318. }
  319. else if (command.equals("SelectTickLabelFont")) {
  320. attemptTickLabelFontSelection();
  321. }
  322. else if (command.equals("LabelInsets")) {
  323. editLabelInsets();
  324. }
  325. else if (command.equals("TickLabelInsets")) {
  326. editTickLabelInsets();
  327. }
  328. }
  329. /**
  330. * Presents a font selection dialog to the user.
  331. */
  332. private void attemptLabelFontSelection() {
  333. FontChooserPanel panel = new FontChooserPanel(this.labelFont);
  334. int result = JOptionPane.showConfirmDialog(this, panel,
  335. localizationResources.getString("Font_Selection"),
  336. JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
  337. if (result == JOptionPane.OK_OPTION) {
  338. this.labelFont = panel.getSelectedFont();
  339. this.labelFontField.setText(
  340. this.labelFont.getFontName() + " " + this.labelFont.getSize()
  341. );
  342. }
  343. }
  344. /**
  345. * Allows the user the opportunity to change the outline paint.
  346. */
  347. private void attemptModifyLabelPaint() {
  348. Color c;
  349. c = JColorChooser.showDialog(this, localizationResources.getString("Label_Color"),
  350. Color.blue);
  351. if (c != null) {
  352. this.labelPaintSample.setPaint(c);
  353. }
  354. }
  355. /**
  356. * Presents a tick label font selection dialog to the user.
  357. */
  358. public void attemptTickLabelFontSelection() {
  359. FontChooserPanel panel = new FontChooserPanel(this.tickLabelFont);
  360. int result = JOptionPane.showConfirmDialog(this, panel,
  361. localizationResources.getString("Font_Selection"),
  362. JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
  363. if (result == JOptionPane.OK_OPTION) {
  364. this.tickLabelFont = panel.getSelectedFont();
  365. this.tickLabelFontField.setText(this.tickLabelFont.getFontName() + " "
  366. + this.tickLabelFont.getSize());
  367. }
  368. }
  369. /**
  370. * Presents insets chooser panel allowing user to modify tick label's
  371. * individual insets values. Updates the current insets text field if edit
  372. * is accepted.
  373. */
  374. private void editTickLabelInsets() {
  375. InsetsChooserPanel panel = new InsetsChooserPanel(this.tickLabelInsets);
  376. int result = JOptionPane.showConfirmDialog(this, panel,
  377. localizationResources.getString("Edit_Insets"),
  378. JOptionPane.PLAIN_MESSAGE);
  379. if (result == JOptionPane.OK_OPTION) {
  380. this.tickLabelInsets = panel.getInsets();
  381. this.tickLabelInsetsTextField.setInsets(this.tickLabelInsets);
  382. }
  383. }
  384. /**
  385. * Presents insets chooser panel allowing user to modify label's
  386. * individual insets values. Updates the current insets text field if edit
  387. * is accepted.
  388. */
  389. private void editLabelInsets() {
  390. InsetsChooserPanel panel = new InsetsChooserPanel(this.labelInsets);
  391. int result = JOptionPane.showConfirmDialog(this, panel,
  392. localizationResources.getString("Edit_Insets"),
  393. JOptionPane.PLAIN_MESSAGE);
  394. if (result == JOptionPane.OK_OPTION) {
  395. this.labelInsets = panel.getInsets();
  396. this.labelInsetsTextField.setInsets(this.labelInsets);
  397. }
  398. }
  399. /**
  400. * Sets the properties of the specified axis to match the properties
  401. * defined on this panel.
  402. *
  403. * @param axis the axis.
  404. */
  405. public void setAxisProperties(Axis axis) {
  406. axis.setLabel(getLabel());
  407. axis.setLabelFont(getLabelFont());
  408. axis.setLabelPaint(getLabelPaint());
  409. axis.setTickMarksVisible(isTickMarksVisible());
  410. // axis.setTickMarkStroke(getTickMarkStroke());
  411. axis.setTickLabelsVisible(isTickLabelsVisible());
  412. axis.setTickLabelFont(getTickLabelFont());
  413. axis.setTickLabelPaint(getTickLabelPaint());
  414. axis.setTickLabelInsets(getTickLabelInsets());
  415. axis.setLabelInsets(getLabelInsets());
  416. }
  417. }