Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / MatrixCADTool.java @ 29616

History | View | Annotate | Download (13.6 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 org.gvsig.editing.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.AffineTransform;
47
import java.awt.geom.Point2D;
48
import java.awt.geom.Rectangle2D;
49
import java.util.ArrayList;
50

    
51
import org.gvsig.andami.PluginServices;
52
import org.gvsig.editing.CADExtension;
53
import org.gvsig.editing.gui.cad.DefaultCADTool;
54
import org.gvsig.editing.gui.cad.exception.CommandException;
55
import org.gvsig.editing.gui.cad.panels.matrix.MatrixOperations;
56
import org.gvsig.editing.gui.cad.panels.matrix.MatrixProperty;
57
import org.gvsig.editing.gui.cad.tools.smc.MatrixCADToolContext;
58
import org.gvsig.editing.gui.cad.tools.smc.MatrixCADToolContext.MatrixCADToolState;
59
import org.gvsig.editing.layers.VectorialLayerEdited;
60
import org.gvsig.fmap.dal.exception.DataException;
61
import org.gvsig.fmap.dal.exception.ReadException;
62
import org.gvsig.fmap.dal.feature.DisposableIterator;
63
import org.gvsig.fmap.dal.feature.EditableFeature;
64
import org.gvsig.fmap.dal.feature.Feature;
65
import org.gvsig.fmap.dal.feature.FeatureSelection;
66
import org.gvsig.fmap.dal.feature.FeatureSet;
67
import org.gvsig.fmap.dal.feature.FeatureStore;
68
import org.gvsig.fmap.geom.Geometry;
69
import org.gvsig.fmap.geom.handler.Handler;
70
import org.gvsig.fmap.geom.operation.Draw;
71
import org.gvsig.fmap.geom.operation.DrawOperationContext;
72
import org.gvsig.fmap.geom.operation.GeometryOperationException;
73
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
74
import org.gvsig.fmap.geom.primitive.GeneralPathX;
75
import org.gvsig.fmap.mapcontext.ViewPort;
76

    
77

    
78
/**
79
 * Herramienta para crear una matriz de geometr?as.
80
 *
81
 * @author Vicente Caballero Navarro
82
 */
83
public class MatrixCADTool extends DefaultCADTool {
84
        protected MatrixCADToolContext _fsm;
85
        protected Point2D firstPoint;
86
        protected Point2D secondPoint;
87
        protected MatrixProperty matrixProperty = null;
88
        protected MatrixOperations operations = null;
89
        protected String option;
90

    
91
        /**
92
         * Crea un nuevo MatrixCADTool.
93
         */
94
        public MatrixCADTool() {
95
                matrixProperty = new MatrixProperty();
96
                operations = new MatrixOperations();
97
        }
98

    
99
        /**
100
         * M?todo de inicio, para poner el c?digo de todo lo que se requiera de una
101
         * carga previa a la utilizaci?n de la herramienta.
102
         */
103
        public void init() {
104
                _fsm = new MatrixCADToolContext(this);
105
        }
106

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

    
117
        /*
118
         * (non-Javadoc)
119
         *
120
         * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
121
         *      double)
122
         */
123
        public void transition(double d) {
124
                _fsm.addValue(d);
125
        }
126

    
127
        /*
128
         * (non-Javadoc)
129
         *
130
         * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
131
         *      java.lang.String)
132
         */
133
        public void transition(String s) throws CommandException {
134
                if (!super.changeCommand(s)) {
135
                        _fsm.addOption(s);
136
                }
137
        }
138

    
139
        /**
140
         * DOCUMENT ME!
141
         */
142
        public void selection() {
143
                FeatureSet selection = null;
144
                try {
145
                        selection = (FeatureSet) getVLE().getFeatureStore().getSelection();
146

    
147
                        if (selection.getSize() == 0
148
                                        && !CADExtension
149
                                                        .getCADTool()
150
                                                        .getClass()
151
                                                        .getName()
152
                                                        .equals(
153
                                                                        "com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
154
                                CADExtension.setCADTool("_selection", false);
155
                                ((SelectionCADTool) CADExtension.getCADTool())
156
                                                .setNextTool("_matrix");
157
                        } else {
158
                                // init();
159
                                matrixPropeties();
160
                        }
161
                } catch (ReadException e) {
162
                        // TODO Auto-generated catch block
163
                        e.printStackTrace();
164
                } catch (DataException e) {
165
                        // TODO Auto-generated catch block
166
                        e.printStackTrace();
167
                }
168
        }
169

    
170
        private void matrixPropeties() {
171
                matrixProperty.setMatrixCADTool(this);
172
                PluginServices.getMDIManager().addWindow(matrixProperty);
173
                endMatrix();
174

    
175
        }
176

    
177
        public void endMatrix() {
178
                if (operations.isAccepted()) {
179
                        PluginServices.getMDIManager().setWaitCursor();
180

    
181
                        // ArrayList selectedRow = getSelectedRows();
182
                        ArrayList selectedRowAux = new ArrayList();
183
                        VectorialLayerEdited vle = getVLE();
184
                        FeatureStore featureStore = null;
185
                        DisposableIterator iterator = null;
186
                        try {
187
                                featureStore = vle.getFeatureStore();
188

    
189
                                featureStore.beginEditingGroup(getName());
190
                                iterator = ((FeatureSelection) featureStore
191
                                                .getSelection()).iterator();
192
                                while (iterator.hasNext()) {
193
                                        Feature feature = (Feature) iterator.next();
194
                                        // Object[] attributes=new Object[feature.size()];
195
                                        // for (int j = 0; j < feature.size(); j++) {
196
                                        // attributes[j]=feature.get(j);
197
                                        // }
198
                                        if (operations.isRectangular()) {// Si es rectangular la
199
                                                                                                                // matriz
200

    
201
                                                for (int columns = 0; columns < operations
202
                                                                .getNumColumns(); columns++) {
203

    
204
                                                        for (int rows = 0; rows < operations.getNumRows(); rows++) {
205
                                                                if (columns == 0 && rows == 0) {
206
                                                                        continue;
207
                                                                }
208

    
209
                                                                // DefaultFeature feaCloned = (DefaultFeature)
210
                                                                // fea
211
                                                                // .cloneRow();
212
                                                                Geometry geom = (feature
213
                                                                                .getDefaultGeometry()).cloneGeometry();
214
                                                                Rectangle2D originalRec = geom.getBounds2D();
215
                                                                Geometry g = createPoint(
216
                                                                                originalRec.getX()
217
                                                                                                + operations.getDistColumns()
218
                                                                                                * columns, originalRec.getY()
219
                                                                                                + operations.getDistRows()
220
                                                                                                * rows);
221
                                                                AffineTransform at = new AffineTransform();
222
                                                                at.rotate(Math.toRadians(operations
223
                                                                                .getRotation()), originalRec.getMinX(),
224
                                                                                originalRec.getMinY());
225
                                                                g.transform(at);
226
                                                                Point2D pDest = new Point2D.Double(g
227
                                                                                .getBounds2D().getX(), g.getBounds2D()
228
                                                                                .getY());
229

    
230
                                                                double difX = pDest.getX() - originalRec.getX();
231
                                                                double difY = pDest.getY() - originalRec.getY();
232
                                                                Handler[] handlers = geom
233
                                                                                .getHandlers(Geometry.SELECTHANDLER);
234
                                                                for (int j = 0; j < handlers.length; j++) {
235
                                                                        Handler h = handlers[j];
236
                                                                        Point2D p = h.getPoint();
237
                                                                        h.set(p.getX() + (difX), p.getY() + (difY));
238
                                                                }
239
                                                                EditableFeature eFeature = featureStore
240
                                                                                .createNewFeature(feature.getType(),
241
                                                                                                feature);
242
                                                                eFeature.setGeometry(featureStore
243
                                                                                .getDefaultFeatureType()
244
                                                                                .getDefaultGeometryAttributeName(),
245
                                                                                geom);
246
                                                                selectedRowAux.add(eFeature);
247
                                                        }
248

    
249
                                                }
250

    
251
                                        } else { // Polar
252

    
253
                                                double rotation = 360 / operations.getNum();
254

    
255
                                                for (int numElem = 0; numElem < operations.getNum(); numElem++) {
256
                                                        System.out.println("numElem = " + numElem);
257
                                                        if (numElem == 0) {
258
                                                                continue;
259
                                                        }
260

    
261
                                                        // DefaultFeature feaCloned = (DefaultFeature) fea
262
                                                        // .cloneRow();
263
                                                        Geometry geom = (feature
264
                                                                        .getDefaultGeometry()).cloneGeometry();
265

    
266
                                                        if (!operations.isRotateElements()) {
267
                                                                Rectangle2D originalRec = geom.getBounds2D();
268
                                                                Geometry g = createPoint(
269
                                                                                originalRec.getX(), originalRec.getY());
270
                                                                AffineTransform at = new AffineTransform();
271
                                                                at.rotate(Math.toRadians(rotation * numElem),
272
                                                                                operations.getPositionX(), operations
273
                                                                                                .getPositionY());
274
                                                                g.transform(at);
275
                                                                Point2D pDest = new Point2D.Double(g
276
                                                                                .getBounds2D().getX(), g.getBounds2D()
277
                                                                                .getY());
278

    
279
                                                                double difX = pDest.getX() - originalRec.getX();
280
                                                                double difY = pDest.getY() - originalRec.getY();
281
                                                                Handler[] handlers = geom
282
                                                                                .getHandlers(Geometry.SELECTHANDLER);
283
                                                                for (int j = 0; j < handlers.length; j++) {
284
                                                                        Handler h = handlers[j];
285
                                                                        Point2D p = h.getPoint();
286
                                                                        h.set(p.getX() + (difX), p.getY() + (difY));
287
                                                                }
288
                                                        } else {// Cuando los elemtos rotan al mismo tiempo
289
                                                                        // que se van a?adiendo.
290

    
291
                                                                Rectangle2D originalRec = geom.getBounds2D();
292
                                                                AffineTransform at = new AffineTransform();
293
                                                                at.rotate(Math.toRadians(rotation * numElem),
294
                                                                                operations.getPositionX(), operations
295
                                                                                                .getPositionY());
296
                                                                geom.transform(at);
297
                                                                Point2D pDest = new Point2D.Double(geom
298
                                                                                .getBounds2D().getX(), geom
299
                                                                                .getBounds2D().getY());
300

    
301
                                                                double difX = pDest.getX() - originalRec.getX();
302
                                                                double difY = pDest.getY() - originalRec.getY();
303
                                                                Handler[] handlers = geom
304
                                                                                .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 = featureStore
312
                                                                        .createNewFeature(feature.getType(),
313
                                                                                        feature);
314
                                                        eFeature.setGeometry(featureStore
315
                                                                        .getDefaultFeatureType()
316
                                                                        .getDefaultGeometryAttributeName(), geom);
317

    
318
                                                        selectedRowAux.add(eFeature);
319
                                                }
320
                                        }
321
                                }
322
                                featureStore.endEditingGroup();
323
                                // vle.setSelectionCache(VectorialLayerEdited.SAVEPREVIOUS,
324
                                // selectedRowAux);
325
                                PluginServices.getMDIManager().restoreCursor();
326
                                end();
327
                        } catch (DataException e) {
328
                                // TODO Auto-generated catch block
329
                                e.printStackTrace();
330
                        } finally {
331
                                if (iterator != null) {
332
                                        iterator.dispose();
333
                                }
334
                        }
335
                } else {// Cancelado
336

    
337
                }
338

    
339
        }
340

    
341
        /**
342
         * Equivale al transition del prototipo pero sin pasarle como par?metro el
343
         * editableFeatureSource que ya estar? creado.
344
         *
345
         * @param x
346
         *            par?metro x del punto que se pase en esta transici?n.
347
         * @param y
348
         *            par?metro y del punto que se pase en esta transici?n.
349
         */
350
        public void addPoint(double x, double y, InputEvent event) {
351
                // MatrixCADToolState actualState = _fsm.getState();
352
                MatrixCADToolState previousState = (MatrixCADToolState) _fsm
353
                                .getPreviousState();
354
                String status = previousState.getName();
355
                if (status.equals("Matrix.Start") || status.equals("Matrix.FirstPoint")) {
356
                        firstPoint = new Point2D.Double(x, y);
357
                } else if (status.equals("Matrix.SecondPoint")) {
358
                        secondPoint = new Point2D.Double(x, y);
359
                        if (option.equals("lagX") || option.equals("lagXY")) {
360
                                operations.setDistColumns(secondPoint.getX()
361
                                                - firstPoint.getX());
362
                                matrixProperty.refreshLagX();
363
                        }
364
                        if (option.equals("lagY") || option.equals("lagXY")) {
365
                                operations.setDistRows(secondPoint.getY() - firstPoint.getY());
366
                                matrixProperty.refreshLagY();
367
                        }
368
                        if (option.equals("rotation")) {
369

    
370
                                double w;
371
                                double h;
372
                                w = secondPoint.getX() - firstPoint.getX();
373
                                h = secondPoint.getY() - firstPoint.getY();
374
                                operations.setRotation((-Math.atan2(w, h) + (Math.PI / 2))
375
                                                * 180 / Math.PI);
376
                                matrixProperty.refreshRotation();
377
                        }
378
                        firstPoint = null;
379
                        PluginServices.getMDIManager().addWindow(matrixProperty);
380
                }
381
        }
382

    
383
        /**
384
         * M?todo para dibujar lo necesario para el estado en el que nos
385
         * encontremos.
386
         *
387
         * @param g
388
         *            Graphics sobre el que dibujar.
389
         * @param x
390
         *            par?metro x del punto que se pase para dibujar.
391
         * @param y
392
         *            par?metro x del punto que se pase para dibujar.
393
         */
394
        public void drawOperation(Graphics g, double x, double y) {
395
                if (_fsm == null || firstPoint == null) {
396
                        return;
397
                }
398
                GeneralPathX gpx = new GeneralPathX();
399
                gpx.moveTo(firstPoint.getX(), firstPoint.getY());
400
                gpx.lineTo(x, y);
401
                VectorialLayerEdited vle = getVLE();
402
                ViewPort vp = vle.getLayer().getMapContext().getViewPort();
403
                DrawOperationContext doc = new DrawOperationContext();
404
                doc.setGraphics((Graphics2D) g);
405
                doc.setViewPort(vp);
406
                doc.setSymbol(DefaultCADTool.axisReferencesSymbol);
407
                try {
408
                        createCurve(gpx).invokeOperation(Draw.CODE, doc);
409
                } catch (GeometryOperationNotSupportedException e) {
410
                        e.printStackTrace();
411
                } catch (GeometryOperationException e) {
412
                        e.printStackTrace();
413
                }
414

    
415
                // geomFactory.createPolyline2D(gpx).draw((Graphics2D)g,vp,DefaultCADTool.axisReferencesSymbol);
416

    
417
        }
418

    
419
        /**
420
         * Add a diferent option.
421
         *
422
         * @param s
423
         *            Diferent option.
424
         */
425
        public void addOption(String s) {
426
                option = s;
427
                PluginServices.getMDIManager().closeWindow(matrixProperty);
428
                /*
429
                 * MatrixCADToolState actualState = _fsm .getState(); String status =
430
                 * actualState.getName();
431
                 *
432
                 * if (status.equals("Matrix.LagXY")) {
433
                 *
434
                 * }else if (status.equals("Matrix.LagX")) {
435
                 *
436
                 * }else if (status.equals("Matrix.LagY")) {
437
                 *  }
438
                 */
439
        }
440

    
441
        /*
442
         * (non-Javadoc)
443
         *
444
         * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
445
         */
446
        public void addValue(double d) {
447

    
448
        }
449

    
450
        public String getName() {
451
                return PluginServices.getText(this, "matrix_");
452
        }
453

    
454
        public String toString() {
455
                return "_matrix";
456
        }
457

    
458
        public MatrixOperations getOperations() {
459
                return operations;
460
        }
461

    
462
}