Statistics
| Revision:

root / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / MatrixCADTool.java @ 9243

History | View | Annotate | Download (12.3 KB)

1 8937 caballero
/* 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 com.iver.cit.gvsig.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.io.IOException;
50
import java.util.ArrayList;
51
52
import com.iver.andami.PluginServices;
53
import com.iver.cit.gvsig.CADExtension;
54
import com.iver.cit.gvsig.fmap.ViewPort;
55
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
56
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
57
import com.iver.cit.gvsig.fmap.core.Handler;
58
import com.iver.cit.gvsig.fmap.core.IGeometry;
59
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
60
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
61
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
62
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
63
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
64
import com.iver.cit.gvsig.gui.cad.CADTool;
65
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
66
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
67
import com.iver.cit.gvsig.gui.cad.panels.matrix.MatrixOperations;
68
import com.iver.cit.gvsig.gui.cad.panels.matrix.MatrixProperty;
69
import com.iver.cit.gvsig.gui.cad.tools.smc.MatrixCADToolContext;
70
import com.iver.cit.gvsig.gui.cad.tools.smc.MatrixCADToolContext.MatrixCADToolState;
71
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
72
73
74
/**
75
 * Herramienta para crear una matriz de geometr?as.
76
 *
77
 * @author Vicente Caballero Navarro
78
 */
