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: * SWTPlotAppearanceEditor.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.awt.BasicStroke;
044: import java.awt.Stroke;
045: import java.util.ResourceBundle;
046:
047: import org.eclipse.swt.SWT;
048: import org.eclipse.swt.events.SelectionAdapter;
049: import org.eclipse.swt.events.SelectionEvent;
050: import org.eclipse.swt.graphics.Color;
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.Combo;
058: import org.eclipse.swt.widgets.Composite;
059: import org.eclipse.swt.widgets.Group;
060: import org.eclipse.swt.widgets.Label;
061: import org.eclipse.swt.widgets.Spinner;
062: import org.jfree.chart.plot.CategoryPlot;
063: import org.jfree.chart.plot.Plot;
064: import org.jfree.chart.plot.PlotOrientation;
065: import org.jfree.chart.plot.XYPlot;
066: import org.jfree.experimental.swt.SWTPaintCanvas;
067: import org.jfree.experimental.swt.SWTUtils;
068:
069: /**
070: * An editor for plot properties.
071: */
072: class SWTPlotAppearanceEditor extends Composite {
073:
074: private Spinner selectStroke;
075:
076: /** The stroke (pen) used to draw the outline of the plot. */
077: private SWTStrokeCanvas strokeCanvas;
078:
079: /** The paint (color) used to fill the background of the plot. */
080: private SWTPaintCanvas backgroundPaintCanvas;
081:
082: /** The paint (color) used to draw the outline of the plot. */
083: private SWTPaintCanvas outlinePaintCanvas;
084:
085: /** The orientation for the plot. */
086: private PlotOrientation plotOrientation;
087:
088: private Combo orientation;
089:
090: /** Orientation constants. */
091: private final static String[] orientationNames = { "Vertical",
092: "Horizontal" };
093: private final static int ORIENTATION_VERTICAL = 0;
094: private final static int ORIENTATION_HORIZONTAL = 1;
095:
096: /** The resourceBundle for the localization. */
097: protected static ResourceBundle localizationResources = ResourceBundle
098: .getBundle("org.jfree.chart.editor.LocalizationBundle");
099:
100: SWTPlotAppearanceEditor(Composite parent, int style, Plot plot) {
101: super (parent, style);
102: FillLayout layout = new FillLayout();
103: layout.marginHeight = layout.marginWidth = 4;
104: this .setLayout(layout);
105:
106: Group general = new Group(this , SWT.NONE);
107: GridLayout groupLayout = new GridLayout(3, false);
108: groupLayout.marginHeight = groupLayout.marginWidth = 4;
109: general.setLayout(groupLayout);
110: general.setText(localizationResources.getString("General"));
111:
112: // row 1: stroke
113: new Label(general, SWT.NONE).setText(localizationResources
114: .getString("Outline_stroke"));
115: strokeCanvas = new SWTStrokeCanvas(general, SWT.NONE);
116: strokeCanvas.setStroke(plot.getOutlineStroke());
117: GridData strokeGridData = new GridData(SWT.FILL, SWT.CENTER,
118: true, false);
119: strokeGridData.heightHint = 20;
120: strokeCanvas.setLayoutData(strokeGridData);
121: selectStroke = new Spinner(general, SWT.BORDER);
122: selectStroke.setMinimum(1);
123: selectStroke.setMaximum(3);
124: selectStroke.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
125: false, false));
126: selectStroke.addSelectionListener(new SelectionAdapter() {
127: public void widgetSelected(SelectionEvent event) {
128: int w = selectStroke.getSelection();
129: if (w > 0) {
130: strokeCanvas.setStroke(new BasicStroke(w));
131: strokeCanvas.redraw();
132: }
133: }
134: });
135: // row 2: outline color
136: new Label(general, SWT.NONE).setText(localizationResources
137: .getString("Outline_Paint"));
138: outlinePaintCanvas = new SWTPaintCanvas(general, SWT.NONE,
139: SWTUtils.toSwtColor(getDisplay(), plot
140: .getOutlinePaint()));
141: GridData outlineGridData = new GridData(SWT.FILL, SWT.CENTER,
142: true, false);
143: outlineGridData.heightHint = 20;
144: outlinePaintCanvas.setLayoutData(outlineGridData);
145: Button selectOutlineColor = new Button(general, SWT.PUSH);
146: selectOutlineColor.setText(localizationResources
147: .getString("Select..."));
148: selectOutlineColor.setLayoutData(new GridData(SWT.CENTER,
149: SWT.CENTER, false, false));
150: selectOutlineColor.addSelectionListener(new SelectionAdapter() {
151: public void widgetSelected(SelectionEvent event) {
152: ColorDialog dlg = new ColorDialog(getShell());
153: dlg.setText(localizationResources
154: .getString("Outline_Paint"));
155: dlg.setRGB(outlinePaintCanvas.getColor().getRGB());
156: RGB rgb = dlg.open();
157: if (rgb != null) {
158: outlinePaintCanvas.setColor(new Color(getDisplay(),
159: rgb));
160: }
161: }
162: });
163: // row 3: background paint
164: new Label(general, SWT.NONE).setText(localizationResources
165: .getString("Background_paint"));
166: backgroundPaintCanvas = new SWTPaintCanvas(general, SWT.NONE,
167: SWTUtils.toSwtColor(getDisplay(), plot
168: .getBackgroundPaint()));
169: GridData bgGridData = new GridData(SWT.FILL, SWT.CENTER, true,
170: false);
171: bgGridData.heightHint = 20;
172: backgroundPaintCanvas.setLayoutData(bgGridData);
173: Button selectBgPaint = new Button(general, SWT.PUSH);
174: selectBgPaint.setText(localizationResources
175: .getString("Select..."));
176: selectBgPaint.setLayoutData(new GridData(SWT.CENTER,
177: SWT.CENTER, false, false));
178: selectBgPaint.addSelectionListener(new SelectionAdapter() {
179: public void widgetSelected(SelectionEvent event) {
180: ColorDialog dlg = new ColorDialog(getShell());
181: dlg.setText(localizationResources
182: .getString("Background_paint"));
183: dlg.setRGB(backgroundPaintCanvas.getColor().getRGB());
184: RGB rgb = dlg.open();
185: if (rgb != null) {
186: backgroundPaintCanvas.setColor(new Color(
187: getDisplay(), rgb));
188: }
189: }
190: });
191: // row 4: orientation
192: if (plot instanceof CategoryPlot) {
193: this .plotOrientation = ((CategoryPlot) plot)
194: .getOrientation();
195: } else if (plot instanceof XYPlot) {
196: this .plotOrientation = ((XYPlot) plot).getOrientation();
197: }
198: if (this .plotOrientation != null) {
199: boolean isVertical = this .plotOrientation
200: .equals(PlotOrientation.VERTICAL);
201: int index = isVertical ? ORIENTATION_VERTICAL
202: : ORIENTATION_HORIZONTAL;
203: new Label(general, SWT.NONE).setText(localizationResources
204: .getString("Orientation"));
205: orientation = new Combo(general, SWT.DROP_DOWN);
206: orientation.setItems(orientationNames);
207: orientation.select(index);
208: orientation.setLayoutData(new GridData(SWT.RIGHT,
209: SWT.CENTER, true, false, 2, 1));
210: orientation.addSelectionListener(new SelectionAdapter() {
211: public void widgetSelected(SelectionEvent event) {
212: switch (orientation.getSelectionIndex()) {
213: case ORIENTATION_VERTICAL:
214: plotOrientation = PlotOrientation.VERTICAL;
215: break;
216: case ORIENTATION_HORIZONTAL:
217: plotOrientation = PlotOrientation.HORIZONTAL;
218: break;
219: default:
220: plotOrientation = PlotOrientation.VERTICAL;
221: }
222: }
223: });
224: }
225: }
226:
227: /**
228: * Returns the plot orientation.
229: *
230: * @return The plot orientation.
231: */
232: public PlotOrientation getPlotOrientation() {
233: return this .plotOrientation;
234: }
235:
236: /**
237: * Returns the background paint.
238: *
239: * @return The background paint.
240: */
241: public Color getBackGroundPaint() {
242: return backgroundPaintCanvas.getColor();
243: }
244:
245: /**
246: * Returns the outline paint.
247: *
248: * @return The outline paint.
249: */
250: public Color getOutlinePaint() {
251: return outlinePaintCanvas.getColor();
252: }
253:
254: /**
255: * Returns the stroke.
256: *
257: * @return The stroke.
258: */
259: public Stroke getStroke() {
260: return strokeCanvas.getStroke();
261: }
262: }
|