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.geom.GeneralPath;
023: import java.awt.geom.PathIterator;
024:
025: import net.refractions.udig.project.command.MapCommand;
026: import net.refractions.udig.project.ui.commands.AbstractDrawCommand;
027: import net.refractions.udig.ui.graphics.SWTGraphics;
028: import net.refractions.udig.ui.graphics.ViewportGraphics;
029:
030: import org.eclipse.core.runtime.IProgressMonitor;
031: import org.eclipse.swt.graphics.Device;
032: import org.eclipse.swt.graphics.Path;
033:
034: /**
035: * Draws the outline of a shape on the Acetate layer.
036: *
037: * @author jeichar
038: * @since 0.3
039: */
040: public class DrawPathCommand extends AbstractDrawCommand {
041:
042: Path path = null;
043: Color paint = null;
044: private int style = -1;
045: private int width;
046: private Color fill;
047: private boolean closeShape;
048: private int closex1;
049: private int closex2;
050: private int closey1;
051: private int closey2;
052:
053: /**
054: * Creates a new instance of DrawShapeCommand
055: */
056: public DrawPathCommand() {
057: this ((Path) null, Color.BLACK, ViewportGraphics.LINE_SOLID, 1);
058: }
059:
060: /**
061: * Creates a new instance of DrawShapeCommand
062: *
063: * @param path The path to draw
064: * @param color The paint to draw the shape with.
065: * @param lineStyle the line style to use for the shape outline
066: * @param lineWidth the line width in pixels.
067: */
068: public DrawPathCommand(Path path, Color color, int lineStyle,
069: int lineWidth) {
070: this .path = path;
071: this .paint = color;
072: this .style = lineStyle;
073: this .width = lineWidth;
074: }
075:
076: /**
077: * Creates a new instance of DrawShapeCommand
078: *
079: * @param path The path to draw
080: * @param color The paint to draw the shape with.
081: * @param lineStyle the line style to use for the shape outline
082: * @param lineWidth the line width in pixels.
083: */
084: public DrawPathCommand(Device device, PathIterator path,
085: Color color, int lineStyle, int lineWidth) {
086: this .paint = color;
087: this .style = lineStyle;
088: this .width = lineWidth;
089: this .path = SWTGraphics.createPath(path, device);
090: }
091:
092: /**
093: * @see MapCommand#run()
094: */
095: public void run(IProgressMonitor monitor) {
096: if (path == null)
097: return;
098: fill();
099:
100: draw();
101:
102: close();
103: }
104:
105: private void draw() {
106: if (paint != null || fill == null) {
107: if (paint != null)
108: graphics.setColor(paint);
109: if (style > -1)
110: graphics.setStroke(style, width);
111: if (path != null)
112: graphics.drawPath(path);
113: }
114:
115: }
116:
117: private void close() {
118: if (closeShape)
119: graphics.drawLine(closex1, closey1, closex2, closey2);
120: }
121:
122: private void fill() {
123: if (fill != null) {
124: graphics.setColor(fill);
125: if (path != null)
126: graphics.fillPath(path);
127: }
128: }
129:
130: /**
131: * @return Returns the paint.
132: */
133: public Paint getPaint() {
134: return paint;
135: }
136:
137: /**
138: * @param paint The paint to set.
139: */
140: public void setPaint(Color paint) {
141: this .paint = paint;
142: }
143:
144: /**
145: * @param path The new path.
146: */
147: public void setPath(Path path) {
148: this .path = path;
149: }
150:
151: /**
152: * @param path The new path.
153: */
154: public void setPath(Device device, PathIterator path) {
155: this .path = SWTGraphics.createPath(path, device);
156:
157: }
158:
159: /**
160: * @return Returns the line style.
161: */
162: public int getLineStyle() {
163: return style;
164: }
165:
166: /**
167: * @return Returns the line width.
168: */
169: public int getLineWidth() {
170: return width;
171: }
172:
173: /**
174: * Sets the line style
175: *
176: * @param lineStyle the style of the line
177: * @param lineWidth the width of the line
178: */
179: public void setStroke(int lineStyle, int lineWidth) {
180: this .style = lineStyle;
181: this .width = lineWidth;
182: }
183:
184: /**
185: * Sets the color that the shape will be filled with.
186: * If fill is null then no fill will be applied.
187: *
188: * @param fillColor a color to be used to fill the shapeor null.
189: */
190: public void setFill(Color fillColor) {
191: this .fill = fillColor;
192: }
193:
194: public Rectangle getValidArea() {
195: if (path != null) {
196: float[] bounds = new float[4];
197: path.getBounds(bounds);
198: return new Rectangle((int) bounds[0], (int) bounds[1],
199: (int) bounds[2], (int) bounds[3]);
200: }
201: return null;
202: }
203:
204: /**
205: * Disposes of the Path object if it is not null and sets this command as invalid.
206: *
207: */
208: public void dispose() {
209: if (path != null) {
210: path.dispose();
211: path = null;
212: }
213: super .setValid(false);
214: }
215:
216: @Override
217: public void setValid(boolean valid) {
218: if (!valid) {
219: dispose();
220: } else {
221: super .setValid(true);
222: }
223: }
224:
225: /**
226: * if this is called then a line is also drawn from (x1,y1) to (x2,y2). This would be used to make the path appear to be closed. Actually closing
227: * the path is problematic because it cannot be undone. So this is a method to simulate it. Obviously if the path is not going to need to be re-opened
228: * then this method does not need to be called.
229: *
230: */
231: public void line(int x1, int y1, int x2, int y2) {
232: closeShape = true;
233: this.closex1 = x1;
234: this.closex2 = x2;
235: this.closey1 = y1;
236: this.closey2 = y2;
237: }
238: }
|