Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / JoinCADTool.java @ 24363

History | View | Annotate | Download (11.2 KB)

1 5984 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 15617 vcaballero
import java.awt.Component;
44 5984 caballero
import java.awt.Graphics;
45
import java.awt.Graphics2D;
46
import java.awt.event.InputEvent;
47
import java.awt.geom.Point2D;
48
import java.util.ArrayList;
49 23424 vcaballero
import java.util.Iterator;
50 5984 caballero
51 15617 vcaballero
import javax.swing.JOptionPane;
52
53 23754 jjdelcerro
import org.gvsig.fmap.data.exceptions.DataException;
54
import org.gvsig.fmap.data.exceptions.ReadException;
55 24265 vcaballero
import org.gvsig.fmap.data.feature.EditableFeature;
56 23424 vcaballero
import org.gvsig.fmap.data.feature.Feature;
57 24265 vcaballero
import org.gvsig.fmap.data.feature.FeatureSelection;
58 23424 vcaballero
import org.gvsig.fmap.data.feature.FeatureStore;
59 21668 vcaballero
import org.gvsig.fmap.geom.Geometry;
60
import org.gvsig.fmap.geom.GeometryFactory;
61
import org.gvsig.fmap.geom.GeometryManager;
62
import org.gvsig.fmap.geom.handler.Handler;
63 23424 vcaballero
import org.gvsig.fmap.geom.operation.Draw;
64
import org.gvsig.fmap.geom.operation.DrawOperationContext;
65
import org.gvsig.fmap.geom.operation.GeometryOperationException;
66
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
67 21668 vcaballero
import org.gvsig.fmap.geom.primitive.GeneralPathX;
68
import org.gvsig.fmap.geom.util.UtilFunctions;
69
import org.gvsig.fmap.mapcontext.ViewPort;
70
71 5984 caballero
import com.iver.andami.PluginServices;
72 12739 caballero
import com.iver.andami.messages.NotificationManager;
73 5984 caballero
import com.iver.cit.gvsig.CADExtension;
74
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
75
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
76
import com.iver.cit.gvsig.gui.cad.tools.smc.JoinCADToolContext;
77
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
78
79
80
/**
81
 * DOCUMENT ME!
82
 *
83
 * @author Vicente Caballero Navarro
84
 */
