Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / MatrixCADTool.java @ 39127

History | View | Annotate | Download (18.8 KB)

1 37138 cordinyana
/* gvSIG. Geographic Information System of the Valencian Government
2 8937 caballero
 *
3 37138 cordinyana
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6 8937 caballero
 * 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 2
9
 * of the License, or (at your option) any later version.
10 37138 cordinyana
 *
11 8937 caballero
 * 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 37138 cordinyana
 *
16 8937 caballero
 * 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 37138 cordinyana
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21 8937 caballero
 */
22 29616 jpiera
package org.gvsig.editing.gui.cad.tools;
23 8937 caballero
24
import java.awt.event.InputEvent;
25
import java.awt.geom.AffineTransform;
26
import java.awt.geom.Point2D;
27
import java.awt.geom.Rectangle2D;
28
import java.util.ArrayList;
29
30 29616 jpiera
import org.gvsig.andami.PluginServices;
31
import org.gvsig.editing.CADExtension;
32
import org.gvsig.editing.gui.cad.DefaultCADTool;
33
import org.gvsig.editing.gui.cad.exception.CommandException;
34
import org.gvsig.editing.gui.cad.panels.matrix.MatrixOperations;
35
import org.gvsig.editing.gui.cad.panels.matrix.MatrixProperty;
36
import org.gvsig.editing.gui.cad.tools.smc.MatrixCADToolContext;
37
import org.gvsig.editing.gui.cad.tools.smc.MatrixCADToolContext.MatrixCADToolState;
38
import org.gvsig.editing.layers.VectorialLayerEdited;
39 24500 jmvivo
import org.gvsig.fmap.dal.exception.DataException;
40
import org.gvsig.fmap.dal.exception.ReadException;
41 24490 jmvivo
import org.gvsig.fmap.dal.feature.EditableFeature;
42
import org.gvsig.fmap.dal.feature.Feature;
43
import org.gvsig.fmap.dal.feature.FeatureSelection;
44
import org.gvsig.fmap.dal.feature.FeatureSet;
45
import org.gvsig.fmap.dal.feature.FeatureStore;
46 21668 vcaballero
import org.gvsig.fmap.geom.Geometry;
47
import org.gvsig.fmap.geom.handler.Handler;
48 30335 jpiera
import org.gvsig.fmap.geom.primitive.Curve;
49 39055 jldominguez
import org.gvsig.fmap.geom.primitive.Envelope;
50 21668 vcaballero
import org.gvsig.fmap.geom.primitive.GeneralPathX;
51 37328 cordinyana
import org.gvsig.fmap.geom.type.GeometryType;
52 21668 vcaballero
import org.gvsig.fmap.mapcontext.ViewPort;
53 39055 jldominguez
import org.gvsig.fmap.mapcontext.layers.SpatialCache;
54
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
55 30335 jpiera
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
56 34696 cordinyana
import org.gvsig.tools.dispose.DisposableIterator;
57 21668 vcaballero
58 8937 caballero
/**
59
 * Herramienta para crear una matriz de geometr?as.
60 37138 cordinyana
 *
61 8937 caballero
 * @author Vicente Caballero Navarro
62
 */
