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. * TitlePropertyEditPanel.java
  26. * ---------------------------
  27. * (C) Copyright 2000-2004, by Object Refinery Limited.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): Arnaud Lelievre;
  31. * Daniel Gredler;
  32. *
  33. * $Id: TitlePropertyEditPanel.java,v 1.4 2005/03/04 17:47:35 mungady Exp $
  34. *
  35. * Changes (from 24-Aug-2001)
  36. * --------------------------
  37. * 24-Aug-2001 : Added standard source headaer. Fixed DOS encoding problem (DG);
  38. * 07-Nov-2001 : Separated the JCommon Class Library classes, JFreeChart now requires
  39. * jcommon.jar (DG);
  40. * 31-Jan-2002 : Removed Title.java and StandardTitle.java. Disabled some methods in this class
  41. * until support for AbstractTitle is added (DG);
  42. * 20-May-2003 : Restored initialisation of titleField and titlePaint to prevent
  43. * NullPointer when using this class. (TM)
  44. * 08-Sep-2003 : Added internationalization via use of properties resourceBundle (RFE 690236) (AL);
  45. * 08-Jan-2004 : Renamed AbstractTitle --> Title and moved to new package (DG);
  46. * 24-Aug-2004 : Applied patch 1014378 (DG);
  47. *
  48. */
  49. package org.jfree.chart.ui;
  50. import java.awt.BorderLayout;
  51. import java.awt.Color;
  52. import java.awt.Font;
  53. import java.awt.Paint;
  54. import java.awt.event.ActionEvent;
  55. import java.awt.event.ActionListener;
  56. import java.util.ResourceBundle;
  57. import javax.swing.BorderFactory;
  58. import javax.swing.JButton;
  59. import javax.swing.JCheckBox;
  60. import javax.swing.JColorChooser;
  61. import javax.swing.JLabel;
  62. import javax.swing.JOptionPane;
  63. import javax.swing.JPanel;
  64. import javax.swing.JTextField;
  65. import org.jfree.chart.JFreeChart;
  66. import org.jfree.chart.title.Title;
  67. import org.jfree.chart.title.TextTitle;
  68. import org.jfree.layout.LCBLayout;
  69. import org.jfree.ui.FontChooserPanel;
  70. import org.jfree.ui.FontDisplayField;
  71. import org.jfree.ui.PaintSample;
  72. /**
  73. * A panel for editing the properties of a chart title.
  74. */
  75. public class TitlePropertyEditPanel extends JPanel implements ActionListener {
  76. /** Whether or not to display the title on the chart. */
  77. private boolean showTitle;
  78. /** The checkbox to indicate whether or not to display the title. */
  79. private JCheckBox showTitleCheckBox;
  80. /** A field for displaying/editing the title text. */
  81. private JTextField titleField;
  82. /** The font used to draw the title. */
  83. private Font titleFont;
  84. /** A field for displaying a description of the title font. */
  85. private JTextField fontfield;
  86. /** The button to use to select a new title font. */
  87. private JButton selectFontButton;
  88. /** The paint (color) used to draw the title. */
  89. private PaintSample titlePaint;
  90. /** The button to use to select a new paint (color) to draw the title. */
  91. private JButton selectPaintButton;
  92. /** The resourceBundle for the localization. */
  93. protected static ResourceBundle localizationResources
  94. = ResourceBundle.getBundle("org.jfree.chart.ui.LocalizationBundle");
  95. /**
  96. * Standard constructor: builds a panel for displaying/editing the
  97. * properties of the specified title.
  98. *
  99. * @param title the title, which should be changed.
  100. */
  101. public TitlePropertyEditPanel(Title title) {
  102. TextTitle t = (title != null ? (TextTitle) title
  103. : new TextTitle(localizationResources.getString("Title")));
  104. this.showTitle = (title != null);
  105. this.titleFont = t.getFont();
  106. this.titleField = new JTextField(t.getText());
  107. this.titlePaint = new PaintSample(t.getPaint());
  108. setLayout(new BorderLayout());
  109. JPanel general = new JPanel(new BorderLayout());
  110. general.setBorder(
  111. BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),
  112. localizationResources.getString("General"))
  113. );
  114. JPanel interior = new JPanel(new LCBLayout(4));
  115. interior.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
  116. interior.add(new JLabel(localizationResources.getString("Show_Title")));
  117. this.showTitleCheckBox = new JCheckBox();
  118. this.showTitleCheckBox.setSelected(this.showTitle);
  119. this.showTitleCheckBox.setActionCommand("ShowTitle");
  120. this.showTitleCheckBox.addActionListener(this);
  121. interior.add(new JPanel());
  122. interior.add(this.showTitleCheckBox);
  123. JLabel titleLabel = new JLabel(localizationResources.getString("Text"));
  124. interior.add(titleLabel);
  125. interior.add(this.titleField);
  126. interior.add(new JPanel());
  127. JLabel fontLabel = new JLabel(localizationResources.getString("Font"));
  128. this.fontfield = new FontDisplayField(this.titleFont);
  129. this.selectFontButton = new JButton(localizationResources.getString("Select..."));
  130. this.selectFontButton.setActionCommand("SelectFont");
  131. this.selectFontButton.addActionListener(this);
  132. interior.add(fontLabel);
  133. interior.add(this.fontfield);
  134. interior.add(this.selectFontButton);
  135. JLabel colorLabel = new JLabel(localizationResources.getString("Color"));
  136. this.selectPaintButton = new JButton(localizationResources.getString("Select..."));
  137. this.selectPaintButton.setActionCommand("SelectPaint");
  138. this.selectPaintButton.addActionListener(this);
  139. interior.add(colorLabel);
  140. interior.add(this.titlePaint);
  141. interior.add(this.selectPaintButton);
  142. this.enableOrDisableControls();
  143. general.add(interior);
  144. add(general, BorderLayout.NORTH);
  145. }
  146. /**
  147. * Returns the title text entered in the panel.
  148. *
  149. * @return The title text entered in the panel.
  150. */
  151. public String getTitleText() {
  152. return this.titleField.getText();
  153. }
  154. /**
  155. * Returns the font selected in the panel.
  156. *
  157. * @return The font selected in the panel.
  158. */
  159. public Font getTitleFont() {
  160. return this.titleFont;
  161. }
  162. /**
  163. * Returns the paint selected in the panel.
  164. *
  165. * @return The paint selected in the panel.
  166. */
  167. public Paint getTitlePaint() {
  168. return this.titlePaint.getPaint();
  169. }
  170. /**
  171. * Handles button clicks by passing control to an appropriate handler method.
  172. *
  173. * @param event the event
  174. */
  175. public void actionPerformed(ActionEvent event) {
  176. String command = event.getActionCommand();
  177. if (command.equals("SelectFont")) {
  178. attemptFontSelection();
  179. }
  180. else if (command.equals("SelectPaint")) {
  181. attemptPaintSelection();
  182. }
  183. else if (command.equals("ShowTitle")) {
  184. attemptModifyShowTitle();
  185. }
  186. }
  187. /**
  188. * Presents a font selection dialog to the user.
  189. */
  190. public void attemptFontSelection() {
  191. FontChooserPanel panel = new FontChooserPanel(this.titleFont);
  192. int result =
  193. JOptionPane.showConfirmDialog(this, panel,
  194. localizationResources.getString("Font_Selection"),
  195. JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
  196. if (result == JOptionPane.OK_OPTION) {
  197. this.titleFont = panel.getSelectedFont();
  198. this.fontfield.setText(this.titleFont.getFontName() + " " + this.titleFont.getSize());
  199. }
  200. }
  201. /**
  202. * Allow the user the opportunity to select a Paint object. For now, we
  203. * just use the standard color chooser - all colors are Paint objects, but
  204. * not all Paint objects are colors (later we can implement a more general
  205. * Paint chooser).
  206. */
  207. public void attemptPaintSelection() {
  208. Paint p = this.titlePaint.getPaint();
  209. Color defaultColor = (p instanceof Color ? (Color) p : Color.blue);
  210. Color c = JColorChooser.showDialog(
  211. this, localizationResources.getString("Title_Color"), defaultColor
  212. );
  213. if (c != null) {
  214. this.titlePaint.setPaint(c);
  215. }
  216. }
  217. /**
  218. * Allow the user the opportunity to change whether the title is
  219. * displayed on the chart or not.
  220. */
  221. private void attemptModifyShowTitle() {
  222. this.showTitle = this.showTitleCheckBox.isSelected();
  223. this.enableOrDisableControls();
  224. }
  225. /**
  226. * If we are supposed to show the title, the controls are enabled.
  227. * If we are not supposed to show the title, the controls are disabled.
  228. */
  229. private void enableOrDisableControls() {
  230. boolean enabled = (this.showTitle == true);
  231. this.titleField.setEnabled(enabled);
  232. this.selectFontButton.setEnabled(enabled);
  233. this.selectPaintButton.setEnabled(enabled);
  234. }
  235. /**
  236. * Sets the properties of the specified title to match the properties
  237. * defined on this panel.
  238. *
  239. * @param chart the chart whose title is to be modified.
  240. */
  241. public void setTitleProperties(JFreeChart chart) {
  242. if (this.showTitle) {
  243. TextTitle title = chart.getTitle();
  244. if (title == null) {
  245. title = new TextTitle();
  246. chart.setTitle(title);
  247. }
  248. title.setText(this.getTitleText());
  249. title.setFont(this.getTitleFont());
  250. title.setPaint(this.getTitlePaint());
  251. }
  252. else {
  253. chart.setTitle((TextTitle) null);
  254. }
  255. }
  256. }