Statistics
| Revision:

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

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

    
51
import org.gvsig.andami.PluginServices;
52
import org.gvsig.andami.messages.NotificationManager;
53
import org.gvsig.editing.CADExtension;
54
import org.gvsig.editing.gui.cad.DefaultCADTool;
55
import org.gvsig.editing.gui.cad.exception.CommandException;
56
import org.gvsig.editing.gui.cad.tools.smc.RotateCADToolContext;
57
import org.gvsig.editing.gui.cad.tools.smc.RotateCADToolContext.RotateCADToolState;
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.FeatureSet;
65
import org.gvsig.fmap.dal.feature.FeatureStore;
66
import org.gvsig.fmap.geom.Geometry;
67
import org.gvsig.fmap.geom.util.UtilFunctions;
68
import org.gvsig.fmap.mapcontext.ViewPort;
69

    
70

    
71
/**
72
 * DOCUMENT ME!
73
 *
74
 * @author Vicente Caballero Navarro
75
 */
76
public class RotateCADTool extends DefaultCADTool {
77
        private RotateCADToolContext _fsm;
78
        private Point2D firstPoint;
79
        private Point2D lastPoint;
80

    
81
        /**
82
         * Crea un nuevo PolylineCADTool.
83
         */
84
        public RotateCADTool() {
85
        }
86

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

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

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

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

    
127
        /**
128
         * DOCUMENT ME!
129
         */
130
        public void selection() {
131
                FeatureSet selection = null;
132
                try {
133
                        selection = (FeatureSet) getVLE().getFeatureStore().getSelection();
134

    
135
                        if (selection.getSize() == 0
136
                                        && !CADExtension
137
                                                        .getCADTool()
138
                                                        .getClass()
139
                                                        .getName()
140
                                                        .equals(
141
                                                                        "com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
142
                                CADExtension.setCADTool("_selection", false);
143
                                ((SelectionCADTool) CADExtension.getCADTool())
144
                                                .setNextTool("_rotate");
145
                        }
146
                } catch (ReadException e) {
147
                        // TODO Auto-generated catch block
148
                        e.printStackTrace();
149
                } catch (DataException e) {
150
                        // TODO Auto-generated catch block
151
                        e.printStackTrace();
152
                }
153
        }
154

    
155
        /**
156
         * Equivale al transition del prototipo pero sin pasarle como par?metro el
157
         * editableFeatureSource que ya estar? creado.
158
         *
159
         * @param x
160
         *            par?metro x del punto que se pase en esta transici?n.
161
         * @param y
162
         *            par?metro y del punto que se pase en esta transici?n.
163
         */
164
        public void addPoint(double x, double y, InputEvent event) {
165
                RotateCADToolState actualState = (RotateCADToolState) _fsm
166
                                .getPreviousState();
167
                String status = actualState.getName();
168
                VectorialLayerEdited vle = getVLE();
169
                FeatureStore featureStore = null;
170
                try {
171
                        featureStore = vle.getFeatureStore();
172
                } catch (ReadException e1) {
173
                        // TODO Auto-generated catch block
174
                        e1.printStackTrace();
175
                }
176
                DisposableIterator iterator = null;
177
                try {
178
                        FeatureSet selection = (FeatureSet) featureStore.getSelection();// getSelectedRows();
179
                        ArrayList selectedRowAux = new ArrayList();
180

    
181
                        if (status.equals("Rotate.PointMain")) {
182
                                firstPoint = new Point2D.Double(x, y);
183
                        } else if (status.equals("Rotate.AngleOrPoint")) {
184
                                PluginServices.getMDIManager().setWaitCursor();
185
                                lastPoint = new Point2D.Double(x, y);
186

    
187
                                double w;
188
                                double h;
189
                                w = lastPoint.getX() - firstPoint.getX();
190
                                h = lastPoint.getY() - firstPoint.getY();
191

    
192
                                featureStore.beginEditingGroup(getName());
193
                                try {
194
                                        iterator = selection.iterator();
195
                                        while (iterator.hasNext()) {
196
                                                Feature feature = (Feature) iterator.next();
197

    
198
                                                // }
199
                                                // for (int i = 0; i < selectedRow.size(); i++) {
200
                                                // DefaultRowEdited row=(DefaultRowEdited)
201
                                                // selectedRow.get(i);
202
                                                // DefaultFeature fea = (DefaultFeature)
203
                                                // row.getLinkedRow().cloneRow();
204
                                                // Rotamos la geometry
205
                                                Geometry geometry = (feature.getDefaultGeometry())
206
                                                                .cloneGeometry();
207
                                                EditableFeature eFeature = feature.getEditable();
208
                                                UtilFunctions.rotateGeom(geometry, -Math.atan2(w, h)
209
                                                                + (Math.PI / 2), firstPoint.getX(), firstPoint
210
                                                                .getY());
211
                                                eFeature.setGeometry(featureStore
212
                                                                .getDefaultFeatureType()
213
                                                                .getDefaultGeometryAttributeName(), geometry);
214
                                                featureStore.update(eFeature);
215
                                                // vea.modifyRow(row.getIndex(), fea,
216
                                                // getName(),EditionEvent.GRAPHIC);
217
                                                // selectedRowAux.add(new
218
                                                // DefaultRowEdited(fea,IRowEdited.STATUS_MODIFIED,row.getIndex()));
219
                                        }
220
                                } finally {
221

    
222
                                        featureStore.endEditingGroup();
223
                                }
224
                                // vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS,
225
                                // selectedRowAux);
226
                                // clearSelection();
227
                                // selectedRow.addAll(selectedRowAux);
228

    
229
                                PluginServices.getMDIManager().restoreCursor();
230
                        }
231
                } catch (DataException e) {
232
                        NotificationManager.addError(e.getMessage(), e);
233
                } finally {
234
                        if (iterator != null) {
235
                                iterator.dispose();
236
                        }
237
                }
238
        }
239

    
240
        /**
241
         * M?todo para dibujar la lo necesario para el estado en el que nos
242
         * encontremos.
243
         *
244
         * @param g
245
         *            Graphics sobre el que dibujar.
246
         * @param x
247
         *            par?metro x del punto que se pase para dibujar.
248
         * @param y
249
         *            par?metro x del punto que se pase para dibujar.
250
         */
251
        public void drawOperation(Graphics g, double x, double y) {
252
                RotateCADToolState actualState = _fsm.getState();
253
                String status = actualState.getName();
254
                VectorialLayerEdited vle = getVLE();
255
                // ArrayList selectedRow=getSelectedRows();
256

    
257
                // drawHandlers(g, selectedRow,
258
                // getCadToolAdapter().getMapControl().getViewPort()
259
                // .getAffineTransform());
260
                if (status.equals("Rotate.AngleOrPoint")) {
261
                        double w;
262
                        double h;
263
                        w = x - firstPoint.getX();
264
                        h = y - firstPoint.getY();
265
                        ViewPort vp = vle.getLayer().getMapContext().getViewPort();
266
                        Point2D point = vp.fromMapPoint(firstPoint.getX(), firstPoint
267
                                        .getY());
268
                        AffineTransform at = AffineTransform.getRotateInstance(Math.atan2(
269
                                        w, h)
270
                                        - (Math.PI / 2), (int) point.getX(), (int) point.getY());
271

    
272
                        Image imgSel = vle.getSelectionImage();
273
                        ((Graphics2D) g).drawImage(imgSel, at, null);
274
                        drawLine((Graphics2D) g, firstPoint, new Point2D.Double(x, y),
275
                                        DefaultCADTool.axisReferencesSymbol);
276
                        // /AffineTransform at =
277
                        // AffineTransform.getRotateInstance(+Math.atan2(
278
                        // / w, h) - (Math.PI / 2), (int) point.getX(),
279
                        // / (int) point.getY());
280
                        // /Image img =
281
                        // getCadToolAdapter().getVectorialAdapter().getImage();
282

    
283
                        // /((Graphics2D) g).drawImage(img, at, null);
284

    
285
                        // /drawLine((Graphics2D) g, firstPoint, new Point2D.Double(x, y));
286

    
287
                        /*
288
                         * for (int i = 0; i < selectedRow.size(); i++) { // IGeometry
289
                         * geometry = //
290
                         * getCadToolAdapter().getVectorialAdapter().getShape(i); IRowEdited
291
                         * edRow = (IRowEdited) selectedRow.get(i); IFeature feat =
292
                         * (IFeature) edRow.getLinkedRow(); IGeometry geometry =
293
                         * feat.getGeometry().cloneGeometry(); // Rotamos la geometry
294
                         * UtilFunctions.rotateGeom(geometry, -Math.atan2(w, h) + Math.PI /
295
                         * 2, firstPoint.getX(), firstPoint.getY());
296
                         *
297
                         * geometry.draw((Graphics2D) g, getCadToolAdapter()
298
                         * .getMapControl().getViewPort(), CADTool.drawingSymbol);
299
                         * GeneralPathX elShape = new GeneralPathX(
300
                         * GeneralPathX.WIND_EVEN_ODD, 2); elShape.moveTo(firstPoint.getX(),
301
                         * firstPoint.getY()); elShape.lineTo(x, y);
302
                         * ShapeFactory.createPolyline2D(elShape).draw((Graphics2D) g,
303
                         * getCadToolAdapter().getMapControl().getViewPort(),
304
                         * CADTool.drawingSymbol); }
305
                         */
306
                } else {
307
                        if (!vle.getLayer().isVisible()) {
308
                                return;
309
                        }
310
                        Image imgSel = vle.getSelectionImage();
311
                        if (imgSel != null) {
312
                                g.drawImage(imgSel, 0, 0, null);
313
                        }
314
                        Image imgHand = vle.getHandlersImage();
315
                        if (imgHand != null) {
316
                                g.drawImage(imgHand, 0, 0, null);
317
                        }
318
                }
319
        }
320

    
321
        /**
322
         * Add a diferent option.
323
         *
324
         * @param s
325
         *            Diferent option.
326
         */
327
        public void addOption(String s) {
328
        }
329

    
330
        /*
331
         * (non-Javadoc)
332
         *
333
         * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
334
         */
335
        public void addValue(double d) {
336
                RotateCADToolState actualState = (RotateCADToolState) _fsm
337
                                .getPreviousState();
338
                String status = actualState.getName();
339
                VectorialLayerEdited vle = getVLE();
340
                FeatureStore featureStore = null;
341
                try {
342
                        featureStore = vle.getFeatureStore();
343
                } catch (ReadException e1) {
344
                        // TODO Auto-generated catch block
345
                        e1.printStackTrace();
346
                }
347
                DisposableIterator iterator = null;
348
                try {
349
                        FeatureSet selection = (FeatureSet) featureStore.getSelection();// getSelectedRows();
350

    
351
                        if (status.equals("Rotate.AngleOrPoint")) {
352

    
353
                                featureStore.beginEditingGroup(getName());
354
                                try {
355
                                        // /ArrayList selectedRowAux=new ArrayList();
356
                                        iterator = selection.iterator();
357
                                        while (iterator.hasNext()) {
358
                                                Feature feature = (Feature) iterator.next();
359

    
360
                                                // }
361
                                                // for (int i = 0; i < selectedRow.size(); i++) {
362
                                                // DefaultRowEdited row=(DefaultRowEdited)
363
                                                // selectedRow.get(i);
364
                                                // DefaultFeature fea = (DefaultFeature)
365
                                                // row.getLinkedRow().cloneRow();
366
                                                // Rotamos la geometry
367
                                                AffineTransform at = new AffineTransform();
368
                                                at.rotate(Math.toRadians(d), firstPoint.getX(),
369
                                                                firstPoint.getY());
370
                                                Geometry geometry = (feature.getDefaultGeometry())
371
                                                                .cloneGeometry();
372
                                                EditableFeature eFeature = feature.getEditable();
373
                                                geometry.transform(at);
374
                                                eFeature.setGeometry(featureStore
375
                                                                .getDefaultFeatureType()
376
                                                                .getDefaultGeometryAttributeName(), geometry);
377
                                                featureStore.update(eFeature);
378
                                                // vea.modifyRow(row.getIndex(),
379
                                                // fea,getName(),EditionEvent.GRAPHIC);
380
                                                // /selectedRowAux.add(new
381
                                                // DefaultRowEdited(fea,IRowEdited.STATUS_MODIFIED,index));
382
                                        }
383
                                } finally {
384
                                        featureStore.endEditingGroup();
385
                                }
386
                                clearSelection();
387
                                // /selectedRow=selectedRowAux;
388

    
389
                        }
390
                } catch (DataException e) {
391
                        NotificationManager.addError(e.getMessage(), e);
392
                } finally {
393
                        if (iterator != null) {
394
                                iterator.dispose();
395
                        }
396
                }
397
        }
398

    
399
        public String getName() {
400
                return PluginServices.getText(this, "rotate_");
401
        }
402

    
403
        public String toString() {
404
                return "_rotate";
405
        }
406

    
407
}