Statistics
| Revision:

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

History | View | Annotate | Download (13.4 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.Graphics2D;
44
import java.awt.event.InputEvent;
45
import java.awt.geom.AffineTransform;
46
import java.awt.geom.Point2D;
47
import java.awt.geom.Rectangle2D;
48
import java.util.ArrayList;
49

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

    
78

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

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

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

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

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

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

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

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

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

    
176
        }
177

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

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

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

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

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

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

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

    
250
                                                }
251

    
252
                                        } else { // Polar
253

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

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

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

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

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

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

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

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

    
338
                }
339

    
340
        }
341

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

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

    
384
        /**
385
         * M?todo para dibujar lo necesario para el estado en el que nos
386
         * encontremos.
387
         *
388
         * @param g
389
         *            Graphics sobre el que dibujar.
390
         * @param x
391
         *            par?metro x del punto que se pase para dibujar.
392
         * @param y
393
         *            par?metro x del punto que se pase para dibujar.
394
         */
395
        public void drawOperation(MapControlDrawer renderer, double x, double y) {
396
                if (_fsm == null || firstPoint == null) {
397
                        return;
398
                }
399
                GeneralPathX gpx = new GeneralPathX();
400
                gpx.moveTo(firstPoint.getX(), firstPoint.getY());
401
                gpx.lineTo(x, y);
402
                VectorialLayerEdited vle = getVLE();
403
                ViewPort vp = vle.getLayer().getMapContext().getViewPort();
404
                
405
                Curve curve = createCurve(gpx);
406
                renderer.draw(curve, mapControlManager.getAxisReferenceSymbol());
407
        }
408

    
409
        /**
410
         * Add a diferent option.
411
         *
412
         * @param s
413
         *            Diferent option.
414
         */
415
        public void addOption(String s) {
416
                option = s;
417
                PluginServices.getMDIManager().closeWindow(matrixProperty);
418
                /*
419
                 * MatrixCADToolState actualState = _fsm .getState(); String status =
420
                 * actualState.getName();
421
                 *
422
                 * if (status.equals("Matrix.LagXY")) {
423
                 *
424
                 * }else if (status.equals("Matrix.LagX")) {
425
                 *
426
                 * }else if (status.equals("Matrix.LagY")) {
427
                 *  }
428
                 */
429
        }
430

    
431
        /*
432
         * (non-Javadoc)
433
         *
434
         * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
435
         */
436
        public void addValue(double d) {
437

    
438
        }
439

    
440
        public String getName() {
441
                return PluginServices.getText(this, "matrix_");
442
        }
443

    
444
        public String toString() {
445
                return "_matrix";
446
        }
447

    
448
        public MatrixOperations getOperations() {
449
                return operations;
450
        }
451

    
452
}