85
public class JoinCADTool extends DefaultCADTool {
86
        private JoinCADToolContext _fsm;
87
        private SelectionCADTool selectionCADTool;
88 21668 vcaballero
        private GeometryFactory geomFactory=GeometryManager.getInstance().getGeometryFactory();
89 5984 caballero
        /**
90
     * Crea un nuevo JoinCADTool.
91
     */
92
    public JoinCADTool() {
93
    }
94
95
    /**
96
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
97
     * carga previa a la utilizaci?n de la herramienta.
98
     */
99
    public void init() {
100
        _fsm = new JoinCADToolContext(this);
101 10626 caballero
        try {
102
                        clearSelection();
103 21668 vcaballero
                } catch (ReadException e) {
104 12739 caballero
                        NotificationManager.addError(e.getMessage(),e);
105 10626 caballero
                }
106 5984 caballero
        selectionCADTool=new SelectionCADTool();//(SelectionCADTool) CADExtension.getCADTool();
107
        selectionCADTool.init();
108
        selectionCADTool.multipleSelection(true);
109
    }
110
111
    /* (non-Javadoc)
112
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
113
     */
114
    public void transition(double x, double y, InputEvent event) {
115
        _fsm.addPoint(x, y, event);
116
    }
117
118
    /* (non-Javadoc)
119
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
120
     */
121
    public void transition(double d) {
122
        _fsm.addValue(d);
123
    }
124
125
    /* (non-Javadoc)
126
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
127
     */
128
    public void transition(String s) throws CommandException {
129
            if (!super.changeCommand(s)){
130
                    _fsm.addOption(s);
131
            }
132
    }
133
134
    /**
135
     * DOCUMENT ME!
136
     */
137
    public void selection() {
138
            if (!CADExtension.getCADTool().getClass().getName().equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
139
            CADExtension.setCADTool("_selection",false);
140
            ((SelectionCADTool)CADExtension.getCADTool()).multipleSelection(true);
141
            ((SelectionCADTool) CADExtension.getCADTool()).setNextTool(
142
                "_join");
143
        }
144
    }
145
146
    /**
147
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
148
     * editableFeatureSource que ya estar? creado.
149
     *
150
     * @param x par?metro x del punto que se pase en esta transici?n.
151
     * @param y par?metro y del punto que se pase en esta transici?n.
152
     */
153
    public void addPoint(double x, double y,InputEvent event) {
154
                selectionCADTool.selectFeatures(x,y,event);
155
    }
156
157
        /**
158
     * M?todo para dibujar la lo necesario para el estado en el que nos
159
     * encontremos.
160
     *
161
     * @param g Graphics sobre el que dibujar.
162
     * @param x par?metro x del punto que se pase para dibujar.
163
     * @param y par?metro x del punto que se pase para dibujar.
164
     */
165
    public void drawOperation(Graphics g, double x, double y) {
166
             VectorialLayerEdited vle=getVLE();
167 23424 vcaballero
             Iterator iterator=null;
168
                try {
169 24265 vcaballero
                        iterator = ((FeatureSelection)vle.getFeatureStore().getSelection()).iterator();
170 23424 vcaballero
                } catch (ReadException e1) {
171
                        // TODO Auto-generated catch block
172
                        e1.printStackTrace();
173 24265 vcaballero
                } catch (DataException e) {
174
                        // TODO Auto-generated catch block
175
                        e.printStackTrace();
176 23424 vcaballero
                }
177
//         ArrayList selectedRows=vle.getSelectedRow();
178 5984 caballero
         ViewPort vp=CADExtension.getEditionManager().getMapControl().getViewPort();
179 23424 vcaballero
//         DefaultRowEdited[] dres=(DefaultRowEdited[])selectedRows.toArray(new DefaultRowEdited[0]);
180
//         for (int i=0;i<dres.length;i++) {
181
        while (iterator.hasNext()) {
182
                        Feature feature = (Feature) iterator.next();
183
                        Geometry geom=((Geometry)feature.getDefaultGeometry()).cloneGeometry();
184
                        DrawOperationContext doc=new DrawOperationContext();
185
                        doc.setGraphics((Graphics2D)g);
186
                        doc.setViewPort(getCadToolAdapter().getMapControl().getViewPort());
187
                        doc.setSymbol(DefaultCADTool.geometrySelectSymbol);
188
                try {
189
                                geom.invokeOperation(Draw.CODE,doc);
190
                        } catch (GeometryOperationNotSupportedException e) {
191
                                e.printStackTrace();
192
                        } catch (GeometryOperationException e) {
193
                                e.printStackTrace();
194
                        }
195
//                        geom.draw((Graphics2D)g,vp,DefaultCADTool.geometrySelectSymbol);
196 5984 caballero
         }
197
    }
198
  /**
199
     * Add a diferent option.
200
     *
201
     * @param s Diferent option.
202
     */
203
    public void addOption(String s) {
204 9121 caballero
            if (s.equals(PluginServices.getText(this,"cancel"))){
205 5984 caballero
                    init();
206
            }else {
207
                    try {
208
                                joinGeometries();
209 21668 vcaballero
                        } catch (ReadException e) {
210 12739 caballero
                                NotificationManager.addError(e.getMessage(),e);
211 23424 vcaballero
                        } catch (DataException e) {
212 12739 caballero
                                NotificationManager.addError(e.getMessage(),e);
213 15617 vcaballero
                        }
214 5984 caballero
            }
215
    }
216 23424 vcaballero
    private Handler[] getHandlers() throws ReadException {
217 24265 vcaballero
            Iterator iterator = null;
218
                try {
219
                        iterator = ((FeatureSelection)getVLE().getFeatureStore().getSelection()).iterator();
220
                } catch (DataException e) {
221
                        // TODO Auto-generated catch block
222
                        e.printStackTrace();
223
                }
224 5984 caballero
            ArrayList handlers=new ArrayList();
225 23424 vcaballero
            while (iterator.hasNext()) {
226
                        Feature feature = (Feature) iterator.next();
227
                    Geometry geometry=(Geometry)feature.getDefaultGeometry();
228 21668 vcaballero
                    Handler[] hs=geometry.getHandlers(Geometry.SELECTHANDLER);
229 5984 caballero
                    for (int j=0;j<hs.length;j++) {
230
                            handlers.add(hs[j]);
231
                    }
232
            }
233
            return (Handler[])handlers.toArray(new Handler[0]);
234
    }
235
    private boolean isIntoRect(Point2D p,Handler[] handlers) {
236
            double tol=0.02;
237 11356 caballero
            double angle=Double.NEGATIVE_INFINITY;
238 5984 caballero
239
            //for (int i=0;i<handlers.length;i++) {
240
            //if (handlers.length>0) {
241
            Point2D p0=p;
242
                    for(int j=0;j<handlers.length;j++) {
243
                            Point2D p1Aux=handlers[j].getPoint();
244
                            if (!handlers[0].equalsPoint(handlers[j])) {
245
                                    double angleAux=UtilFunctions.getAngle(p0,p1Aux);
246 11356 caballero
                                    if (angle!=Double.NEGATIVE_INFINITY && (angle>angleAux+tol || angle<angleAux-tol)) {
247 5984 caballero
                                            return false;
248
                                    }
249
                                    angle=angleAux;
250
                            }
251
                    }
252
            //}
253
            return true;
254
    }
255
private Point2D[] startAndEndPoints(Handler[] handlers) {
256
        Point2D first=null;
257
        Point2D end=null;
258
        for (int i=0;i<handlers.length;i++) {
259
                Point2D aux=(Point2D)handlers[i].getPoint().clone();
260
                if (first == null || aux.getX()<first.getX()) {
261
                        first=aux;
262
                }
263
                if (end == null || aux.getX()>end.getX()) {
264
                        end=aux;
265
                }
266
        }
267
        if (first.getX()==end.getX()) {
268
                for (int i=0;i<handlers.length;i++) {
269
                        Point2D aux=(Point2D)handlers[i].getPoint().clone();
270
                        if (first == null || aux.getY()<first.getY()) {
271
                                first=aux;
272
                        }
273
                        if (end == null || aux.getY()>end.getY()) {
274
                                end=aux;
275
                        }
276
                }
277
        }
278
        return new Point2D[] {first,end};
279
}
280
281
282
283 23424 vcaballero
    private void joinGeometries() throws DataException{
284
285
//            DefaultRowEdited[] rows = (DefaultRowEdited[]) getSelectedRows()
286
//                                .toArray(new DefaultRowEdited[0]);
287
                Handler[] handlers = getHandlers();
288 5984 caballero
                if (handlers.length < 2) {
289
                        return;
290
                }
291
                Point2D[] points = startAndEndPoints(handlers);
292
293 15617 vcaballero
                if (!isIntoRect(points[0], handlers)){
294
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"no_se_pueden_unir_las_geometrias_al_no_estar_en_la_misma_linea"));
295 5984 caballero
                        return;
296 15617 vcaballero
                }
