Statistics
| Revision:

root / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / RectangleCADTool.java @ 9243

History | View | Annotate | Download (8.91 KB)

1 3766 caballero
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41 3782 caballero
package com.iver.cit.gvsig.gui.cad.tools;
42
43 3832 caballero
import java.awt.Graphics;
44
import java.awt.Graphics2D;
45 4313 fjp
import java.awt.event.InputEvent;
46 3832 caballero
import java.awt.geom.Point2D;
47
48 4584 caballero
import com.iver.andami.PluginServices;
49 5170 caballero
import com.iver.cit.gvsig.fmap.core.FShape;
50 3782 caballero
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
51
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
52
import com.iver.cit.gvsig.gui.cad.CADTool;
53 3832 caballero
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
54 5735 caballero
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
55 3782 caballero
import com.iver.cit.gvsig.gui.cad.tools.smc.RectangleCADToolContext;
56
import com.iver.cit.gvsig.gui.cad.tools.smc.RectangleCADToolContext.RectangleCADToolState;
57
58
59
/**
60
 * DOCUMENT ME!
61
 *
62
 * @author Vicente Caballero Navarro
63
 */
64 3766 caballero
public class RectangleCADTool extends DefaultCADTool {
65
    private RectangleCADToolContext _fsm;
66
    private Point2D firstPoint;
67
    private Point2D lastPoint;
68
69
    /**
70
     * Crea un nuevo LineCADTool.
71
     */
72
    public RectangleCADTool() {
73 3883 caballero
74 3766 caballero
    }
75
76
    /**
77
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
78
     * carga previa a la utilizaci?n de la herramienta.
79
     */
80
    public void init() {
81 3883 caballero
            _fsm = new RectangleCADToolContext(this);
82 3766 caballero
    }
83
84
    /* (non-Javadoc)
85 3782 caballero
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
86
     */
87 4313 fjp
    public void transition(double x, double y, InputEvent event) {
88 4324 caballero
        _fsm.addPoint(x, y, event);
89 3766 caballero
    }
90 3782 caballero
91 3766 caballero
    /* (non-Javadoc)
92 3782 caballero
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
93
     */
94 3832 caballero
    public void transition(double d) {
95 5735 caballero
        _fsm.addValue(d);
96 3782 caballero
    }
97 3766 caballero
98 3782 caballero
    /* (non-Javadoc)
99
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
100
     */
101 5735 caballero
    public void transition(String s) throws CommandException {
102 4892 caballero
            if (!super.changeCommand(s)){
103
                    _fsm.addOption(s);
104
            }
105 3782 caballero
    }
106
107 3766 caballero
    /**
108
     * Equivale al transition del prototipo pero sin pasarle como par? metro el
109
     * editableFeatureSource que ya estar? creado.
110
     *
111
     * @param sel Bitset con las geometr?as que est?n seleccionadas.
112
     * @param x par?metro x del punto que se pase en esta transici?n.
113
     * @param y par?metro y del punto que se pase en esta transici?n.
114
     */
115 4365 caballero
    public void addPoint(double x, double y,InputEvent event) {
116 3766 caballero
        RectangleCADToolState actualState = (RectangleCADToolState) _fsm.getPreviousState();
117
118
        String status = actualState.getName();
119 3782 caballero
120 3978 caballero
        if (status.equals("Rectangle.FirstPoint")) {
121 3766 caballero
            firstPoint = new Point2D.Double(x, y);
122 3978 caballero
        } else if (status == "Rectangle.SecondPointOrSquare") {
123 3766 caballero
            lastPoint = new Point2D.Double(x, y);
124
125 4549 caballero
            GeneralPathX elShape = new GeneralPathX();
126 3782 caballero
            elShape.moveTo(firstPoint.getX(), firstPoint.getY());
127
            elShape.lineTo(lastPoint.getX(), firstPoint.getY());
128
            elShape.lineTo(lastPoint.getX(), lastPoint.getY());
129
            elShape.lineTo(firstPoint.getX(), lastPoint.getY());
130 5908 caballero
            elShape.lineTo(firstPoint.getX(), firstPoint.getY());
131 4549 caballero
            elShape.closePath();
132 5880 fjp
            addGeometry(ShapeFactory.createPolyline2D(elShape));
133 3782 caballero
            firstPoint = (Point2D) lastPoint.clone();
134 3978 caballero
        } else if (status == "Rectangle.SecondPointSquare") {
135 3782 caballero
            lastPoint = new Point2D.Double(x, y);
136 3766 caballero
137 4549 caballero
            GeneralPathX elShape = new GeneralPathX();
138 3782 caballero
            elShape.moveTo(firstPoint.getX(), firstPoint.getY());
139
            elShape.lineTo(lastPoint.getX(), firstPoint.getY());
140 3766 caballero
141 3782 caballero
            if (((lastPoint.getY() <= firstPoint.getY()) &&
142
                    (lastPoint.getX() <= firstPoint.getX())) ||
143
                    ((lastPoint.getY() > firstPoint.getY()) &&
144
                    (lastPoint.getX() > firstPoint.getX()))) {
145
                elShape.lineTo(lastPoint.getX(),
146
                    firstPoint.getY() + (lastPoint.getX() - firstPoint.getX()));
147
                elShape.lineTo(firstPoint.getX(),
148
                    firstPoint.getY() + (lastPoint.getX() - firstPoint.getX()));
149
            } else {
150
                elShape.lineTo(lastPoint.getX(),
151
                    firstPoint.getY() - (lastPoint.getX() - firstPoint.getX()));
152
                elShape.lineTo(firstPoint.getX(),
153
                    firstPoint.getY() - (lastPoint.getX() - firstPoint.getX()));
154
            }
155 3766 caballero
156 4549 caballero
            //elShape.lineTo(firstPoint.getX(), firstPoint.getY());
157
            elShape.closePath();
158 5880 fjp
            addGeometry(ShapeFactory.createPolyline2D(elShape));
159 3782 caballero
            firstPoint = (Point2D) lastPoint.clone();
160 3766 caballero
        }
161
    }
162
163
    /**
164
     * M?todo para dibujar la lo necesario para el estado en el que nos
165
     * encontremos.
166
     *
167
     * @param g Graphics sobre el que dibujar.
168
     * @param selectedGeometries BitSet con las geometr?as seleccionadas.
169
     * @param x par?metro x del punto que se pase para dibujar.
170
     * @param y par?metro x del punto que se pase para dibujar.
171
     */
172 3832 caballero
    public void drawOperation(Graphics g, double x,
173 3766 caballero
        double y) {
174
        RectangleCADToolState actualState = _fsm.getState();
175
        String status = actualState.getName();
176
177 3978 caballero
        if (status == "Rectangle.SecondPointOrSquare") {
178 3782 caballero
            GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
179
                    4);
180
            elShape.moveTo(firstPoint.getX(), firstPoint.getY());
181
            elShape.lineTo(x, firstPoint.getY());
182
            elShape.lineTo(x, y);
183
            elShape.lineTo(firstPoint.getX(), y);
184
            elShape.lineTo(firstPoint.getX(), firstPoint.getY());
185 5880 fjp
            ShapeFactory.createPolyline2D(elShape).draw((Graphics2D) g,
186
                getCadToolAdapter().getMapControl().getViewPort(),
187
                CADTool.drawingSymbol);
188 3978 caballero
        } else if (status == "Rectangle.SecondPointSquare") {
189 3782 caballero
            GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
190
                    4);
191
            elShape.moveTo(firstPoint.getX(), firstPoint.getY());
192
            elShape.lineTo(x, firstPoint.getY());
193
194
            if (((y <= firstPoint.getY()) && (x <= firstPoint.getX())) ||
195
                    ((y > firstPoint.getY()) && (x > firstPoint.getX()))) {
196
                elShape.lineTo(x, firstPoint.getY() + (x - firstPoint.getX()));
197
                elShape.lineTo(firstPoint.getX(),
198
                    firstPoint.getY() + (x - firstPoint.getX()));
199
                elShape.lineTo(firstPoint.getX(), firstPoint.getY());
200
            } else {
201
                elShape.lineTo(x, firstPoint.getY() - (x - firstPoint.getX()));
202
                elShape.lineTo(firstPoint.getX(),
203
                    firstPoint.getY() - (x - firstPoint.getX()));
204
                elShape.lineTo(firstPoint.getX(), firstPoint.getY());
205
            }
206
207 5880 fjp
            ShapeFactory.createPolyline2D(elShape).draw((Graphics2D) g,
208
                getCadToolAdapter().getMapControl().getViewPort(),
209
                CADTool.drawingSymbol);
210 3782 caballero
        }
