Statistics
| Revision:

root / branches / gvSIG_CAD / applications / appgvSIG / src / com / iver / cit / gvsig / gui / cad / tools / RectangleCadTool.java @ 3513

History | View | Annotate | Download (11.2 KB)

1
/* 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
package com.iver.cit.gvsig.gui.cad.tools;
42

    
43
import com.iver.cit.gvsig.fmap.core.FGeometryCollection;
44
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
45
import com.iver.cit.gvsig.fmap.core.IGeometry;
46
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
47
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
48
import com.iver.cit.gvsig.fmap.edition.EditableFeatureSource;
49
import com.iver.cit.gvsig.fmap.edition.cad.Status;
50
import com.iver.cit.gvsig.fmap.layers.FBitSet;
51
import com.iver.cit.gvsig.gui.cad.CadTool;
52
import com.iver.cit.gvsig.gui.cad.automaton.Rectangulo;
53

    
54
import com.iver.fsac.Automaton;
55

    
56
import java.awt.Graphics;
57
import java.awt.Graphics2D;
58
import java.awt.geom.Point2D;
59

    
60
import java.io.IOException;
61

    
62
import java.util.ArrayList;
63

    
64

    
65
/**
66
 * Herramienta para a?adir una geometr?a rectangulo o cuadrado.
67
 *
68
 * @author Vicente Caballero Navarro
69
 */
70
public class RectangleCadTool extends AbstractCadTool {
71
        private static Status[] STATUS = {
72
                        new Status("Precise primer punto de esquina(x,y):"),
73
                        new Status("Precise esquina opuesta(x,y) o tipo Cuadrado(C)"),
74
                        new Status("Precise esquina opuesta(x,y):")
75
                };
76
        private Rectangulo rectangleStatus = new Rectangulo();
77
        private Point2D firstPoint;
78
        private Point2D lastPoint;
79
        private ArrayList list = new ArrayList();
80

    
81
        /**
82
         * @see com.iver.cit.gvsig.gui.cad.CadTool#transition(java.lang.String,
83
         *                 com.iver.cit.gvsig.fmap.edition.EditableFeatureSource,
84
         *                 com.iver.cit.gvsig.fmap.layers.FBitSet, double[])
85
         */
86
        public int transition(String text, EditableFeatureSource editingSource,
87
                FBitSet selectedGeometries, double[] values) {
88
                int ret = rectangleStatus.transition(text);
89

    
90
                int status = rectangleStatus.getStatus();
91

    
92
                if (status == 0) {
93
                        //firstPoint=new FPoint2D(values[0], values[1]);
94
                } else if (status == 1) {
95
                        if (values.length != 0) {
96
                                firstPoint = new Point2D.Double(values[0], values[1]);
97
                        }
98

    
99
                        System.out.println("1");
100
                } else if (status == 2) {
101
                        System.out.println("2");
102
                } else if (status == 3) {
103
                        System.out.println("3");
104

    
105
                        if (values.length != 0) {
106
                                lastPoint = new Point2D.Double(values[0], values[1]);
107
                        }
108

    
109
                        /*GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
110
                           4);
111
                           elShape.moveTo(firstPoint.getX(), firstPoint.getY());
112
                           elShape.lineTo(lastPoint.getX(), firstPoint.getY());
113
                           elShape.lineTo(lastPoint.getX(), lastPoint.getY());
114
                           elShape.lineTo(firstPoint.getX(), lastPoint.getY());
115
                           elShape.lineTo(firstPoint.getX(), firstPoint.getY());
116
                           editingSource.addGeometry(ShapeFactory.createPolyline2D(
117
                                           elShape));
118
                         */
119
                        GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
120
                                        2);
121
                        elShape.moveTo(firstPoint.getX(), firstPoint.getY());
122
                        elShape.lineTo(lastPoint.getX(), firstPoint.getY());
123
                        list.add(ShapeFactory.createPolyline2D(elShape));
124

    
125
                        elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2);
126
                        elShape.moveTo(lastPoint.getX(), firstPoint.getY());
127
                        elShape.lineTo(lastPoint.getX(), lastPoint.getY());
128
                        list.add(ShapeFactory.createPolyline2D(elShape));
129

    
130
                        elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2);
131
                        elShape.moveTo(lastPoint.getX(), lastPoint.getY());
132
                        elShape.lineTo(firstPoint.getX(), lastPoint.getY());
133
                        list.add(ShapeFactory.createPolyline2D(elShape));
134

    
135
                        elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2);
136
                        elShape.moveTo(firstPoint.getX(), lastPoint.getY());
