Statistics
| Revision:

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

History | View | Annotate | Download (17.2 KB)

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