Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.editing.app / org.gvsig.editing.app.mainplugin / src / main / java / org / gvsig / editing / gui / cad / tools / BreakCADTool.java @ 40557

History | View | Annotate | Download (28.2 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.editing.gui.cad.tools;
25

    
26
import java.awt.event.InputEvent;
27
import java.awt.geom.PathIterator;
28
import java.awt.geom.Point2D;
29
import java.awt.geom.Rectangle2D;
30
import java.util.ArrayList;
31

    
32
import org.gvsig.andami.PluginServices;
33
import org.gvsig.andami.messages.NotificationManager;
34
import org.gvsig.editing.CADExtension;
35
import org.gvsig.editing.gui.cad.exception.CommandException;
36
import org.gvsig.editing.gui.cad.tools.smc.BreakCADToolContext;
37
import org.gvsig.editing.gui.cad.tools.smc.BreakCADToolContext.BreakCADToolState;
38
import org.gvsig.editing.layers.VectorialLayerEdited;
39
import org.gvsig.fmap.dal.exception.DataException;
40
import org.gvsig.fmap.dal.exception.ReadException;
41
import org.gvsig.fmap.dal.feature.EditableFeature;
42
import org.gvsig.fmap.dal.feature.Feature;
43
import org.gvsig.fmap.dal.feature.FeatureSet;
44
import org.gvsig.fmap.dal.feature.FeatureStore;
45
import org.gvsig.fmap.geom.Geometry;
46
import org.gvsig.fmap.geom.primitive.GeneralPathX;
47
import org.gvsig.fmap.mapcontext.ViewPort;
48
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
49
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
50
import org.gvsig.tools.dispose.DisposableIterator;
51

    
52
/**
53
 * Recorta una polil?nea en dos partes.
54
 * 
55
 * @author Vicente Caballero Navarro
56
 */
57
public class BreakCADTool extends AbstractCurveCADTool {
58

    
59
    protected BreakCADToolContext _fsm;
60
    protected Point2D firstPoint;
61
    protected Point2D secondPoint;
62
    protected Feature rowEdited;
63

    
64
    /**
65
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
66
     * carga previa a la utilizaci?n de la herramienta.
67
     */
68
    public void init() {
69
        _fsm = new BreakCADToolContext(this);
70
        firstPoint = null;
71
        secondPoint = null;
72
    }
73

    
74
    public void transition(double x, double y, InputEvent event) {
75
        _fsm.addPoint(x, y, event);
76
    }
77

    
78
    public void transition(double d) {
79
        _fsm.addValue(d);
80
    }
81

    
82
    public void transition(String s) throws CommandException {
83
        if (!super.changeCommand(s)) {
84
            _fsm.addOption(s);
85
        }
86
    }
87

    
88
    public void selection() {
89
        FeatureSet selection = null;
90
        try {
91
            selection = (FeatureSet) getVLE().getFeatureStore().getSelection();
92

    
93
            if (selection.getSize() == 0
94
                && !SelectionCADTool.isInstance(CADExtension.getCADTool(), true)) {
95
                
96
                CADExtension.setCADTool("_selection", false);
97
                ((SelectionCADTool) CADExtension.getCADTool())
98
                    .setNextTool("_break");
99
            }
100
        } catch (ReadException e) {
101
            // TODO Auto-generated catch block
102
            e.printStackTrace();
103
        } catch (DataException e) {
104
            // TODO Auto-generated catch block
105
            e.printStackTrace();
106
        }
107
    }
108

    
109
    /**
110
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
111
     * editableFeatureSource que ya estar? creado.
112
     * 
113
     * @param x
114
     *            par?metro x del punto que se pase en esta transici?n.
115
     * @param y
116
     *            par?metro y del punto que se pase en esta transici?n.
117
     */
118
    public void addPoint(double x, double y, InputEvent event) {
119
        BreakCADToolState actualState =
120
            (BreakCADToolState) _fsm.getPreviousState();
121
        String status = actualState.getName();
122

    
123
        if (status.equals("Break.FirstPoint")) {
124
            // if (rowEdited!=null &&
125
            // intersects(((DefaultFeature)rowEdited.getLinkedRow()).getGeometry(),new
126
            // Point2D.Double(x,y)))
127
            firstPoint = new Point2D.Double(x, y);
128

    
129
        } else
130
            if (status.equals("Break.SecondPoint")) {
131
                // if (rowEdited !=null &&
132
                // intersects(((DefaultFeature)rowEdited.getLinkedRow()).getGeometry(),new
133
                // Point2D.Double(x,y))){
134
                secondPoint = new Point2D.Double(x, y);
135
                try {
136
                    // IGeometry
137
                    // geom=((DefaultFeature)rowEdited.getLinkedRow()).getGeometry();
138
                    // if (geom instanceof FGeometryCollection) {
139
                    // breakGeometryGC(rowEdited);
140
                    // }else {
141
                    breakGeometry(rowEdited);
142
                    // }
143
                } catch (ReadException e) {
144
                    NotificationManager.addError(e.getMessage(), e);
145
                }
146
            }
147
        // }
148
    }
149

    
150
    /*
151
     * private void breakGeometryGC(DefaultRowEdited dre) throws IOException,
152
     * DriverIOException { GeneralPathX newGp1 = new GeneralPathX();
153
     * GeneralPathX newGp2 = new GeneralPathX(); FGeometryCollection
154
     * gc=(FGeometryCollection)((DefaultFeature)rowEdited.getLinkedRow()).
155
     * getGeometry();
156
     * IGeometry[] geoms=gc.getGeometries(); for (int i = 0;i<geoms.length;i++)
157
     * {
158
     * PathIterator theIterator=geoms[i].getPathIterator(null); double[] theData
159
     * =
160
     * new double[6]; boolean isFirstPart=true; boolean isCut=false; int
161
     * theType; int numParts = 0;
162
     * 
163
     * Point2D previous=null;
164
     * 
165
     * while (!theIterator.isDone()) { theType =
166
     * theIterator.currentSegment(theData); switch (theType) {
167
     * 
168
     * case PathIterator.SEG_MOVETO: numParts++; previous=new
169
     * Point2D.Double(theData[0], theData[1]); if (isFirstPart)
170
     * newGp1.moveTo(theData[0], theData[1]); else newGp2.moveTo(theData[0],
171
     * theData[1]); break;
172
     * 
173
     * case PathIterator.SEG_LINETO: if (previous!=null){ GeneralPathX gpx=new
174
     * GeneralPathX(); gpx.moveTo(previous.getX(),previous.getY());
175
     * gpx.lineTo(theData[0], theData[1]); IGeometry
176
     * geom=ShapeFactory.createPolyline2D(gpx); Point2D
177
     * p1=getNearPoint(previous); Point2D p2=getDistantPoint(previous); if
178
     * (intersects(geom,p1)){ isFirstPart=false;
179
     * newGp1.lineTo(p1.getX(),p1.getY()); newGp2.moveTo(p2.getX(),p2.getY());
180
     * isCut=true; } } previous=new Point2D.Double(theData[0], theData[1]); if
181
     * (isFirstPart) newGp1.lineTo(theData[0], theData[1]); else
182
     * newGp2.lineTo(theData[0], theData[1]); break;
183
     * 
184
     * case PathIterator.SEG_QUADTO: if (previous!=null){ GeneralPathX gpx=new
185
     * GeneralPathX(); gpx.moveTo(previous.getX(),previous.getY());
186
     * gpx.quadTo(theData[0], theData[1],theData[2], theData[3]); IGeometry
187
     * geom=ShapeFactory.createPolyline2D(gpx); Point2D
188
     * p1=getNearPoint(previous); Point2D p2=getDistantPoint(previous); if
189
     * (intersects(geom,p1)){ isFirstPart=false;
190
     * newGp1.lineTo(p1.getX(),p1.getY()); newGp2.moveTo(p2.getX(),p2.getY());
191
     * isCut=true; } } previous=new Point2D.Double(theData[0], theData[1]); if
192
     * (isFirstPart) newGp1.quadTo(theData[0], theData[1],theData[2],
193
     * theData[3]); else newGp2.quadTo(theData[0], theData[1],theData[2],
194
     * theData[3]);
195
     * 
196
     * break;
197
     * 
198
     * case PathIterator.SEG_CUBICTO: if (previous!=null){ GeneralPathX gpx=new
199
     * GeneralPathX(); gpx.moveTo(previous.getX(),previous.getY());
200
     * gpx.curveTo(theData[0], theData[1],theData[2], theData[3],theData[4],
201
     * theData[5]); IGeometry geom=ShapeFactory.createPolyline2D(gpx); Point2D
202
     * p1=getNearPoint(previous); Point2D p2=getDistantPoint(previous); if
203
     * (intersects(geom,p1)){ isFirstPart=false;
204
     * newGp1.lineTo(p1.getX(),p1.getY()); newGp2.moveTo(p2.getX(),p2.getY());
205
     * isCut=true; } } previous=new Point2D.Double(theData[0], theData[1]); if
206
     * (isFirstPart) newGp1.curveTo(theData[0], theData[1],theData[2],
207
     * theData[3],theData[4], theData[5]); else newGp2.curveTo(theData[0],
208
     * theData[1],theData[2], theData[3],theData[4], theData[5]);
209
     * 
210
     * break;
211
     * 
212
     * case PathIterator.SEG_CLOSE: //if (isFirstPart) // newGp1.closePath();
213
     * //else // newGp2.closePath(); break; } //end switch
214
     * 
215
     * theIterator.next(); } //end while loop
216
     * 
217
     * if (isCut) { IGeometry geom1 = ShapeFactory.createPolyline2D(newGp1);
218
     * IGeometry geom2 = ShapeFactory.createPolyline2D(newGp2);
219
     * VectorialLayerEdited vle = getVLE(); VectorialEditableAdapter vea =
220
     * vle.getVEA(); ArrayList selectedRow = vle.getSelectedRow();
221
     * vea.startComplexRow(); vea.removeRow(dre.getIndex(), getName(),
222
     * EditionEvent.GRAPHIC); int num = vea.getRowCount(); if (gc.isClosed()) {
223
     * ArrayList geomsAux1 = new ArrayList(); geomsAux1.add(geom2); for (int k =
224
     * i + 1; k < geoms.length; k++) { geomsAux1.add(geoms[k]); } for (int k =
225
     * 0; k < i; k++) { geomsAux1.add(geoms[k]); } geomsAux1.add(geom1);
226
     * 
227
     * DefaultFeature df1 = new DefaultFeature( new
228
     * FGeometryCollection((IGeometry[]) geomsAux1 .toArray(new IGeometry[0])),
229
     * dre .getAttributes(), String.valueOf(num)); int index1 = vea.addRow(df1,
230
     * PluginServices.getText(this, "parte1"), EditionEvent.GRAPHIC);
231
     * 
232
     * clearSelection(); selectedRow.add(new DefaultRowEdited(df1,
233
     * IRowEdited.STATUS_ADDED, index1)); vea.endComplexRow(); return; }else {
234
     * 
235
     * ArrayList geomsAux1 = new ArrayList(); for (int k = 0; k < i; k++) {
236
     * geomsAux1.add(geoms[k]); } geomsAux1.add(geom1);
237
     * 
238
     * ArrayList geomsAux2 = new ArrayList(); geomsAux2.add(geom2); for (int k =
239
     * i + 1; k < geoms.length; k++) { geomsAux2.add(geoms[k]); }
240
     * 
241
     * DefaultFeature df1 = new DefaultFeature( new
242
     * FGeometryCollection((IGeometry[]) geomsAux1 .toArray(new IGeometry[0])),
243
     * dre .getAttributes(), String.valueOf(num)); int index1 = vea.addRow(df1,
244
     * PluginServices.getText(this, "parte1"), EditionEvent.GRAPHIC);
245
     * DefaultFeature df2 = new DefaultFeature( new
246
     * FGeometryCollection((IGeometry[]) geomsAux2 .toArray(new IGeometry[0])),
247
     * dre .getAttributes(), String.valueOf(num + 1)); int index2 =
248
     * vea.addRow(df2, PluginServices.getText(this, "parte2"),
249
     * EditionEvent.GRAPHIC); clearSelection(); selectedRow.add(new
250
     * DefaultRowEdited(df2, IRowEdited.STATUS_ADDED, index2));
251
     * selectedRow.add(new DefaultRowEdited(df1, IRowEdited.STATUS_ADDED,
252
     * index1));
253
     * 
254
     * vea.endComplexRow(); return; }
255
     * } } }
256
     */
257

    
258
    private void breakGeometry(Feature dre) throws ReadException {
259
        breakGeom(dre);
260
    }
261

    
262
    private void breakGeom(Feature dre) throws ReadException {
263
        GeneralPathX newGp1 = new GeneralPathX();
264
        GeneralPathX newGp2 = new GeneralPathX();
265
        Geometry geomAux = (Geometry) rowEdited.getDefaultGeometry();
266
        PathIterator theIterator =
267
            geomAux.getPathIterator(null, geomManager.getFlatness());
268
        Point2D[] pointsOrdered = getOrderPoints(geomAux);
269
        double[] theData = new double[6];
270
        boolean isFirstPart = true;
271
        boolean intersectsP2 = false;
272
        int theType;
273
        int numParts = 0;
274
        boolean isBreaked = false;
275
        Point2D previous = null;
276

    
277
        while (!theIterator.isDone()) {
278
            theType = theIterator.currentSegment(theData);
279
            switch (theType) {
280

    
281
            case PathIterator.SEG_MOVETO:
282
                numParts++;
283

    
284
                previous = new Point2D.Double(theData[0], theData[1]);
285

    
286
                if (isFirstPart)
287
                    newGp1.moveTo(theData[0], theData[1]);
288
                else
289
                    newGp2.moveTo(theData[0], theData[1]);
290
                break;
291

    
292
            case PathIterator.SEG_LINETO:
293

    
294
                if (previous != null) {
295
                    GeneralPathX gpx = new GeneralPathX();
296
                    gpx.moveTo(previous.getX(), previous.getY());
297
                    gpx.lineTo(theData[0], theData[1]);
298
                    Geometry geom = createCurve(gpx);
299
                    Point2D p1 = pointsOrdered[0];
300
                    Point2D p2 = pointsOrdered[1];
301

    
302
                    if (intersects(geom, p1) && !isBreaked) {
303
                        isFirstPart = false;
304
                        newGp1.lineTo(p1.getX(), p1.getY());
305
                    }
306
                    if (intersects(geom, p2) && !isBreaked) {
307
                        isBreaked = true;
308
                        intersectsP2 = true;
309
                        newGp2.moveTo(p2.getX(), p2.getY());
310
                    }
311
                }
312
                previous = new Point2D.Double(theData[0], theData[1]);
313
                if (isFirstPart)
314
                    newGp1.lineTo(theData[0], theData[1]);
315
                else
316
                    if (intersectsP2) {
317
                        newGp2.lineTo(theData[0], theData[1]);
318
                    }
319

    
320
                break;
321

    
322
            case PathIterator.SEG_QUADTO:
323
                if (previous != null) {
324
                    GeneralPathX gpx = new GeneralPathX();
325
                    gpx.moveTo(previous.getX(), previous.getY());
326
                    gpx.quadTo(theData[0], theData[1], theData[2], theData[3]);
327
                    Geometry geom = createCurve(gpx);
328
                    Point2D p1 = pointsOrdered[0];
329
                    Point2D p2 = pointsOrdered[1];
330
                    if (intersects(geom, p1) && !isBreaked) {
331
                        isFirstPart = false;
332
                        newGp1.lineTo(p1.getX(), p1.getY());
333
                    }
334
                    if (intersects(geom, p2) && !isBreaked) {
335
                        isBreaked = true;
336
                        intersectsP2 = true;
337
                        newGp2.moveTo(p2.getX(), p2.getY());
338

    
339
                    }
340
                }
341
                previous = new Point2D.Double(theData[0], theData[1]);
342
                if (isFirstPart)
343
                    newGp1.quadTo(theData[0], theData[1], theData[2],
344
                        theData[3]);
345
                else
346
                    newGp2.quadTo(theData[0], theData[1], theData[2],
347
                        theData[3]);
348

    
349
                break;
350

    
351
            case PathIterator.SEG_CUBICTO:
352
                if (previous != null) {
353
                    GeneralPathX gpx = new GeneralPathX();
354
                    gpx.moveTo(previous.getX(), previous.getY());
355
                    gpx.curveTo(theData[0], theData[1], theData[2], theData[3],
356
                        theData[4], theData[5]);
357
                    Geometry geom = createCurve(gpx);
358
                    Point2D p1 = pointsOrdered[0];
359
                    Point2D p2 = pointsOrdered[1];
360
                    if (intersects(geom, p1) && !isBreaked) {
361
                        isFirstPart = false;
362
                        newGp1.lineTo(p1.getX(), p1.getY());
363
                    }
364
                    if (intersects(geom, p2) && !isBreaked) {
365
                        isBreaked = true;
366
                        intersectsP2 = true;
367
                        newGp2.moveTo(p2.getX(), p2.getY());
368

    
369
                    }
370
                }
371
                previous = new Point2D.Double(theData[0], theData[1]);
372
                if (isFirstPart)
373
                    newGp1.curveTo(theData[0], theData[1], theData[2],
374
                        theData[3], theData[4], theData[5]);
375
                else
376
                    newGp2.curveTo(theData[0], theData[1], theData[2],
377
                        theData[3], theData[4], theData[5]);
378

    
379
                break;
380

    
381
            case PathIterator.SEG_CLOSE:
382
                // if (isFirstPart)
383
                // newGp1.closePath();
384
                // else
385
                // newGp2.closePath();
386
                break;
387
            } // end switch
388

    
389
            theIterator.next();
390
        } // end while loop
391
        GeneralPathX gpx = new GeneralPathX();
392
        gpx.append(geomAux.getInternalShape().getPathIterator(null), true);
393
        VectorialLayerEdited vle = getVLE();
394
        FeatureStore featureStore =
395
            ((FLyrVect) vle.getLayer()).getFeatureStore();
396
        // VectorialEditableAdapter vea = vle.getVEA();
397
        ArrayList selectedRowAux = new ArrayList();
398
        try {
399
            featureStore.beginEditingGroup(getName());
400

    
401
            featureStore.delete(dre);
402
            // vea.startComplexRow();
403
            if (gpx.isClosed()) {
404

    
405
                newGp2.append(
406
                    newGp1.getPathIterator(null, geomManager.getFlatness()),
407
                    true);
408
                Geometry geom1 = createCurve(newGp2);
409

    
410
                // Feature dfLine1 = dre.cloneRow();
411
                EditableFeature eFeature =
412
                    featureStore.createNewFeature(dre.getType(), dre);
413
                eFeature.setGeometry(featureStore.getDefaultFeatureType()
414
                    .getDefaultGeometryAttributeName(), geom1);
415
                selectedRowAux.add(eFeature);
416

    
417
            } else {
418
                Geometry geom1 = createCurve(newGp1);
419
                Geometry geom2 = createCurve(newGp2);
420

    
421
                // DefaultFeature dfLine1 = (DefaultFeature)
422
                // dre.getLinkedRow().cloneRow();
423
                EditableFeature eFeature =
424
                    featureStore.createNewFeature(dre.getType(), dre);
425
                eFeature.setGeometry(featureStore.getDefaultFeatureType()
426
                    .getDefaultGeometryAttributeName(), geom2);
427

    
428
                selectedRowAux.add(eFeature);
429

    
430
                // dre.editing();
431
                // dre.setGeometry(geom2);
432
                // DefaultFeature dfLine2 = (DefaultFeature)
433
                // dre.getLinkedRow().cloneRow();
434
                // dfLine2.setGeometry(geom2);
435
                // int indexLine2 = addGeometry(dre);
436

    
437
                // selectedRowAux.add(new DefaultRowEdited(dfLine2,
438
                // IRowEdited.STATUS_ADDED, indexLine2));
439

    
440
            }
441

    
442
            // vea.removeRow(dre.getIndex(), getName(), EditionEvent.GRAPHIC);
443
            // vea.endComplexRow(getName());
444
            featureStore.endEditingGroup();
445
        } catch (DataException e) {
446
            // TODO Auto-generated catch block
447
            e.printStackTrace();
448
        }
449
        // vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS,
450
        // selectedRowAux);
451

    
452
    }
453

    
454
    private Point2D[] getOrderPoints(Geometry geomAux) {
455
        PathIterator theIterator =
456
            geomAux.getPathIterator(null, geomManager.getFlatness());
457
        double[] theData = new double[6];
458
        Point2D previous = null;
459
        ArrayList points = new ArrayList();
460
        boolean isFirstPointBreak = false;
461
        boolean isSecondPointBreak = false;
462
        while (!theIterator.isDone()) {
463
            int theType = theIterator.currentSegment(theData);
464
            switch (theType) {
465

    
466
            case PathIterator.SEG_MOVETO:
467
                previous = new Point2D.Double(theData[0], theData[1]);
468
                break;
469

    
470
            case PathIterator.SEG_LINETO:
471

    
472
                if (previous != null) {
473
                    GeneralPathX gpx = new GeneralPathX();
474
                    gpx.moveTo(previous.getX(), previous.getY());
475
                    gpx.lineTo(theData[0], theData[1]);
476
                    Geometry geom = createCurve(gpx);
477
                    boolean intersectFirst = intersects(geom, firstPoint);
478
                    boolean intersectSecond = intersects(geom, secondPoint);
479
                    if (intersectFirst && intersectSecond && !isFirstPointBreak) {
480
                        isFirstPointBreak = true;
481
                        isSecondPointBreak = true;
482
                        points.add(getNearPoint(previous));
483
                        points.add(getDistantPoint(previous));
484
                        return (Point2D[]) points.toArray(new Point2D[0]);
485
                    } else
486
                        if (intersectFirst && !isFirstPointBreak) {
487
                            isFirstPointBreak = true;
488
                            points.add(firstPoint);
489
                        } else
490
                            if (intersectSecond && !isSecondPointBreak) {
491
                                isSecondPointBreak = true;
492
                                points.add(secondPoint);
493
                            }
494
                }
495
                previous = new Point2D.Double(theData[0], theData[1]);
496
                break;
497

    
498
            case PathIterator.SEG_QUADTO:
499
                if (previous != null) {
500
                    GeneralPathX gpx = new GeneralPathX();
501
                    gpx.moveTo(previous.getX(), previous.getY());
502
                    gpx.quadTo(theData[0], theData[1], theData[2], theData[3]);
503
                    Geometry geom = createCurve(gpx);
504
                    boolean intersectFirst = intersects(geom, firstPoint);
505
                    boolean intersectSecond = intersects(geom, secondPoint);
506
                    if (intersectFirst && intersectSecond && !isFirstPointBreak) {
507
                        isFirstPointBreak = true;
508
                        isSecondPointBreak = true;
509
                        points.add(getNearPoint(previous));
510
                        points.add(getDistantPoint(previous));
511
                        return (Point2D[]) points.toArray(new Point2D[0]);
512
                    } else
513
                        if (intersectFirst && !isFirstPointBreak) {
514
                            isFirstPointBreak = true;
515
                            points.add(firstPoint);
516
                        } else
517
                            if (intersectSecond && !isSecondPointBreak) {
518
                                isSecondPointBreak = true;
519
                                points.add(secondPoint);
520
                            }
521
                }
522
                previous = new Point2D.Double(theData[0], theData[1]);
523

    
524
                break;
525

    
526
            case PathIterator.SEG_CUBICTO:
527
                if (previous != null) {
528
                    GeneralPathX gpx = new GeneralPathX();
529
                    gpx.moveTo(previous.getX(), previous.getY());
530
                    gpx.curveTo(theData[0], theData[1], theData[2], theData[3],
531
                        theData[4], theData[5]);
532
                    Geometry geom = createCurve(gpx);
533
                    boolean intersectFirst = intersects(geom, firstPoint);
534
                    boolean intersectSecond = intersects(geom, secondPoint);
535
                    if (intersectFirst && intersectSecond && !isFirstPointBreak) {
536
                        isFirstPointBreak = true;
537
                        isSecondPointBreak = true;
538
                        points.add(getNearPoint(previous));
539
                        points.add(getDistantPoint(previous));
540
                        return (Point2D[]) points.toArray(new Point2D[0]);
541
                    } else
542
                        if (intersectFirst && !isFirstPointBreak) {
543
                            isFirstPointBreak = true;
544
                            points.add(firstPoint);
545
                        } else
546
                            if (intersectSecond && !isSecondPointBreak) {
547
                                isSecondPointBreak = true;
548
                                points.add(secondPoint);
549
                            }
550
                }
551
                previous = new Point2D.Double(theData[0], theData[1]);
552

    
553
                break;
554

    
555
            case PathIterator.SEG_CLOSE:
556
                // if (isFirstPart)
557
                // newGp1.closePath();
558
                // else
559
                // newGp2.closePath();
560
                break;
561
            } // end switch
562

    
563
            theIterator.next();
564
        } // end while loop
565

    
566
        return (Point2D[]) points.toArray(new Point2D[0]);
567
    }
568

    
569
    private Point2D getDistantPoint(Point2D previous) {
570
        if (firstPoint.distance(previous) > secondPoint.distance(previous)) {
571
            return firstPoint;
572
        }
573
        return secondPoint;
574
    }
575

    
576
    private Point2D getNearPoint(Point2D previous) {
577
        if (firstPoint.distance(previous) <= secondPoint.distance(previous)) {
578
            return firstPoint;
579
        }
580
        return secondPoint;
581
    }
582

    
583
    /**
584
     * M?todo para dibujar la lo necesario para el estado en el que nos
585
     * encontremos.
586
     * 
587
     * @param g
588
     *            Graphics sobre el que dibujar.
589
     * @param x
590
     *            par?metro x del punto que se pase para dibujar.
591
     * @param y
592
     *            par?metro x del punto que se pase para dibujar.
593
     */
594
    public void drawOperation(MapControlDrawer renderer, double x, double y) {
595
        VectorialLayerEdited vle = getVLE();
596
        FeatureSet selection = null;
597
        DisposableIterator iterator = null;
598
        try {
599
            selection = (FeatureSet) vle.getFeatureStore().getSelection();
600

    
601
            ViewPort vp =
602
                CADExtension.getEditionManager().getMapControl().getViewPort();
603
            if (selection.getSize() == 1) {
604
                if (firstPoint != null) {
605
                    Geometry g1 =
606
                        createCircle(createPoint(firstPoint),
607
                            vp.toMapDistance(3));
608
                    Geometry g2 =
609
                        createCircle(createPoint(firstPoint),
610
                            vp.toMapDistance(5));
611

    
612
                    renderer.draw(g1,
613
                        mapControlManager.getAxisReferenceSymbol());
614
                    renderer.draw(g2,
615
                        mapControlManager.getAxisReferenceSymbol());
616
                }
617
                iterator = selection.iterator();
618
                rowEdited = (Feature) iterator.next();
619
                Geometry geom =
620
                    ((Geometry) rowEdited.getDefaultGeometry()).cloneGeometry();
621
                if (intersects(geom, new Point2D.Double(x, y))) {
622
                    renderer.draw(geom,
623
                        mapControlManager.getGeometrySelectionSymbol());
624
                }
625
            }
626
        } catch (ReadException e1) {
627
            // TODO Auto-generated catch block
628
            e1.printStackTrace();
629
        } catch (DataException e) {
630
            // TODO Auto-generated catch block
631
            e.printStackTrace();
632
        } finally {
633
            if (iterator != null) {
634
                iterator.dispose();
635
            }
636
        }
637

    
638
    }
639

    
640
    public boolean intersects(double x, double y) {
641
        Point2D p = new Point2D.Double(x, y);
642
        VectorialLayerEdited vle = getVLE();
643
        FeatureSet selection = null;
644
        DisposableIterator iterator = null;
645
        try {
646
            selection = (FeatureSet) vle.getFeatureStore().getSelection();
647

    
648
            if (selection.getSize() == 1) {
649
                iterator = selection.iterator();
650
                rowEdited = (Feature) iterator.next();
651
                Geometry g =
652
                    ((Geometry) rowEdited.getDefaultGeometry()).cloneGeometry();
653
                return intersects(g, p);
654
            }
655
        } catch (ReadException e) {
656
            // TODO Auto-generated catch block
657
            e.printStackTrace();
658
        } catch (DataException e) {
659
            // TODO Auto-generated catch block
660
            e.printStackTrace();
661
        } finally {
662
            if (iterator != null) {
663
                iterator.dispose();
664
            }
665
        }
666
        return false;
667
    }
668

    
669
    private boolean intersects(Geometry geom, Point2D p) {
670
        double tol = 1;
671
        tol =
672
            CADExtension.getEditionManager().getMapControl().getViewPort()
673
                .toMapDistance((int) tol);
674
        Rectangle2D r =
675
            new Rectangle2D.Double(p.getX() - tol / 2, p.getY() - tol / 2, tol,
676
                tol);
677
        return (geom.intersects(r) && !geom.getShape().contains(r));
678
    }
679

    
680
    /**
681
     * Add a diferent option.
682
     * 
683
     * @param s
684
     *            Diferent option.
685
     */
686
    public void addOption(String s) {
687
        if (s.equals(PluginServices.getText(this, "cancel")) || s.equals("c")
688
            || s.equals("C")) {
689
            init();
690
        }
691
    }
692

    
693
    /*
694
     * (non-Javadoc)
695
     * 
696
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
697
     */
698
    public void addValue(double d) {
699
    }
700

    
701
    public String getName() {
702
        return PluginServices.getText(this, "break_");
703
    }
704

    
705
    public String toString() {
706
        return "_break";
707
    }
708

    
709
    @Override
710
    protected int getSupportedPrimitiveGeometryType() {
711
        return CURVE;
712
    }
713

    
714
}