KickJava   Java API By Example, From Geeks To Geeks.

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


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  * SWTAxisEditor.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.awt.Paint JavaDoc;
44 import java.util.ResourceBundle JavaDoc;
45
46 import org.eclipse.swt.SWT;
47 import org.eclipse.swt.events.SelectionAdapter;
48 import org.eclipse.swt.events.SelectionEvent;
49 import org.eclipse.swt.graphics.Color;
50 import org.eclipse.swt.graphics.Font;
51 import org.eclipse.swt.graphics.FontData;
52 import org.eclipse.swt.graphics.RGB;
53 import org.eclipse.swt.layout.FillLayout;
54 import org.eclipse.swt.layout.GridData;
55 import org.eclipse.swt.layout.GridLayout;
56 import org.eclipse.swt.widgets.Button;
57 import org.eclipse.swt.widgets.ColorDialog;
58 import org.eclipse.swt.widgets.Composite;
59 import org.eclipse.swt.widgets.FontDialog;
60 import org.eclipse.swt.widgets.Group;
61 import org.eclipse.swt.widgets.Label;
62 import org.eclipse.swt.widgets.TabFolder;
63 import org.eclipse.swt.widgets.TabItem;
64 import org.eclipse.swt.widgets.Text;
65 import org.jfree.chart.axis.Axis;
66 import org.jfree.chart.axis.NumberAxis;
67 import org.jfree.experimental.swt.SWTPaintCanvas;
68 import org.jfree.experimental.swt.SWTUtils;
69
70 /**
71  * An editor for axis properties.
72  */

