Statistics
| Revision:

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

History | View | Annotate | Download (10.2 KB)

1 6063 caballero
/* 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.io.IOException;
48
import java.util.ArrayList;
49
50
import com.iver.andami.PluginServices;
51
import com.iver.cit.gvsig.CADExtension;
52
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
53
import com.iver.cit.gvsig.fmap.core.FShape;
54
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
55
import com.iver.cit.gvsig.fmap.core.IGeometry;
56
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
57
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
58
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
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.UtilFunctions;
63
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
64
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
65
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
66
import com.iver.cit.gvsig.gui.cad.tools.smc.ExtendCADToolContext;
67
import com.iver.cit.gvsig.gui.cad.tools.smc.ExtendCADToolContext.ExtendCADToolState;
68
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
69
70
71
/**
72
 * DOCUMENT ME!
73
 *
74
 * @author Vicente Caballero Navarro
75
 */
76
public class ExtendCADTool extends DefaultCADTool {
77
    private ExtendCADToolContext _fsm;
78
79
    /**
80
     * Crea un nuevo ExtendCADTool.
81
     */
82
    public ExtendCADTool() {
83
    }
84
85
    /**
86
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
87
     * carga previa a la utilizaci?n de la herramienta.
88
     */
89
    public void init() {
90
        _fsm = new ExtendCADToolContext(this);
91
    }
92
93
    /* (non-Javadoc)
94
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
95
     */
96
    public void transition(double x, double y, InputEvent event) {
97
        _fsm.addPoint(x, y, event);
98
    }
99
100
    /* (non-Javadoc)
101
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
102
     */
103
    public void transition(double d) {
104
        _fsm.addValue(d);
105
    }
106
107
    /* (non-Javadoc)
108
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
109
     */
110
    public void transition(String s) throws CommandException {
111
            if (!super.changeCommand(s)){
112
                    _fsm.addOption(s);
113
            }
114
    }
115
116
    /**
117
     * DOCUMENT ME!
118
     */
119
    public void selection() {
120
        ArrayList rowSelected=getSelectedRows();
121
        if (rowSelected.size() == 0 && !CADExtension.getCADTool().getClass().getName().equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
122
            CADExtension.setCADTool("_selection",false);
123
            ((SelectionCADTool) CADExtension.getCADTool()).setNextTool(
124
                "_extend");
125
        }
126
    }
127
128
    /**
129
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
130
     * editableFeatureSource que ya estar? creado.
131
     *
132
     * @param x par?metro x del punto que se pase en esta transici?n.
133
     * @param y par?metro y del punto que se pase en esta transici?n.
134
     */
135
    public void addPoint(double x, double y,InputEvent event) {
136
        ExtendCADToolState actualState = (ExtendCADToolState) _fsm.getPreviousState();
137
        String status = actualState.getName();
138
139
140
        if (status.equals("Extend.SelectGeometryToExtend")) {
141
142
                VectorialLayerEdited vle=getVLE();
143
                    VectorialEditableAdapter vea=vle.getVEA();
144
                    vea.startComplexRow();
145
                    ArrayList selectedRow=getSelectedRows();
146
                    ArrayList selectedRowAux=new ArrayList();
147
                    //for (int i=0;i<selectedRow.size();i++) {
148
                            selectedRowAux.addAll(selectedRow);
149
                    //}
150
                    //selection();
151
                    vle.selectWithPoint(x,y,false);
152
                    ArrayList newSelectedRow=getSelectedRows();
153
                    try {
154
                    for (int i=0;i<selectedRowAux.size();i++) {
155
                    IRowEdited edRow1 = (IRowEdited) selectedRowAux.get(i);
156
                    DefaultFeature fea1 = (DefaultFeature) edRow1.getLinkedRow().cloneRow();
157
                        IGeometry geometry1 = null;
158
                        geometry1 = fea1.getGeometry();
159
160
                        IRowEdited edRow2 = (IRowEdited) newSelectedRow.get(i);
161
                    DefaultFeature fea2 = (DefaultFeature) edRow2.getLinkedRow().cloneRow();
162
                        IGeometry geometry2 = null;
163
                        geometry2 = fea2.getGeometry();
164
                    //for (int j=0;j<newSelectedRow.size();j++) {
165
                            //if (geometry1 instanceof FPolygon2D) {
166
                                    fea2.setGeometry(intersectsGeometry(geometry2,geometry1));
167
                            //}
168
                    //}
169
170
                                vea.modifyRow(edRow2.getIndex(),fea2,getName(),EditionEvent.GRAPHIC);
171
                        clearSelection();
172
                        newSelectedRow.add(new DefaultRowEdited(fea2,IRowEdited.STATUS_MODIFIED,edRow2.getIndex()));
173
                    }
174
175 6071 caballero
                        vea.endComplexRow(getName());
176 8938 caballero
                        vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS, newSelectedRow);
177 6063 caballero
        } catch (IOException e) {
178
                        e.printStackTrace();
179
                } catch (DriverIOException e) {
180
                        e.printStackTrace();
181
                }
182
        }
183
    }
