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: * SWTStrokeCanvas.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:
046: import org.eclipse.swt.SWT;
047: import org.eclipse.swt.events.PaintEvent;
048: import org.eclipse.swt.events.PaintListener;
049: import org.eclipse.swt.graphics.Image;
050: import org.eclipse.swt.graphics.Rectangle;
051: import org.eclipse.swt.graphics.Transform;
052: import org.eclipse.swt.widgets.Canvas;
053: import org.eclipse.swt.widgets.Composite;
054:
055: /**
056: * A control for displaying a <code>Stroke</code> sample.
057: */
058: class SWTStrokeCanvas extends Canvas {
059:
060: /**
061: * Creates a new instance.
062: *
063: * @param parent the parent.
064: * @param style the style.
065: * @param image the image.
066: */
067: public SWTStrokeCanvas(Composite parent, int style, Image image) {
068: this (parent, style);
069: }
070:
071: /**
072: * Creates a new instance.
073: *
074: * @param parent the parent.
075: * @param style the style.
076: */
077: public SWTStrokeCanvas(Composite parent, int style) {
078: super (parent, style);
079: addPaintListener(new PaintListener() {
080: public void paintControl(PaintEvent e) {
081: BasicStroke stroke = (BasicStroke) getStroke();
082: if (stroke != null) {
083: int x, y;
084: Rectangle rect = getClientArea();
085: x = (rect.width - 100) / 2;
086: y = (rect.height - 16) / 2;
087: Transform swtTransform = new Transform(e.gc
088: .getDevice());
089: e.gc.getTransform(swtTransform);
090: swtTransform.translate(x, y);
091: e.gc.setTransform(swtTransform);
092: swtTransform.dispose();
093: e.gc.setBackground(getDisplay().getSystemColor(
094: SWT.COLOR_BLACK));
095: e.gc.setLineWidth((int) stroke.getLineWidth());
096: e.gc.drawLine(10, 8, 90, 8);
097: }
098: }
099: });
100: }
101:
102: /**
103: * Sets the stroke.
104: *
105: * @param stroke the stroke.
106: */
107: public void setStroke(Stroke stroke) {
108: if (stroke instanceof BasicStroke) {
109: this .setData(stroke);
110: } else {
111: throw new RuntimeException(
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 getStroke() {
122: return (BasicStroke) this.getData();
123: }
124: }
|