73 class SWTAxisEditor extends Composite {
74     
75     /** The axis label. */
76     private Text label;
77     
78     /** The font used to draw the axis labels. */
79     private FontData labelFont;
80
81     /** The paint (color) used to draw the axis labels. */
82     private Color labelPaintColor;
83
84     /** The font used to draw the axis tick labels. */
85     private FontData tickLabelFont;
86
87     /** The paint (color) used to draw the axis tick labels. */
88     private Color tickLabelPaintColor;
89
90     /** A field showing a description of the label font. */
91     private Text labelFontField;
92     
93     /**
94      * A field containing a description of the font
95      * for displaying tick labels on the axis.
96      */

97     private Text tickLabelFontField;
98
99     /** The resourceBundle for the localization. */
100     protected static ResourceBundle JavaDoc localizationResources =
101         ResourceBundle.getBundle("org.jfree.chart.editor.LocalizationBundle");
102
103     /** Font object used to handle a change of font. */
104     private Font font;
105
106     /** A flag that indicates whether or not the tick labels are visible. */
107     private Button showTickLabelsCheckBox;
108     
109     /** A flag that indicates whether or not the tick marks are visible. */
110     private Button showTickMarksCheckBox;
111     
112     /** A tabbed pane for... */
113     private TabFolder otherTabs;
114     
115     /**
116      * Standard constructor: builds a composite for displaying/editing
117      * the properties of the specified axis.
118      *
119      * @param parent The parent composite.
120      * @param style The SWT style of the SwtAxisEditor.
121      * @param axis the axis whose properties are to be displayed/edited
122      * in the composite.
123      */

124     public SWTAxisEditor(Composite parent, int style, Axis axis) {
125         super(parent, style);
126         this.labelFont = SWTUtils.toSwtFontData(getDisplay(),
127                 axis.getLabelFont(), true);
128         this.labelPaintColor = SWTUtils.toSwtColor(getDisplay(),
129                 axis.getLabelPaint());
130         this.tickLabelFont = SWTUtils.toSwtFontData(getDisplay(),
131                 axis.getTickLabelFont(), true);
132         this.tickLabelPaintColor = SWTUtils.toSwtColor(getDisplay(),
133                 axis.getTickLabelPaint());
134         
135         FillLayout layout = new FillLayout(SWT.VERTICAL);
136         layout.marginHeight = layout.marginWidth = 4;
137         this.setLayout(layout);
138         Group general = new Group(this, SWT.NONE);
139         general.setLayout(new GridLayout(3, false));
140         general.setText(localizationResources.getString("General"));
141         // row 1
142
new Label(general, SWT.NONE).setText(localizationResources.getString(
143                 "Label"));
144         label = new Text(general, SWT.BORDER);
145         label.setText(axis.getLabel());
146         label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
147         new Label(general, SWT.NONE).setText(""); //empty cell
148
// row 2
149
new Label(general, SWT.NONE).setText(localizationResources.getString(
150                 "Font"));
151         labelFontField = new Text(general, SWT.BORDER);
152         labelFontField.setText(this.labelFont.toString());
153         labelFontField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
154                 false));
155         Button selectFontButton = new Button(general, SWT.PUSH);
156         selectFontButton.setText(localizationResources.getString("Select..."));
157         selectFontButton.addSelectionListener(
158                 new SelectionAdapter() {
159                     public void widgetSelected(SelectionEvent event) {
160                         // Create the color-change dialog
161
FontDialog dlg = new FontDialog(getShell());
162                         dlg.setText(localizationResources.getString(
163                                 "Font_Selection"));
164                         dlg.setFontList(new FontData[] { labelFont });
165                         if (dlg.open() != null) {
166                             // Dispose of any fonts we have created
167
if (font != null) {
168                                 font.dispose();
169                             }
170                             // Create the new font and set it into the title
171
// label
172
font = new Font(getShell().getDisplay(),
173                                     dlg.getFontList());
174                             //label.setFont( font );
175
labelFontField.setText(
176                                     font.getFontData()[0].toString());
177                             labelFont = font.getFontData()[0];
178                         }
179                     }
180                 }
181         );
182         // row 3
183
new Label(general, SWT.NONE).setText(localizationResources.getString(
184                 "Paint"));
185         // Use a colored text field to show the color
186
final SWTPaintCanvas colorCanvas = new SWTPaintCanvas(general,
187                 SWT.NONE, this.labelPaintColor);
188         GridData canvasGridData = new GridData(SWT.FILL, SWT.CENTER, true,
189                 false);
190         canvasGridData.heightHint = 20;
191         colorCanvas.setLayoutData(canvasGridData);
192         Button 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(labelPaintColor.getRGB());
202                         RGB rgb = dlg.open();
203                         if (rgb != null) {
204                           // create the new color and set it to the
205
// SwtPaintCanvas
206
labelPaintColor = new Color(getDisplay(), rgb);
207                           colorCanvas.setColor(labelPaintColor);
208                         }
209                     }
210                 }
211         );
212         Group other = new Group(this, SWT.NONE);
213         FillLayout tabLayout = new FillLayout();
214         tabLayout.marginHeight = tabLayout.marginWidth = 4;
215         other.setLayout(tabLayout);
216         other.setText(localizationResources.getString("Other"));
217         
218         otherTabs = new TabFolder(other, SWT.NONE);
219         TabItem item1 = new TabItem(otherTabs, SWT.NONE);
220         item1.setText(" " + localizationResources.getString("Ticks") + " ");
221         Composite ticks = new Composite(otherTabs, SWT.NONE);
222         ticks.setLayout(new GridLayout(3, false));
223         showTickLabelsCheckBox = new Button(ticks, SWT.CHECK);
224         showTickLabelsCheckBox.setText(localizationResources.getString(
225                 "Show_tick_labels"));
226         showTickLabelsCheckBox.setSelection(axis.isTickLabelsVisible());
227         showTickLabelsCheckBox.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
228                 true, false, 3, 1));
229         new Label(ticks, SWT.NONE).setText(localizationResources.getString(
230                 "Tick_label_font"));
231         tickLabelFontField = new Text(ticks, SWT.BORDER);
232         tickLabelFontField.setText(this.tickLabelFont.toString());
233         //tickLabelFontField.setFont(SwtUtils.toSwtFontData(getDisplay(),
234
// axis.getTickLabelFont()));
235
tickLabelFontField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
236                 true, false));
237         Button selectTickLabelFontButton = new Button(ticks, SWT.PUSH);
238         selectTickLabelFontButton.setText(localizationResources.getString(
239                 "Select..."));
240         selectTickLabelFontButton.addSelectionListener(
241                 new SelectionAdapter() {
242                     public void widgetSelected(SelectionEvent event) {
243                         // Create the font-change dialog
244
FontDialog dlg = new FontDialog(getShell());
245                         dlg.setText( localizationResources.getString(
246                                 "Font_Selection"));
247                         dlg.setFontList(new FontData[] {tickLabelFont});
248                         if (dlg.open() != null) {
249                             // Dispose of any fonts we have created
250
if (font != null) font.dispose();
251                             // Create the new font and set it into the title label
252
font = new Font(getShell().getDisplay(),
253                                     dlg.getFontList());
254                             //tickLabelFontField.setFont(font);
255
tickLabelFontField.setText(
256                                     font.getFontData()[0].toString());
257                             tickLabelFont = font.getFontData()[0];
258                         }
259                     }
260                 }
261         );
262         showTickMarksCheckBox = new Button(ticks, SWT.CHECK);
263         showTickMarksCheckBox.setText(localizationResources.getString(
264                 "Show_tick_marks"));
265         showTickMarksCheckBox.setSelection(axis.isTickMarksVisible());
266         showTickMarksCheckBox.setLayoutData(new GridData( SWT.FILL, SWT.CENTER,
267                 true, false, 3, 1));
268         item1.setControl(ticks);
269     }
270
271     /**
272      * A static method that returns a panel that is appropriate
273      * for the axis type.
274      *
275      * @param parent the parent.
276      * @param style the style.
277      * @param axis the axis whose properties are to be displayed/edited
278      * in the composite.
279      * @return A composite or <code>null</code< if axis is <code>null</code>.
280      */

