KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > experimental > chart > swt > editor > SWTTitleEditor


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2006, 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
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * -------------------
28  * SWTTitleEditor.java
29  * -------------------
30  * (C) Copyright 2006, by Henry Proudhon and Contributors.
31  *
32  * Original Author: Henry Proudhon (henry.proudhon AT insa-lyon.fr);
33  * Contributor(s): David Gilbert (for Object Refinery Limited);
34  *
35  * Changes
36  * -------
37  * 01-Aug-2006 : New class (HP);
38  *
39  */

40
41 package org.jfree.experimental.chart.swt.editor;
42
43 import java.util.ResourceBundle JavaDoc;
44
45 import org.eclipse.swt.SWT;
46 import org.eclipse.swt.events.SelectionAdapter;
47 import org.eclipse.swt.events.SelectionEvent;
48 import org.eclipse.swt.graphics.Color;
49 import org.eclipse.swt.graphics.Font;
50 import org.eclipse.swt.graphics.FontData;
51 import org.eclipse.swt.graphics.RGB;
52 import org.eclipse.swt.layout.FillLayout;
53 import org.eclipse.swt.layout.GridData;
54 import org.eclipse.swt.layout.GridLayout;
55 import org.eclipse.swt.widgets.Button;
56 import org.eclipse.swt.widgets.ColorDialog;
57 import org.eclipse.swt.widgets.Composite;
58 import org.eclipse.swt.widgets.FontDialog;
59 import org.eclipse.swt.widgets.Group;
60 import org.eclipse.swt.widgets.Label;
61 import org.eclipse.swt.widgets.Text;
62 import org.jfree.chart.JFreeChart;
63 import org.jfree.chart.title.TextTitle;
64 import org.jfree.chart.title.Title;
65 import org.jfree.experimental.swt.SWTPaintCanvas;
66 import org.jfree.experimental.swt.SWTUtils;
67
68 /**
69  * An editor for chart title properties.
70  */

71 class SWTTitleEditor extends Composite {
72     
73     /** Whether or not to display the title on the chart. */
74     private boolean showTitle;
75
76     /** The checkbox to indicate whether or not to display the title. */
77     private Button showTitleCheckBox;
78
79     /** A field for displaying/editing the title text. */
80     private Text titleField;
81
82     /** The font used to draw the title. */
83     private FontData titleFont;
84
85     /** A field for displaying a description of the title font. */
86     private Text fontField;
87
88     /** The button to use to select a new title font. */
89     private Button selectFontButton;
90
91     /** The paint (color) used to draw the title. */
92     private Color titleColor;
93
94     /** The button to use to select a new paint (color) to draw the title. */
95     private Button selectColorButton;
96
97     /** The resourceBundle for the localization. */
98     protected static ResourceBundle JavaDoc localizationResources
99         = ResourceBundle.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.getString("Title")));
118         this.showTitle = (title != null);
119         this.titleFont = SWTUtils.toSwtFontData(getDisplay(), t.getFont(),
120                 true);
121         this.titleColor = SWTUtils.toSwtColor(getDisplay(), t.getPaint());
122         
123         Group general = new Group(this, SWT.NONE);
124         general.setLayout(new GridLayout(3, false));
125         general.setText(localizationResources.getString("General"));
126         // row 1
127
Label label = new Label(general, SWT.NONE);
128         label.setText(localizationResources.getString("Show_Title"));
129         GridData gridData = new GridData();
130         gridData.horizontalSpan = 2;
131         label.setLayoutData(gridData);
132         showTitleCheckBox = new Button(general, SWT.CHECK);
133         showTitleCheckBox.setSelection(this.showTitle);
134         showTitleCheckBox.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER,
135                 false, false));
136         showTitleCheckBox.addSelectionListener(
137                 new SelectionAdapter() {
138                     public void widgetSelected(SelectionEvent event) {
139                         showTitle = showTitleCheckBox.getSelection();
140                     }
141                 });
142         // row 2
143
new Label(general, SWT.NONE).setText(localizationResources.getString(
144                 "Text"));
145         titleField = new Text(general, SWT.BORDER);
146         titleField.setText(t.getText());
147         titleField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
148                 false));
149         new Label(general, SWT.NONE).setText("");
150         // row 3
151
new Label(general, SWT.NONE).setText(localizationResources.getString(
152                 "Font"));
153         fontField = new Text(general, SWT.BORDER);
154         fontField.setText(titleFont.toString());
155         fontField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
156                 false));
157         selectFontButton = new Button(general, SWT.PUSH);
158         selectFontButton.setText(localizationResources.getString("Select..."));
159         selectFontButton.addSelectionListener(
160                 new SelectionAdapter() {
161                     public void widgetSelected(SelectionEvent event) {
162                         // Create the font-change dialog
163
FontDialog dlg = new FontDialog(getShell());
164                         dlg.setText(localizationResources.getString(
165                                 "Font_Selection"));
166                         dlg.setFontList(new FontData[] { titleFont });
167                         if (dlg.open() != null) {
168                             // Dispose of any fonts we have created
169
if (font != null) font.dispose();
170                             // Create the new font and set it into the title
171
// label
172
font = new Font(getShell().getDisplay(),
173                                     dlg.getFontList());
174                             //titleField.setFont(font);
175
fontField.setText(font.getFontData()[0].toString());
176                             titleFont = font.getFontData()[0];
177                         }
178                     }
179                 }
180         );
181         // row 4
182
new Label(general, SWT.NONE).setText(localizationResources.getString(
183                 "Color"));
184         // Use a SwtPaintCanvas to show the color, note that we must set the
185
// heightHint.
186
final SWTPaintCanvas colorCanvas = new SWTPaintCanvas(general,
187                 SWT.NONE, this.titleColor);
188         GridData canvasGridData = new GridData(SWT.FILL, SWT.CENTER, true,
189                 false);
190         canvasGridData.heightHint = 20;
191         colorCanvas.setLayoutData(canvasGridData);
192         selectColorButton = new Button(general, SWT.PUSH);
193         selectColorButton.setText(localizationResources.getString("Select..."));
194         selectColorButton.addSelectionListener(
195                 new SelectionAdapter() {
196                     public void widgetSelected(SelectionEvent event) {
197                         // Create the color-change dialog
198
ColorDialog dlg = new ColorDialog(getShell());
199                         dlg.setText(localizationResources.getString(
200                                 "Title_Color"));
201                         dlg.setRGB(titleColor.getRGB());
202                         RGB rgb = dlg.open();
203                         if (rgb != null) {
204                             // create the new color and set it to the
205
// SwtPaintCanvas
206
titleColor = new Color(getDisplay(), rgb);
207                             colorCanvas.setColor( titleColor );
208                         }
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 JavaDoc 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(), getTitleFont(),
256                     true));
257             title.setPaint(SWTUtils.toAwtColor(getTitleColor()));
258         }
259         else {
260             chart.setTitle((TextTitle) null);
261         }
262     }
263 }
264
Popular Tags