211 3766 caballero
    }
212
213
    /**
214
     * Add a diferent option.
215
     *
216 3782 caballero
     * @param sel DOCUMENT ME!
217 3766 caballero
     * @param s Diferent option.
218
     */
219 3832 caballero
    public void addOption(String s) {
220 3782 caballero
        RectangleCADToolState actualState = (RectangleCADToolState) _fsm.getPreviousState();
221
        String status = actualState.getName();
222
223 3978 caballero
        if (status == "Rectangle.SecondPointOrSquare") {
224 9121 caballero
            if (s.equalsIgnoreCase(PluginServices.getText(this,"RectangleCADTool.square"))) {
225 3782 caballero
                //Opci?n correcta
226
            }
227
        }
228 3766 caballero
    }
229
230
    /* (non-Javadoc)
231
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
232
     */
233 3832 caballero
    public void addValue(double d) {
234 3766 caballero
    }
235 3883 caballero
236 4118 caballero
        public String getName() {
237 4584 caballero
                return PluginServices.getText(this,"rectangle_");
238 4118 caballero
        }
239
240 4892 caballero
        public String toString() {
241
                return "_rectangle";
242
        }
243 5170 caballero
        public boolean isApplicable(int shapeType) {
244
                switch (shapeType) {
245
                case FShape.POINT:
246 6024 caballero
                case FShape.MULTIPOINT:
247 5170 caballero
                        return false;
248
                }
249
                return true;
250
        }
251 3766 caballero
}