001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, Refractions Research Inc.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package net.refractions.udig.project.ui.internal.commands.draw;
018:
019: import java.awt.Color;
020: import java.awt.Paint;
021: import java.awt.Rectangle;
022: import java.awt.Shape;
023: import java.awt.geom.AffineTransform;
024: import java.awt.geom.Ellipse2D;
025: import java.awt.geom.Rectangle2D;
026:
027: import net.refractions.udig.project.command.MapCommand;
028: import net.refractions.udig.project.ui.commands.AbstractDrawCommand;
029: import net.refractions.udig.ui.graphics.SWTGraphics;
030: import net.refractions.udig.ui.graphics.ViewportGraphics;
031:
032: import org.eclipse.core.runtime.IProgressMonitor;
033: import org.eclipse.swt.graphics.Path;
034: import org.eclipse.swt.widgets.Display;
035:
036: /**
037: * Draws the outline of a shape on the Acetate layer.
038: *
039: * @author jeichar
040: * @since 0.3
041: */
042: public class DrawShapeCommand extends AbstractDrawCommand {
043:
044: private Path path = null;
045: private Shape shape = null;
046: private Color paint = null;
047: private int style = -1;
048: private int width;
049: private Color fill;
050:
051: /**
052: * Creates a new instance of DrawShapeCommand
053: */
054: public DrawShapeCommand() {
055: this (null, Color.BLACK, ViewportGraphics.LINE_SOLID, 1);
056: }
057:
058: /**
059: * Creates a new instance of DrawShapeCommand
060: *
061: * @param shape The shape to draw
062: * @param color The paint to draw the shape with.
063: * @param lineStyle the line style to use for the shape outline
064: * @param lineWidth the line width in pixels.
065: */
066: public DrawShapeCommand(Shape shape, Color color, int lineStyle,
067: int lineWidth) {
068: setShape(shape);
069: this .paint = color;
070: this .style = lineStyle;
071: this .width = lineWidth;
072:
073: }
074:
075: /**
076: * @see MapCommand#run()
077: */
078: public void run(IProgressMonitor monitor) {
079: if (shape == null)
080: return;
081: if (fill != null) {
082: graphics.setColor(fill);
083: graphics.fill(shape);
084: }
085:
086: if (paint != null)
087: graphics.setColor(paint);
088: if (style > -1)
089: graphics.setStroke(style, width);
090: doDraw();
091: }
092:
093: private void doDraw() {
094: if (shape instanceof Rectangle2D) {
095: Rectangle2D rect = (Rectangle2D) shape;
096: graphics.drawRect((int) rect.getX(), (int) rect.getY(),
097: (int) rect.getWidth(), (int) rect.getHeight());
098: return;
099: }
100:
101: if (shape instanceof Ellipse2D) {
102: Ellipse2D ellipse = (Ellipse2D) shape;
103: graphics.drawOval((int) ellipse.getMinX(), (int) ellipse
104: .getMinY(), (int) ellipse.getWidth(), (int) ellipse
105: .getHeight());
106: return;
107: }
108:
109: if (path == null || path.isDisposed())
110: path = SWTGraphics.createPath(shape
111: .getPathIterator(new AffineTransform()), Display
112: .getCurrent());
113: graphics.drawPath(path);
114: }
115:
116: /**
117: * @return Returns the paint.
118: */
119: public Paint getPaint() {
120: return paint;
121: }
122:
123: /**
124: * @param paint The paint to set.
125: */
126: public void setPaint(Color paint) {
127: this .paint = paint;
128: }
129:
130: /**
131: * @return Returns the shape but it is a copy so changing won't affect actual rendering.
132: */
133: public Shape getShape() {
134: return shape;
135: }
136:
137: /**
138: * @param shape The shape to set.
139: */
140: public void setShape(Shape shape) {
141: this .shape = shape;
142: path = null;
143: }
144:
145: /**
146: * @return Returns the line style.
147: */
148: public int getLineStyle() {
149: return style;
150: }
151:
152: /**
153: * @return Returns the line width.
154: */
155: public int getLineWidth() {
156: return width;
157: }
158:
159: /**
160: * Sets the line style
161: *
162: * @param lineStyle the style of the line
163: * @param lineWidth the width of the line
164: */
165: public void setStroke(int lineStyle, int lineWidth) {
166: this .style = lineStyle;
167: this .width = lineWidth;
168: }
169:
170: /**
171: * Sets the color that the shape will be filled with.
172: * If fill is null then no fill will be applied.
173: *
174: * @param fillColor a color to be used to fill the shapeor null.
175: */
176: public void setFill(Color fillColor) {
177: this .fill = fillColor;
178: }
179:
180: public Rectangle getValidArea() {
181: if (shape == null)
182: return null;
183: return shape.getBounds();
184: }
185:
186: @Override
187: public void setValid(boolean valid) {
188: super .setValid(valid);
189: }
190:
191: @Override
192: public void dispose() {
193: if (path != null && !path.isDisposed())
194: path.dispose();
195: }
196: }
|