KickJava   Java API By Example, From Geeks To Geeks.

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


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  * SWTStrokeCanvas.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
46 import org.eclipse.swt.SWT;
47 import org.eclipse.swt.events.PaintEvent;
48 import org.eclipse.swt.events.PaintListener;
49 import org.eclipse.swt.graphics.Image;
50 import org.eclipse.swt.graphics.Rectangle;
51 import org.eclipse.swt.graphics.Transform;
52 import org.eclipse.swt.widgets.Canvas;
53 import org.eclipse.swt.widgets.Composite;
54
55 /**
56  * A control for displaying a <code>Stroke</code> sample.
57  */

58 class SWTStrokeCanvas extends Canvas {
59     
60     /**
61      * Creates a new instance.
62      *
63      * @param parent the parent.
64      * @param style the style.
65      * @param image the image.
66      */

67     public SWTStrokeCanvas(Composite parent, int style, Image image) {
68         this(parent, style);
69     }
70
71     /**
72      * Creates a new instance.
73      *
74      * @param parent the parent.
75      * @param style the style.
76      */

77     public SWTStrokeCanvas(Composite parent, int style) {
78         super(parent, style);
79         addPaintListener(new PaintListener() {
80             public void paintControl(PaintEvent e) {
81                 BasicStroke JavaDoc stroke = (BasicStroke JavaDoc) getStroke();
82                 if (stroke != null) {
83                     int x, y;
84                     Rectangle rect = getClientArea();
85                     x = (rect.width - 100) / 2;
86                     y = (rect.height - 16) / 2;
87                     Transform swtTransform = new Transform(e.gc.getDevice());
88                     e.gc.getTransform(swtTransform);
89                     swtTransform.translate(x, y);
90                     e.gc.setTransform(swtTransform);
91                     swtTransform.dispose();
92                     e.gc.setBackground(getDisplay().getSystemColor(
93                             SWT.COLOR_BLACK));
94                     e.gc.setLineWidth((int) stroke.getLineWidth());
95                     e.gc.drawLine(10, 8, 90, 8);
96                 }
97             }
98         });
99     }
100     
101     /**
102      * Sets the stroke.
103      *
104      * @param stroke the stroke.
105      */

106     public void setStroke(Stroke JavaDoc stroke) {
107         if (stroke instanceof BasicStroke JavaDoc) {
108             this.setData( stroke );
109         }
110         else {
111             throw new RuntimeException JavaDoc(
112                 "Can only handle 'Basic Stroke' at present.");
113         }
114     }
115
116     /**
117      * Returns the stroke.
118      *
119      * @return The stroke.
120      */

121     public BasicStroke JavaDoc getStroke() {
122         return (BasicStroke JavaDoc) this.getData();
123     }
124 }
125
Popular Tags