Statistics
| Revision:

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

History | View | Annotate | Download (18.8 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22
package org.gvsig.editing.gui.cad.tools;
23

    
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
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
import org.gvsig.fmap.dal.exception.DataException;
40
import org.gvsig.fmap.dal.exception.ReadException;
41
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
import org.gvsig.fmap.geom.Geometry;
47
import org.gvsig.fmap.geom.handler.Handler;
48
import org.gvsig.fmap.geom.primitive.Curve;
49
import org.gvsig.fmap.geom.primitive.Envelope;
50
import org.gvsig.fmap.geom.primitive.GeneralPathX;
51
import org.gvsig.fmap.geom.type.GeometryType;
52
import org.gvsig.fmap.mapcontext.ViewPort;
53
import org.gvsig.fmap.mapcontext.layers.SpatialCache;
54
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
55
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
56
import org.gvsig.tools.dispose.DisposableIterator;
57

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

    
65
    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

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

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

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

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

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

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

    
134
            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

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

    
161
    }
162

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

    
167
            // 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

    
175
                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

    
187
                        for (int columns = 0; columns < operations
188
                            .getNumColumns(); columns++) {
189

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

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

    
220
                                double difX = pDest.getX() - original_env.getMinimum(0);
221
                                double difY = pDest.getY() - original_env.getMinimum(1);
222
                                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

    
238
                        }
239

    
240
                    } else { // Polar
241

    
242
                        double rotation = 360 / operations.getNum();
243

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

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

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

    
274
                                double difX = pDest.getX() - orig_env.getMinimum(0);
275
                                double difY = pDest.getY() - orig_env.getMinimum(1);
276
                                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

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

    
301
                                double difX = pDest.getX() - orig_env.getMinimum(0);
302
                                double difY = pDest.getY() - orig_env.getMinimum(1);
303
                                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

    
318
                            selectedRowAux.add(eFeature);
319
                        }
320
                    }
321
                }
322
                
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
                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
}