Statistics
| Revision:

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

History | View | Annotate | Download (13 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.util.ArrayList;
47

    
48
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
49
import com.iver.andami.PluginServices;
50
import com.iver.cit.gvsig.CADExtension;
51
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
52
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
53
import com.iver.cit.gvsig.fmap.core.FGeometryCollection;
54
import com.iver.cit.gvsig.fmap.core.FShape;
55
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
56
import com.iver.cit.gvsig.fmap.core.IGeometry;
57
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
58
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
59
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
60
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
61
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
62
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
63
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
64
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
65
import com.iver.cit.gvsig.gui.cad.tools.smc.ExploitCADToolContext;
66
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
67

    
68

    
69
/**
70
 * Elimina la geometr?a compuesta y a?ade todas la geometr?as simples que la
71
 * componen, implementado para polil?neas y pol?gonos.
72
 *
73
 * @author Vicente Caballero Navarro
74
 */
75
public class ExploitCADTool extends DefaultCADTool {
76
    private ExploitCADToolContext _fsm;
77

    
78
    /**
79
     * Crea un nuevo ExploitCADTool.
80
     */
81
    public ExploitCADTool() {
82
    }
83

    
84
    /**
85
     * M?todo de inicio, para poner el c?digo de todo lo que se requiera de una
86
     * carga previa a la utilizaci?n de la herramienta.
87
     */
88
    public void init() {
89
        _fsm = new ExploitCADToolContext(this);
90
    }
91

    
92
    /* (non-Javadoc)
93
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
94
     */
95
    public void transition(double x, double y, InputEvent event) {
96
        _fsm.addPoint(x, y, event);
97
    }
98

    
99
    /* (non-Javadoc)
100
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
101
     */
102
    public void transition(double d) {
103
        _fsm.addValue(d);
104
    }
105

    
106
    /* (non-Javadoc)
107
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
108
     */
109
    public void transition(String s) throws CommandException {
110
            if (!super.changeCommand(s)){
111
                    _fsm.addOption(s);
112
            }
113
    }
114

    
115
    /**
116
     * DOCUMENT ME!
117
     */
118
    public void selection() {
119
       ArrayList selectedRows=getSelectedRows();
120
        if (selectedRows.size() == 0 && !CADExtension.getCADTool().getClass().getName().equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
121
            CADExtension.setCADTool("_selection",false);
122
            ((SelectionCADTool) CADExtension.getCADTool()).setNextTool(
123
                "_exploit");
124
        }
125
    }
126

    
127
    /**
128
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
129
     * editableFeatureSource que ya estar? creado.
130
     *
131
     * @param x par?metro x del punto que se pase en esta transici?n.
132
     * @param y par?metro y del punto que se pase en esta transici?n.
133
     */
134
    public void addPoint(double x, double y,InputEvent event) {
135
    }
136

    
137
    /**
138
     * M?todo para dibujar la lo necesario para el estado en el que nos
139
     * encontremos.
140
     *
141
     * @param g Graphics sobre el que dibujar.
142
     * @param x par?metro x del punto que se pase para dibujar.
143
     * @param y par?metro x del punto que se pase para dibujar.
144
     */
145
    public void drawOperation(Graphics g, double x, double y) {
146
    }
147
    public void exploit() {
148
                ArrayList selectedRow = getSelectedRows();
149
                ArrayList selectedRowAux = new ArrayList();
150
                VectorialLayerEdited vle = getVLE();
151
                VectorialEditableAdapter vea = vle.getVEA();
152
                try {
153
                        vea.startComplexRow();
154
                        for (int i = 0; i < selectedRow.size(); i++) {
155
                                DefaultRowEdited dre = (DefaultRowEdited) selectedRow.get(i);
156
                                DefaultFeature df = (DefaultFeature) dre.getLinkedRow()
157
                                                .cloneRow();
158
                                IGeometry geom=df.getGeometry();
159
                                if (geom instanceof FGeometryCollection) {
160
                                        exploitGeometryCollection(dre,selectedRowAux);
161
                                }else {
162
                                        if (geom.getGeometryType()==FShape.LINE) {
163
                                                exploitLine(dre,selectedRowAux);
164
                                        }else if (geom.getGeometryType()==FShape.POLYGON) {
165
                                                exploitPolygon(dre,selectedRowAux);
166
                                        }
167
                                }
168
                                vea.removeRow(dre.getIndex(), getName(), EditionEvent.GRAPHIC);
169
                        }
170
                        vea.endComplexRow(getName());
171
                        vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS, selectedRowAux);
172
                        //clearSelection();
173
                        //selectedRow.addAll(selectedRowAux);
174
                } catch (ReadDriverException e) {
175
                        // TODO Auto-generated catch block
176
                        e.printStackTrace();
177
                } catch (ExpansionFileReadException e) {
178
                        // TODO Auto-generated catch block
179
                        e.printStackTrace();
180
                }
181
        }
182
    private void exploitGeometryCollection(DefaultRowEdited dre,ArrayList selectedRowAux) {
183
            DefaultFeature df = (DefaultFeature) dre.getLinkedRow()
184
                        .cloneRow();
185
            IGeometry geom=df.getGeometry();
186
                FGeometryCollection gc=(FGeometryCollection)geom;
187
                IGeometry[] geoms=gc.getGeometries();
188
                for (int i=0;i<geoms.length;i++) {
189
                        DefaultFeature dfAux=(DefaultFeature)df.cloneRow();
190
                        int index = addGeometry(geoms[i], dfAux.getAttributes());
191
                        selectedRowAux.add(new DefaultRowEdited(dfAux,
192
                                        IRowEdited.STATUS_ADDED, index));
193
                }
194
                getCadToolAdapter().getMapControl().drawMap(false);
195
        }
196
    private void exploitLine(DefaultRowEdited dre,ArrayList selectedRowAux){
197
            GeneralPathX newGp1 = new GeneralPathX();
198
            DefaultFeature df = (DefaultFeature) dre.getLinkedRow()
199
                        .cloneRow();
200
//            Point2D firstPoint=null;
201
//            Point2D lastPoint=null;
202
            PathIterator theIterator = df.getGeometry().getPathIterator(null,FConverter.FLATNESS);
203
                double[] theData = new double[6];
204
                int theType;
205
                int numParts = 0;
206
                while (!theIterator.isDone()) {
207
                        theType = theIterator.currentSegment(theData);
208
                        switch (theType) {
209

    
210
                        case PathIterator.SEG_MOVETO:
211
                                numParts++;
212
                                newGp1 = new GeneralPathX();
213
//                                firstPoint=new Point2D.Double(theData[0], theData[1]);
214
                                newGp1.moveTo(theData[0], theData[1]);
215
//                                lastPoint=new Point2D.Double(theData[0], theData[1]);
216
                                break;
217

    
218
                        case PathIterator.SEG_LINETO:
219
                                newGp1.lineTo(theData[0], theData[1]);
220
                                IGeometry geomLine = ShapeFactory
221
                                                .createPolyline2D(newGp1);
222
                                DefaultFeature dfLine = (DefaultFeature) df.cloneRow();
223
                                dfLine.setGeometry(geomLine);
224
                                int indexLine = addGeometry(geomLine, dfLine
225
                                                .getAttributes());
226
                                selectedRowAux.add(new DefaultRowEdited(dfLine,
227
                                                IRowEdited.STATUS_ADDED, indexLine));
228
                                newGp1 = new GeneralPathX();
229
                                newGp1.moveTo(theData[0], theData[1]);
230
//                                lastPoint=new Point2D.Double(theData[0], theData[1]);
231
                                break;
232

    
233
                /*        case PathIterator.SEG_QUADTO:
234
                                Point2D current=newGp1.getCurrentPoint();
235
                                newGp1.quadTo(theData[0], theData[1], theData[2],
236
                                                theData[3]);
237
                                IGeometry geomQuad = ShapeFactory
238
                                                .createArc(current,new Point2D.Double(theData[0], theData[1]),new Point2D.Double(theData[2], theData[3]));
239
                                DefaultFeature dfQuad = (DefaultFeature) df.cloneRow();
240
                                dfQuad.setGeometry(geomQuad);
241
                                int indexQuad = addGeometry(geomQuad, dfQuad
242
                                                .getAttributes());
243
                                selectedRowAux.add(new DefaultRowEdited(dfQuad,
244
                                                IRowEdited.STATUS_ADDED, indexQuad));
245
                                newGp1 = new GeneralPathX();
246
                                newGp1.moveTo(theData[0], theData[1]);
247
//                                lastPoint=new Point2D.Double(theData[0], theData[1]);
248
                                break;
249
                        case PathIterator.SEG_CUBICTO:
250
                                newGp1.curveTo(theData[0], theData[1], theData[2],
251
                                                theData[3], theData[4], theData[5]);
252
                                IGeometry geomCurve = ShapeFactory
253
                                                .createArc(new Point2D.Double(theData[0], theData[1]),new Point2D.Double(theData[2], theData[3]),new Point2D.Double(theData[4], theData[5]));
254
                                DefaultFeature dfCurve = (DefaultFeature) df.cloneRow();
255
                                dfCurve.setGeometry(geomCurve);
256
                                int indexCurve = addGeometry(geomCurve, dfCurve
257
                                                .getAttributes());
258
                                selectedRowAux.add(new DefaultRowEdited(dfCurve,
259
                                                IRowEdited.STATUS_ADDED, indexCurve));
260
                                newGp1 = new GeneralPathX();
261
                                newGp1.moveTo(theData[0], theData[1]);
262
//                                lastPoint=new Point2D.Double(theData[0], theData[1]);
263
                                break;
264
                        case PathIterator.SEG_CLOSE:
265
//                                if (firstPoint!=null) {
266
//                                        newGp1=new GeneralPathX();
267
//                                        newGp1.moveTo(firstPoint.getX(),firstPoint.getY());
268
//                                        newGp1.lineTo(lastPoint.getX(),lastPoint.getY());
269
//                                        IGeometry geomClose = ShapeFactory
270
//                                                        .createPolyline2D(newGp1);
271
//                                        DefaultFeature dfClose = (DefaultFeature) df.cloneRow();
272
//                                        dfClose.setGeometry(geomClose);
273
//                                        int indexClose = addGeometry(geomClose, dfClose
274
//                                                        .getAttributes());
275
//                                        selectedRowAux.add(new DefaultRowEdited(dfClose,
276
//                                                        IRowEdited.STATUS_ADDED, indexClose));
277
//                                        newGp1 = new GeneralPathX();
278
//                                        newGp1.moveTo(theData[0], theData[1]);
279
//                                }
280
                                // if (isFirstPart)
281
                                // newGp1.closePath();
282
                                // else
283
                                // newGp2.closePath();
284
                                break;
285
                                */
286
                        } // end switch
287

    
288
                        theIterator.next();
289
                } // end while loop
290
                getCadToolAdapter().getMapControl().drawMap(false);
291
    }
292
    private void exploitPolygon(DefaultRowEdited dre,ArrayList selectedRowAux) {
293
            GeneralPathX newGp1 = null;
294
            DefaultFeature df = (DefaultFeature) dre.getLinkedRow()
295
                        .cloneRow();
296
            PathIterator theIterator = df.getGeometry().getPathIterator(null,FConverter.FLATNESS);
297
                double[] theData = new double[6];
298
                int theType;
299
                int numParts = 0;
300
                while (!theIterator.isDone()) {
301
                        theType = theIterator.currentSegment(theData);
302
                        switch (theType) {
303

    
304
                        case PathIterator.SEG_MOVETO:
305
                                numParts++;
306
                                if (newGp1!=null) {
307
                                        IGeometry geomLine = ShapeFactory
308
                                                .createPolygon2D(newGp1);
309
                                        DefaultFeature dfLine = (DefaultFeature) df.cloneRow();
310
                                        dfLine.setGeometry(geomLine);
311
                                        int indexLine = addGeometry(geomLine, dfLine
312
                                                        .getAttributes());
313
                                        selectedRowAux.add(new DefaultRowEdited(dfLine,
314
                                        IRowEdited.STATUS_ADDED, indexLine));
315
                                }
316
                                newGp1 = new GeneralPathX();
317
                                newGp1.moveTo(theData[0], theData[1]);
318
                                break;
319

    
320
                        case PathIterator.SEG_LINETO:
321
                                newGp1.lineTo(theData[0], theData[1]);
322
                                //newGp1 = new GeneralPathX();
323
                                //newGp1.moveTo(theData[0], theData[1]);
324
                                break;
325

    
326
                        case PathIterator.SEG_QUADTO:
327
                                newGp1.quadTo(theData[0], theData[1], theData[2],
328
                                                theData[3]);
329
                                //newGp1 = new GeneralPathX();
330
                                //newGp1.moveTo(theData[0], theData[1]);
331
                                break;
332
                        case PathIterator.SEG_CUBICTO:
333
                                newGp1.curveTo(theData[0], theData[1], theData[2],
334
                                                theData[3], theData[4], theData[5]);
335
                                //newGp1 = new GeneralPathX();
336
                                //newGp1.moveTo(theData[0], theData[1]);
337
                                break;
338
                        case PathIterator.SEG_CLOSE:
339
                                System.out.println("aqu?");
340
                                // if (isFirstPart)
341
                                // newGp1.closePath();
342
                                // else
343
                                // newGp2.closePath();
344
                                break;
345
                        } // end switch
346

    
347
                        theIterator.next();
348
                } // end while loop
349
                if (newGp1!=null) {
350
                        IGeometry geomLine = ShapeFactory
351
                                .createPolygon2D(newGp1);
352
                        DefaultFeature dfLine = (DefaultFeature) df.cloneRow();
353
                        dfLine.setGeometry(geomLine);
354
                        int indexLine = addGeometry(geomLine, dfLine
355
                                        .getAttributes());
356
                        selectedRowAux.add(new DefaultRowEdited(dfLine,
357
                        IRowEdited.STATUS_ADDED, indexLine));
358
                }
359
                getCadToolAdapter().getMapControl().drawMap(false);
360
    }
361
    /**
362
         * Add a diferent option.
363
         *
364
         * @param s
365
         *            Diferent option.
366
         */
367
    public void addOption(String s) {
368
    }
369

    
370
    /*
371
         * (non-Javadoc)
372
         *
373
         * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
374
         */
375
    public void addValue(double d) {
376

    
377
    }
378

    
379
        public String getName() {
380
                return PluginServices.getText(this,"exploit_");
381
        }
382

    
383
        public String toString() {
384
                return "_exploit";
385
        }
386
        public boolean isApplicable(int shapeType) {
387
                switch (shapeType) {
388
                case FShape.POINT:
389
                case FShape.CIRCLE:
390
                case FShape.ARC:
391
                case FShape.ELLIPSE:
392
                case FShape.MULTIPOINT:
393
                        return false;
394
                }
395
                return true;
396
        }
397
}