79
public class MatrixCADTool extends DefaultCADTool {
80
    private MatrixCADToolContext _fsm;
81
    private Point2D firstPoint;
82
    private Point2D secondPoint;
83
    private MatrixProperty matrixProperty=new MatrixProperty();
84
    private MatrixOperations operations=new MatrixOperations();
85
        private String option;
86
    /**
87
     * Crea un nuevo MatrixCADTool.
88
     */
89
    public MatrixCADTool() {
90
    }
91
92
    /**
93
     * M?todo de inicio, para poner el c?digo de todo lo que se requiera de una
94
     * carga previa a la utilizaci?n de la herramienta.
95
     */
96
    public void init() {
97
            _fsm = new MatrixCADToolContext(this);
98
    }
99
100
    /* (non-Javadoc)
101
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
102
     */
103
    public void transition(double x, double y, InputEvent event) {
104
        _fsm.addPoint(x, y, event);
105
    }
106
107
    /* (non-Javadoc)
108
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
109
     */
110
    public void transition(double d) {
111
        _fsm.addValue(d);
112
    }
113
114
    /* (non-Javadoc)
115
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
116
     */
117
    public void transition(String s) throws CommandException {
118
            if (!super.changeCommand(s)){
119
                    _fsm.addOption(s);
120
            }
121
    }
122
123
    /**
124
     * DOCUMENT ME!
125
     */
126
    public void selection() {
127
       ArrayList selectedRows=getSelectedRows();
128
        if (selectedRows.size() == 0 && !CADExtension.getCADTool().getClass().getName().equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
129
            CADExtension.setCADTool("_selection",false);
130
            ((SelectionCADTool) CADExtension.getCADTool()).setNextTool(
131
                "_matrix");
132
        }else {
133
                //init();
134
                matrixPropeties();
135
        }
136
    }
137
    private void matrixPropeties() {
138
            matrixProperty.setMatrixCADTool(this);
139
            PluginServices.getMDIManager().addWindow(matrixProperty);
140
            endMatrix();
141
142
        }
143
144
        public void endMatrix() {
145
                if (operations.isAccepted()) {
146
                        PluginServices.getMDIManager().setWaitCursor();
147
                        ArrayList selectedRow = getSelectedRows();
148
                        ArrayList selectedRowAux = new ArrayList();
149
                        VectorialLayerEdited vle = getVLE();
150
                        VectorialEditableAdapter vea = vle.getVEA();
151
                        try {
152
                                vea.startComplexRow();
153
                                for (int i = 0; i < selectedRow.size(); i++) {
154
                                        DefaultRowEdited row = (DefaultRowEdited) selectedRow
155
                                                        .get(i);
156
                                        DefaultFeature fea = (DefaultFeature) row.getLinkedRow()
157
                                                        .cloneRow();
158
                                        if (operations.isRectangular()) {//Si es rectangular la matriz
159
160
                                                for (int columns = 0; columns < operations
161
                                                                .getNumColumns(); columns++) {
162
163
                                                        for (int rows = 0; rows < operations.getNumRows(); rows++) {
164
                                                                if (columns == 0 && rows == 0)
165
                                                                        continue;
166
167
                                                                DefaultFeature feaCloned = (DefaultFeature) fea
168
                                                                                .cloneRow();
169
                                                                IGeometry geom = feaCloned.getGeometry();
170
                                                                Rectangle2D originalRec = geom.getBounds2D();
171
                                                                IGeometry g = ShapeFactory.createPoint2D(
172
                                                                                originalRec.getX()
173
                                                                                                + operations.getDistColumns()
174
                                                                                                * columns, originalRec.getY()
175
                                                                                                + operations.getDistRows()
176
                                                                                                * rows);
177
                                                                AffineTransform at = new AffineTransform();
178
                                                                at.rotate(Math.toRadians(operations
179
                                                                                .getRotation()), originalRec.getMinX(),
180
                                                                                originalRec.getMinY());
181
                                                                g.transform(at);
182
                                                                Point2D pDest = new Point2D.Double(g
183
                                                                                .getBounds2D().getX(), g.getBounds2D()
184
                                                                                .getY());
185
186
                                                                double difX = pDest.getX() - originalRec.getX();
187
                                                                double difY = pDest.getY() - originalRec.getY();
188
                                                                Handler[] handlers = geom
189
                                                                                .getHandlers(IGeometry.SELECTHANDLER);
190
                                                                for (int j = 0; j < handlers.length; j++) {
191
                                                                        Handler h = handlers[j];
192
                                                                        Point2D p = h.getPoint();
193
                                                                        h.set(p.getX() + (difX), p.getY() + (difY));
194
                                                                }
195
                                                                int index = addGeometry(geom, feaCloned
196
                                                                                .getAttributes());
197
                                                                selectedRowAux.add(new DefaultRowEdited(
198
                                                                                feaCloned, IRowEdited.STATUS_ADDED,
199
                                                                                index));
200
                                                        }
201
202
                                                }
203
204
                                        } else { // Polar
205
206
                                                double rotation=360/operations.getNum();
207
208
                                                for (int numElem = 0; numElem < operations
209
                                                                .getNum(); numElem++) {
210
                                                        System.out.println("numElem = "+numElem);
211
                                                                if (numElem == 0)
212
                                                                        continue;
213
214
                                                                DefaultFeature feaCloned = (DefaultFeature) fea
215
                                                                                .cloneRow();
216
                                                                IGeometry geom = feaCloned.getGeometry();
217
218
                                                                if (!operations.isRotateElements()) {
219
                                                                        Rectangle2D originalRec = geom.getBounds2D();
220
                                                                        IGeometry g = ShapeFactory.createPoint2D(
221
                                                                                originalRec.getX(), originalRec.getY()
222
                                                                                                );
223
                                                                        AffineTransform at = new AffineTransform();
224
                                                                        at.rotate(Math.toRadians(rotation*numElem), operations.getPositionX(),
225
                                                                                operations.getPositionY());
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(IGeometry.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
                                                                } else {//Cuando los elemtos rotan al mismo tiempo que se van a?adiendo.
241
242
                                                                        Rectangle2D originalRec = geom.getBounds2D();
243
                                                                        AffineTransform at = new AffineTransform();
244
                                                                        at.rotate(Math.toRadians(rotation*numElem), operations.getPositionX(),
245
                                                                                operations.getPositionY());
246
                                                                        geom.transform(at);
247
                                                                        Point2D pDest = new Point2D.Double(geom
248
                                                                                .getBounds2D().getX(), geom.getBounds2D()
249
                                                                                .getY());
250
251
                                                                        double difX = pDest.getX() - originalRec.getX();
252
                                                                        double difY = pDest.getY() - originalRec.getY();
253
                                                                        Handler[] handlers = geom
254
                                                                                .getHandlers(IGeometry.SELECTHANDLER);
255
                                                                        for (int j = 0; j < handlers.length; j++) {
256
                                                                                Handler h = handlers[j];
257
                                                                                Point2D p = h.getPoint();
258
                                                                                h.set(p.getX() + (difX), p.getY() + (difY));
259
                                                                        }
260
                                                                }
261
                                                                int index = addGeometry(geom, feaCloned
262
                                                                                .getAttributes());
263
                                                                selectedRowAux.add(new DefaultRowEdited(
264
                                                                                feaCloned, IRowEdited.STATUS_ADDED,
265
                                                                                index));
266
                                                }
267
                                        }
268
                                }
269
                                vea.endComplexRow(getName());
270
                                vle.setSelectionCache(VectorialLayerEdited.SAVEPREVIOUS, selectedRowAux);
271
                        } catch (DriverIOException e) {
272
                                e.printStackTrace();
273
                        } catch (IOException e1) {
274
                                e1.printStackTrace();
275
                        }
276
277
                        PluginServices.getMDIManager().restoreCursor();
278
                        end();
279
                } else {// Cancelado
280
281
                }
282
283
        }
284
285
        /**
286
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
287
     * editableFeatureSource que ya estar? creado.
288
     *
289
     * @param x par?metro x del punto que se pase en esta transici?n.
290
     * @param y par?metro y del punto que se pase en esta transici?n.
291
     */
292
    public void addPoint(double x, double y,InputEvent event) {
293
        //MatrixCADToolState actualState = _fsm.getState();
294
        MatrixCADToolState previousState= (MatrixCADToolState)_fsm.getPreviousState();
295
        String status = previousState.getName();
296
        if (status.equals("Matrix.Start")||status.equals("Matrix.FirstPoint")) {
297
                firstPoint = new Point2D.Double(x, y);
298
            } else if (status.equals("Matrix.SecondPoint")) {
299
                    secondPoint = new Point2D.Double(x,y);
300
                    if (option.equals("lagX") || option.equals("lagXY")) {
301
                            operations.setDistColumns(secondPoint.getX()-firstPoint.getX());
302
                            matrixProperty.refreshLagX();
303
                    }
304
                    if (option.equals("lagY") || option.equals("lagXY")) {
305
                            operations.setDistRows(secondPoint.getY()-firstPoint.getY());
306
                            matrixProperty.refreshLagY();
307
                    }
308
                    if (option.equals("rotation")) {
309
310
                            double w;
311
                            double h;
312
                            w = secondPoint.getX() - firstPoint.getX();
313
                            h = secondPoint.getY() - firstPoint.getY();
314
                            operations.setRotation((-Math.atan2(w,
315
                                                h)
316
                                                + (Math.PI / 2))*180/Math.PI);
317
                            matrixProperty.refreshRotation();
318
                    }
319
                    firstPoint=null;
320
                    PluginServices.getMDIManager().addWindow(matrixProperty);
321
            }
322
    }
323
324
    /**
325
     * M?todo para dibujar lo necesario para el estado en el que nos
326
     * encontremos.
327
     *
328
     * @param g Graphics sobre el que dibujar.
329
     * @param x par?metro x del punto que se pase para dibujar.
330
     * @param y par?metro x del punto que se pase para dibujar.
331
     */
332
    public void drawOperation(Graphics g, double x, double y) {
333
        if (_fsm==null || firstPoint==null)
334
                return;
335
        GeneralPathX gpx=new GeneralPathX();
336
            gpx.moveTo(firstPoint.getX(),firstPoint.getY());
337
            gpx.lineTo(x,y);
338
            VectorialLayerEdited vle=getVLE();
339
            ViewPort vp=vle.getLayer().getMapContext().getViewPort();
340
            ShapeFactory.createPolyline2D(gpx).draw((Graphics2D)g,vp,CADTool.modifySymbol);
341
342
   }
343
344
    /**
345
         * Add a diferent option.
346
         *
347
         * @param s
348
         *            Diferent option.
349
         */
350
    public void addOption(String s) {
351
            option=s;
352
            PluginServices.getMDIManager().closeWindow(matrixProperty);
353
            /* MatrixCADToolState actualState = _fsm
354
                                .getState();
355
                String status = actualState.getName();
356

357
                if (status.equals("Matrix.LagXY")) {
358

359
                }else if (status.equals("Matrix.LagX")) {
360

361
                }else if (status.equals("Matrix.LagY")) {
362

363
                }
364
                */
365
    }
366
367
    /*
368
         * (non-Javadoc)
369
         *
370
         * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
371
         */
372
    public void addValue(double d) {
373
374
    }
375
376
        public String getName() {
377
                return PluginServices.getText(this,"matrix_");
378
        }
379
380
        public String toString() {
381
                return "_matrix";
382
        }
383
384
        public MatrixOperations getOperations() {
385
                return operations;
386
        }
387
388
}