Statistics
| Revision:

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

History | View | Annotate | Download (9.3 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 3832 caballero
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
53 5735 caballero
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
54 3782 caballero
import com.iver.cit.gvsig.gui.cad.tools.smc.RectangleCADToolContext;
55
import com.iver.cit.gvsig.gui.cad.tools.smc.RectangleCADToolContext.RectangleCADToolState;
56
57
58
/**
59
 * DOCUMENT ME!
60
 *
61
 * @author Vicente Caballero Navarro
62
 */
63 3766 caballero
public class RectangleCADTool extends DefaultCADTool {
64 23993 vcaballero
    protected RectangleCADToolContext _fsm;
65
    protected Point2D firstPoint;
66
    protected Point2D lastPoint;
67 3766 caballero
68
    /**
69
     * Crea un nuevo LineCADTool.
70
     */
71
    public RectangleCADTool() {
72 3883 caballero
73 3766 caballero
    }
74
75
    /**
76
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
77
     * carga previa a la utilizaci?n de la herramienta.
78
     */
79
    public void init() {
80 3883 caballero
            _fsm = new RectangleCADToolContext(this);
81 3766 caballero
    }
82
83
    /* (non-Javadoc)
84 3782 caballero
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
85
     */
86 4313 fjp
    public void transition(double x, double y, InputEvent event) {
87 4324 caballero
        _fsm.addPoint(x, y, event);
88 3766 caballero
    }
89 3782 caballero
90 3766 caballero
    /* (non-Javadoc)
91 3782 caballero
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
92
     */
93 3832 caballero
    public void transition(double d) {
94 5735 caballero
        _fsm.addValue(d);
95 3782 caballero
    }
96 3766 caballero
97 3782 caballero
    /* (non-Javadoc)
98
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
99
     */
100 5735 caballero
    public void transition(String s) throws CommandException {
101 4892 caballero
            if (!super.changeCommand(s)){
102
                    _fsm.addOption(s);
103
            }
104 3782 caballero
    }
105
106 3766 caballero
    /**
107
     * Equivale al transition del prototipo pero sin pasarle como par? metro el
108
     * editableFeatureSource que ya estar? creado.
109
     *
110
     * @param sel Bitset con las geometr?as que est?n seleccionadas.
111
     * @param x par?metro x del punto que se pase en esta transici?n.
112
     * @param y par?metro y del punto que se pase en esta transici?n.
113
     */
114 4365 caballero
    public void addPoint(double x, double y,InputEvent event) {
115 3766 caballero
        RectangleCADToolState actualState = (RectangleCADToolState) _fsm.getPreviousState();
116
117
        String status = actualState.getName();
118 3782 caballero
119 3978 caballero
        if (status.equals("Rectangle.FirstPoint")) {
120 3766 caballero
            firstPoint = new Point2D.Double(x, y);
121 3978 caballero
        } else if (status == "Rectangle.SecondPointOrSquare") {
122 3766 caballero
            lastPoint = new Point2D.Double(x, y);
123
124 4549 caballero
            GeneralPathX elShape = new GeneralPathX();
125 3782 caballero
            elShape.moveTo(firstPoint.getX(), firstPoint.getY());
126
            elShape.lineTo(lastPoint.getX(), firstPoint.getY());
127
            elShape.lineTo(lastPoint.getX(), lastPoint.getY());
128
            elShape.lineTo(firstPoint.getX(), lastPoint.getY());
129 10427 caballero
            //elShape.lineTo(firstPoint.getX(), firstPoint.getY());
130 4549 caballero
            elShape.closePath();
131 12297 caballero
            int type=getCadToolAdapter().getActiveLayerType();
132
            if (type==FShape.POLYGON){
133
                    addGeometry(ShapeFactory.createPolygon2D(elShape));
134
            }else{
135
                    addGeometry(ShapeFactory.createPolyline2D(elShape));
136
            }
137 3782 caballero
            firstPoint = (Point2D) lastPoint.clone();
138 3978 caballero
        } else if (status == "Rectangle.SecondPointSquare") {
139 3782 caballero
            lastPoint = new Point2D.Double(x, y);
140 3766 caballero
141 4549 caballero
            GeneralPathX elShape = new GeneralPathX();
142 3782 caballero
            elShape.moveTo(firstPoint.getX(), firstPoint.getY());
143
            elShape.lineTo(lastPoint.getX(), firstPoint.getY());
144 3766 caballero
145 3782 caballero
            if (((lastPoint.getY() <= firstPoint.getY()) &&
146
                    (lastPoint.getX() <= firstPoint.getX())) ||
147
                    ((lastPoint.getY() > firstPoint.getY()) &&
148
                    (lastPoint.getX() > firstPoint.getX()))) {
149
                elShape.lineTo(lastPoint.getX(),
150
                    firstPoint.getY() + (lastPoint.getX() - firstPoint.getX()));
151
                elShape.lineTo(firstPoint.getX(),
152
                    firstPoint.getY() + (lastPoint.getX() - firstPoint.getX()));
153
            } else {
154
                elShape.lineTo(lastPoint.getX(),
155
                    firstPoint.getY() - (lastPoint.getX() - firstPoint.getX()));
156
                elShape.lineTo(firstPoint.getX(),
157
                    firstPoint.getY() - (lastPoint.getX() - firstPoint.getX()));
158
            }
159 3766 caballero
160 4549 caballero
            //elShape.lineTo(firstPoint.getX(), firstPoint.getY());
161
            elShape.closePath();
162 12297 caballero
            int type=getCadToolAdapter().getActiveLayerType();
163
            if (type==FShape.POLYGON){
164
                    addGeometry(ShapeFactory.createPolygon2D(elShape));
165
            }else{
166
                    addGeometry(ShapeFactory.createPolyline2D(elShape));
167
            }
168 3782 caballero
            firstPoint = (Point2D) lastPoint.clone();
169 3766 caballero
        }
170
    }
171
172
    /**
173
     * M?todo para dibujar la lo necesario para el estado en el que nos
174
     * encontremos.
175
     *
176
     * @param g Graphics sobre el que dibujar.
177
     * @param selectedGeometries BitSet con las geometr?as seleccionadas.
178
     * @param x par?metro x del punto que se pase para dibujar.
179
     * @param y par?metro x del punto que se pase para dibujar.
180
     */
181 3832 caballero
    public void drawOperation(Graphics g, double x,
182 3766 caballero
        double y) {
183
        RectangleCADToolState actualState = _fsm.getState();
184
        String status = actualState.getName();
185
186 3978 caballero
        if (status == "Rectangle.SecondPointOrSquare") {
187 3782 caballero
            GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
188
                    4);
189
            elShape.moveTo(firstPoint.getX(), firstPoint.getY());
190
            elShape.lineTo(x, firstPoint.getY());
191
            elShape.lineTo(x, y);
192
            elShape.lineTo(firstPoint.getX(), y);
193
            elShape.lineTo(firstPoint.getX(), firstPoint.getY());
194 5880 fjp
            ShapeFactory.createPolyline2D(elShape).draw((Graphics2D) g,
195
                getCadToolAdapter().getMapControl().getViewPort(),
196 11437 caballero
                DefaultCADTool.geometrySelectSymbol);
197 3978 caballero
        } else if (status == "Rectangle.SecondPointSquare") {
198 3782 caballero
            GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
199
                    4);
200
            elShape.moveTo(firstPoint.getX(), firstPoint.getY());
201
            elShape.lineTo(x, firstPoint.getY());
202
203
            if (((y <= firstPoint.getY()) && (x <= firstPoint.getX())) ||
204
                    ((y > firstPoint.getY()) && (x > firstPoint.getX()))) {
205
                elShape.lineTo(x, firstPoint.getY() + (x - firstPoint.getX()));
206
                elShape.lineTo(firstPoint.getX(),
207
                    firstPoint.getY() + (x - firstPoint.getX()));
208
                elShape.lineTo(firstPoint.getX(), firstPoint.getY());
209
            } else {
210
                elShape.lineTo(x, firstPoint.getY() - (x - firstPoint.getX()));
211
                elShape.lineTo(firstPoint.getX(),
212
                    firstPoint.getY() - (x - firstPoint.getX()));
213
                elShape.lineTo(firstPoint.getX(), firstPoint.getY());
214
            }
215
216 5880 fjp
            ShapeFactory.createPolyline2D(elShape).draw((Graphics2D) g,
217
                getCadToolAdapter().getMapControl().getViewPort(),
218 11437 caballero
                DefaultCADTool.geometrySelectSymbol);
219 3782 caballero
        }
220 3766 caballero
    }
