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.awt.Shape;
020: import java.awt.geom.AffineTransform;
021: import java.util.List;
022:
023: import net.refractions.udig.project.ui.commands.AbstractDrawCommand;
024: import net.refractions.udig.tools.edit.EditPlugin;
025: import net.refractions.udig.tools.edit.EditToolHandler;
026: import net.refractions.udig.tools.edit.preferences.PreferenceConstants;
027: import net.refractions.udig.tools.edit.support.CurrentEditGeomPathIterator;
028: import net.refractions.udig.tools.edit.support.EditGeom;
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.core.runtime.Platform;
036: import org.eclipse.jface.preference.IPreferenceStore;
037: import org.eclipse.swt.graphics.Path;
038: import org.eclipse.swt.widgets.Display;
039:
040: /**
041: * Draws all the {@link net.refractions.udig.tools.edit.support.EditGeom}s on the provided
042: * {@link net.refractions.udig.tools.edit.support.EditBlackboard}
043: *
044: * @author jones
045: * @since 1.1.0
046: */
047: public class DrawEditGeomsCommand extends AbstractDrawCommand {
048:
049: Color line = Color.RED;
050: Color fill = new Color(255, 0, 0, 100);
051: int lineWidth = 2;
052: private Point location;
053: private PrimitiveShape currentShape;
054: private EditToolHandler handler;
055: IPreferenceStore store = EditPlugin.getDefault()
056: .getPreferenceStore();
057:
058: public DrawEditGeomsCommand(EditToolHandler handler) {
059: this .handler = handler;
060: }
061:
062: public void run(IProgressMonitor monitor) throws Exception {
063: List<EditGeom> geoms = handler.getEditBlackboard(
064: handler.getEditLayer()).getGeoms();
065: for (EditGeom geom : geoms) {
066: graphics.setColor(line);
067: if (geom.getShell().getNumPoints() > 0) {
068: CurrentEditGeomPathIterator pathIterator = CurrentEditGeomPathIterator
069: .getPathIterator(geom);
070: if (currentShape != null
071: && currentShape.getEditGeom() == geom)
072: pathIterator.setLocation(location, currentShape);
073: else {
074: pathIterator.setLocation(null, null);
075: }
076: if (true || Platform.getOS() == Platform.OS_LINUX)
077: fillShape(geom, pathIterator);
078: else
079: fillPath(geom, pathIterator);
080: }
081: }
082: }
083:
084: private void fillPath(EditGeom geom,
085: CurrentEditGeomPathIterator pathIterator) {
086: Path shape = pathIterator.toPath(Display.getCurrent());
087: try {
088: graphics.drawPath(shape);
089: if (store.getBoolean(PreferenceConstants.P_FILL_POLYGONS)
090: && geom.getShapeType() == ShapeType.POLYGON) {
091: graphics.setColor(fill);
092: graphics.fillPath(shape);
093: }
094: } finally {
095: shape.dispose();
096: }
097: }
098:
099: private void fillShape(EditGeom geom,
100: CurrentEditGeomPathIterator pathIterator) {
101: Shape shape = pathIterator.toShape();
102: graphics.draw(shape);
103: if (store.getBoolean(PreferenceConstants.P_FILL_POLYGONS)
104: && geom.getShapeType() == ShapeType.POLYGON) {
105: graphics.setStroke(ViewportGraphics.LINE_SOLID, lineWidth);
106: graphics.setColor(fill);
107: graphics.fill(shape);
108: }
109: }
110:
111: @Override
112: public void setValid(boolean valid) {
113: super .setValid(valid);
114: }
115:
116: /**
117: * @return Returns the fill.
118: */
119: public Color getFill() {
120: return fill;
121: }
122:
123: /**
124: * @param fill The fill to set.
125: */
126: public void setFill(Color fill) {
127: this .fill = fill;
128: }
129:
130: /**
131: * @return Returns the line.
132: */
133: public Color getLine() {
134: return line;
135: }
136:
137: /**
138: * @param line The line to set.
139: */
140: public void setLine(Color line) {
141: this .line = line;
142: }
143:
144: /**
145: * @return Returns the currentShape.
146: */
147: public PrimitiveShape getCurrentShape() {
148: return currentShape;
149: }
150:
151: /**
152: * @param l the current location of the mouse
153: * @param currentShape The sub-shape of the geometry that is being editted.
154: * @return true if the new setting requires a repaint();
155: */
156: public boolean setCurrentLocation(Point l,
157: PrimitiveShape currentShape) {
158: boolean result = false;
159: if (currentShape != this .currentShape
160: || (l != null && !l.equals(location)))
161: result = true;
162: location = l;
163: this .currentShape = currentShape;
164: return result;
165: }
166:
167: public Rectangle getValidArea() {
168: return null;
169: }
170:
171: }
|