184
185
    private IGeometry intersectsGeometry(IGeometry geometry1, IGeometry geometry2) {
186
            Point2D p3=null;
187
        Point2D p4=null;
188
            Point2D p1=null;
189
        Point2D p2=null;
190
                GeneralPathX gpx=new GeneralPathX();
191 7072 caballero
                PathIterator theIterator=geometry1.getInternalShape().getPathIterator(null,FConverter.FLATNESS);
192 6063 caballero
                boolean first=true;
193
                double[] theData = new double[6];
194
        int theType;
195
                        while (!theIterator.isDone()) {
196
                theType = theIterator.currentSegment(theData);
197
                switch (theType) {
198
199
                    case PathIterator.SEG_MOVETO:
200
                            p1=new Point2D.Double(theData[0], theData[1]);
201
                        gpx.moveTo(p1.getX(),p1.getY());
202
                            break;
203
204
                    case PathIterator.SEG_LINETO:
205
                            p2=new Point2D.Double(theData[0], theData[1]);
206
                            ArrayList lines=getLines(geometry2);
207
                            boolean isLineTo=true;
208
                            for (int i=0;i<lines.size();i++) {
209
                                    Point2D[] ps1=(Point2D[])lines.get(i);
210
                                    Point2D p=UtilFunctions.getIntersection(ps1[0],ps1[1],p1,p2);
211
//                                    GeneralPathX gpxAux=new GeneralPathX();
212
//                                    gpxAux.moveTo(p.getX(),p.getY());
213
//                                    gpxAux.lineTo(ps1[0].getX(),ps1[0].getY());
214
//                                    Geometry gjts1=FConverter.java2d_to_jts((FShape)ShapeFactory.createPolyline2D(gpxAux).getInternalShape());
215
//                                    Geometry gjts2=FConverter.java2d_to_jts((FShape)geometry2.getInternalShape());
216
//                                    GeometryCollection result=(GeometryCollection)gjts1.intersection(gjts2);
217
//                                    Point point=(Point)result.getGeometryN(1);
218
//                                    p=new Point2D.Double(point.getX(),point.getY());
219
                                    if (p!=null && first) {
220
                                            gpx.lineTo(p.getX(),p.getY());
221
                                            first=false;
222
                                            isLineTo=false;
223
                                            break;
224
                                    }else {
225
                                            //gpx.lineTo(p2.getX(),p2.getY());
226
                                    }
227
                            }
228
                            if (!first && isLineTo)
229
                                    gpx.lineTo(p2.getX(),p2.getY());
230
231
                            break;
232
233
                } //end switch
234
235
                theIterator.next();
236
            } //end while loop
237
                        return ShapeFactory.createPolyline2D(gpx);
238
        }
239
240
241
        private ArrayList getLines(IGeometry geometry1) {
242
                Point2D p1=null;
243
        Point2D p2=null;
244
                ArrayList lines=new ArrayList();
245 7072 caballero
            PathIterator theIterator=geometry1.getInternalShape().getPathIterator(null,FConverter.FLATNESS);
246 6063 caballero
                double[] theData = new double[6];
247
        int theType;
248
                        while (!theIterator.isDone()) {
249
                theType = theIterator.currentSegment(theData);
250
                switch (theType) {
251
                    case PathIterator.SEG_MOVETO:
252
                            p1=new Point2D.Double(theData[0], theData[1]);
253
                        break;
254
255
                    case PathIterator.SEG_LINETO:
256
                            p2=new Point2D.Double(theData[0], theData[1]);
257
                            lines.add(new Point2D[] {p1,p2});
258
                            break;
259
260
                } //end switch
261
262
                theIterator.next();
263
            } //end while loop
264
                return lines;
265
        }
266
        /**
267
     * M?todo para dibujar la lo necesario para el estado en el que nos
268
     * encontremos.
269
     *
270
     * @param g Graphics sobre el que dibujar.
271
     * @param x par?metro x del punto que se pase para dibujar.
272
     * @param y par?metro x del punto que se pase para dibujar.
273
     */
274
    public void drawOperation(Graphics g, double x, double y) {
275
        }
276
277
    /**
278
         * Add a diferent option.
279
         *
280
         * @param s
281
         *            Diferent option.
282
         */
283
    public void addOption(String s) {
284
    }
285
286
    /* (non-Javadoc)
287
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
288
     */
289
    public void addValue(double d) {
290
    }
291
292
    public String getName() {
293
                return PluginServices.getText(this,"extend_");
294
        }
295
296
        public String toString() {
297
                return "_extend";
298
        }
299
        public boolean isApplicable(int shapeType) {
300
                switch (shapeType) {
301
                case FShape.LINE:
302
                case FShape.MULTI:
303
                        return true;
304
                }
305
                return false;
306
        }
307
308
}