137
                        elShape.lineTo(firstPoint.getX(), firstPoint.getY());
138
                        list.add(ShapeFactory.createPolyline2D(elShape));
139

    
140
                        IGeometry[] geoms = (IGeometry[]) list.toArray(new IGeometry[0]);
141

    
142
                        FGeometryCollection fgc = new FGeometryCollection(geoms);
143

    
144
                        try {
145
                                editingSource.addGeometry(fgc);
146
                        } catch (DriverIOException e1) {
147
                                e1.printStackTrace();
148
                        } catch (IOException e1) {
149
                                e1.printStackTrace();
150
                        }
151
                        list.clear();
152
                        ret = ret | rectangleStatus.transition("cancel");
153
                } else if (status == 4) {
154
                        System.out.println("4");
155

    
156
                        if (values.length != 0) {
157
                                lastPoint = new Point2D.Double(values[0], values[1]);
158
                        }
159

    
160
                        /*
161
                           try {
162
                                   GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
163
                                                   4);
164
                                   elShape.moveTo(firstPoint.getX(), firstPoint.getY());
165
                                   elShape.lineTo(lastPoint.getX(), firstPoint.getY());
166
                                   elShape.lineTo(lastPoint.getX(),firstPoint.getY()-(lastPoint.getX()-firstPoint.getX()));
167
                                   elShape.lineTo(firstPoint.getX(), firstPoint.getY()-(lastPoint.getX()-firstPoint.getX()));
168
                                   elShape.lineTo(firstPoint.getX(), firstPoint.getY());
169
                                   editingSource.addGeometry(ShapeFactory.createPolyline2D(
170
                                                   elShape));
171
                           } catch (DriverIOException e) {
172
                                   e.printStackTrace();
173
                           } catch (IOException e) {
174
                                   e.printStackTrace();
175
                           }
176
                         */
177
                        GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
178
                                        2);
179
                        elShape.moveTo(firstPoint.getX(), firstPoint.getY());
180
                        elShape.lineTo(lastPoint.getX(), firstPoint.getY());
181
                        list.add(ShapeFactory.createPolyline2D(elShape));
182

    
183
                        elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2);
184
                        elShape.moveTo(lastPoint.getX(), firstPoint.getY());
185
                        if ((lastPoint.getY()<=firstPoint.getY() && lastPoint.getX()<=firstPoint.getX())||(lastPoint.getY()>firstPoint.getY() && lastPoint.getX()>firstPoint.getX())){
186
                                elShape.lineTo(lastPoint.getX(), firstPoint.getY() + (lastPoint.getX() - firstPoint.getX()));
187
                                list.add(ShapeFactory.createPolyline2D(elShape));
188

    
189
                                elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2);
190
                                elShape.moveTo(lastPoint.getX(),
191
                                                firstPoint.getY() + (lastPoint.getX() - firstPoint.getX()));
192
                                elShape.lineTo(firstPoint.getX(), firstPoint.getY() + (lastPoint.getX() - firstPoint.getX()));
193
                                list.add(ShapeFactory.createPolyline2D(elShape));
194

    
195
                                elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2);
196
                                elShape.moveTo(firstPoint.getX(),
197
                                                firstPoint.getY() + (lastPoint.getX() - firstPoint.getX()));
198
                                
199
                        }else {
200
                                elShape.lineTo(lastPoint.getX(), firstPoint.getY() - (lastPoint.getX() - firstPoint.getX()));
201
                                list.add(ShapeFactory.createPolyline2D(elShape));
202

    
203
                                elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2);
204
                                elShape.moveTo(lastPoint.getX(),
205
                                                firstPoint.getY() - (lastPoint.getX() - firstPoint.getX()));
206
                                elShape.lineTo(firstPoint.getX(), firstPoint.getY() - (lastPoint.getX() - firstPoint.getX()));
207
                                list.add(ShapeFactory.createPolyline2D(elShape));
208

    
209
                                elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2);
210
                                elShape.moveTo(firstPoint.getX(),
211
                                                firstPoint.getY() - (lastPoint.getX() - firstPoint.getX()));
212
                        }
213
                        
214
                        elShape.lineTo(firstPoint.getX(), firstPoint.getY());
215
                        list.add(ShapeFactory.createPolyline2D(elShape));
216
                        
