Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.editing.app / org.gvsig.editing.app.mainplugin / src / main / java / org / gvsig / editing / gui / cad / tools / MatrixCADTool.java @ 40557

History | View | Annotate | Download (18.8 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * 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 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * 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
 *
16
 * 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
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.editing.gui.cad.tools;
25

    
26
import java.awt.event.InputEvent;
27
import java.awt.geom.AffineTransform;
28
import java.awt.geom.Point2D;
29
import java.awt.geom.Rectangle2D;
30
import java.util.ArrayList;
31

    
32
import org.gvsig.andami.PluginServices;
33
import org.gvsig.editing.CADExtension;
34
import org.gvsig.editing.gui.cad.DefaultCADTool;
35
import org.gvsig.editing.gui.cad.exception.CommandException;
36
import org.gvsig.editing.gui.cad.panels.matrix.MatrixOperations;
37
import org.gvsig.editing.gui.cad.panels.matrix.MatrixProperty;
38
import org.gvsig.editing.gui.cad.tools.smc.MatrixCADToolContext;
39
import org.gvsig.editing.gui.cad.tools.smc.MatrixCADToolContext.MatrixCADToolState;
40
import org.gvsig.editing.layers.VectorialLayerEdited;
41
import org.gvsig.fmap.dal.exception.DataException;
42
import org.gvsig.fmap.dal.exception.ReadException;
43
import org.gvsig.fmap.dal.feature.EditableFeature;
44
import org.gvsig.fmap.dal.feature.Feature;
45
import org.gvsig.fmap.dal.feature.FeatureSelection;
46
import org.gvsig.fmap.dal.feature.FeatureSet;
47
import org.gvsig.fmap.dal.feature.FeatureStore;
48
import org.gvsig.fmap.geom.Geometry;
49
import org.gvsig.fmap.geom.handler.Handler;
50
import org.gvsig.fmap.geom.primitive.Curve;
51
import org.gvsig.fmap.geom.primitive.Envelope;
52
import org.gvsig.fmap.geom.primitive.GeneralPathX;
53
import org.gvsig.fmap.geom.type.GeometryType;
54
import org.gvsig.fmap.mapcontext.ViewPort;
55
import org.gvsig.fmap.mapcontext.layers.SpatialCache;
56
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
57
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
58
import org.gvsig.tools.dispose.DisposableIterator;
59

    
60
/**
61
 * Herramienta para crear una matriz de geometr?as.
62
 * 
63
 * @author Vicente Caballero Navarro
64
 */
65
public class MatrixCADTool extends DefaultCADTool {
66

    
67
    protected MatrixCADToolContext _fsm;
68
    protected Point2D firstPoint;
69
    protected Point2D secondPoint;
70
    protected MatrixProperty matrixProperty = null;
71
    protected MatrixOperations operations = null;
72
    protected String option;
73

    
74
    /**
75
     * Crea un nuevo MatrixCADTool.
76
     */
77
    public MatrixCADTool() {
78
        matrixProperty = new MatrixProperty();
79
        operations = new MatrixOperations();
80
    }
81

    
82
    /**
83
     * M?todo de inicio, para poner el c?digo de todo lo que se requiera de una
84
     * carga previa a la utilizaci?n de la herramienta.
85
     */
86
    public void init() {
87
        _fsm = new MatrixCADToolContext(this);
88
    }
89

    
90
    /*
91
     * (non-Javadoc)
92
     * 
93
     * @see
94
     * com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
95
     * .layers.FBitSet,
96
     * double, double)
97
     */
98
    public void transition(double x, double y, InputEvent event) {
99
        _fsm.addPoint(x, y, event);
100
    }
101

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

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

    
128
    /**
129
     * DOCUMENT ME!
130
     */
131
    public void selection() {
132
        FeatureSet selection = null;
133
        try {
134
            selection = (FeatureSet) getVLE().getFeatureStore().getSelection();
135

    
136
            if (selection.getSize() == 0
137
                && !SelectionCADTool.isInstance(CADExtension.getCADTool(), true)) {
138
                CADExtension.setCADTool("_selection", false);
139
                ((SelectionCADTool) CADExtension.getCADTool())
140
                    .setNextTool("_matrix");
141
            } else {
142
                // init();
143
                matrixPropeties();
144
            }
145
        } catch (ReadException e) {
146
            // TODO Auto-generated catch block
147
            e.printStackTrace();
148
        } catch (DataException e) {
149
            // TODO Auto-generated catch block
150
            e.printStackTrace();
151
        }
152
    }
153

    
154
    private void matrixPropeties() {
155
        matrixProperty.setMatrixCADTool(this);
156
        PluginServices.getMDIManager().addWindow(matrixProperty);
157
        endMatrix();
158

    
159
    }
160

    
161
    public void endMatrix() {
162
        if (operations.isAccepted()) {
163
            PluginServices.getMDIManager().setWaitCursor();
164

    
165
            // ArrayList selectedRow = getSelectedRows();
166
            ArrayList selectedRowAux = new ArrayList();
167
            VectorialLayerEdited vle = getVLE();
168
            FeatureStore featureStore = null;
169
            DisposableIterator iterator = null;
170
            try {
171
                featureStore = vle.getFeatureStore();
172

    
173
                featureStore.beginEditingGroup(getName());
174
                iterator =
175
                    ((FeatureSelection) featureStore.getSelection()).iterator();
176
                while (iterator.hasNext()) {
177
                    Feature feature = (Feature) iterator.next();
178
                    // Object[] attributes=new Object[feature.size()];
179
                    // for (int j = 0; j < feature.size(); j++) {
180
                    // attributes[j]=feature.get(j);
181
                    // }
182
                    if (operations.isRectangular()) {// Si es rectangular la
183
                                                     // matriz
184

    
185
                        for (int columns = 0; columns < operations
186
                            .getNumColumns(); columns++) {
187

    
188
                            for (int rows = 0; rows < operations.getNumRows(); rows++) {
189
                                if (columns == 0 && rows == 0) {
190
                                    continue;
191
                                }
192

    
193
                                // DefaultFeature feaCloned = (DefaultFeature)
194
                                // fea
195
                                // .cloneRow();
196
                                Geometry geom =
197
                                    (feature.getDefaultGeometry())
198
                                        .cloneGeometry();
199
                                Envelope original_env = geom.getEnvelope();
200
                                Geometry g =
201
                                    createPoint(
202
                                        original_env.getMinimum(0)
203
                                            + operations.getDistColumns()
204
                                            * columns, original_env.getMinimum(1)
205
                                            + operations.getDistRows() * rows);
206
                                AffineTransform at = new AffineTransform();
207
                                at.rotate(
208
                                    Math.toRadians(operations.getRotation()),
209
                                    original_env.getMinimum(0),
210
                                    original_env.getMinimum(1));
211
                                g.transform(at);
212
                                Envelope new_env = g.getEnvelope();
213
                                Point2D pDest =
214
                                    new Point2D.Double(
215
                                        new_env.getMinimum(0),
216
                                        new_env.getMinimum(1));
217

    
218
                                double difX = pDest.getX() - original_env.getMinimum(0);
219
                                double difY = pDest.getY() - original_env.getMinimum(1);
220
                                Handler[] handlers =
221
                                    geom.getHandlers(Geometry.SELECTHANDLER);
222
                                for (int j = 0; j < handlers.length; j++) {
223
                                    Handler h = handlers[j];
224
                                    Point2D p = h.getPoint();
225
                                    h.set(p.getX() + (difX), p.getY() + (difY));
226
                                }
227
                                EditableFeature eFeature =
228
                                    featureStore.createNewFeature(
229
                                        feature.getType(), feature);
230
                                eFeature.setGeometry(featureStore
231
                                    .getDefaultFeatureType()
232
                                    .getDefaultGeometryAttributeName(), geom);
233
                                selectedRowAux.add(eFeature);
234
                            }
235

    
236
                        }
237

    
238
                    } else { // Polar
239

    
240
                        double rotation = 360 / operations.getNum();
241

    
242
                        for (int numElem = 0; numElem < operations.getNum(); numElem++) {
243
                            System.out.println("numElem = " + numElem);
244
                            if (numElem == 0) {
245
                                continue;
246
                            }
247

    
248
                            // DefaultFeature feaCloned = (DefaultFeature) fea
249
                            // .cloneRow();
250
                            Geometry geom =
251
                                (feature.getDefaultGeometry()).cloneGeometry();
252

    
253
                            if (!operations.isRotateElements()) {
254
                                Envelope orig_env = geom.getEnvelope();
255
                                Geometry g =
256
                                    createPoint(
257
                                        orig_env.getMinimum(0),
258
                                        orig_env.getMinimum(1));
259
                                AffineTransform at = new AffineTransform();
260
                                at.rotate(Math.toRadians(rotation * numElem),
261
                                    operations.getPositionX(),
262
                                    operations.getPositionY());
263
                                g.transform(at);
264
                                
265
                                Envelope new_env = g.getEnvelope();
266
                                
267
                                Point2D pDest =
268
                                    new Point2D.Double(
269
                                        new_env.getMinimum(0),
270
                                        new_env.getMinimum(1));
271

    
272
                                double difX = pDest.getX() - orig_env.getMinimum(0);
273
                                double difY = pDest.getY() - orig_env.getMinimum(1);
274
                                Handler[] handlers =
275
                                    geom.getHandlers(Geometry.SELECTHANDLER);
276
                                for (int j = 0; j < handlers.length; j++) {
277
                                    Handler h = handlers[j];
278
                                    Point2D p = h.getPoint();
279
                                    h.set(p.getX() + (difX), p.getY() + (difY));
280
                                }
281
                            } else {// Cuando los elemtos rotan al mismo tiempo
282
                                    // que se van a?adiendo.
283

    
284
                                Envelope orig_env = geom.getEnvelope();
285
                                AffineTransform at = new AffineTransform();
286
                                at.rotate(Math.toRadians(rotation * numElem),
287
                                    operations.getPositionX(),
288
                                    operations.getPositionY());
289
                                geom.transform(at);
290
                                
291
                                Envelope new_env = geom.getEnvelope();
292
                                
293
                                Point2D pDest =
294
                                    new Point2D.Double(
295
                                        new_env.getMinimum(0),
296
                                        new_env.getMinimum(1)
297
                                        );
298

    
299
                                double difX = pDest.getX() - orig_env.getMinimum(0);
300
                                double difY = pDest.getY() - orig_env.getMinimum(1);
301
                                Handler[] handlers =
302
                                    geom.getHandlers(Geometry.SELECTHANDLER);
303
                                for (int j = 0; j < handlers.length; j++) {
304
                                    Handler h = handlers[j];
305
                                    Point2D p = h.getPoint();
306
                                    h.set(p.getX() + (difX), p.getY() + (difY));
307
                                }
308
                            }
309
                            EditableFeature eFeature =
310
                                featureStore.createNewFeature(
311
                                    feature.getType(), feature);
312
                            eFeature.setGeometry(featureStore
313
                                .getDefaultFeatureType()
314
                                .getDefaultGeometryAttributeName(), geom);
315

    
316
                            selectedRowAux.add(eFeature);
317
                        }
318
                    }
319
                }
320
                
321
                // =================================================
322
                /*
323
                 * Inserting geometries of matrix
324
                 */
325
//                SpatialCache spatialCache =
326
//                    ((FLyrVect) vle.getLayer()).getSpatialCache();
327
                
328
                for (int i=0; i<selectedRowAux.size(); i++) {
329
                        super.insertFeature((Feature) selectedRowAux.get(i));
330
//                        
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
                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

    
358
        }
359

    
360
    }
361

    
362
    /**
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

    
393
                    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

    
406
    /**
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

    
427
        Curve curve = createCurve(gpx);
428
        renderer.draw(curve, mapControlManager.getAxisReferenceSymbol());
429
    }
430

    
431
    /**
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

    
453
    /*
454
     * (non-Javadoc)
455
     * 
456
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
457
     */
458
    public void addValue(double d) {
459

    
460
    }
461

    
462
    public String getName() {
463
        return PluginServices.getText(this, "matrix_");
464
    }
465

    
466
    public String toString() {
467
        return "_matrix";
468
    }
469

    
470
    public MatrixOperations getOperations() {
471
        return operations;
472
    }
473

    
474
    @Override
475
    public boolean isApplicable(GeometryType geometryType) {
476
        return true;
477
    }
478

    
479
    @Override
480
    protected int[] getSupportedGeometryTypes() {
481
        return null;
482
    }
483
}