Statistics
| Revision:

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

History | View | Annotate | Download (12.3 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.DefaultCADTool;
63
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
64
import com.iver.cit.gvsig.gui.cad.panels.matrix.MatrixOperations;
65
import com.iver.cit.gvsig.gui.cad.panels.matrix.MatrixProperty;
66
import com.iver.cit.gvsig.gui.cad.tools.smc.MatrixCADToolContext;
67
import com.iver.cit.gvsig.gui.cad.tools.smc.MatrixCADToolContext.MatrixCADToolState;
68
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
69

    
70

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

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

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

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

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

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

    
143
        }
144

    
145
        public void endMatrix() {
146
                if (operations.isAccepted()) {
147
                        PluginServices.getMDIManager().setWaitCursor();
148
                        ArrayList selectedRow = getSelectedRows();
149
                        ArrayList selectedRowAux = new ArrayList();
150
                        VectorialLayerEdited vle = getVLE();
151
                        VectorialEditableAdapter vea = vle.getVEA();
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
                        PluginServices.getMDIManager().restoreCursor();
272
                        end();
273
                } else {// Cancelado
274

    
275
                }
276

    
277
        }
278

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

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

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

    
336
   }
337

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

351
                if (status.equals("Matrix.LagXY")) {
352

353
                }else if (status.equals("Matrix.LagX")) {
354

355
                }else if (status.equals("Matrix.LagY")) {
356

357
                }
358
                */
359
    }
360

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

    
368
    }
369

    
370
        public String getName() {
371
                return PluginServices.getText(this,"matrix_");
372
        }
373

    
374
        public String toString() {
375
                return "_matrix";
376
        }
377

    
378
        public MatrixOperations getOperations() {
379
                return operations;
380
        }
381

    
382
}