001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2006, 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: * SWTTitleEditor.java
029: * -------------------
030: * (C) Copyright 2006, by Henry Proudhon and Contributors.
031: *
032: * Original Author: Henry Proudhon (henry.proudhon AT insa-lyon.fr);
033: * Contributor(s): David Gilbert (for Object Refinery Limited);
034: *
035: * Changes
036: * -------
037: * 01-Aug-2006 : New class (HP);
038: *
039: */
040:
041: package org.jfree.experimental.chart.swt.editor;
042:
043: import java.util.ResourceBundle;
044:
045: import org.eclipse.swt.SWT;
046: import org.eclipse.swt.events.SelectionAdapter;
047: import org.eclipse.swt.events.SelectionEvent;
048: import org.eclipse.swt.graphics.Color;
049: import org.eclipse.swt.graphics.Font;
050: import org.eclipse.swt.graphics.FontData;
051: import org.eclipse.swt.graphics.RGB;
052: import org.eclipse.swt.layout.FillLayout;
053: import org.eclipse.swt.layout.GridData;
054: import org.eclipse.swt.layout.GridLayout;
055: import org.eclipse.swt.widgets.Button;
056: import org.eclipse.swt.widgets.ColorDialog;
057: import org.eclipse.swt.widgets.Composite;
058: import org.eclipse.swt.widgets.FontDialog;
059: import org.eclipse.swt.widgets.Group;
060: import org.eclipse.swt.widgets.Label;
061: import org.eclipse.swt.widgets.Text;
062: import org.jfree.chart.JFreeChart;
063: import org.jfree.chart.title.TextTitle;
064: import org.jfree.chart.title.Title;
065: import org.jfree.experimental.swt.SWTPaintCanvas;
066: import org.jfree.experimental.swt.SWTUtils;
067:
068: /**
069: * An editor for chart title properties.
070: */
071: class SWTTitleEditor extends Composite {
072:
073: /** Whether or not to display the title on the chart. */
074: private boolean showTitle;
075:
076: /** The checkbox to indicate whether or not to display the title. */
077: private Button showTitleCheckBox;
078:
079: /** A field for displaying/editing the title text. */
080: private Text titleField;
081:
082: /** The font used to draw the title. */
083: private FontData titleFont;
084:
085: /** A field for displaying a description of the title font. */
086: private Text fontField;
087:
088: /** The button to use to select a new title font. */
089: private Button selectFontButton;
090:
091: /** The paint (color) used to draw the title. */
092: private Color titleColor;
093:
094: /** The button to use to select a new paint (color) to draw the title. */
095: private Button selectColorButton;
096:
097: /** The resourceBundle for the localization. */
098: protected static ResourceBundle localizationResources = ResourceBundle
099: .getBundle("org.jfree.chart.editor.LocalizationBundle");
100:
101: /** Font object used to handle a change of font. */
102: private Font font;
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: SWTTitleEditor(Composite parent, int style, Title title) {
111: super (parent, style);
112: FillLayout layout = new FillLayout();
113: layout.marginHeight = layout.marginWidth = 4;
114: setLayout(layout);
115:
116: TextTitle t = (title != null ? (TextTitle) title
117: : new TextTitle(localizationResources
118: .getString("Title")));
119: this .showTitle = (title != null);
120: this .titleFont = SWTUtils.toSwtFontData(getDisplay(), t
121: .getFont(), true);
122: this .titleColor = SWTUtils.toSwtColor(getDisplay(), t
123: .getPaint());
124:
125: Group general = new Group(this , SWT.NONE);
126: general.setLayout(new GridLayout(3, false));
127: general.setText(localizationResources.getString("General"));
128: // row 1
129: Label label = new Label(general, SWT.NONE);
130: label.setText(localizationResources.getString("Show_Title"));
131: GridData gridData = new GridData();
132: gridData.horizontalSpan = 2;
133: label.setLayoutData(gridData);
134: showTitleCheckBox = new Button(general, SWT.CHECK);
135: showTitleCheckBox.setSelection(this .showTitle);
136: showTitleCheckBox.setLayoutData(new GridData(SWT.CENTER,
137: SWT.CENTER, false, false));
138: showTitleCheckBox.addSelectionListener(new SelectionAdapter() {
139: public void widgetSelected(SelectionEvent event) {
140: showTitle = showTitleCheckBox.getSelection();
141: }
142: });
143: // row 2
144: new Label(general, SWT.NONE).setText(localizationResources
145: .getString("Text"));
146: titleField = new Text(general, SWT.BORDER);
147: titleField.setText(t.getText());
148: titleField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
149: true, false));
150: new Label(general, SWT.NONE).setText("");
151: // row 3
152: new Label(general, SWT.NONE).setText(localizationResources
153: .getString("Font"));
154: fontField = new Text(general, SWT.BORDER);
155: fontField.setText(titleFont.toString());
156: fontField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
157: true, false));
158: selectFontButton = new Button(general, SWT.PUSH);
159: selectFontButton.setText(localizationResources
160: .getString("Select..."));
161: selectFontButton.addSelectionListener(new SelectionAdapter() {
162: public void widgetSelected(SelectionEvent event) {
163: // Create the font-change dialog
164: FontDialog dlg = new FontDialog(getShell());
165: dlg.setText(localizationResources
166: .getString("Font_Selection"));
167: dlg.setFontList(new FontData[] { titleFont });
168: if (dlg.open() != null) {
169: // Dispose of any fonts we have created
170: if (font != null)
171: font.dispose();
172: // Create the new font and set it into the title
173: // label
174: font = new Font(getShell().getDisplay(), dlg
175: .getFontList());
176: //titleField.setFont(font);
177: fontField.setText(font.getFontData()[0].toString());
178: titleFont = font.getFontData()[0];
179: }
180: }
181: });
182: // row 4
183: new Label(general, SWT.NONE).setText(localizationResources
184: .getString("Color"));
185: // Use a SwtPaintCanvas to show the color, note that we must set the
186: // heightHint.
187: final SWTPaintCanvas colorCanvas = new SWTPaintCanvas(general,
188: SWT.NONE, this .titleColor);
189: GridData canvasGridData = new GridData(SWT.FILL, SWT.CENTER,
190: true, false);
191: canvasGridData.heightHint = 20;
192: colorCanvas.setLayoutData(canvasGridData);
193: selectColorButton = new Button(general, SWT.PUSH);
194: selectColorButton.setText(localizationResources
195: .getString("Select..."));
196: selectColorButton.addSelectionListener(new SelectionAdapter() {
197: public void widgetSelected(SelectionEvent event) {
198: // Create the color-change dialog
199: ColorDialog dlg = new ColorDialog(getShell());
200: dlg.setText(localizationResources
201: .getString("Title_Color"));
202: dlg.setRGB(titleColor.getRGB());
203: RGB rgb = dlg.open();
204: if (rgb != null) {
205: // create the new color and set it to the
206: // SwtPaintCanvas
207: titleColor = new Color(getDisplay(), rgb);
208: colorCanvas.setColor(titleColor);
209: }
210: }
211: });
212: }
213:
214: /**
215: * Returns the title text entered in the panel.
216: *
217: * @return The title text entered in the panel.
218: */
219: public String getTitleText() {
220: return this .titleField.getText();
221: }
222:
223: /**
224: * Returns the font selected in the panel.
225: *
226: * @return The font selected in the panel.
227: */
228: public FontData getTitleFont() {
229: return this .titleFont;
230: }
231:
232: /**
233: * Returns the font selected in the panel.
234: *
235: * @return The font selected in the panel.
236: */
237: public Color getTitleColor() {
238: return this .titleColor;
239: }
240:
241: /**
242: * Sets the properties of the specified title to match the properties
243: * defined on this panel.
244: *
245: * @param chart the chart whose title is to be modified.
246: */
247: public void setTitleProperties(JFreeChart chart) {
248: if (this .showTitle) {
249: TextTitle title = chart.getTitle();
250: if (title == null) {
251: title = new TextTitle();
252: chart.setTitle(title);
253: }
254: title.setText(getTitleText());
255: title.setFont(SWTUtils.toAwtFont(getDisplay(),
256: getTitleFont(), true));
257: title.setPaint(SWTUtils.toAwtColor(getTitleColor()));
258: } else {
259: chart.setTitle((TextTitle) null);
260: }
261: }
262: }
|