Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1008 / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / BreakCADTool.java @ 12520

History | View | Annotate | Download (31.3 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.Graphics2D;
45
import java.awt.event.InputEvent;
46
import java.awt.geom.PathIterator;
47
import java.awt.geom.Point2D;
48
import java.awt.geom.Rectangle2D;
49
import java.awt.image.BufferedImage;
50
import java.io.IOException;
51
import java.util.ArrayList;
52

    
53
import com.hardcode.gdbms.engine.data.driver.DriverException;
54
import com.hardcode.gdbms.engine.values.Value;
55
import com.iver.andami.PluginServices;
56
import com.iver.cit.gvsig.CADExtension;
57
import com.iver.cit.gvsig.fmap.ViewPort;
58
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
59
import com.iver.cit.gvsig.fmap.core.FShape;
60
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
61
import com.iver.cit.gvsig.fmap.core.IGeometry;
62
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
63
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
64
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
65
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
66
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
67
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
68
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
69
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
70
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
71
import com.iver.cit.gvsig.gui.cad.tools.smc.BreakCADToolContext;
72
import com.iver.cit.gvsig.gui.cad.tools.smc.BreakCADToolContext.BreakCADToolState;
73
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
74

    
75

    
76
/**
77
 * DOCUMENT ME!
78
 *
79
 * @author Vicente Caballero Navarro
80
 */
81
public class BreakCADTool extends DefaultCADTool {
82
    private BreakCADToolContext _fsm;
83
    private Point2D firstPoint;
84
    private Point2D secondPoint;
85
    private DefaultRowEdited rowEdited;
86
    /**
87
     * Crea un nuevo PolylineCADTool.
88
     */
89
    public BreakCADTool() {
90
    }
91

    
92
    /**
93
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
94
     * carga previa a la utilizaci?n de la herramienta.
95
     */
96
    public void init() {
97
        _fsm = new BreakCADToolContext(this);
98
        firstPoint=null;
99
        secondPoint=null;
100
    }
101

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

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

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

    
125
    /**
126
     * DOCUMENT ME!
127
     */
128
    public void selection() {
129
        ArrayList selectedRow=getSelectedRows();
130
        if (selectedRow.size() == 0 && !CADExtension.getCADTool().getClass().getName().equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
131
            CADExtension.setCADTool("_selection",false);
132
            ((SelectionCADTool) CADExtension.getCADTool()).setNextTool(
133
                "_break");
134
        }
135
    }
136

    
137
    /**
138
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
139
     * editableFeatureSource que ya estar? creado.
140
     *
141
     * @param x par?metro x del punto que se pase en esta transici?n.
142
     * @param y par?metro y del punto que se pase en esta transici?n.
143
     */
144
    public void addPoint(double x, double y,InputEvent event) {
145
         BreakCADToolState actualState = (BreakCADToolState) _fsm.getPreviousState();
146
         String status = actualState.getName();
147

    
148
         if (status.equals("Break.FirstPoint")) {
149
            // if (rowEdited!=null && intersects(((DefaultFeature)rowEdited.getLinkedRow()).getGeometry(),new Point2D.Double(x,y)))
150
                 firstPoint=new Point2D.Double(x,y);
151

    
152
         } else if (status.equals("Break.SecondPoint")) {
153
          // if (rowEdited !=null && intersects(((DefaultFeature)rowEdited.getLinkedRow()).getGeometry(),new Point2D.Double(x,y))){
154
               secondPoint=new Point2D.Double(x,y);
155
               try {
156
//                           IGeometry geom=((DefaultFeature)rowEdited.getLinkedRow()).getGeometry();
157
//                           if (geom instanceof FGeometryCollection) {
158
//                             breakGeometryGC(rowEdited);
159
//                        }else {
160
                    breakGeometry(rowEdited);
161
//                                }
162
            } catch (IOException e) {
163
                e.printStackTrace();
164
            } catch (DriverIOException e) {
165
                e.printStackTrace();
166
            }
167
           }
168
         //}
169
    }
170
  /*  private void breakGeometryGC(DefaultRowEdited dre) throws IOException, DriverIOException {
171
        GeneralPathX newGp1 = new GeneralPathX();
172
        GeneralPathX newGp2 = new GeneralPathX();
173
        FGeometryCollection gc=(FGeometryCollection)((DefaultFeature)rowEdited.getLinkedRow()).getGeometry();
174
        IGeometry[] geoms=gc.getGeometries();
175
        for (int i = 0;i<geoms.length;i++) {
176
            PathIterator theIterator=geoms[i].getPathIterator(null);
177
            double[] theData = new double[6];
178
            boolean isFirstPart=true;
179
            boolean isCut=false;
180
            int theType;
181
            int numParts = 0;
182

183
            Point2D previous=null;
184

185
            while (!theIterator.isDone()) {
186
                theType = theIterator.currentSegment(theData);
187
                switch (theType) {
188

189
                    case PathIterator.SEG_MOVETO:
190
                        numParts++;
191
                        previous=new Point2D.Double(theData[0], theData[1]);
192
                        if (isFirstPart)
193
                            newGp1.moveTo(theData[0], theData[1]);
194
                        else
195
                            newGp2.moveTo(theData[0], theData[1]);
196
                        break;
197

198
                    case PathIterator.SEG_LINETO:
199
                         if (previous!=null){
200
                                GeneralPathX gpx=new GeneralPathX();
201
                                gpx.moveTo(previous.getX(),previous.getY());
202
                                gpx.lineTo(theData[0], theData[1]);
203
                                IGeometry geom=ShapeFactory.createPolyline2D(gpx);
204
                                Point2D p1=getNearPoint(previous);
205
                                Point2D p2=getDistantPoint(previous);
206
                                if (intersects(geom,p1)){
207
                                    isFirstPart=false;
208
                                    newGp1.lineTo(p1.getX(),p1.getY());
209
                                    newGp2.moveTo(p2.getX(),p2.getY());
210
                                    isCut=true;
211
                                }
212
                            }
213
                         previous=new Point2D.Double(theData[0], theData[1]);
214
                        if (isFirstPart)
215
                            newGp1.lineTo(theData[0], theData[1]);
216
                        else
217
                            newGp2.lineTo(theData[0], theData[1]);
218
                        break;
219

220
                    case PathIterator.SEG_QUADTO:
221
                         if (previous!=null){
222
                                GeneralPathX gpx=new GeneralPathX();
223
                                gpx.moveTo(previous.getX(),previous.getY());
224
                                gpx.quadTo(theData[0], theData[1],theData[2], theData[3]);
225
                                IGeometry geom=ShapeFactory.createPolyline2D(gpx);
226
                                Point2D p1=getNearPoint(previous);
227
                                Point2D p2=getDistantPoint(previous);
228
                                if (intersects(geom,p1)){
229
                                    isFirstPart=false;
230
                                    newGp1.lineTo(p1.getX(),p1.getY());
231
                                    newGp2.moveTo(p2.getX(),p2.getY());
232
                                    isCut=true;
233
                                }
234
                            }
235
                         previous=new Point2D.Double(theData[0], theData[1]);
236
                        if (isFirstPart)
237
                            newGp1.quadTo(theData[0], theData[1],theData[2], theData[3]);
238
                        else
239
                            newGp2.quadTo(theData[0], theData[1],theData[2], theData[3]);
240

241
                        break;
242

243
                    case PathIterator.SEG_CUBICTO:
244
                         if (previous!=null){
245
                                GeneralPathX gpx=new GeneralPathX();
246
                                gpx.moveTo(previous.getX(),previous.getY());
247
                                gpx.curveTo(theData[0], theData[1],theData[2], theData[3],theData[4], theData[5]);
248
                                IGeometry geom=ShapeFactory.createPolyline2D(gpx);
249
                                Point2D p1=getNearPoint(previous);
250
                                Point2D p2=getDistantPoint(previous);
251
                                if (intersects(geom,p1)){
252
                                    isFirstPart=false;
253
                                    newGp1.lineTo(p1.getX(),p1.getY());
254
                                    newGp2.moveTo(p2.getX(),p2.getY());
255
                                    isCut=true;
256
                                }
257
                            }
258
                         previous=new Point2D.Double(theData[0], theData[1]);
259
                        if (isFirstPart)
260
                            newGp1.curveTo(theData[0], theData[1],theData[2], theData[3],theData[4], theData[5]);
261
                        else
262
                            newGp2.curveTo(theData[0], theData[1],theData[2], theData[3],theData[4], theData[5]);
263

264
                        break;
265

266
                    case PathIterator.SEG_CLOSE:
267
                        //if (isFirstPart)
268
                        //        newGp1.closePath();
269
                        //else
270
                        //        newGp2.closePath();
271
                        break;
272
                } //end switch
273

274
                theIterator.next();
275
            } //end while loop
276

277
            if (isCut) {
278
                IGeometry geom1 = ShapeFactory.createPolyline2D(newGp1);
279
                IGeometry geom2 = ShapeFactory.createPolyline2D(newGp2);
280
                VectorialLayerEdited vle = getVLE();
281
                VectorialEditableAdapter vea = vle.getVEA();
282
                ArrayList selectedRow = vle.getSelectedRow();
283
                vea.startComplexRow();
284
                vea.removeRow(dre.getIndex(), getName(), EditionEvent.GRAPHIC);
285
                int num = vea.getRowCount();
286
                if (gc.isClosed()) {
287
                    ArrayList geomsAux1 = new ArrayList();
288
                    geomsAux1.add(geom2);
289
                    for (int k = i + 1; k < geoms.length; k++) {
290
                        geomsAux1.add(geoms[k]);
291
                    }
292
                    for (int k = 0; k < i; k++) {
293
                        geomsAux1.add(geoms[k]);
294
                    }
295
                    geomsAux1.add(geom1);
296

297
                    DefaultFeature df1 = new DefaultFeature(
298
                            new FGeometryCollection((IGeometry[]) geomsAux1
299
                                    .toArray(new IGeometry[0])), dre
300
                                    .getAttributes(), String.valueOf(num));
301
                    int index1 = vea.addRow(df1, PluginServices.getText(this,
302
                            "parte1"), EditionEvent.GRAPHIC);
303

304
                    clearSelection();
305
                    selectedRow.add(new DefaultRowEdited(df1,
306
                            IRowEdited.STATUS_ADDED, index1));
307
                    vea.endComplexRow();
308
                    return;
309
                }else {
310

311
                    ArrayList geomsAux1 = new ArrayList();
312
                    for (int k = 0; k < i; k++) {
313
                        geomsAux1.add(geoms[k]);
314
                    }
315
                    geomsAux1.add(geom1);
316

317
                    ArrayList geomsAux2 = new ArrayList();
318
                    geomsAux2.add(geom2);
319
                    for (int k = i + 1; k < geoms.length; k++) {
320
                        geomsAux2.add(geoms[k]);
321
                    }
322

323
                    DefaultFeature df1 = new DefaultFeature(
324
                            new FGeometryCollection((IGeometry[]) geomsAux1
325
                                    .toArray(new IGeometry[0])), dre
326
                                    .getAttributes(), String.valueOf(num));
327
                    int index1 = vea.addRow(df1, PluginServices.getText(this,
328
                            "parte1"), EditionEvent.GRAPHIC);
329
                    DefaultFeature df2 = new DefaultFeature(
330
                            new FGeometryCollection((IGeometry[]) geomsAux2
331
                                .toArray(new IGeometry[0])), dre
332
                                .getAttributes(), String.valueOf(num + 1));
333
                    int index2 = vea.addRow(df2, PluginServices.getText(this,
334
                        "parte2"), EditionEvent.GRAPHIC);
335
                    clearSelection();
336
                    selectedRow.add(new DefaultRowEdited(df2,
337
                            IRowEdited.STATUS_ADDED, index2));
338
                    selectedRow.add(new DefaultRowEdited(df1,
339
                            IRowEdited.STATUS_ADDED, index1));
340

341
                    vea.endComplexRow();
342
                    return;
343
                }
344

345
            }
346
        }
347
    }
348
*/
349

    
350
    private void breakGeometry(DefaultRowEdited dre) throws IOException, DriverIOException {
351
        GeneralPathX newGp1 = new GeneralPathX();
352
        GeneralPathX newGp2 = new GeneralPathX();
353
        IGeometry geomAux=((DefaultFeature)rowEdited.getLinkedRow()).getGeometry();
354
        PathIterator theIterator=geomAux.getPathIterator(null,FConverter.FLATNESS);
355
        Point2D[] pointsOrdered=getOrderPoints(geomAux);
356
        double[] theData = new double[6];
357
        boolean isFirstPart=true;
358
        boolean intersectsP2=false;
359
        int theType;
360
        int numParts = 0;
361

    
362
        Point2D previous=null;
363

    
364
            while (!theIterator.isDone()) {
365
                theType = theIterator.currentSegment(theData);
366
                switch (theType) {
367

    
368
                    case PathIterator.SEG_MOVETO:
369
                        numParts++;
370

    
371
                        previous=new Point2D.Double(theData[0], theData[1]);
372

    
373
                        if (isFirstPart)
374
                            newGp1.moveTo(theData[0], theData[1]);
375
                        else
376
                            newGp2.moveTo(theData[0], theData[1]);
377
                        break;
378

    
379
                    case PathIterator.SEG_LINETO:
380

    
381
                        if (previous!=null){
382
                                GeneralPathX gpx=new GeneralPathX();
383
                                gpx.moveTo(previous.getX(),previous.getY());
384
                                gpx.lineTo(theData[0], theData[1]);
385
                                IGeometry geom=ShapeFactory.createPolyline2D(gpx);
386
                                Point2D p1=pointsOrdered[0];
387
                                Point2D p2=pointsOrdered[1];
388

    
389
                                if (intersects(geom,p1)){
390
                                    isFirstPart=false;
391
                                    newGp1.lineTo(p1.getX(),p1.getY());
392
                                }
393
                                if (intersects(geom,p2)) {
394
                                    intersectsP2=true;
395
                                    newGp2.moveTo(p2.getX(),p2.getY());
396

    
397
                                }
398
                            }
399
                         previous=new Point2D.Double(theData[0], theData[1]);
400
                        if (isFirstPart)
401
                            newGp1.lineTo(theData[0], theData[1]);
402
                        else if (intersectsP2){
403
                            newGp2.lineTo(theData[0], theData[1]);
404
                        }
405

    
406
                        break;
407

    
408
                    case PathIterator.SEG_QUADTO:
409
                         if (previous!=null){
410
                                GeneralPathX gpx=new GeneralPathX();
411
                                gpx.moveTo(previous.getX(),previous.getY());
412
                                gpx.quadTo(theData[0], theData[1],theData[2], theData[3]);
413
                                IGeometry geom=ShapeFactory.createPolyline2D(gpx);
414
                                Point2D p1=pointsOrdered[0];
415
                                Point2D p2=pointsOrdered[1];
416
                                if (intersects(geom,p1)){
417
                                    isFirstPart=false;
418
                                    newGp1.lineTo(p1.getX(),p1.getY());
419
                                    newGp2.moveTo(p2.getX(),p2.getY());
420
                                }
421
                            }
422
                         previous=new Point2D.Double(theData[0], theData[1]);
423
                        if (isFirstPart)
424
                            newGp1.quadTo(theData[0], theData[1],theData[2], theData[3]);
425
                        else
426
                            newGp2.quadTo(theData[0], theData[1],theData[2], theData[3]);
427

    
428
                        break;
429

    
430
                    case PathIterator.SEG_CUBICTO:
431
                         if (previous!=null){
432
                                GeneralPathX gpx=new GeneralPathX();
433
                                gpx.moveTo(previous.getX(),previous.getY());
434
                                gpx.curveTo(theData[0], theData[1],theData[2], theData[3],theData[4], theData[5]);
435
                                IGeometry geom=ShapeFactory.createPolyline2D(gpx);
436
                                Point2D p1=pointsOrdered[0];
437
                                Point2D p2=pointsOrdered[1];
438
                                if (intersects(geom,p1)){
439
                                    isFirstPart=false;
440
                                    newGp1.lineTo(p1.getX(),p1.getY());
441
                                    newGp2.moveTo(p2.getX(),p2.getY());
442
                                }
443
                            }
444
                         previous=new Point2D.Double(theData[0], theData[1]);
445
                        if (isFirstPart)
446
                            newGp1.curveTo(theData[0], theData[1],theData[2], theData[3],theData[4], theData[5]);
447
                        else
448
                            newGp2.curveTo(theData[0], theData[1],theData[2], theData[3],theData[4], theData[5]);
449

    
450
                        break;
451

    
452
                    case PathIterator.SEG_CLOSE:
453
                        //if (isFirstPart)
454
                        //        newGp1.closePath();
455
                        //else
456
                        //        newGp2.closePath();
457
                        break;
458
                } //end switch
459

    
460
                theIterator.next();
461
            } //end while loop
462
            GeneralPathX gpx=new GeneralPathX();
463
            gpx.append(geomAux.getInternalShape(),true);
464
            if (gpx.isClosed()) {
465
                newGp2.append(newGp1.getPathIterator(null,FConverter.FLATNESS),true);
466
                IGeometry geom1=ShapeFactory.createPolyline2D(newGp2);
467
                VectorialLayerEdited vle = getVLE();
468
                VectorialEditableAdapter vea = vle.getVEA();
469
                String newFID;
470
//                                try {
471
                                        newFID = vea.getNewFID();
472
//                                } catch (DriverException e) {
473
//                                        throw new DriverIOException(e);
474
//                                }
475
                DefaultFeature df1 = new DefaultFeature(geom1, dre.getAttributes(),newFID);
476

    
477
                vea.startComplexRow();
478
                vea.removeRow(dre.getIndex(),getName(),EditionEvent.GRAPHIC);
479
                int index1=vea.addRow(df1,PluginServices.getText(this,"parte1"),EditionEvent.GRAPHIC);
480
                vea.endComplexRow(getName());
481
                ViewPort vp=CADExtension.getEditionManager().getMapControl().getViewPort();
482
                BufferedImage selectionImage = new BufferedImage(vp.getImageWidth(), vp.getImageHeight(), BufferedImage.TYPE_INT_ARGB);
483
                Graphics2D gs = selectionImage.createGraphics();
484
                //clearSelection();
485
                ArrayList selectedRowAux = new ArrayList();//vle.getSelectedRow();
486
                selectedRowAux.add(new DefaultRowEdited(df1, IRowEdited.STATUS_ADDED, vea.getInversedIndex(index1)));
487
                vle.setSelectionCache(selectedRowAux);
488
                geom1.cloneGeometry().draw(gs, vp, DefaultCADTool.selectionSymbol);
489
                vle.drawHandlers(geom1.cloneGeometry(),gs,vp);
490
                vea.setSelectionImage(selectionImage);
491
            }else {
492
            IGeometry geom1=ShapeFactory.createPolyline2D(newGp1);
493
            IGeometry geom2=ShapeFactory.createPolyline2D(newGp2);
494
            VectorialLayerEdited vle = getVLE();
495
            VectorialEditableAdapter vea = vle.getVEA();
496
            String newFID;
497
//                        try {
498
                                newFID = vea.getNewFID();
499
//                        } catch (DriverException e) {
500
//                                throw new DriverIOException(e);
501
//                        }
502
            Value[] values=dre.getAttributes();
503
            DefaultFeature df1 = new DefaultFeature(geom1,values,newFID);
504
//            try {
505
                                newFID = vea.getNewFID();
506
//                        } catch (DriverException e) {
507
//                                throw new DriverIOException(e);
508
//                        }
509
            DefaultFeature df2 = new DefaultFeature(geom2, values,newFID);
510

    
511
            vea.startComplexRow();
512
            int index1=vea.addRow(df1,PluginServices.getText(this,"parte1"),EditionEvent.GRAPHIC);
513
            int index2=vea.addRow(df2,PluginServices.getText(this,"parte2"),EditionEvent.GRAPHIC);
514
            vea.removeRow(dre.getIndex(),getName(),EditionEvent.GRAPHIC);
515

    
516
            vea.endComplexRow(getName());
517
            ViewPort vp=CADExtension.getEditionManager().getMapControl().getViewPort();
518
            BufferedImage selectionImage = new BufferedImage(vp.getImageWidth(), vp.getImageHeight(), BufferedImage.TYPE_INT_ARGB);
519
            Graphics2D gs = selectionImage.createGraphics();
520
            //clearSelection();
521
            ArrayList selectedRowAux = new ArrayList();//vle.getSelectedRow();
522
            selectedRowAux.add(new DefaultRowEdited(df1, IRowEdited.STATUS_ADDED, vea.getInversedIndex(index1)));
523
            selectedRowAux.add(new DefaultRowEdited(df2, IRowEdited.STATUS_ADDED, vea.getInversedIndex(index2)));
524
            vle.setSelectionCache(selectedRowAux);
525
            geom1.cloneGeometry().draw(gs, vp, DefaultCADTool.selectionSymbol);
526
            vle.drawHandlers(geom1.cloneGeometry(),gs,vp);
527
            geom2.cloneGeometry().draw(gs, vp, DefaultCADTool.selectionSymbol);
528
            vle.drawHandlers(geom2.cloneGeometry(),gs,vp);
529
            vea.setSelectionImage(selectionImage);
530
            }
531
    }
532

    
533
    private Point2D[] getOrderPoints(IGeometry geomAux) {
534
            PathIterator theIterator=geomAux.getPathIterator(null,FConverter.FLATNESS);
535
            double[] theData = new double[6];
536
        Point2D previous=null;
537
        ArrayList points =new ArrayList();
538
        while (!theIterator.isDone()) {
539
            int theType = theIterator.currentSegment(theData);
540
            switch (theType) {
541

    
542
                case PathIterator.SEG_MOVETO:
543
                    previous=new Point2D.Double(theData[0], theData[1]);
544
                    break;
545

    
546
                case PathIterator.SEG_LINETO:
547

    
548
                    if (previous!=null){
549
                            GeneralPathX gpx=new GeneralPathX();
550
                            gpx.moveTo(previous.getX(),previous.getY());
551
                            gpx.lineTo(theData[0], theData[1]);
552
                            IGeometry geom=ShapeFactory.createPolyline2D(gpx);
553
                            boolean intersectFirst=intersects(geom,firstPoint);
554
                            boolean intersectSecond=intersects(geom,secondPoint);
555
                            if (intersectFirst && intersectSecond){
556
                                points.add(getNearPoint(previous));
557
                                points.add(getDistantPoint(previous));
558
                                return (Point2D[])points.toArray(new Point2D[0]);
559
                            }
560
                            if (intersectFirst) {
561
                                points.add(firstPoint);
562
                            }
563
                            if (intersectSecond) {
564
                                points.add(secondPoint);
565
                            }
566
                        }
567
                     previous=new Point2D.Double(theData[0], theData[1]);
568
                                      break;
569

    
570
                case PathIterator.SEG_QUADTO:
571
                     if (previous!=null){
572
                            GeneralPathX gpx=new GeneralPathX();
573
                            gpx.moveTo(previous.getX(),previous.getY());
574
                            gpx.quadTo(theData[0], theData[1],theData[2], theData[3]);
575
                            IGeometry geom=ShapeFactory.createPolyline2D(gpx);
576
                            boolean intersectFirst=intersects(geom,firstPoint);
577
                            boolean intersectSecond=intersects(geom,secondPoint);
578
                            if (intersectFirst && intersectSecond){
579
                                points.add(getNearPoint(previous));
580
                                points.add(getDistantPoint(previous));
581
                                return (Point2D[])points.toArray(new Point2D[0]);
582
                            }
583
                            if (intersectFirst) {
584
                                points.add(firstPoint);
585
                            }
586
                            if (intersectSecond) {
587
                                points.add(secondPoint);
588
                            }
589
                     }
590
                     previous=new Point2D.Double(theData[0], theData[1]);
591

    
592
                    break;
593

    
594
                case PathIterator.SEG_CUBICTO:
595
                     if (previous!=null){
596
                            GeneralPathX gpx=new GeneralPathX();
597
                            gpx.moveTo(previous.getX(),previous.getY());
598
                            gpx.curveTo(theData[0], theData[1],theData[2], theData[3],theData[4], theData[5]);
599
                            IGeometry geom=ShapeFactory.createPolyline2D(gpx);
600
                            boolean intersectFirst=intersects(geom,firstPoint);
601
                            boolean intersectSecond=intersects(geom,secondPoint);
602
                            if (intersectFirst && intersectSecond){
603
                                points.add(getNearPoint(previous));
604
                                points.add(getDistantPoint(previous));
605
                                return (Point2D[])points.toArray(new Point2D[0]);
606
                            }
607
                            if (intersectFirst) {
608
                                points.add(firstPoint);
609
                            }
610
                            if (intersectSecond) {
611
                                points.add(secondPoint);
612
                            }
613
                     }
614
                     previous=new Point2D.Double(theData[0], theData[1]);
615

    
616
                    break;
617

    
618
                case PathIterator.SEG_CLOSE:
619
                    //if (isFirstPart)
620
                    //        newGp1.closePath();
621
                    //else
622
                    //        newGp2.closePath();
623
                    break;
624
            } //end switch
625

    
626
            theIterator.next();
627
        } //end while loop
628

    
629
        return (Point2D[])points.toArray(new Point2D[0]);
630
    }
631
    private Point2D getDistantPoint(Point2D previous){
632
        if (firstPoint.distance(previous)>secondPoint.distance(previous)){
633
            return firstPoint;
634
        }
635
        return secondPoint;
636
    }
637
private Point2D getNearPoint(Point2D previous) {
638
        if (firstPoint.distance(previous)<=secondPoint.distance(previous)){
639
        return firstPoint;
640
    }
641
    return secondPoint;
642
}
643
    /**
644
     * M?todo para dibujar la lo necesario para el estado en el que nos
645
     * encontremos.
646
     *
647
     * @param g Graphics sobre el que dibujar.
648
     * @param x par?metro x del punto que se pase para dibujar.
649
     * @param y par?metro x del punto que se pase para dibujar.
650
     */
651
    public void drawOperation(Graphics g, double x, double y) {
652
         VectorialLayerEdited vle=getVLE();
653
         ArrayList selectedRows=vle.getSelectedRow();
654
         ViewPort vp=CADExtension.getEditionManager().getMapControl().getViewPort();
655
         if (selectedRows.size()==1){
656
             if (firstPoint!=null){
657
                 IGeometry g1=ShapeFactory.createCircle(firstPoint,vp.toMapDistance(3));
658
                 IGeometry g2=ShapeFactory.createCircle(firstPoint,vp.toMapDistance(5));
659
                 g1.draw((Graphics2D)g,vp,DefaultCADTool.axisReferencesSymbol);
660
                 g2.draw((Graphics2D)g,vp,DefaultCADTool.axisReferencesSymbol);
661
             }
662
             rowEdited = (DefaultRowEdited)((DefaultRowEdited)selectedRows.get(0));
663
                 //.getLinkedRow()
664
                 //        .cloneRow();
665
             IGeometry geom=((DefaultFeature)rowEdited.getLinkedRow()).getGeometry().cloneGeometry();
666
             if (intersects(geom,new Point2D.Double(x,y))){
667
                 geom.draw((Graphics2D)g,vp,DefaultCADTool.geometrySelectSymbol);
668
             }
669
         }
670
    }
671
    public boolean intersects(double x,double y){
672
        Point2D p = new Point2D.Double(x, y);
673
        VectorialLayerEdited vle = getVLE();
674
        ArrayList selectedRows = vle.getSelectedRow();
675
        if (selectedRows.size() == 1) {
676
            rowEdited = (DefaultRowEdited) ((DefaultRowEdited) selectedRows
677
                    .get(0));
678
            IGeometry g = ((DefaultFeature) rowEdited.getLinkedRow())
679
                    .getGeometry().cloneGeometry();
680
            return intersects(g, p);
681
        }
682
        return false;
683
    }
684
    private boolean intersects(IGeometry geom, Point2D p){
685
        double tol = 1;
686
        tol = CADExtension.getEditionManager().getMapControl().getViewPort()
687
                .toMapDistance((int) tol);
688
        Rectangle2D r = new Rectangle2D.Double(p.getX() - tol / 2, p.getY()
689
                - tol / 2, tol, tol);
690
        return (geom.intersects(r) && !geom.contains(r));
691
    }
692
    /**
693
     * Add a diferent option.
694
     *
695
     * @param s Diferent option.
696
     */
697
    public void addOption(String s) {
698
        if (s.equals(PluginServices.getText(this,"cancel"))|| s.equals("c")|| s.equals("C")){
699
            init();
700
        }
701
    }
702
    /* (non-Javadoc)
703
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
704
     */
705
    public void addValue(double d) {
706
    }
707
    public String getName() {
708
        return PluginServices.getText(this,"break_");
709
    }
710

    
711
    public String toString() {
712
        return "_break";
713
    }
714

    
715
    public boolean isApplicable(int shapeType) {
716
        switch (shapeType) {
717
        case FShape.MULTI:
718
        case FShape.LINE:
719
            return true;
720
        }
721
        return false;
722
    }
723

    
724

    
725
}