Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / MatrixCADTool.java @ 10626

History | View | Annotate | Download (12.1 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 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.util.ArrayList;
50

    
51
import com.iver.andami.PluginServices;
52
import com.iver.cit.gvsig.CADExtension;
53
import com.iver.cit.gvsig.fmap.ViewPort;
54
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
55
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
56
import com.iver.cit.gvsig.fmap.core.Handler;
57
import com.iver.cit.gvsig.fmap.core.IGeometry;
58
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
59
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
60
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
61
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
62
import com.iver.cit.gvsig.gui.cad.CADTool;
63
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
64
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
65
import com.iver.cit.gvsig.gui.cad.panels.matrix.MatrixOperations;
66
import com.iver.cit.gvsig.gui.cad.panels.matrix.MatrixProperty;
67
import com.iver.cit.gvsig.gui.cad.tools.smc.MatrixCADToolContext;
68
import com.iver.cit.gvsig.gui.cad.tools.smc.MatrixCADToolContext.MatrixCADToolState;
69
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
70

    
71

    
72
/**
73
 * Herramienta para crear una matriz de geometr?as.
74
 *
75
 * @author Vicente Caballero Navarro
76
 */
77
public class MatrixCADTool extends DefaultCADTool {
78
    private MatrixCADToolContext _fsm;
79
    private Point2D firstPoint;
80
    private Point2D secondPoint;
81
    private MatrixProperty matrixProperty=new MatrixProperty();
82
    private MatrixOperations operations=new MatrixOperations();
83
        private String option;
84
    /**
85
     * Crea un nuevo MatrixCADTool.
86
     */
87
    public MatrixCADTool() {
88
    }
89

    
90
    /**
91
     * M?todo de inicio, para poner el c?digo de todo lo que se requiera de una
92
     * carga previa a la utilizaci?n de la herramienta.
93
     */
94
    public void init() {
95
            _fsm = new MatrixCADToolContext(this);
96
    }
97

    
98
    /* (non-Javadoc)
99
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
100
     */
101
    public void transition(double x, double y, InputEvent event) {
102
        _fsm.addPoint(x, y, event);
103
    }
104

    
105
    /* (non-Javadoc)
106
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
107
     */
108
    public void transition(double d) {
109
        _fsm.addValue(d);
110
    }
111

    
112
    /* (non-Javadoc)
113
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
114
     */
115
    public void transition(String s) throws CommandException {
116
            if (!super.changeCommand(s)){
117
                    _fsm.addOption(s);
118
            }
119
    }
120

    
121
    /**
122
     * DOCUMENT ME!
123
     */
124
    public void selection() {
125
       ArrayList selectedRows=getSelectedRows();
126
        if (selectedRows.size() == 0 && !CADExtension.getCADTool().getClass().getName().equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
127
            CADExtension.setCADTool("_selection",false);
128
            ((SelectionCADTool) CADExtension.getCADTool()).setNextTool(
129
                "_matrix");
130
        }else {
131
                //init();
132
                matrixPropeties();
133
        }
134
    }
135
    private void matrixPropeties() {
136
            matrixProperty.setMatrixCADTool(this);
137
            PluginServices.getMDIManager().addWindow(matrixProperty);
138
            endMatrix();
139

    
140
        }
141

    
142
        public void endMatrix() {
143
                if (operations.isAccepted()) {
144
                        PluginServices.getMDIManager().setWaitCursor();
145
                        ArrayList selectedRow = getSelectedRows();
146
                        ArrayList selectedRowAux = new ArrayList();
147
                        VectorialLayerEdited vle = getVLE();
148
                        VectorialEditableAdapter vea = vle.getVEA();
149
                                vea.startComplexRow();
150
                                for (int i = 0; i < selectedRow.size(); i++) {
151
                                        DefaultRowEdited row = (DefaultRowEdited) selectedRow
152
                                                        .get(i);
153
                                        DefaultFeature fea = (DefaultFeature) row.getLinkedRow()
154
                                                        .cloneRow();
155
                                        if (operations.isRectangular()) {//Si es rectangular la matriz
156

    
157
                                                for (int columns = 0; columns < operations
158
                                                                .getNumColumns(); columns++) {
159

    
160
                                                        for (int rows = 0; rows < operations.getNumRows(); rows++) {
161
                                                                if (columns == 0 && rows == 0)
162
                                                                        continue;
163

    
164
                                                                DefaultFeature feaCloned = (DefaultFeature) fea
165
                                                                                .cloneRow();
166
                                                                IGeometry geom = feaCloned.getGeometry();
167
                                                                Rectangle2D originalRec = geom.getBounds2D();
168
                                                                IGeometry g = ShapeFactory.createPoint2D(
169
                                                                                originalRec.getX()
170
                                                                                                + operations.getDistColumns()
171
                                                                                                * columns, originalRec.getY()
172
                                                                                                + operations.getDistRows()
173
                                                                                                * rows);
174
                                                                AffineTransform at = new AffineTransform();
175
                                                                at.rotate(Math.toRadians(operations
176
                                                                                .getRotation()), originalRec.getMinX(),
177
                                                                                originalRec.getMinY());
178
                                                                g.transform(at);
179
                                                                Point2D pDest = new Point2D.Double(g
180
                                                                                .getBounds2D().getX(), g.getBounds2D()
181
                                                                                .getY());
182

    
183
                                                                double difX = pDest.getX() - originalRec.getX();
184
                                                                double difY = pDest.getY() - originalRec.getY();
185
                                                                Handler[] handlers = geom
186
                                                                                .getHandlers(IGeometry.SELECTHANDLER);
187
                                                                for (int j = 0; j < handlers.length; j++) {
188
                                                                        Handler h = handlers[j];
189
                                                                        Point2D p = h.getPoint();
190
                                                                        h.set(p.getX() + (difX), p.getY() + (difY));
191
                                                                }
192
                                                                int index = addGeometry(geom, feaCloned
193
                                                                                .getAttributes());
194
                                                                selectedRowAux.add(new DefaultRowEdited(
195
                                                                                feaCloned, IRowEdited.STATUS_ADDED,
196
                                                                                index));
197
                                                        }
198

    
199
                                                }
200

    
201
                                        } else { // Polar
202

    
203
                                                double rotation=360/operations.getNum();
204

    
205
                                                for (int numElem = 0; numElem < operations
206
                                                                .getNum(); numElem++) {
207
                                                        System.out.println("numElem = "+numElem);
208
                                                                if (numElem == 0)
209
                                                                        continue;
210

    
211
                                                                DefaultFeature feaCloned = (DefaultFeature) fea
212
                                                                                .cloneRow();
213
                                                                IGeometry geom = feaCloned.getGeometry();
214

    
215
                                                                if (!operations.isRotateElements()) {
216
                                                                        Rectangle2D originalRec = geom.getBounds2D();
217
                                                                        IGeometry g = ShapeFactory.createPoint2D(
218
                                                                                originalRec.getX(), originalRec.getY()
219
                                                                                                );
220
                                                                        AffineTransform at = new AffineTransform();
221
                                                                        at.rotate(Math.toRadians(rotation*numElem), operations.getPositionX(),
222
                                                                                operations.getPositionY());
223
                                                                        g.transform(at);
224
                                                                        Point2D pDest = new Point2D.Double(g
225
                                                                                .getBounds2D().getX(), g.getBounds2D()
226
                                                                                .getY());
227

    
228
                                                                        double difX = pDest.getX() - originalRec.getX();
229
                                                                        double difY = pDest.getY() - originalRec.getY();
230
                                                                        Handler[] handlers = geom
231
                                                                                .getHandlers(IGeometry.SELECTHANDLER);
232
                                                                        for (int j = 0; j < handlers.length; j++) {
233
                                                                                Handler h = handlers[j];
234
                                                                                Point2D p = h.getPoint();
235
                                                                                h.set(p.getX() + (difX), p.getY() + (difY));
236
                                                                        }
237
                                                                } else {//Cuando los elemtos rotan al mismo tiempo que se van a?adiendo.
238

    
239
                                                                        Rectangle2D originalRec = geom.getBounds2D();
240
                                                                        AffineTransform at = new AffineTransform();
241
                                                                        at.rotate(Math.toRadians(rotation*numElem), operations.getPositionX(),
242
                                                                                operations.getPositionY());
243
                                                                        geom.transform(at);
244
                                                                        Point2D pDest = new Point2D.Double(geom
245
                                                                                .getBounds2D().getX(), geom.getBounds2D()
246
                                                                                .getY());
247

    
248
                                                                        double difX = pDest.getX() - originalRec.getX();
249
                                                                        double difY = pDest.getY() - originalRec.getY();
250
                                                                        Handler[] handlers = geom
251
                                                                                .getHandlers(IGeometry.SELECTHANDLER);
252
                                                                        for (int j = 0; j < handlers.length; j++) {
253
                                                                                Handler h = handlers[j];
254
                                                                                Point2D p = h.getPoint();
255
                                                                                h.set(p.getX() + (difX), p.getY() + (difY));
256
                                                                        }
257
                                                                }
258
                                                                int index = addGeometry(geom, feaCloned
259
                                                                                .getAttributes());
260
                                                                selectedRowAux.add(new DefaultRowEdited(
261
                                                                                feaCloned, IRowEdited.STATUS_ADDED,
262
                                                                                index));
263
                                                }
264
                                        }
265
                                }
266
                                vea.endComplexRow(getName());
267
                                vle.setSelectionCache(VectorialLayerEdited.SAVEPREVIOUS, selectedRowAux);
268
                        PluginServices.getMDIManager().restoreCursor();
269
                        end();
270
                } else {// Cancelado
271

    
272
                }
273

    
274
        }
275

    
276
        /**
277
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
278
     * editableFeatureSource que ya estar? creado.
279
     *
280
     * @param x par?metro x del punto que se pase en esta transici?n.
281
     * @param y par?metro y del punto que se pase en esta transici?n.
282
     */
283
    public void addPoint(double x, double y,InputEvent event) {
284
        //MatrixCADToolState actualState = _fsm.getState();
285
        MatrixCADToolState previousState= (MatrixCADToolState)_fsm.getPreviousState();
286
        String status = previousState.getName();
287
        if (status.equals("Matrix.Start")||status.equals("Matrix.FirstPoint")) {
288
                firstPoint = new Point2D.Double(x, y);
289
            } else if (status.equals("Matrix.SecondPoint")) {
290
                    secondPoint = new Point2D.Double(x,y);
291
                    if (option.equals("lagX") || option.equals("lagXY")) {
292
                            operations.setDistColumns(secondPoint.getX()-firstPoint.getX());
293
                            matrixProperty.refreshLagX();
294
                    }
295
                    if (option.equals("lagY") || option.equals("lagXY")) {
296
                            operations.setDistRows(secondPoint.getY()-firstPoint.getY());
297
                            matrixProperty.refreshLagY();
298
                    }
299
                    if (option.equals("rotation")) {
300

    
301
                            double w;
302
                            double h;
303
                            w = secondPoint.getX() - firstPoint.getX();
304
                            h = secondPoint.getY() - firstPoint.getY();
305
                            operations.setRotation((-Math.atan2(w,
306
                                                h)
307
                                                + (Math.PI / 2))*180/Math.PI);
308
                            matrixProperty.refreshRotation();
309
                    }
310
                    firstPoint=null;
311
                    PluginServices.getMDIManager().addWindow(matrixProperty);
312
            }
313
    }
314

    
315
    /**
316
     * M?todo para dibujar lo necesario para el estado en el que nos
317
     * encontremos.
318
     *
319
     * @param g Graphics sobre el que dibujar.
320
     * @param x par?metro x del punto que se pase para dibujar.
321
     * @param y par?metro x del punto que se pase para dibujar.
322
     */
323
    public void drawOperation(Graphics g, double x, double y) {
324
        if (_fsm==null || firstPoint==null)
325
                return;
326
        GeneralPathX gpx=new GeneralPathX();
327
            gpx.moveTo(firstPoint.getX(),firstPoint.getY());
328
            gpx.lineTo(x,y);
329
            VectorialLayerEdited vle=getVLE();
330
            ViewPort vp=vle.getLayer().getMapContext().getViewPort();
331
            ShapeFactory.createPolyline2D(gpx).draw((Graphics2D)g,vp,CADTool.modifySymbol);
332

    
333
   }
334

    
335
    /**
336
         * Add a diferent option.
337
         *
338
         * @param s
339
         *            Diferent option.
340
         */
341
    public void addOption(String s) {
342
            option=s;
343
            PluginServices.getMDIManager().closeWindow(matrixProperty);
344
            /* MatrixCADToolState actualState = _fsm
345
                                .getState();
346
                String status = actualState.getName();
347

348
                if (status.equals("Matrix.LagXY")) {
349

350
                }else if (status.equals("Matrix.LagX")) {
351

352
                }else if (status.equals("Matrix.LagY")) {
353

354
                }
355
                */
356
    }
357

    
358
    /*
359
         * (non-Javadoc)
360
         *
361
         * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
362
         */
363
    public void addValue(double d) {
364

    
365
    }
366

    
367
        public String getName() {
368
                return PluginServices.getText(this,"matrix_");
369
        }
370

    
371
        public String toString() {
372
                return "_matrix";
373
        }
374

    
375
        public MatrixOperations getOperations() {
376
                return operations;
377
        }
378

    
379
}