281     public static SWTAxisEditor getInstance(Composite parent, int style,
282             Axis axis) {
283         
284         if (axis != null) {
285             // return the appropriate axis editor
286
if (axis instanceof NumberAxis)
287                 return new SWTNumberAxisEditor(parent, style,
288                         (NumberAxis) axis );
289             else return new SWTAxisEditor(parent, style, axis);
290         }
291         else return null;
292     }
293     
294     /**
295      * Returns a reference to the tabbed composite.
296      *
297      * @return A reference to the tabbed composite.
298      */

299     public TabFolder getOtherTabs() {
300         return this.otherTabs;
301     }
302
303     /**
304      * Returns the current axis label.
305      *
306      * @return The current axis label.
307      */

308     public String JavaDoc getLabel() {
309         return this.label.getText();
310     }
311
312     /**
313      * Returns the current label font.
314      *
315      * @return The current label font.
316      */

317     public java.awt.Font JavaDoc getLabelFont() {
318         return SWTUtils.toAwtFont(getDisplay(), this.labelFont, true);
319     }
320
321     /**
322      * Returns the current label paint.
323      *
324      * @return The current label paint.
325      */

326     public Paint JavaDoc getTickLabelPaint() {
327         return SWTUtils.toAwtColor(this.tickLabelPaintColor);
328     }
329     
330     /**
331      * Returns the current label font.
332      *
333      * @return The current label font.
334      */

335     public java.awt.Font JavaDoc getTickLabelFont() {
336         return SWTUtils.toAwtFont(getDisplay(), tickLabelFont, true);
337     }
338
339     /**
340      * Returns the current label paint.
341      *
342      * @return The current label paint.
343      */

344     public Paint JavaDoc getLabelPaint() {
345         return SWTUtils.toAwtColor(this.labelPaintColor);
346     }
347     
348     /**
349      * Sets the properties of the specified axis to match
350      * the properties defined on this panel.
351      *
352      * @param axis the axis.
353      */

354     public void setAxisProperties(Axis axis) {
355         axis.setLabel(getLabel());
356         axis.setLabelFont(getLabelFont());
357         axis.setLabelPaint(getLabelPaint());
358         axis.setTickMarksVisible(showTickMarksCheckBox.getSelection());
359         axis.setTickLabelsVisible(showTickLabelsCheckBox.getSelection());
360         axis.setTickLabelFont(getTickLabelFont());
361         axis.setTickLabelPaint(getTickLabelPaint());
362     }
363 }
364
Popular Tags