Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extEditing / src / com / iver / cit / gvsig / gui / cad / tools / ExtendCADTool.java @ 24500

History | View | Annotate | Download (10.5 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 java.awt.Graphics;
44
import java.awt.event.InputEvent;
45
import java.awt.geom.PathIterator;
46
import java.awt.geom.Point2D;
47
import java.util.ArrayList;
48
import java.util.Iterator;
49

    
50
import org.gvsig.fmap.dal.exception.DataException;
51
import org.gvsig.fmap.dal.exception.ReadException;
52
import org.gvsig.fmap.dal.feature.EditableFeature;
53
import org.gvsig.fmap.dal.feature.Feature;
54
import org.gvsig.fmap.dal.feature.FeatureSelection;
55
import org.gvsig.fmap.dal.feature.FeatureSet;
56
import org.gvsig.fmap.dal.feature.FeatureStore;
57
import org.gvsig.fmap.geom.Geometry;
58
import org.gvsig.fmap.geom.GeometryFactory;
59
import org.gvsig.fmap.geom.GeometryManager;
60
import org.gvsig.fmap.geom.primitive.GeneralPathX;
61
import org.gvsig.fmap.geom.util.Converter;
62
import org.gvsig.fmap.geom.util.UtilFunctions;
63

    
64
import com.iver.andami.PluginServices;
65
import com.iver.andami.messages.NotificationManager;
66
import com.iver.cit.gvsig.CADExtension;
67
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
68
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
69
import com.iver.cit.gvsig.gui.cad.tools.smc.ExtendCADToolContext;
70
import com.iver.cit.gvsig.gui.cad.tools.smc.ExtendCADToolContext.ExtendCADToolState;
71
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
72

    
73
/**
74
 * DOCUMENT ME!
75
 *
76
 * @author Vicente Caballero Navarro
77
 */
78
public class ExtendCADTool extends DefaultCADTool {
79
        private ExtendCADToolContext _fsm;
80
        private GeometryFactory geomFactory = GeometryManager.getInstance()
81
                        .getGeometryFactory();
82

    
83
        /**
84
         * Crea un nuevo ExtendCADTool.
85
         */
86
        public ExtendCADTool() {
87
        }
88

    
89
        /**
90
         * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
91
         * carga previa a la utilizaci?n de la herramienta.
92
         */
93
        public void init() {
94
                _fsm = new ExtendCADToolContext(this);
95
        }
96

    
97
        /*
98
         * (non-Javadoc)
99
         *
100
         * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
101
         *      double, double)
102
         */
103
        public void transition(double x, double y, InputEvent event) {
104
                _fsm.addPoint(x, y, event);
105
        }
106

    
107
        /*
108
         * (non-Javadoc)
109
         *
110
         * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
111
         *      double)
112
         */
113
        public void transition(double d) {
114
                _fsm.addValue(d);
115
        }
116

    
117
        /*
118
         * (non-Javadoc)
119
         *
120
         * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
121
         *      java.lang.String)
122
         */
123
        public void transition(String s) throws CommandException {
124
                if (!super.changeCommand(s)) {
125
                        _fsm.addOption(s);
126
                }
127
        }
128

    
129
        /**
130
         * DOCUMENT ME!
131
         */
132
        public void selection() {
133
                FeatureSet selection = null;
134
                try {
135
                        selection = (FeatureSet) getVLE().getFeatureStore().getSelection();
136

    
137
                        if (selection.getSize() == 0
138
                                        && !CADExtension
139
                                                        .getCADTool()
140
                                                        .getClass()
141
                                                        .getName()
142
                                                        .equals(
143
                                                                        "com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
144
                                CADExtension.setCADTool("_selection", false);
145
                                ((SelectionCADTool) CADExtension.getCADTool())
146
                                                .setNextTool("_extend");
147
                        }
148
                } catch (ReadException e) {
149
                        // TODO Auto-generated catch block
150
                        e.printStackTrace();
151
                } catch (DataException e) {
152
                        // TODO Auto-generated catch block
153
                        e.printStackTrace();
154
                }
155
        }
156

    
157
        /**
158
         * Equivale al transition del prototipo pero sin pasarle como par?metro el
159
         * editableFeatureSource que ya estar? creado.
160
         *
161
         * @param x
162
         *            par?metro x del punto que se pase en esta transici?n.
163
         * @param y
164
         *            par?metro y del punto que se pase en esta transici?n.
165
         */
166
        public void addPoint(double x, double y, InputEvent event) {
167
                ExtendCADToolState actualState = (ExtendCADToolState) _fsm
168
                                .getPreviousState();
169
                String status = actualState.getName();
170

    
171
                if (status.equals("Extend.SelectGeometryToExtend")) {
172

    
173
                        VectorialLayerEdited vle = getVLE();
174
                        FeatureStore featureStore = null;
175
                        try {
176
                                featureStore = vle.getFeatureStore();
177

    
178
                                featureStore.beginEditingGroup(getName());
179
                                Iterator iterator = null;
180
                                iterator = ((FeatureSelection) featureStore.getSelection())
181
                                                .iterator();
182

    
183
                                // ArrayList selectedRow=getSelectedRows();
184
                                ArrayList selectedRowAux = new ArrayList();
185
                                // for (int i=0;i<selectedRow.size();i++) {
186
                                // selectedRowAux.addAll(selectedRow);
187
                                // }
188
                                // selection();
189
                                vle.selectWithPoint(x, y, false);
190
                                // ArrayList newSelectedRow=new ArrayList();
191
                                while (iterator.hasNext()) {
192
                                        Feature feature = (Feature) iterator.next();
193

    
194
                                        // }
195
                                        // for (int i=0;i<selectedRowAux.size();i++) {
196
                                        // IRowEdited edRow1 = (IRowEdited) selectedRowAux.get(i);
197
                                        // DefaultFeature fea1 = (DefaultFeature)
198
                                        // edRow1.getLinkedRow().cloneRow();
199
                                        Geometry geometry1 = ((Geometry) feature
200
                                                        .getDefaultGeometry()).cloneGeometry();
201

    
202
                                        // IRowEdited edRow2 = (IRowEdited) newSelectedRow.get(i);
203
                                        // DefaultFeature fea2 = (DefaultFeature)
204
                                        // edRow2.getLinkedRow().cloneRow();
205
                                        Geometry geometry2 = ((Geometry) feature
206
                                                        .getDefaultGeometry()).cloneGeometry();
207
                                        // for (int j=0;j<newSelectedRow.size();j++) {
208
                                        // if (geometry1 instanceof FPolygon2D) {
209
                                        EditableFeature eFeature = feature.getEditable();
210
                                        eFeature.setGeometry(featureStore.getDefaultFeatureType()
211
                                                        .getDefaultGeometryAttributeName(),
212
                                                        intersectsGeometry(geometry2, geometry1));
213
                                        // fea2.setGeometry(intersectsGeometry(geometry2,geometry1));
214
                                        // }
215
                                        // }
216
                                        featureStore.update(eFeature);
217
                                        // vea.modifyRow(edRow2.getIndex(),fea2,getName(),EditionEvent.GRAPHIC);
218
                                        clearSelection();
219
                                        // newSelectedRow.add(new
220
                                        // DefaultRowEdited(fea2,IRowEdited.STATUS_MODIFIED,edRow2.getIndex()));
221
                                }
222

    
223
                                featureStore.endEditingGroup();
224
                                // vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS,
225
                                // newSelectedRow);
226
                        } catch (ReadException e) {
227
                                NotificationManager.addError(e.getMessage(), e);
228
                        } catch (DataException e) {
229
                                NotificationManager.addError(e.getMessage(), e);
230
                        }
231
                }
232
        }
233

    
234
        private Geometry intersectsGeometry(Geometry geometry1, Geometry geometry2) {
235
                Point2D p3 = null;
236
                Point2D p4 = null;
237
                Point2D p1 = null;
238
                Point2D p2 = null;
239
                GeneralPathX gpx = new GeneralPathX();
240
                PathIterator theIterator = geometry1.getInternalShape()
241
                                .getPathIterator(null, Converter.FLATNESS);
242
                boolean first = true;
243
                double[] theData = new double[6];
244
                int theType;
245
                while (!theIterator.isDone()) {
246
                        theType = theIterator.currentSegment(theData);
247
                        switch (theType) {
248

    
249
                        case PathIterator.SEG_MOVETO:
250
                                p1 = new Point2D.Double(theData[0], theData[1]);
251
                                gpx.moveTo(p1.getX(), p1.getY());
252
                                break;
253

    
254
                        case PathIterator.SEG_LINETO:
255
                                p2 = new Point2D.Double(theData[0], theData[1]);
256
                                ArrayList lines = getLines(geometry2);
257
                                boolean isLineTo = true;
258
                                for (int i = 0; i < lines.size(); i++) {
259
                                        Point2D[] ps1 = (Point2D[]) lines.get(i);
260
                                        Point2D p = UtilFunctions.getIntersection(ps1[0], ps1[1],
261
                                                        p1, p2);
262
                                        // GeneralPathX gpxAux=new GeneralPathX();
263
                                        // gpxAux.moveTo(p.getX(),p.getY());
264
                                        // gpxAux.lineTo(ps1[0].getX(),ps1[0].getY());
265
                                        // Geometry
266
                                        // gjts1=FConverter.java2d_to_jts((FShape)ShapeFactory.createPolyline2D(gpxAux).getInternalShape());
267
                                        // Geometry
268
                                        // gjts2=FConverter.java2d_to_jts((FShape)geometry2.getInternalShape());
269
                                        // GeometryCollection
270
                                        // result=(GeometryCollection)gjts1.intersection(gjts2);
271
                                        // Point point=(Point)result.getGeometryN(1);
272
                                        // p=new Point2D.Double(point.getX(),point.getY());
273
                                        if (p != null && first) {
274
                                                gpx.lineTo(p.getX(), p.getY());
275
                                                first = false;
276
                                                isLineTo = false;
277
                                                break;
278
                                        } else {
279
                                                // gpx.lineTo(p2.getX(),p2.getY());
280
                                        }
281
                                }
282
                                if (!first && isLineTo)
283
                                        gpx.lineTo(p2.getX(), p2.getY());
284

    
285
                                break;
286

    
287
                        } // end switch
288

    
289
                        theIterator.next();
290
                } // end while loop
291
                return geomFactory.createPolyline2D(gpx);
292
        }
293

    
294
        private ArrayList getLines(Geometry geometry1) {
295
                Point2D p1 = null;
296
                Point2D p2 = null;
297
                ArrayList lines = new ArrayList();
298
                PathIterator theIterator = geometry1.getInternalShape()
299
                                .getPathIterator(null, Converter.FLATNESS);
300
                double[] theData = new double[6];
301
                int theType;
302
                while (!theIterator.isDone()) {
303
                        theType = theIterator.currentSegment(theData);
304
                        switch (theType) {
305
                        case PathIterator.SEG_MOVETO:
306
                                p1 = new Point2D.Double(theData[0], theData[1]);
307
                                break;
308

    
309
                        case PathIterator.SEG_LINETO:
310
                                p2 = new Point2D.Double(theData[0], theData[1]);
311
                                lines.add(new Point2D[] { p1, p2 });
312
                                break;
313

    
314
                        } // end switch
315

    
316
                        theIterator.next();
317
                } // end while loop
318
                return lines;
319
        }
320

    
321
        /**
322
         * M?todo para dibujar la lo necesario para el estado en el que nos
323
         * encontremos.
324
         *
325
         * @param g
326
         *            Graphics sobre el que dibujar.
327
         * @param x
328
         *            par?metro x del punto que se pase para dibujar.
329
         * @param y
330
         *            par?metro x del punto que se pase para dibujar.
331
         */
332
        public void drawOperation(Graphics g, double x, double y) {
333
        }
334

    
335
        /**
336
         * Add a diferent option.
337
         *
338
         * @param s
339
         *            Diferent option.
340
         */
341
        public void addOption(String s) {
342
        }
343

    
344
        /*
345
         * (non-Javadoc)
346
         *
347
         * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
348
         */
349
        public void addValue(double d) {
350
        }
351

    
352
        public String getName() {
353
                return PluginServices.getText(this, "extend_");
354
        }
355

    
356
        public String toString() {
357
                return "_extend";
358
        }
359

    
360
        public boolean isApplicable(int shapeType) {
361
                switch (shapeType) {
362
                case Geometry.TYPES.CURVE:
363
                case Geometry.TYPES.GEOMETRY:
364
                        return true;
365
                }
366
                return false;
367
        }
368

    
369
}