217
                /*        
218
                        
219
                        elShape.lineTo(lastPoint.getX(),
220
                                firstPoint.getY() - (lastPoint.getX() - firstPoint.getX()));
221
                        list.add(ShapeFactory.createPolyline2D(elShape));
222

223
                        elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2);
224
                        elShape.moveTo(lastPoint.getX(),
225
                                firstPoint.getY() - (lastPoint.getX() - firstPoint.getX()));
226
                        elShape.lineTo(firstPoint.getX(),
227
                                firstPoint.getY() - (lastPoint.getX() - firstPoint.getX()));
228
                        list.add(ShapeFactory.createPolyline2D(elShape));
229

230
                        elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2);
231
                        elShape.moveTo(firstPoint.getX(),
232
                                firstPoint.getY() - (lastPoint.getX() - firstPoint.getX()));
233
                        elShape.lineTo(firstPoint.getX(), firstPoint.getY());
234
                        list.add(ShapeFactory.createPolyline2D(elShape));
235
*/
236
                        IGeometry[] geoms = (IGeometry[]) list.toArray(new IGeometry[0]);
237

    
238
                        FGeometryCollection fgc = new FGeometryCollection(geoms);
239

    
240
                        try {
241
                                editingSource.addGeometry(fgc);
242
                        } catch (DriverIOException e1) {
243
                                e1.printStackTrace();
244
                        } catch (IOException e1) {
245
                                e1.printStackTrace();
246
                        }
247

    
248
                        list.clear();
249
                        ret = ret | rectangleStatus.transition("cancel");
250
                }
251

    
252
                return ret;
253
        }
254

    
255
        /**
256
         * @see com.iver.cit.gvsig.gui.cad.CadTool#drawOperation(java.awt.Graphics,
257
         *                 com.iver.cit.gvsig.fmap.edition.EditableFeatureSource,
258
         *                 com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
259
         */
260
        public void drawOperation(Graphics g, EditableFeatureSource efs,
261
                FBitSet selectedGeometries, double x, double y) {
262
                int status = rectangleStatus.getStatus();
263

    
264
                if (status == 1) {
265
                        GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
266
                                        4);
267
                        elShape.moveTo(firstPoint.getX(), firstPoint.getY());
268
                        elShape.lineTo(x, firstPoint.getY());
269
                        elShape.lineTo(x, y);
270
                        elShape.lineTo(firstPoint.getX(), y);
271
                        elShape.lineTo(firstPoint.getX(), firstPoint.getY());
272
                        ShapeFactory.createPolyline2D(elShape).draw((Graphics2D) g,
273
                                getCadToolAdapter().getMapControl().getViewPort(),
274
                                CadTool.modifySymbol);
275
                } else if (status == 2) {
276
                        GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
277
                                        4);
278
                        elShape.moveTo(firstPoint.getX(), firstPoint.getY());
279
                        elShape.lineTo(x, firstPoint.getY());
280
                        if ((y<=firstPoint.getY() && x<=firstPoint.getX())||(y>firstPoint.getY() && x>firstPoint.getX())){
281
                                elShape.lineTo(x, firstPoint.getY() + (x - firstPoint.getX()));
282
                                elShape.lineTo(firstPoint.getX(), firstPoint.getY() + (x - firstPoint.getX()));
283
                                elShape.lineTo(firstPoint.getX(), firstPoint.getY());
284
                        }else {
285
                                elShape.lineTo(x, firstPoint.getY() - (x - firstPoint.getX()));
286
                                elShape.lineTo(firstPoint.getX(), firstPoint.getY() - (x - firstPoint.getX()));
287
                                elShape.lineTo(firstPoint.getX(), firstPoint.getY());
288
                        }
289
                        ShapeFactory.createPolyline2D(elShape).draw((Graphics2D) g,
290
                                getCadToolAdapter().getMapControl().getViewPort(),
291
                                CadTool.modifySymbol);
292
                }
293
        }
294

    
295
        /**
296
         * @see com.iver.cit.gvsig.gui.cad.CadTool#getQuestion()
297
         */
298
        public String getQuestion() {
299
                return STATUS[rectangleStatus.getStatus()].getQuestion();
300
        }
301

    
302
        /**
303
         * @see com.iver.cit.gvsig.gui.cad.CadTool#initializeStatus()
304
         */
305
        public void initializeStatus() {
306
                rectangleStatus.initialize();
307
        }
308

    
309
        /**
310
         * @see com.iver.cit.gvsig.gui.cad.CadTool#getAutomaton()
311
         */
312
        public Automaton getAutomaton() {
313
                return rectangleStatus;
314
        }
315

    
316
        /**
317
         * @see com.iver.cit.gvsig.gui.cad.CadTool#getName()
318
         */
319
        public String getName() {
320
                return "RECT?NGULO";
321
        }
322
}