221
222
    /**
223
     * Add a diferent option.
224
     *
225 3782 caballero
     * @param sel DOCUMENT ME!
226 3766 caballero
     * @param s Diferent option.
227
     */
228 3832 caballero
    public void addOption(String s) {
229 3782 caballero
        RectangleCADToolState actualState = (RectangleCADToolState) _fsm.getPreviousState();
230
        String status = actualState.getName();
231
232 3978 caballero
        if (status == "Rectangle.SecondPointOrSquare") {
233 9121 caballero
            if (s.equalsIgnoreCase(PluginServices.getText(this,"RectangleCADTool.square"))) {
234 3782 caballero
                //Opci?n correcta
235
            }
236
        }
237 3766 caballero
    }
238
239
    /* (non-Javadoc)
240
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
241
     */
242 3832 caballero
    public void addValue(double d) {
243 3766 caballero
    }
244 3883 caballero
245 4118 caballero
        public String getName() {
246 4584 caballero
                return PluginServices.getText(this,"rectangle_");
247 4118 caballero
        }
248
249 4892 caballero
        public String toString() {
250
                return "_rectangle";
251
        }
252 5170 caballero
        public boolean isApplicable(int shapeType) {
253
                switch (shapeType) {
254
                case FShape.POINT:
255 6024 caballero
                case FShape.MULTIPOINT:
256 5170 caballero
                        return false;
257
                }
258
                return true;
259
        }
260 3766 caballero
}