Statistics
| Revision:

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

History | View | Annotate | Download (9.84 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.Point2D;
47
import java.util.ArrayList;
48

    
49
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
50
import com.iver.andami.PluginServices;
51
import com.iver.cit.gvsig.CADExtension;
52
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
53
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileWriteException;
54
import com.iver.cit.gvsig.exceptions.validate.ValidateRowException;
55
import com.iver.cit.gvsig.fmap.ViewPort;
56
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
57
import com.iver.cit.gvsig.fmap.core.FShape;
58
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
59
import com.iver.cit.gvsig.fmap.core.Handler;
60
import com.iver.cit.gvsig.fmap.core.IFeature;
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.edition.DefaultRowEdited;
64
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
65
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
66
import com.iver.cit.gvsig.fmap.edition.UtilFunctions;
67
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
68
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
69
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
70
import com.iver.cit.gvsig.gui.cad.tools.smc.JoinCADToolContext;
71
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
72

    
73

    
74
/**
75
 * DOCUMENT ME!
76
 *
77
 * @author Vicente Caballero Navarro
78
 */
79
public class JoinCADTool extends DefaultCADTool {
80
        private JoinCADToolContext _fsm;
81
        private SelectionCADTool selectionCADTool;
82
        /**
83
     * Crea un nuevo JoinCADTool.
84
     */
85
    public JoinCADTool() {
86
    }
87

    
88
    /**
89
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
90
     * carga previa a la utilizaci?n de la herramienta.
91
     */
92
    public void init() {
93
        _fsm = new JoinCADToolContext(this);
94
        try {
95
                        clearSelection();
96
                } catch (ReadDriverException e) {
97
                        e.printStackTrace();
98
                }
99
        selectionCADTool=new SelectionCADTool();//(SelectionCADTool) CADExtension.getCADTool();
100
        selectionCADTool.init();
101
        selectionCADTool.multipleSelection(true);
102
    }
103

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

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

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

    
127
    /**
128
     * DOCUMENT ME!
129
     */
130
    public void selection() {
131
            if (!CADExtension.getCADTool().getClass().getName().equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
132
            CADExtension.setCADTool("_selection",false);
133
            ((SelectionCADTool)CADExtension.getCADTool()).multipleSelection(true);
134
            ((SelectionCADTool) CADExtension.getCADTool()).setNextTool(
135
                "_join");
136
        }
137
    }
138

    
139
    /**
140
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
141
     * editableFeatureSource que ya estar? creado.
142
     *
143
     * @param x par?metro x del punto que se pase en esta transici?n.
144
     * @param y par?metro y del punto que se pase en esta transici?n.
145
     */
146
    public void addPoint(double x, double y,InputEvent event) {
147
                selectionCADTool.selectFeatures(x,y,event);
148
    }
149

    
150
        /**
151
     * M?todo para dibujar la lo necesario para el estado en el que nos
152
     * encontremos.
153
     *
154
     * @param g Graphics sobre el que dibujar.
155
     * @param x par?metro x del punto que se pase para dibujar.
156
     * @param y par?metro x del punto que se pase para dibujar.
157
     */
158
    public void drawOperation(Graphics g, double x, double y) {
159
             VectorialLayerEdited vle=getVLE();
160
         ArrayList selectedRows=vle.getSelectedRow();
161
         ViewPort vp=CADExtension.getEditionManager().getMapControl().getViewPort();
162
         DefaultRowEdited[] dres=(DefaultRowEdited[])selectedRows.toArray(new DefaultRowEdited[0]);
163
         for (int i=0;i<dres.length;i++) {
164
                 IGeometry geom=((IFeature)dres[i].getLinkedRow()).getGeometry().cloneGeometry();
165
                 geom.draw((Graphics2D)g,vp,DefaultCADTool.geometrySelectSymbol);
166
         }
167
    }
168
  /**
169
     * Add a diferent option.
170
     *
171
     * @param s Diferent option.
172
     */
173
    public void addOption(String s) {
174
            if (s.equals(PluginServices.getText(this,"cancel"))){
175
                    init();
176
            }else {
177
                    try {
178
                                joinGeometries();
179
                        } catch (ReadDriverException e) {
180
                                // TODO Auto-generated catch block
181
                                e.printStackTrace();
182
                        } catch (ExpansionFileReadException e) {
183
                                // TODO Auto-generated catch block
184
                                e.printStackTrace();
185
                        } catch (ValidateRowException e) {
186
                                // TODO Auto-generated catch block
187
                                e.printStackTrace();
188
                        } catch (ExpansionFileWriteException e) {
189
                                // TODO Auto-generated catch block
190
                                e.printStackTrace();
191
                        }
192
            }
193
    }
194
    private Handler[] getHandlers(DefaultRowEdited[] selectedRows) {
195
            ArrayList handlers=new ArrayList();
196
            for (int i=0;i<selectedRows.length;i++) {
197
                    IGeometry geometry=((IFeature)selectedRows[i].getLinkedRow()).getGeometry();
198
                    Handler[] hs=geometry.getHandlers(IGeometry.SELECTHANDLER);
199
                    for (int j=0;j<hs.length;j++) {
200
                            handlers.add(hs[j]);
201
                    }
202
            }
203
            return (Handler[])handlers.toArray(new Handler[0]);
204
    }
205
    private boolean isIntoRect(Point2D p,Handler[] handlers) {
206
            double tol=0.02;
207
            double angle=Double.NEGATIVE_INFINITY;
208

    
209
            //for (int i=0;i<handlers.length;i++) {
210
            //if (handlers.length>0) {
211
            Point2D p0=p;
212
                    for(int j=0;j<handlers.length;j++) {
213
                            Point2D p1Aux=handlers[j].getPoint();
214
                            if (!handlers[0].equalsPoint(handlers[j])) {
215
                                    double angleAux=UtilFunctions.getAngle(p0,p1Aux);
216
                                    if (angle!=Double.NEGATIVE_INFINITY && (angle>angleAux+tol || angle<angleAux-tol)) {
217
                                            return false;
218
                                    }
219
                                    angle=angleAux;
220
                            }
221
                    }
222
            //}
223
            return true;
224
    }
225
private Point2D[] startAndEndPoints(Handler[] handlers) {
226
        Point2D first=null;
227
        Point2D end=null;
228
        for (int i=0;i<handlers.length;i++) {
229
                Point2D aux=(Point2D)handlers[i].getPoint().clone();
230
                if (first == null || aux.getX()<first.getX()) {
231
                        first=aux;
232
                }
233
                if (end == null || aux.getX()>end.getX()) {
234
                        end=aux;
235
                }
236
        }
237
        if (first.getX()==end.getX()) {
238
                for (int i=0;i<handlers.length;i++) {
239
                        Point2D aux=(Point2D)handlers[i].getPoint().clone();
240
                        if (first == null || aux.getY()<first.getY()) {
241
                                first=aux;
242
                        }
243
                        if (end == null || aux.getY()>end.getY()) {
244
                                end=aux;
245
                        }
246
                }
247
        }
248
        return new Point2D[] {first,end};
249
}
250

    
251

    
252

    
253
    private void joinGeometries() throws ReadDriverException, ExpansionFileReadException, ValidateRowException, ExpansionFileWriteException {
254
            DefaultRowEdited[] rows = (DefaultRowEdited[]) getSelectedRows()
255
                                .toArray(new DefaultRowEdited[0]);
256
                Handler[] handlers = getHandlers(rows);
257
                if (handlers.length < 2) {
258
                        return;
259
                }
260
                Point2D[] points = startAndEndPoints(handlers);
261

    
262
                if (!isIntoRect(points[0], handlers))
263
                        return;
264

    
265
                VectorialLayerEdited vle = getVLE();
266
                VectorialEditableAdapter vea = vle.getVEA();
267
                vea.startComplexRow();
268

    
269
                String newFID = vea.getNewFID();
270
                GeneralPathX gpx = new GeneralPathX();
271
                gpx.moveTo(points[0].getX(), points[0].getY());
272
                gpx.lineTo(points[1].getX(), points[1].getY());
273

    
274
                IGeometry geom = ShapeFactory.createPolyline2D(gpx);
275
                DefaultFeature df1 = new DefaultFeature(geom, null, newFID);
276

    
277
                for (int i = rows.length - 1; i >= 0; i--) {
278
                        vea.removeRow(rows[i].getIndex(), getName(), EditionEvent.GRAPHIC);
279
                }
280
                int index1 = vea.addRow(df1, PluginServices.getText(this, "parte1"),
281
                                EditionEvent.GRAPHIC);
282
                //clearSelection();
283
                ArrayList selectedRowAux = new ArrayList();
284
                selectedRowAux.add(new DefaultRowEdited(df1, IRowEdited.STATUS_ADDED,
285
                                vea.getInversedIndex(index1)));
286
                vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS, selectedRowAux);
287
                vea.endComplexRow(getName());
288

    
289
        }
290

    
291
        /*
292
         * (non-Javadoc)
293
         *
294
         * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
295
         */
296
    public void addValue(double d) {
297
    }
298
    public String getName() {
299
                return PluginServices.getText(this,"join_");
300
        }
301

    
302
        public String toString() {
303
                return "_join";
304
        }
305

    
306
        public boolean isApplicable(int shapeType) {
307
                switch (shapeType) {
308
                case FShape.MULTI:
309
                case FShape.LINE:
310
                        return true;
311
                }
312
                return false;
313
        }
314

    
315

    
316
}