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. * LegendPropertyEditPanel.java
  26. * ----------------------------
  27. * (C) Copyright 2000-2004, by Object Refinery Limited.
  28. *
  29. * Original Author: David Gilbert;
  30. * Contributor(s): Arnaud Lelievre;
  31. * Daniel Gredler;
  32. *
  33. * $Id: LegendPropertyEditPanel.java,v 1.3 2004/11/29 15:18:37 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. * 25-Jun-2002 : Revised header, removed redundant code (DG);
  41. * 08-Sep-2003 : Added internationalization via use of properties resourceBundle (RFE 690236) (AL);
  42. * 24-Aug-2004 : Applied patch 1014378 (DG);
  43. *
  44. */
  45. package org.jfree.chart.ui;
  46. import java.awt.BasicStroke;
  47. import java.awt.BorderLayout;
  48. import java.awt.Color;
  49. import java.awt.Font;
  50. import java.awt.Paint;
  51. import java.awt.Stroke;
  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 org.jfree.chart.JFreeChart;
  63. import org.jfree.chart.Legend;
  64. import org.jfree.chart.StandardLegend;
  65. import org.jfree.layout.LCBLayout;
  66. import org.jfree.ui.FontChooserPanel;
  67. import org.jfree.ui.FontDisplayField;
  68. import org.jfree.ui.PaintSample;
  69. import org.jfree.ui.StrokeChooserPanel;
  70. import org.jfree.ui.StrokeSample;
  71. /**
  72. * A panel for editing the properties of a {@link Legend}.
  73. */
  74. class LegendPropertyEditPanel extends JPanel implements ActionListener {
  75. /** Whether or not to display the legend on the chart. */
  76. private boolean showLegend;
  77. /** The checkbox to indicate whether or not to display the legend. */
  78. private JCheckBox showLegendCheckBox;
  79. /** The stroke (pen) used to draw the legend outline. */
  80. private StrokeSample outlineStroke;
  81. /** The button used to select the legend outline stroke. */
  82. private JButton selectOutlineStrokeButton;
  83. /** The paint (color) used to draw the legend outline. */
  84. private PaintSample outlinePaint;
  85. /** The button used to select the legend outline color. */
  86. private JButton selectOutlinePaintButton;
  87. /** The paint (color) used to fill the legend background. */
  88. private PaintSample backgroundPaint;
  89. /** The button used to select the legend background color. */
  90. private JButton selectBackgroundPaintButton;
  91. /** The font used to draw the series names. */
  92. private Font seriesFont;
  93. /** The button used to select the series name font. */
  94. private JButton selectSeriesFontButton;
  95. /** The paint (color) used to draw the series names. */
  96. private PaintSample seriesPaint;
  97. /** The button used to select the series name paint. */
  98. private JButton selectSeriesPaintButton;
  99. /** An array of strokes (pens) to select from. */
  100. private StrokeSample[] availableStrokeSamples;
  101. /** A field for displaying the series label font. */
  102. private FontDisplayField fontDisplayField;
  103. /** The resourceBundle for the localization. */
  104. protected static ResourceBundle localizationResources =
  105. ResourceBundle.getBundle("org.jfree.chart.ui.LocalizationBundle");
  106. /**
  107. * Standard constructor: builds a panel based on the specified legend.
  108. * If the specified legend is <tt>null</tt>, the panel will reflect the
  109. * fact that no legend is to be displayed.
  110. *
  111. * @param legend the legend, which should be changed.
  112. */
  113. public LegendPropertyEditPanel(Legend legend) {
  114. StandardLegend l = (legend != null ? (StandardLegend) legend : new StandardLegend());
  115. this.showLegend = (legend != null);
  116. this.outlineStroke = new StrokeSample(l.getOutlineStroke());
  117. this.outlinePaint = new PaintSample(l.getOutlinePaint());
  118. this.backgroundPaint = new PaintSample(l.getBackgroundPaint());
  119. this.seriesFont = l.getItemFont();
  120. this.seriesPaint = new PaintSample(l.getItemPaint());
  121. this.availableStrokeSamples = new StrokeSample[4];
  122. this.availableStrokeSamples[0] = new StrokeSample(new BasicStroke(1.0f));
  123. this.availableStrokeSamples[1] = new StrokeSample(new BasicStroke(2.0f));
  124. this.availableStrokeSamples[2] = new StrokeSample(new BasicStroke(3.0f));
  125. this.availableStrokeSamples[3] = new StrokeSample(new BasicStroke(4.0f));
  126. setLayout(new BorderLayout());
  127. JPanel general = new JPanel(new BorderLayout());
  128. general.setBorder(BorderFactory.createTitledBorder(
  129. BorderFactory.createEtchedBorder(),
  130. localizationResources.getString("General")));
  131. JPanel interior = new JPanel(new LCBLayout(6));
  132. interior.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
  133. interior.add(new JLabel(localizationResources.getString("Show_Legend")));
  134. this.showLegendCheckBox = new JCheckBox();
  135. this.showLegendCheckBox.setSelected(this.showLegend);
  136. this.showLegendCheckBox.setActionCommand("ShowLegend");
  137. this.showLegendCheckBox.addActionListener(this);
  138. interior.add(new JPanel());
  139. interior.add(this.showLegendCheckBox);
  140. interior.add(new JLabel(localizationResources.getString("Outline")));
  141. interior.add(this.outlineStroke);
  142. this.selectOutlineStrokeButton = new JButton(localizationResources.getString("Select..."));
  143. this.selectOutlineStrokeButton.setActionCommand("OutlineStroke");
  144. this.selectOutlineStrokeButton.addActionListener(this);
  145. interior.add(this.selectOutlineStrokeButton);
  146. interior.add(new JLabel(localizationResources.getString("Outline_Paint")));
  147. this.selectOutlinePaintButton = new JButton(localizationResources.getString("Select..."));
  148. this.selectOutlinePaintButton.setActionCommand("OutlinePaint");
  149. this.selectOutlinePaintButton.addActionListener(this);
  150. interior.add(this.outlinePaint);
  151. interior.add(this.selectOutlinePaintButton);
  152. interior.add(new JLabel(localizationResources.getString("Background")));
  153. this.selectBackgroundPaintButton = new JButton(
  154. localizationResources.getString("Select...")
  155. );
  156. this.selectBackgroundPaintButton.setActionCommand("BackgroundPaint");
  157. this.selectBackgroundPaintButton.addActionListener(this);
  158. interior.add(this.backgroundPaint);
  159. interior.add(this.selectBackgroundPaintButton);
  160. interior.add(new JLabel(localizationResources.getString("Series_label_font")));
  161. this.selectSeriesFontButton = new JButton(localizationResources.getString("Select..."));
  162. this.selectSeriesFontButton.setActionCommand("SeriesFont");
  163. this.selectSeriesFontButton.addActionListener(this);
  164. this.fontDisplayField = new FontDisplayField(this.seriesFont);
  165. interior.add(this.fontDisplayField);
  166. interior.add(this.selectSeriesFontButton);
  167. interior.add(new JLabel(localizationResources.getString("Series_label_paint")));
  168. this.selectSeriesPaintButton = new JButton(localizationResources.getString("Select..."));
  169. this.selectSeriesPaintButton.setActionCommand("SeriesPaint");
  170. this.selectSeriesPaintButton.addActionListener(this);
  171. interior.add(this.seriesPaint);
  172. interior.add(this.selectSeriesPaintButton);
  173. this.enableOrDisableControls();
  174. general.add(interior);
  175. add(general, BorderLayout.NORTH);
  176. }
  177. /**
  178. * Returns the current outline stroke.
  179. *
  180. * @return The current outline stroke.
  181. */
  182. public Stroke getOutlineStroke() {
  183. return this.outlineStroke.getStroke();
  184. }
  185. /**
  186. * Returns the current outline paint.
  187. *
  188. * @return The current outline paint.
  189. */
  190. public Paint getOutlinePaint() {
  191. return this.outlinePaint.getPaint();
  192. }
  193. /**
  194. * Returns the current background paint.
  195. *
  196. * @return The current background paint.
  197. */
  198. public Paint getBackgroundPaint() {
  199. return this.backgroundPaint.getPaint();
  200. }
  201. /**
  202. * Returns the current series label font.
  203. *
  204. * @return The current series label font.
  205. */
  206. public Font getSeriesFont() {
  207. return this.seriesFont;
  208. }
  209. /**
  210. * Returns the current series label paint.
  211. *
  212. * @return The current series label paint.
  213. */
  214. public Paint getSeriesPaint() {
  215. return this.seriesPaint.getPaint();
  216. }
  217. /**
  218. * Handles user interactions with the panel.
  219. *
  220. * @param event the event.
  221. */
  222. public void actionPerformed(ActionEvent event) {
  223. String command = event.getActionCommand();
  224. if (command.equals("OutlineStroke")) {
  225. attemptModifyOutlineStroke();
  226. }
  227. else if (command.equals("OutlinePaint")) {
  228. attemptModifyOutlinePaint();
  229. }
  230. else if (command.equals("BackgroundPaint")) {
  231. attemptModifyBackgroundPaint();
  232. }
  233. else if (command.equals("SeriesFont")) {
  234. attemptModifySeriesFont();
  235. }
  236. else if (command.equals("SeriesPaint")) {
  237. attemptModifySeriesPaint();
  238. }
  239. else if (command.equals("ShowLegend")) {
  240. attemptModifyShowLegend();
  241. }
  242. }
  243. /**
  244. * Allows the user the opportunity to change the outline stroke.
  245. */
  246. private void attemptModifyOutlineStroke() {
  247. StrokeChooserPanel panel = new StrokeChooserPanel(
  248. this.outlineStroke, this.availableStrokeSamples
  249. );
  250. int result = JOptionPane.showConfirmDialog(
  251. this, panel, localizationResources.getString("Pen_Stroke_Selection"),
  252. JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE
  253. );
  254. if (result == JOptionPane.OK_OPTION) {
  255. this.outlineStroke.setStroke(panel.getSelectedStroke());
  256. }
  257. }
  258. /**
  259. * Allows the user the opportunity to change the outline paint.
  260. */
  261. private void attemptModifyOutlinePaint() {
  262. Color c;
  263. c = JColorChooser.showDialog(this, localizationResources.getString("Outline_Color"),
  264. Color.blue);
  265. if (c != null) {
  266. this.outlinePaint.setPaint(c);
  267. }
  268. }
  269. /**
  270. * Allows the user the opportunity to change the background paint.
  271. */
  272. private void attemptModifyBackgroundPaint() {
  273. Color c;
  274. c = JColorChooser.showDialog(this, localizationResources.getString("Background_Color"),
  275. Color.blue);
  276. if (c != null) {
  277. this.backgroundPaint.setPaint(c);
  278. }
  279. }
  280. /**
  281. * Allows the user the opportunity to change the series label font.
  282. */
  283. public void attemptModifySeriesFont() {
  284. FontChooserPanel panel = new FontChooserPanel(this.seriesFont);
  285. int result =
  286. JOptionPane.showConfirmDialog(this, panel,
  287. localizationResources.getString("Font_Selection"),
  288. JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
  289. if (result == JOptionPane.OK_OPTION) {
  290. this.seriesFont = panel.getSelectedFont();
  291. this.fontDisplayField.setText(
  292. this.seriesFont.getFontName() + ", " + this.seriesFont.getSize()
  293. );
  294. }
  295. }
  296. /**
  297. * Allows the user the opportunity to change the series label paint.
  298. */
  299. private void attemptModifySeriesPaint() {
  300. Color c;
  301. c = JColorChooser.showDialog(this, localizationResources.getString("Series_Label_Color"),
  302. Color.blue);
  303. if (c != null) {
  304. this.seriesPaint.setPaint(c);
  305. }
  306. }
  307. /**
  308. * Allow the user the opportunity to change whether the legend is
  309. * displayed on the chart or not.
  310. */
  311. private void attemptModifyShowLegend() {
  312. this.showLegend = this.showLegendCheckBox.isSelected();
  313. this.enableOrDisableControls();
  314. }
  315. /**
  316. * If we are supposed to show the legend, the controls are enabled.
  317. * If we are not supposed to show the legend, the controls are disabled.
  318. */
  319. private void enableOrDisableControls() {
  320. boolean enabled = (this.showLegend == true);
  321. this.selectOutlineStrokeButton.setEnabled(enabled);
  322. this.selectOutlinePaintButton.setEnabled(enabled);
  323. this.selectBackgroundPaintButton.setEnabled(enabled);
  324. this.selectSeriesFontButton.setEnabled(enabled);
  325. this.selectSeriesPaintButton.setEnabled(enabled);
  326. }
  327. /**
  328. * Sets the properties of the specified legend to match the properties
  329. * defined on this panel.
  330. *
  331. * @param chart the chart whose legend is to be modified.
  332. */
  333. public void setLegendProperties(JFreeChart chart) {
  334. if (this.showLegend) {
  335. Legend legend = chart.getLegend();
  336. if (legend == null) {
  337. legend = new StandardLegend();
  338. chart.setLegend(legend);
  339. }
  340. if (legend instanceof StandardLegend) {
  341. // only supports StandardLegend at present
  342. StandardLegend standard = (StandardLegend) legend;
  343. standard.setOutlineStroke(getOutlineStroke());
  344. standard.setOutlinePaint(getOutlinePaint());
  345. standard.setBackgroundPaint(getBackgroundPaint());
  346. standard.setItemFont(getSeriesFont());
  347. standard.setItemPaint(getSeriesPaint());
  348. }
  349. else {
  350. // raise exception - unrecognised legend
  351. }
  352. }
  353. else {
  354. chart.setLegend(null);
  355. }
  356. }
  357. }