297 5984 caballero
298
                VectorialLayerEdited vle = getVLE();
299 23424 vcaballero
//                VectorialEditableAdapter vea = vle.getVEA();
300
                FeatureStore featureStore=vle.getFeatureStore();
301 24265 vcaballero
                featureStore.beginEditingGroup(getName());
302 5984 caballero
303 23424 vcaballero
//                String newFID = vea.getNewFID();
304 5984 caballero
                GeneralPathX gpx = new GeneralPathX();
305
                gpx.moveTo(points[0].getX(), points[0].getY());
306
                gpx.lineTo(points[1].getX(), points[1].getY());
307
308 21668 vcaballero
                Geometry geom = geomFactory.createPolyline2D(gpx);
309 24265 vcaballero
                EditableFeature eFeature=featureStore.createNewFeature();
310
                eFeature.setGeometry(featureStore.getDefaultFeatureType().getDefaultGeometryAttributeName(), geom);
311 23424 vcaballero
//                DefaultFeature df1 = new DefaultFeature(geom, null, newFID);
312 24265 vcaballero
                Iterator iterator=((FeatureSelection)featureStore.getSelection()).iterator();
313 23424 vcaballero
                while (iterator.hasNext()) {
314
                        Feature feature = (Feature) iterator.next();
315 5984 caballero
316 23424 vcaballero
//                }
317
//                for (int i = rows.length - 1; i >= 0; i--) {
318
                        featureStore.delete(feature);
319 5984 caballero
                }
320 24265 vcaballero
                featureStore.insert(eFeature);
321 23424 vcaballero
//                int index1 = vea.addRow(df1, PluginServices.getText(this, "parte1"),
322
//                                EditionEvent.GRAPHIC);
323 6008 caballero
                //clearSelection();
324
                ArrayList selectedRowAux = new ArrayList();
325 24265 vcaballero
                selectedRowAux.add(eFeature);
326 23424 vcaballero
//                vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS, selectedRowAux);
327 24265 vcaballero
                featureStore.endEditingGroup();
328 5984 caballero
329
        }
330
331
        /*
332
         * (non-Javadoc)
333
         *
334
         * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
335
         */
336
    public void addValue(double d) {
337
    }
338
    public String getName() {
339
                return PluginServices.getText(this,"join_");
340
        }
341
342
        public String toString() {
343
                return "_join";
344
        }
345
346
        public boolean isApplicable(int shapeType) {
347
                switch (shapeType) {
348 22036 vcaballero
                case Geometry.TYPES.GEOMETRY:
349 21668 vcaballero
                case Geometry.TYPES.CURVE:
350 5984 caballero
                        return true;
351
                }
352
                return false;
353
        }
354
355
356
}