001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2005, 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: * DefaultTitleEditor.java
029: * -----------------------
030: * (C) Copyright 2005, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): Arnaud Lelievre;
034: * Daniel Gredler;
035: *
036: * $Id: DefaultTitleEditor.java,v 1.1.2.1 2005/11/24 16:11:48 mungady Exp $
037: *
038: * Changes
039: * -------
040: * 24-Nov-2005 : Version 1, based on TitlePropertyEditPanel.java (DG);
041: *
042: */
043:
044: package org.jfree.chart.editor;
045:
046: import java.awt.BorderLayout;
047: import java.awt.Color;
048: import java.awt.Font;
049: import java.awt.Paint;
050: import java.awt.event.ActionEvent;
051: import java.awt.event.ActionListener;
052: import java.util.ResourceBundle;
053:
054: import javax.swing.BorderFactory;
055: import javax.swing.JButton;
056: import javax.swing.JCheckBox;
057: import javax.swing.JColorChooser;
058: import javax.swing.JLabel;
059: import javax.swing.JOptionPane;
060: import javax.swing.JPanel;
061: import javax.swing.JTextField;
062:
063: import org.jfree.chart.JFreeChart;
064: import org.jfree.chart.title.Title;
065: import org.jfree.chart.title.TextTitle;
066: import org.jfree.layout.LCBLayout;
067: import org.jfree.ui.FontChooserPanel;
068: import org.jfree.ui.FontDisplayField;
069: import org.jfree.ui.PaintSample;
070:
071: /**
072: * A panel for editing the properties of a chart title.
073: */
074: class DefaultTitleEditor extends JPanel implements ActionListener {
075:
076: /** Whether or not to display the title on the chart. */
077: private boolean showTitle;
078:
079: /** The checkbox to indicate whether or not to display the title. */
080: private JCheckBox showTitleCheckBox;
081:
082: /** A field for displaying/editing the title text. */
083: private JTextField titleField;
084:
085: /** The font used to draw the title. */
086: private Font titleFont;
087:
088: /** A field for displaying a description of the title font. */
089: private JTextField fontfield;
090:
091: /** The button to use to select a new title font. */
092: private JButton selectFontButton;
093:
094: /** The paint (color) used to draw the title. */
095: private PaintSample titlePaint;
096:
097: /** The button to use to select a new paint (color) to draw the title. */
098: private JButton selectPaintButton;
099:
100: /** The resourceBundle for the localization. */
101: protected static ResourceBundle localizationResources = ResourceBundle
102: .getBundle("org.jfree.chart.editor.LocalizationBundle");
103:
104: /**
105: * Standard constructor: builds a panel for displaying/editing the
106: * properties of the specified title.
107: *
108: * @param title the title, which should be changed.
109: */
110: public DefaultTitleEditor(Title title) {
111:
112: TextTitle t = (title != null ? (TextTitle) title
113: : new TextTitle(localizationResources
114: .getString("Title")));
115: this .showTitle = (title != null);
116: this .titleFont = t.getFont();
117: this .titleField = new JTextField(t.getText());
118: this .titlePaint = new PaintSample(t.getPaint());
119:
120: setLayout(new BorderLayout());
121:
122: JPanel general = new JPanel(new BorderLayout());
123: general.setBorder(BorderFactory.createTitledBorder(
124: BorderFactory.createEtchedBorder(),
125: localizationResources.getString("General")));
126:
127: JPanel interior = new JPanel(new LCBLayout(4));
128: interior.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
129:
130: interior.add(new JLabel(localizationResources
131: .getString("Show_Title")));
132: this .showTitleCheckBox = new JCheckBox();
133: this .showTitleCheckBox.setSelected(this .showTitle);
134: this .showTitleCheckBox.setActionCommand("ShowTitle");
135: this .showTitleCheckBox.addActionListener(this );
136: interior.add(new JPanel());
137: interior.add(this .showTitleCheckBox);
138:
139: JLabel titleLabel = new JLabel(localizationResources
140: .getString("Text"));
141: interior.add(titleLabel);
142: interior.add(this .titleField);
143: interior.add(new JPanel());
144:
145: JLabel fontLabel = new JLabel(localizationResources
146: .getString("Font"));
147: this .fontfield = new FontDisplayField(this .titleFont);
148: this .selectFontButton = new JButton(localizationResources
149: .getString("Select..."));
150: this .selectFontButton.setActionCommand("SelectFont");
151: this .selectFontButton.addActionListener(this );
152: interior.add(fontLabel);
153: interior.add(this .fontfield);
154: interior.add(this .selectFontButton);
155:
156: JLabel colorLabel = new JLabel(localizationResources
157: .getString("Color"));
158: this .selectPaintButton = new JButton(localizationResources
159: .getString("Select..."));
160: this .selectPaintButton.setActionCommand("SelectPaint");
161: this .selectPaintButton.addActionListener(this );
162: interior.add(colorLabel);
163: interior.add(this .titlePaint);
164: interior.add(this .selectPaintButton);
165:
166: this .enableOrDisableControls();
167:
168: general.add(interior);
169: add(general, BorderLayout.NORTH);
170: }
171:
172: /**
173: * Returns the title text entered in the panel.
174: *
175: * @return The title text entered in the panel.
176: */
177: public String getTitleText() {
178: return this .titleField.getText();
179: }
180:
181: /**
182: * Returns the font selected in the panel.
183: *
184: * @return The font selected in the panel.
185: */
186: public Font getTitleFont() {
187: return this .titleFont;
188: }
189:
190: /**
191: * Returns the paint selected in the panel.
192: *
193: * @return The paint selected in the panel.
194: */
195: public Paint getTitlePaint() {
196: return this .titlePaint.getPaint();
197: }
198:
199: /**
200: * Handles button clicks by passing control to an appropriate handler
201: * method.
202: *
203: * @param event the event
204: */
205: public void actionPerformed(ActionEvent event) {
206:
207: String command = event.getActionCommand();
208:
209: if (command.equals("SelectFont")) {
210: attemptFontSelection();
211: } else if (command.equals("SelectPaint")) {
212: attemptPaintSelection();
213: } else if (command.equals("ShowTitle")) {
214: attemptModifyShowTitle();
215: }
216: }
217:
218: /**
219: * Presents a font selection dialog to the user.
220: */
221: public void attemptFontSelection() {
222:
223: FontChooserPanel panel = new FontChooserPanel(this .titleFont);
224: int result = JOptionPane
225: .showConfirmDialog(this , panel, localizationResources
226: .getString("Font_Selection"),
227: JOptionPane.OK_CANCEL_OPTION,
228: JOptionPane.PLAIN_MESSAGE);
229:
230: if (result == JOptionPane.OK_OPTION) {
231: this .titleFont = panel.getSelectedFont();
232: this .fontfield.setText(this .titleFont.getFontName() + " "
233: + this .titleFont.getSize());
234: }
235: }
236:
237: /**
238: * Allow the user the opportunity to select a Paint object. For now, we
239: * just use the standard color chooser - all colors are Paint objects, but
240: * not all Paint objects are colors (later we can implement a more general
241: * Paint chooser).
242: */
243: public void attemptPaintSelection() {
244: Paint p = this .titlePaint.getPaint();
245: Color defaultColor = (p instanceof Color ? (Color) p
246: : Color.blue);
247: Color c = JColorChooser.showDialog(this , localizationResources
248: .getString("Title_Color"), defaultColor);
249: if (c != null) {
250: this .titlePaint.setPaint(c);
251: }
252: }
253:
254: /**
255: * Allow the user the opportunity to change whether the title is
256: * displayed on the chart or not.
257: */
258: private void attemptModifyShowTitle() {
259: this .showTitle = this .showTitleCheckBox.isSelected();
260: this .enableOrDisableControls();
261: }
262:
263: /**
264: * If we are supposed to show the title, the controls are enabled.
265: * If we are not supposed to show the title, the controls are disabled.
266: */
267: private void enableOrDisableControls() {
268: boolean enabled = (this .showTitle == true);
269: this .titleField.setEnabled(enabled);
270: this .selectFontButton.setEnabled(enabled);
271: this .selectPaintButton.setEnabled(enabled);
272: }
273:
274: /**
275: * Sets the properties of the specified title to match the properties
276: * defined on this panel.
277: *
278: * @param chart the chart whose title is to be modified.
279: */
280: public void setTitleProperties(JFreeChart chart) {
281: if (this .showTitle) {
282: TextTitle title = chart.getTitle();
283: if (title == null) {
284: title = new TextTitle();
285: chart.setTitle(title);
286: }
287: title.setText(getTitleText());
288: title.setFont(getTitleFont());
289: title.setPaint(getTitlePaint());
290: } else {
291: chart.setTitle((TextTitle) null);
292: }
293: }
294:
295: }
|