63
public class MatrixCADTool extends DefaultCADTool {
64 15954 vcaballero
65 37138 cordinyana
    protected MatrixCADToolContext _fsm;
66
    protected Point2D firstPoint;
67
    protected Point2D secondPoint;
68
    protected MatrixProperty matrixProperty = null;
69
    protected MatrixOperations operations = null;
70
    protected String option;
71 21668 vcaballero
72 37138 cordinyana
    /**
73
     * Crea un nuevo MatrixCADTool.
74
     */
75
    public MatrixCADTool() {
76
        matrixProperty = new MatrixProperty();
77
        operations = new MatrixOperations();
78
    }
79 8937 caballero
80 37138 cordinyana
    /**
81
     * M?todo de inicio, para poner el c?digo de todo lo que se requiera de una
82
     * carga previa a la utilizaci?n de la herramienta.
83
     */
84
    public void init() {
85
        _fsm = new MatrixCADToolContext(this);
86
    }
87 8937 caballero
88 37138 cordinyana
    /*
89
     * (non-Javadoc)
90
     *
91
     * @see
92
     * com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
93
     * .layers.FBitSet,
94
     * double, double)
95
     */
96
    public void transition(double x, double y, InputEvent event) {
97
        _fsm.addPoint(x, y, event);
98
    }
99 8937 caballero
100 37138 cordinyana
    /*
101
     * (non-Javadoc)
102
     *
103
     * @see
104
     * com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
105
     * .layers.FBitSet,
106
     * double)
107
     */
108
    public void transition(double d) {
109
        _fsm.addValue(d);
110
    }
111 8937 caballero
112 37138 cordinyana
    /*
113
     * (non-Javadoc)
114
     *
115
     * @see
116
     * com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
117
     * .layers.FBitSet,
118
     * java.lang.String)
119
     */
120
    public void transition(String s) throws CommandException {
121
        if (!super.changeCommand(s)) {
122
            _fsm.addOption(s);
123
        }
124
    }
125 8937 caballero
126 37138 cordinyana
    /**
127
     * DOCUMENT ME!
128
     */
129
    public void selection() {
130
        FeatureSet selection = null;
131
        try {
132
            selection = (FeatureSet) getVLE().getFeatureStore().getSelection();
133 8937 caballero
134 37138 cordinyana
            if (selection.getSize() == 0
135
                && !CADExtension
136
                    .getCADTool()
137
                    .getClass()
138
                    .getName()
139
                    .equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
140
                CADExtension.setCADTool("_selection", false);
141
                ((SelectionCADTool) CADExtension.getCADTool())
142
                    .setNextTool("_matrix");
143
            } else {
144
                // init();
145
                matrixPropeties();
146
            }
147
        } catch (ReadException e) {
148
            // TODO Auto-generated catch block
149
            e.printStackTrace();
150
        } catch (DataException e) {
151
            // TODO Auto-generated catch block
152
            e.printStackTrace();
153
        }
154
    }
155 24429 vcaballero
156 37138 cordinyana
    private void matrixPropeties() {
157
        matrixProperty.setMatrixCADTool(this);
158
        PluginServices.getMDIManager().addWindow(matrixProperty);
159
        endMatrix();
160 8937 caballero
161 37138 cordinyana
    }
162 23424 vcaballero
163 37138 cordinyana
    public void endMatrix() {
164
        if (operations.isAccepted()) {
165
            PluginServices.getMDIManager().setWaitCursor();
166 8937 caballero
167 37138 cordinyana
            // ArrayList selectedRow = getSelectedRows();
168
            ArrayList selectedRowAux = new ArrayList();
169
            VectorialLayerEdited vle = getVLE();
170
            FeatureStore featureStore = null;
171
            DisposableIterator iterator = null;
172
            try {
173
                featureStore = vle.getFeatureStore();
174 24429 vcaballero
175 37138 cordinyana
                featureStore.beginEditingGroup(getName());
176
                iterator =
177
                    ((FeatureSelection) featureStore.getSelection()).iterator();
178
                while (iterator.hasNext()) {
179
                    Feature feature = (Feature) iterator.next();
180
                    // Object[] attributes=new Object[feature.size()];
181
                    // for (int j = 0; j < feature.size(); j++) {
182
                    // attributes[j]=feature.get(j);
183
                    // }
184
                    if (operations.isRectangular()) {// Si es rectangular la
185
                                                     // matriz
186 8937 caballero
187 37138 cordinyana
                        for (int columns = 0; columns < operations
188
                            .getNumColumns(); columns++) {
189 8937 caballero
190 37138 cordinyana
                            for (int rows = 0; rows < operations.getNumRows(); rows++) {
191
                                if (columns == 0 && rows == 0) {
192
                                    continue;
193
                                }
194 8937 caballero
195 37138 cordinyana
                                // DefaultFeature feaCloned = (DefaultFeature)
196
                                // fea
197
                                // .cloneRow();
198
                                Geometry geom =
199
                                    (feature.getDefaultGeometry())
200
                                        .cloneGeometry();
201 39055 jldominguez
                                Envelope original_env = geom.getEnvelope();
202 37138 cordinyana
                                Geometry g =
203
                                    createPoint(
204 39055 jldominguez
                                        original_env.getMinimum(0)
205 37138 cordinyana
                                            + operations.getDistColumns()
206 39055 jldominguez
                                            * columns, original_env.getMinimum(1)
207 37138 cordinyana
                                            + operations.getDistRows() * rows);
208
                                AffineTransform at = new AffineTransform();
209
                                at.rotate(
210
                                    Math.toRadians(operations.getRotation()),
211 39055 jldominguez
                                    original_env.getMinimum(0),
212
                                    original_env.getMinimum(1));
213 37138 cordinyana
                                g.transform(at);
214 39055 jldominguez
                                Envelope new_env = g.getEnvelope();
215 37138 cordinyana
                                Point2D pDest =
216 39055 jldominguez
                                    new Point2D.Double(
217
                                        new_env.getMinimum(0),
218
                                        new_env.getMinimum(1));
219 8937 caballero
220 39055 jldominguez
                                double difX = pDest.getX() - original_env.getMinimum(0);
221
                                double difY = pDest.getY() - original_env.getMinimum(1);
222 37138 cordinyana
                                Handler[] handlers =
223
                                    geom.getHandlers(Geometry.SELECTHANDLER);
224
                                for (int j = 0; j < handlers.length; j++) {
225
                                    Handler h = handlers[j];
226
                                    Point2D p = h.getPoint();
227
                                    h.set(p.getX() + (difX), p.getY() + (difY));
228
                                }
229
                                EditableFeature eFeature =
230
                                    featureStore.createNewFeature(
231
                                        feature.getType(), feature);
232
                                eFeature.setGeometry(featureStore
233
                                    .getDefaultFeatureType()
234
                                    .getDefaultGeometryAttributeName(), geom);
235
                                selectedRowAux.add(eFeature);
236
                            }
237 8937 caballero
238 37138 cordinyana
                        }
239 8937 caballero
240 37138 cordinyana
                    } else { // Polar
241 8937 caballero
242 37138 cordinyana
                        double rotation = 360 / operations.getNum();
243 8937 caballero
244 37138 cordinyana
                        for (int numElem = 0; numElem < operations.getNum(); numElem++) {
245
                            System.out.println("numElem = " + numElem);
246
                            if (numElem == 0) {
247
                                continue;
248
                            }
249 8937 caballero
250 37138 cordinyana
                            // DefaultFeature feaCloned = (DefaultFeature) fea
251
                            // .cloneRow();
252
                            Geometry geom =
253
                                (feature.getDefaultGeometry()).cloneGeometry();
254 8937 caballero
255 37138 cordinyana
                            if (!operations.isRotateElements()) {
256 39055 jldominguez
                                Envelope orig_env = geom.getEnvelope();
257 37138 cordinyana
                                Geometry g =
258 39055 jldominguez
                                    createPoint(
259
                                        orig_env.getMinimum(0),
260
                                        orig_env.getMinimum(1));
261 37138 cordinyana
                                AffineTransform at = new AffineTransform();
262
                                at.rotate(Math.toRadians(rotation * numElem),
263
                                    operations.getPositionX(),
264
                                    operations.getPositionY());
265
                                g.transform(at);
266 39055 jldominguez
267
                                Envelope new_env = g.getEnvelope();
268
269 37138 cordinyana
                                Point2D pDest =
270 39055 jldominguez
                                    new Point2D.Double(
271
                                        new_env.getMinimum(0),
272
                                        new_env.getMinimum(1));
273 8937 caballero
274 39055 jldominguez
                                double difX = pDest.getX() - orig_env.getMinimum(0);
275
                                double difY = pDest.getY() - orig_env.getMinimum(1);
276 37138 cordinyana
                                Handler[] handlers =
277
                                    geom.getHandlers(Geometry.SELECTHANDLER);
278
                                for (int j = 0; j < handlers.length; j++) {
279
                                    Handler h = handlers[j];
280
                                    Point2D p = h.getPoint();
281
                                    h.set(p.getX() + (difX), p.getY() + (difY));
282
                                }
283
                            } else {// Cuando los elemtos rotan al mismo tiempo
284
                                    // que se van a?adiendo.
285 8937 caballero
286 39055 jldominguez
                                Envelope orig_env = geom.getEnvelope();
287 37138 cordinyana
                                AffineTransform at = new AffineTransform();
288
                                at.rotate(Math.toRadians(rotation * numElem),
289
                                    operations.getPositionX(),
290
                                    operations.getPositionY());
291
                                geom.transform(at);
292 39055 jldominguez
293
                                Envelope new_env = geom.getEnvelope();
294
295 37138 cordinyana
                                Point2D pDest =
296 39055 jldominguez
                                    new Point2D.Double(
297
                                        new_env.getMinimum(0),
298
                                        new_env.getMinimum(1)
299
                                        );
300 24429 vcaballero
301 39055 jldominguez
                                double difX = pDest.getX() - orig_env.getMinimum(0);
302
                                double difY = pDest.getY() - orig_env.getMinimum(1);
303 37138 cordinyana
                                Handler[] handlers =
304
                                    geom.getHandlers(Geometry.SELECTHANDLER);
305
                                for (int j = 0; j < handlers.length; j++) {
306
                                    Handler h = handlers[j];
307
                                    Point2D p = h.getPoint();
308
                                    h.set(p.getX() + (difX), p.getY() + (difY));
309
                                }
310
                            }
311
                            EditableFeature eFeature =
312
                                featureStore.createNewFeature(
313
                                    feature.getType(), feature);
314
                            eFeature.setGeometry(featureStore
315
                                .getDefaultFeatureType()
316
                                .getDefaultGeometryAttributeName(), geom);
317 8937 caballero
318 37138 cordinyana
                            selectedRowAux.add(eFeature);
319
                        }
320
                    }
321
                }
322 39055 jldominguez
323
                // =================================================
324
                /*
325
                 * Inserting geometries of matrix
326
                 */
327
                SpatialCache spatialCache =
328
                    ((FLyrVect) vle.getLayer()).getSpatialCache();
329
330
                for (int i=0; i<selectedRowAux.size(); i++) {
331
                    EditableFeature efeat = (EditableFeature) selectedRowAux.get(i);
332
                    featureStore.insert(efeat);
333
                    Geometry ngeom = efeat.getDefaultGeometry();
334
                    Envelope envelope = ngeom.getEnvelope();
335
                    /*
336
                     * Updating spatial cache
337
                     */
338
                    spatialCache.insert(envelope, ngeom);
339
                }
340
341
                // =================================================
342
343 37138 cordinyana
                featureStore.endEditingGroup();
344
                // vle.setSelectionCache(VectorialLayerEdited.SAVEPREVIOUS,
345
                // selectedRowAux);
346
                PluginServices.getMDIManager().restoreCursor();
347
                end();
348
            } catch (DataException e) {
349
                // TODO Auto-generated catch block
350
                e.printStackTrace();
351
            } finally {
352
                if (iterator != null) {
353
                    iterator.dispose();
354
                }
355
            }
356
        } else {// Cancelado
357 8937 caballero
358 37138 cordinyana
        }
359 8937 caballero
360 37138 cordinyana
    }
361 8937 caballero
362 37138 cordinyana
    /**
363
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
364
     * editableFeatureSource que ya estar? creado.
365
     *
366
     * @param x
367
     *            par?metro x del punto que se pase en esta transici?n.
368
     * @param y
369
     *            par?metro y del punto que se pase en esta transici?n.
370
     */
371
    public void addPoint(double x, double y, InputEvent event) {
372
        // MatrixCADToolState actualState = _fsm.getState();
373
        MatrixCADToolState previousState =
374
            (MatrixCADToolState) _fsm.getPreviousState();
375
        String status = previousState.getName();
376
        if (status.equals("Matrix.Start") || status.equals("Matrix.FirstPoint")) {
377
            firstPoint = new Point2D.Double(x, y);
378
        } else
379
            if (status.equals("Matrix.SecondPoint")) {
380
                secondPoint = new Point2D.Double(x, y);
381
                if (option.equals("lagX") || option.equals("lagXY")) {
382
                    operations.setDistColumns(secondPoint.getX()
383
                        - firstPoint.getX());
384
                    matrixProperty.refreshLagX();
385
                }
386
                if (option.equals("lagY") || option.equals("lagXY")) {
387
                    operations.setDistRows(secondPoint.getY()
388
                        - firstPoint.getY());
389
                    matrixProperty.refreshLagY();
390
                }
391
                if (option.equals("rotation")) {
392 8937 caballero
393 37138 cordinyana
                    double w;
394
                    double h;
395
                    w = secondPoint.getX() - firstPoint.getX();
396
                    h = secondPoint.getY() - firstPoint.getY();
397
                    operations.setRotation((-Math.atan2(w, h) + (Math.PI / 2))
398
                        * 180 / Math.PI);
399
                    matrixProperty.refreshRotation();
400
                }
401
                firstPoint = null;
402
                PluginServices.getMDIManager().addWindow(matrixProperty);
403
            }
404
    }
405 8937 caballero
406 37138 cordinyana
    /**
407
     * M?todo para dibujar lo necesario para el estado en el que nos
408
     * encontremos.
409
     *
410
     * @param g
411
     *            Graphics sobre el que dibujar.
412
     * @param x
413
     *            par?metro x del punto que se pase para dibujar.
414
     * @param y
415
     *            par?metro x del punto que se pase para dibujar.
416
     */
417
    public void drawOperation(MapControlDrawer renderer, double x, double y) {
418
        if (_fsm == null || firstPoint == null) {
419
            return;
420
        }
421
        GeneralPathX gpx = new GeneralPathX();
422
        gpx.moveTo(firstPoint.getX(), firstPoint.getY());
423
        gpx.lineTo(x, y);
424
        VectorialLayerEdited vle = getVLE();
425
        ViewPort vp = vle.getLayer().getMapContext().getViewPort();
426 8937 caballero
427 37138 cordinyana
        Curve curve = createCurve(gpx);
428
        renderer.draw(curve, mapControlManager.getAxisReferenceSymbol());
429
    }
430 8937 caballero
431 37138 cordinyana
    /**
432
     * Add a diferent option.
433
     *
434
     * @param s
435
     *            Diferent option.
436
     */
437
    public void addOption(String s) {
438
        option = s;
439
        PluginServices.getMDIManager().closeWindow(matrixProperty);
440
        /*
441
         * MatrixCADToolState actualState = _fsm .getState(); String status =
442
         * actualState.getName();
443
         *
444
         * if (status.equals("Matrix.LagXY")) {
445
         *
446
         * }else if (status.equals("Matrix.LagX")) {
447
         *
448
         * }else if (status.equals("Matrix.LagY")) {
449
         * }
450
         */
451
    }
452 8937 caballero
453 37138 cordinyana
    /*
454
     * (non-Javadoc)
455
     *
456
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
457
     */
458
    public void addValue(double d) {
459 8937 caballero
460 37138 cordinyana
    }
461 8937 caballero
462 37138 cordinyana
    public String getName() {
463
        return PluginServices.getText(this, "matrix_");
464
    }
465 8937 caballero
466 37138 cordinyana
    public String toString() {
467
        return "_matrix";
468
    }
469
470
    public MatrixOperations getOperations() {
471
        return operations;
472
    }
473
474 37328 cordinyana
    @Override
475
    public boolean isApplicable(GeometryType geometryType) {
476
        return true;
477
    }
478
479
    @Override
480
    protected int[] getSupportedGeometryTypes() {
481
        return null;
482
    }
483 8937 caballero
}