KickJava   Java API By Example, From Geeks To Geeks.

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


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  * SWTPlotAppearanceEditor.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.BasicStroke JavaDoc;
44 import java.awt.Stroke JavaDoc;
45 import java.util.ResourceBundle JavaDoc;
46
47 import org.eclipse.swt.SWT;
48 import org.eclipse.swt.events.SelectionAdapter;
49 import org.eclipse.swt.events.SelectionEvent;
50 import org.eclipse.swt.graphics.Color;
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.Combo;
58 import org.eclipse.swt.widgets.Composite;
59 import org.eclipse.swt.widgets.Group;
60 import org.eclipse.swt.widgets.Label;
61 import org.eclipse.swt.widgets.Spinner;
62 import org.jfree.chart.plot.CategoryPlot;
63 import org.jfree.chart.plot.Plot;
64 import org.jfree.chart.plot.PlotOrientation;
65 import org.jfree.chart.plot.XYPlot;
66 import org.jfree.experimental.swt.SWTPaintCanvas;
67 import org.jfree.experimental.swt.SWTUtils;
68
69 /**
70  * An editor for plot properties.
71  */

72 class SWTPlotAppearanceEditor extends Composite
73 {
74     
75     private Spinner selectStroke;
76     
77     /** The stroke (pen) used to draw the outline of the plot. */
78     private SWTStrokeCanvas strokeCanvas;
79     
80     /** The paint (color) used to fill the background of the plot. */
81     private SWTPaintCanvas backgroundPaintCanvas;
82
83     /** The paint (color) used to draw the outline of the plot. */
84     private SWTPaintCanvas outlinePaintCanvas;
85
86     /** The orientation for the plot. */
87     private PlotOrientation plotOrientation;
88
89     private Combo orientation;
90     
91     /** Orientation constants. */
92     private final static String JavaDoc[] orientationNames = { "Vertical",
93         "Horizontal" };
94     private final static int ORIENTATION_VERTICAL = 0;
95     private final static int ORIENTATION_HORIZONTAL = 1;
96
97     /** The resourceBundle for the localization. */
98     protected static ResourceBundle JavaDoc localizationResources
99         = ResourceBundle.getBundle("org.jfree.chart.editor.LocalizationBundle");
100
101     SWTPlotAppearanceEditor(Composite parent, int style, Plot plot)
102     {
103         super(parent, style);
104         FillLayout layout = new FillLayout();
105         layout.marginHeight = layout.marginWidth = 4;
106         this.setLayout(layout);
107
108         Group general = new Group(this, SWT.NONE);
109         GridLayout groupLayout = new GridLayout(3, false);
110         groupLayout.marginHeight = groupLayout.marginWidth = 4;
111         general.setLayout(groupLayout);
112         general.setText( localizationResources.getString("General"));
113         
114         // row 1: stroke
115
new Label(general, SWT.NONE).setText(localizationResources.getString(
116                 "Outline_stroke"));
117         strokeCanvas = new SWTStrokeCanvas(general, SWT.NONE);
118         strokeCanvas.setStroke(plot.getOutlineStroke());
119         GridData strokeGridData = new GridData(SWT.FILL, SWT.CENTER, true,
120                 false);
121         strokeGridData.heightHint = 20;
122         strokeCanvas.setLayoutData(strokeGridData);
123         selectStroke = new Spinner(general, SWT.BORDER);
124         selectStroke.setMinimum(1);
125         selectStroke.setMaximum(3);
126         selectStroke.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
127                 false));
128         selectStroke.addSelectionListener(
129                 new SelectionAdapter() {
130                     public void widgetSelected(SelectionEvent event) {
131                         int w = selectStroke.getSelection();
132                         if (w > 0) {
133                             strokeCanvas.setStroke(new BasicStroke JavaDoc(w));
134                             strokeCanvas.redraw();
135                         }
136                     }
137                 }
138         );
139         // row 2: outline color
140
new Label(general, SWT.NONE).setText(localizationResources.getString(
141                 "Outline_Paint"));
142         outlinePaintCanvas = new SWTPaintCanvas(general, SWT.NONE,
143                 SWTUtils.toSwtColor(getDisplay(), plot.getOutlinePaint()));
144         GridData outlineGridData = new GridData(SWT.FILL, SWT.CENTER, true,
145                 false);
146         outlineGridData.heightHint = 20;
147         outlinePaintCanvas.setLayoutData(outlineGridData);
148         Button selectOutlineColor = new Button(general, SWT.PUSH);
149         selectOutlineColor.setText(localizationResources.getString(
150                 "Select..."));
151         selectOutlineColor.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER,
152                 false, false));
153         selectOutlineColor.addSelectionListener(
154                 new SelectionAdapter() {
155                     public void widgetSelected(SelectionEvent event) {
156                         ColorDialog dlg = new ColorDialog(getShell());
157                         dlg.setText(localizationResources.getString(
158                                 "Outline_Paint"));
159                         dlg.setRGB(outlinePaintCanvas.getColor().getRGB());
160                         RGB rgb = dlg.open();
161                         if (rgb != null) {
162                             outlinePaintCanvas.setColor(new Color(getDisplay(),
163                                     rgb));
164                         }
165                     }
166                 }
167         );
168         // row 3: background paint
169
new Label(general, SWT.NONE).setText(localizationResources.getString(
170                 "Background_paint"));
171         backgroundPaintCanvas = new SWTPaintCanvas(general, SWT.NONE,
172                 SWTUtils.toSwtColor(getDisplay(), plot.getBackgroundPaint()));
173         GridData bgGridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
174         bgGridData.heightHint = 20;
175         backgroundPaintCanvas.setLayoutData(bgGridData);
176         Button selectBgPaint = new Button(general, SWT.PUSH);
177         selectBgPaint.setText(localizationResources.getString("Select..."));
178         selectBgPaint.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false,
179                 false));
180         selectBgPaint.addSelectionListener(
181                 new SelectionAdapter() {
182                     public void widgetSelected(SelectionEvent event) {
183                         ColorDialog dlg = new ColorDialog(getShell());
184                         dlg.setText(localizationResources.getString(
185                                 "Background_paint"));
186                         dlg.setRGB(backgroundPaintCanvas.getColor().getRGB());
187                         RGB rgb = dlg.open();
188                         if (rgb != null) {
189                             backgroundPaintCanvas.setColor(
190                                     new Color(getDisplay(), rgb));
191                         }
192                     }
193                 }
194         );
195         // row 4: orientation
196
if (plot instanceof CategoryPlot) {
197             this.plotOrientation = ((CategoryPlot) plot).getOrientation();
198         }
199         else if (plot instanceof XYPlot) {
200             this.plotOrientation = ((XYPlot) plot).getOrientation();
201         }
202         if (this.plotOrientation != null) {
203             boolean isVertical
204                     = this.plotOrientation.equals(PlotOrientation.VERTICAL);
205             int index = isVertical ? ORIENTATION_VERTICAL
206                     : ORIENTATION_HORIZONTAL;
207             new Label(general, SWT.NONE).setText(
208                     localizationResources.getString("Orientation"));
209             orientation = new Combo(general, SWT.DROP_DOWN);
210             orientation.setItems(orientationNames);
211             orientation.select(index);
212             orientation.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true,
213                     false, 2, 1));
214             orientation.addSelectionListener(
215                     new SelectionAdapter() {
216                         public void widgetSelected(SelectionEvent event) {
217                             switch (orientation.getSelectionIndex()) {
218                                 case ORIENTATION_VERTICAL:
219                                     plotOrientation = PlotOrientation.VERTICAL;
220                                     break;
221                                 case ORIENTATION_HORIZONTAL:
222                                     plotOrientation
223                                         = PlotOrientation.HORIZONTAL;
224                                     break;
225                                 default:
226                                     plotOrientation = PlotOrientation.VERTICAL;
227                             }
228                         }
229                     }
230             );
231         }
232     }
233
234     /**
235      * Returns the plot orientation.
236      *
237      * @return The plot orientation.
238      */

239     public PlotOrientation getPlotOrientation() {
240         return this.plotOrientation;
241     }
242     
243     /**
244      * Returns the background paint.
245      *
246      * @return The background paint.
247      */

248     public Color getBackGroundPaint() {
249         return backgroundPaintCanvas.getColor();
250     }
251
252     /**
253      * Returns the outline paint.
254      *
255      * @return The outline paint.
256      */

257     public Color getOutlinePaint() {
258         return outlinePaintCanvas.getColor();
259     }
260
261     /**
262      * Returns the stroke.
263      *
264      * @return The stroke.
265      */

266     public Stroke JavaDoc getStroke() {
267         return strokeCanvas.getStroke();
268     }
269 }
270
Popular Tags