001: /* uDig - User Friendly Desktop Internet GIS client
002: * http://udig.refractions.net
003: * (C) 2004, Refractions Research Inc.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation;
008: * version 2.1 of the License.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: */
015: package net.refractions.udig.tools.edit.commands;
016:
017: import java.awt.Color;
018: import java.awt.Rectangle;
019: import java.util.Iterator;
020:
021: import net.refractions.udig.project.ui.commands.AbstractDrawCommand;
022: import net.refractions.udig.project.ui.render.displayAdapter.MapMouseEvent;
023: import net.refractions.udig.project.ui.render.displayAdapter.MapMouseMotionListener;
024: import net.refractions.udig.project.ui.render.displayAdapter.ViewportPane;
025: import net.refractions.udig.tools.edit.EditPlugin;
026: import net.refractions.udig.tools.edit.EditToolHandler;
027: import net.refractions.udig.tools.edit.preferences.PreferenceConstants;
028: import net.refractions.udig.tools.edit.support.EditGeomPointIterator;
029: import net.refractions.udig.tools.edit.support.Point;
030: import net.refractions.udig.tools.edit.support.PrimitiveShape;
031: import net.refractions.udig.tools.edit.support.ShapeType;
032: import net.refractions.udig.ui.graphics.ViewportGraphics;
033:
034: import org.eclipse.core.runtime.IProgressMonitor;
035: import org.eclipse.jface.preference.IPreferenceStore;
036:
037: /**
038: * Draws each vertex point as a rectangle. Listens to mouse events as long as valid and fills vertext when
039: * cursor is over a vertex.
040: *
041: * @author jones
042: * @since 1.1.0
043: */
044: public class DrawPointCommand extends AbstractDrawCommand implements
045: MapMouseMotionListener {
046:
047: private PrimitiveShape shape;
048: private int radius = 3;
049: private Color outline = Color.BLACK;
050: private Color fill = new Color(255, 168, 168);
051: private ViewportPane pane;
052: private Point location;
053: private int radiusDelta = 1;
054: private Color selectionFill = Color.YELLOW;
055: private boolean overPoint;
056: private EditToolHandler handler;
057: private boolean drawCurrentShape;
058: IPreferenceStore store = EditPlugin.getDefault()
059: .getPreferenceStore();
060:
061: public DrawPointCommand(EditToolHandler handler2,
062: PrimitiveShape shape, ViewportPane pane) {
063: this .shape = shape;
064: this .pane = pane;
065: pane.addMouseMotionListener(this );
066: this .handler = handler2;
067: }
068:
069: public void run(IProgressMonitor monitor) throws Exception {
070: graphics.setStroke(ViewportGraphics.LINE_SOLID, 1);
071: if (drawCurrentShape) {
072: shape = handler.getCurrentShape();
073: }
074: if (shape == null || shape.getNumPoints() == 0)
075: return;
076: Rectangle rect = new Rectangle();
077: rect.width = radius * 2;
078: rect.height = radius * 2;
079: boolean selected;
080: for (Iterator<Point> iter = new EditGeomPointIterator(shape
081: .getEditGeom()); iter.hasNext();) {
082: Point p = iter.next();
083: if ((p.getX() < 0 || p.getX() > display.getWidth())
084: && (p.getY() < 0 || p.getY() > display.getHeight()))
085: continue;
086: rect.x = p.getX() - radius;
087: rect.y = p.getY() - radius;
088:
089: setZoomedRect(rect, p, 0);
090: if (shape.getEditBlackboard().getSelection().contains(p)) {
091: graphics.setColor(selectionFill);
092: selected = true;
093: } else {
094: graphics.setColor(fill);
095: selected = false;
096: }
097: if (selected
098: || store
099: .getBoolean(PreferenceConstants.P_FILL_VERTICES)
100: || shape.getEditGeom().getShapeType() == ShapeType.POINT)
101: graphics.fillRect(rect.x, rect.y, rect.width,
102: rect.height);
103:
104: graphics.setColor(outline);
105: graphics.drawRect(rect.x, rect.y, rect.width, rect.height);
106: }
107: if (overPoint) {
108: Point firstPoint = shape.getPoint(0);
109: rect.x = firstPoint.getX() - radius;
110: rect.y = firstPoint.getY() - radius;
111:
112: setZoomedRect(rect, firstPoint, radiusDelta);
113: if (shape.getEditBlackboard().getSelection().contains(
114: firstPoint))
115: graphics.setColor(selectionFill);
116: else
117: graphics.setColor(fill);
118: graphics.fillRect(rect.x, rect.y, rect.width, rect.height);
119:
120: graphics.setColor(outline);
121: graphics.drawRect(rect.x, rect.y, rect.width, rect.height);
122: }
123:
124: }
125:
126: public void setDrawCurrentShape(boolean b) {
127: drawCurrentShape = b;
128: }
129:
130: /**
131: * Sets the size of the point box.
132: *
133: * @param rect rectangle to set
134: * @param p center point
135: * @param widthDelta the number of pixels larger the box should be than the radius.
136: * (radius of box is size of box from center to edge) delta is number of pixel from edge of normal
137: * rect to new edge. So 1 pixel delta would make rectangle 2 pixels larger.
138: */
139: private void setZoomedRect(Rectangle rect, Point p, int widthRad) {
140: rect.width = (widthRad * 2) + radius * 2;
141: rect.height = rect.width;
142: int i = radius + widthRad;
143: rect.x = p.getX() - i;
144: rect.y = p.getY() - i;
145: }
146:
147: private boolean isOverPoint(Point p) {
148: for (int x = -radiusDelta - radius, max = radius + radiusDelta; x < max; x++) {
149: for (int y = -radiusDelta - radius; y < max; y++) {
150: if (Point.valueOf(p.getX() + x, p.getY() + y).equals(
151: location))
152: return true;
153: }
154: }
155: return false;
156: }
157:
158: @Override
159: public void setValid(boolean valid) {
160: if (!valid) {
161: pane.removeMouseMotionListener(this );
162: }
163: super .setValid(valid);
164: }
165:
166: public void mouseMoved(MapMouseEvent event) {
167: boolean oldIsOver = overPoint;
168: this .location = Point.valueOf(event.x, event.y);
169:
170: if (shape == null || shape.getNumPoints() == 0)
171: return;
172:
173: Point firstPoint = shape.getPoint(0);
174: overPoint = isOverPoint(firstPoint);
175: if (overPoint != oldIsOver)
176: handler.repaint();
177: }
178:
179: public void mouseDragged(MapMouseEvent event) {
180: mouseMoved(event);
181: }
182:
183: public void setCurrentShape(PrimitiveShape shape) {
184: if (shape == this .shape)
185: return;
186: this .shape = shape;
187: handler.repaint();
188: }
189:
190: /**
191: *
192: * @return Returns the radiusDelta.
193: */
194: public int getRadiusDelta() {
195: return radiusDelta;
196: }
197:
198: /**
199: * Default is 1.
200: * @param width_rad The radiusDelta to set.
201: */
202: public void setRadiusDelta(int radiusDelta) {
203: this .radiusDelta = radiusDelta;
204: }
205:
206: /**
207: * @return Returns the fill.
208: */
209: public Color getFill() {
210: return fill;
211: }
212:
213: /**
214: * Default is Color( 255,168,168,188 )
215: * @param fill The fill to set.
216: */
217: public void setFill(Color fill) {
218: this .fill = fill;
219: if (fill.getAlpha() != 255) {
220: this .fill = new Color(fill.getRGB());
221: }
222: }
223:
224: /**
225: * @return Returns the outline.
226: */
227: public Color getOutline() {
228: return outline;
229: }
230:
231: /**
232: * Default is Black
233: * @param outline The outline to set.
234: */
235: public void setOutline(Color outline) {
236: this .outline = outline;
237: }
238:
239: /**
240: * @return Returns the radius.
241: */
242: public int getRadius() {
243: return radius;
244: }
245:
246: /**
247: * Default is 3.
248: * @param radius The radius to set.
249: */
250: public void setRadius(int radius) {
251: this .radius = radius - radiusDelta;
252: }
253:
254: /**
255: * @return Returns the geom.
256: */
257: public PrimitiveShape getGeom() {
258: return shape;
259: }
260:
261: /**
262: * @return Returns the selectionFill.
263: */
264: public Color getSelectionFill() {
265: return selectionFill;
266: }
267:
268: /**
269: * @param selectionFill The selectionFill to set.
270: */
271: public void setSelectionFill(Color selectionFill) {
272: this .selectionFill = selectionFill;
273: }
274:
275: public Rectangle getValidArea() {
276: if (shape != null)
277: return shape.getBounds();
278: else
279: return null;
280: }
281:
282: public void mouseHovered(MapMouseEvent event) {
283: }
284:
285: }
|