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.Rectangle;
018:
019: import net.refractions.udig.core.IProvider;
020: import net.refractions.udig.project.ui.commands.AbstractDrawCommand;
021: import net.refractions.udig.project.ui.commands.IDrawCommand;
022: import net.refractions.udig.tools.edit.MouseTracker;
023: import net.refractions.udig.tools.edit.preferences.PreferenceUtil;
024: import net.refractions.udig.tools.edit.support.Point;
025: import net.refractions.udig.tools.edit.support.PrimitiveShape;
026: import net.refractions.udig.tools.edit.support.EditUtils.MinFinder;
027:
028: import org.eclipse.core.runtime.IProgressMonitor;
029:
030: /**
031: * Draws the two end points of the Shape provided by the Provider<PrimitiveShape>
032: *
033: * @author jones
034: * @since 1.1.0
035: */
036: public class DrawEndPointsCommand extends AbstractDrawCommand implements
037: IDrawCommand {
038:
039: private IProvider<PrimitiveShape> provider;
040: private MouseTracker tracker;
041: boolean showMouseOver = true;
042:
043: /**
044: * @param IBlockingProvider
045: */
046: public DrawEndPointsCommand(MouseTracker tracker,
047: IProvider<PrimitiveShape> provider) {
048: this .provider = provider;
049: this .tracker = tracker;
050: }
051:
052: public void run(IProgressMonitor monitor) throws Exception {
053: PrimitiveShape shape = provider.get();
054: if (shape == null || shape.getNumPoints() == 0)
055: return;
056: Point start = shape.getPoint(0);
057: Point end = shape.getPoint(shape.getNumPoints() - 1);
058: int radius = PreferenceUtil.instance().getVertexRadius();
059:
060: if (start == null || end == null)
061: return;
062:
063: if (showMouseOver && tracker.getCurrentPoint() != null) {
064: MinFinder finder = new MinFinder(tracker.getCurrentPoint());
065: graphics.setColor(PreferenceUtil.instance()
066: .getDrawVertexFillColor());
067: if (start != null && end != null
068: && finder.dist(start) < radius) {
069: graphics.fill(new Rectangle(start.getX() - radius,
070: start.getY() - radius, radius * 2, radius * 2));
071: graphics.fill(new Rectangle(end.getX() - radius, end
072: .getY()
073: - radius, radius * 2, radius * 2));
074: }
075: if (start != null && end != null
076: && finder.dist(end) < radius) {
077: graphics.fill(new Rectangle(start.getX() - radius,
078: start.getY() - radius, radius * 2, radius * 2));
079: graphics.fill(new Rectangle(end.getX() - radius, end
080: .getY()
081: - radius, radius * 2, radius * 2));
082: }
083: }
084:
085: graphics.setColor(PreferenceUtil.instance()
086: .getDrawVertexLineColor());
087: graphics.draw(new Rectangle(start.getX() - radius, start.getY()
088: - radius, radius * 2, radius * 2));
089: graphics.draw(new Rectangle(end.getX() - radius, end.getY()
090: - radius, radius * 2, radius * 2));
091:
092: }
093:
094: /**
095: * @return Returns the IBlockingProvider.
096: */
097: public IProvider<PrimitiveShape> getProvider() {
098: return this .provider;
099: }
100:
101: /**
102: * @param IBlockingProvider The IBlockingProvider to set.
103: */
104: public void setProvider(IProvider<PrimitiveShape> provider) {
105: this .provider = provider;
106: }
107:
108: /**
109: * Returns true if the vertex should be filled when the mouse is over it.
110: * @return Returns true if the vertex should be filled when the mouse is over it.
111: */
112: public boolean isShowMouseOver() {
113: return this .showMouseOver;
114: }
115:
116: /**
117: * @param showMouseOver true if vertex should be filled when mouse is over.
118: */
119: public void setShowMouseOver(boolean showMouseOver) {
120: this .showMouseOver = showMouseOver;
121: }
122:
123: public Rectangle getValidArea() {
124: if (provider == null)
125: return null;
126: PrimitiveShape obj = provider.get();
127: if (obj == null)
128: return null;
129: return provider.get().getBounds();
130: }
131: }
|