Statistics
| Revision:

svn-gvsig-desktop / branches / gvSIG_CAD_Layout_version / applications / appgvSIG / src / com / iver / cit / gvsig / gui / cad / tools / RectangleCadTool.java @ 1729

History | View | Annotate | Download (8.65 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"),
73
                        new Status("Precise esquina opuesta o tipo Cuadrado(C)"),
74
                        new Status("Precise esquina opuesta")
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
                                        2);
111
                        elShape.moveTo(firstPoint.getX(), firstPoint.getY());
112
                        elShape.lineTo(lastPoint.getX(), firstPoint.getY());
113
                        list.add(ShapeFactory.createPolyline2D(elShape));
114

    
115
                        elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2);
116
                        elShape.moveTo(lastPoint.getX(), firstPoint.getY());
117
                        elShape.lineTo(lastPoint.getX(), lastPoint.getY());
118
                        list.add(ShapeFactory.createPolyline2D(elShape));
119

    
120
                        elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2);
121
                        elShape.moveTo(lastPoint.getX(), lastPoint.getY());
122
                        elShape.lineTo(firstPoint.getX(), lastPoint.getY());
123
                        list.add(ShapeFactory.createPolyline2D(elShape));
124

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

    
130
                        IGeometry[] geoms = (IGeometry[]) list.toArray(new IGeometry[0]);
131

    
132
                        FGeometryCollection fgc = new FGeometryCollection(geoms);
133

    
134
                        try {
135
                                editingSource.addGeometry(fgc);
136
                        } catch (DriverIOException e1) {
137
                                e1.printStackTrace();
138
                        } catch (IOException e1) {
139
                                e1.printStackTrace();
140
                        }
141
                        list.clear();
142
                        ret = ret | rectangleStatus.transition("cancel");
143
                } else if (status == 4) {
144
                        System.out.println("4");
145

    
146
                        if (values.length != 0) {
147
                                lastPoint = new Point2D.Double(values[0], values[1]);
148
                        }
149

    
150
                        /*
151
                           try {
152
                                   GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
153
                                                   4);
154
                                   elShape.moveTo(firstPoint.getX(), firstPoint.getY());
155
                                   elShape.lineTo(lastPoint.getX(), firstPoint.getY());
156
                                   elShape.lineTo(lastPoint.getX(),firstPoint.getY()-(lastPoint.getX()-firstPoint.getX()));
157
                                   elShape.lineTo(firstPoint.getX(), firstPoint.getY()-(lastPoint.getX()-firstPoint.getX()));
158
                                   elShape.lineTo(firstPoint.getX(), firstPoint.getY());
159
                                   editingSource.addGeometry(ShapeFactory.createPolyline2D(
160
                                                   elShape));
161
                           } catch (DriverIOException e) {
162
                                   e.printStackTrace();
163
                           } catch (IOException e) {
164
                                   e.printStackTrace();
165
                           }
166
                         */
167
                        GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
168
                                        2);
169
                        elShape.moveTo(firstPoint.getX(), firstPoint.getY());
170
                        elShape.lineTo(lastPoint.getX(), firstPoint.getY());
171
                        list.add(ShapeFactory.createPolyline2D(elShape));
172

    
173
                        elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2);
174
                        elShape.moveTo(lastPoint.getX(), firstPoint.getY());
175
                        elShape.lineTo(lastPoint.getX(),
176
                                firstPoint.getY() - (lastPoint.getX() - firstPoint.getX()));
177
                        list.add(ShapeFactory.createPolyline2D(elShape));
178

    
179
                        elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2);
180
                        elShape.moveTo(lastPoint.getX(),
181
                                firstPoint.getY() - (lastPoint.getX() - firstPoint.getX()));
182
                        elShape.lineTo(firstPoint.getX(),
183
                                firstPoint.getY() - (lastPoint.getX() - firstPoint.getX()));
184
                        list.add(ShapeFactory.createPolyline2D(elShape));
185

    
186
                        elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 2);
187
                        elShape.moveTo(firstPoint.getX(),
188
                                firstPoint.getY() - (lastPoint.getX() - firstPoint.getX()));
189
                        elShape.lineTo(firstPoint.getX(), firstPoint.getY());
190
                        list.add(ShapeFactory.createPolyline2D(elShape));
191

    
192
                        IGeometry[] geoms = (IGeometry[]) list.toArray(new IGeometry[0]);
193

    
194
                        FGeometryCollection fgc = new FGeometryCollection(geoms);
195

    
196
                        try {
197
                                editingSource.addGeometry(fgc);
198
                        } catch (DriverIOException e1) {
199
                                e1.printStackTrace();
200
                        } catch (IOException e1) {
201
                                e1.printStackTrace();
202
                        }
203

    
204
                        list.clear();
205
                        ret = ret | rectangleStatus.transition("cancel");
206
                }
207

    
208
                return ret;
209
        }
210

    
211
        /**
212
         * @see com.iver.cit.gvsig.gui.cad.CadTool#drawOperation(java.awt.Graphics,
213
         *                 com.iver.cit.gvsig.fmap.edition.EditableFeatureSource,
214
         *                 com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
215
         */
216
        public void drawOperation(Graphics g, EditableFeatureSource efs,
217
                FBitSet selectedGeometries, double x, double y) {
218
                int status = rectangleStatus.getStatus();
219

    
220
                if (status == 1) {
221
                        GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
222
                                        4);
223
                        elShape.moveTo(firstPoint.getX(), firstPoint.getY());
224
                        elShape.lineTo(x, firstPoint.getY());
225
                        elShape.lineTo(x, y);
226
                        elShape.lineTo(firstPoint.getX(), y);
227
                        elShape.lineTo(firstPoint.getX(), firstPoint.getY());
228
                        ShapeFactory.createPolyline2D(elShape).draw((Graphics2D) g,
229
                                getAT(),
230
                                CadTool.modifySymbol);
231
                } else if (status == 2) {
232
                        GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
233
                                        4);
234
                        elShape.moveTo(firstPoint.getX(), firstPoint.getY());
235
                        elShape.lineTo(x, firstPoint.getY());
236
                        elShape.lineTo(x, firstPoint.getY() - (x - firstPoint.getX()));
237
                        elShape.lineTo(firstPoint.getX(),
238
                                firstPoint.getY() - (x - firstPoint.getX()));
239
                        elShape.lineTo(firstPoint.getX(), firstPoint.getY());
240
                        ShapeFactory.createPolyline2D(elShape).draw((Graphics2D) g,
241
                                        getAT(),
242
                                        CadTool.modifySymbol);
243
                }
244
        }
245

    
246
        /**
247
         * @see com.iver.cit.gvsig.gui.cad.CadTool#getQuestion()
248
         */
249
        public String getQuestion() {
250
                return STATUS[rectangleStatus.getStatus()].getQuestion();
251
        }
252

    
253
        /**
254
         * @see com.iver.cit.gvsig.gui.cad.CadTool#initializeStatus()
255
         */
256
        public void initializeStatus() {
257
                rectangleStatus.initialize();
258
        }
259

    
260
        /**
261
         * @see com.iver.cit.gvsig.gui.cad.CadTool#getAutomaton()
262
         */
263
        public Automaton getAutomaton() {
264
                return rectangleStatus;
265
        }
266
}