Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / RotateCADTool.java @ 23086

History | View | Annotate | Download (11.9 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.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
import java.util.Iterator;
51

    
52
import org.gvsig.fmap.data.DataException;
53
import org.gvsig.fmap.data.ReadException;
54
import org.gvsig.fmap.data.feature.Feature;
55
import org.gvsig.fmap.data.feature.FeatureCollection;
56
import org.gvsig.fmap.data.feature.FeatureStore;
57
import org.gvsig.fmap.geom.Geometry;
58
import org.gvsig.fmap.geom.util.UtilFunctions;
59
import org.gvsig.fmap.mapcontext.ViewPort;
60

    
61
import com.iver.andami.PluginServices;
62
import com.iver.andami.messages.NotificationManager;
63
import com.iver.cit.gvsig.CADExtension;
64
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
65
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
66
import com.iver.cit.gvsig.gui.cad.tools.smc.RotateCADToolContext;
67
import com.iver.cit.gvsig.gui.cad.tools.smc.RotateCADToolContext.RotateCADToolState;
68
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
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
    /* (non-Javadoc)
96
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
97
     */
98
    public void transition(double x, double y, InputEvent event) {
99
        _fsm.addPoint(x, y, event);
100
    }
101

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

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

    
118
    /**
119
     * DOCUMENT ME!
120
     */
121
    public void selection() {
122
            FeatureCollection selection=null;
123
            try {
124
                    selection = (FeatureCollection)getVLE().getFeatureStore().getSelection();
125
            } catch (ReadException e) {
126
                    // TODO Auto-generated catch block
127
                    e.printStackTrace();
128
            }
129
        if (selection.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
                "_rotate");
133
        }
134
    }
135

    
136
    /**
137
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
138
     * editableFeatureSource que ya estar? creado.
139
     *
140
     * @param x par?metro x del punto que se pase en esta transici?n.
141
     * @param y par?metro y del punto que se pase en esta transici?n.
142
     */
143
    public void addPoint(double x, double y,InputEvent event) {
144
            RotateCADToolState actualState = (RotateCADToolState) _fsm.getPreviousState();
145
            String status = actualState.getName();
146
            VectorialLayerEdited vle=getVLE();
147
            FeatureStore featureStore=null;
148
                try {
149
                        featureStore = vle.getFeatureStore();
150
                } catch (ReadException e1) {
151
                        // TODO Auto-generated catch block
152
                        e1.printStackTrace();
153
                }
154
            FeatureCollection selection=(FeatureCollection)featureStore.getSelection();//getSelectedRows();
155
            ArrayList selectedRowAux=new ArrayList();
156

    
157
            if (status.equals("Rotate.PointMain")) {
158
                    firstPoint = new Point2D.Double(x, y);
159
            } else if (status.equals("Rotate.AngleOrPoint")) {
160
                    PluginServices.getMDIManager().setWaitCursor();
161
                    lastPoint = new Point2D.Double(x,y);
162

    
163
                    double w;
164
                    double h;
165
                    w = lastPoint.getX() - firstPoint.getX();
166
                    h = lastPoint.getY() - firstPoint.getY();
167

    
168
                    try {
169
                            featureStore.getCommandsRecord().startComplex();
170
                            Iterator iterator=selection.iterator();
171
                            while (iterator.hasNext()) {
172
                                        Feature feature = (Feature) iterator.next();
173

    
174
//                                }
175
//                            for (int i = 0; i < selectedRow.size(); i++) {
176
//                                    DefaultRowEdited row=(DefaultRowEdited) selectedRow.get(i);
177
//                                    DefaultFeature fea = (DefaultFeature) row.getLinkedRow().cloneRow();
178
                                    // Rotamos la geometry
179
                                    Geometry geometry=((Geometry)feature.getDefaultGeometry()).cloneGeometry();
180
                                    feature.editing();
181
                                        UtilFunctions.rotateGeom(geometry, -Math.atan2(w,
182
                                                    h)
183
                                                    + (Math.PI / 2), firstPoint.getX(), firstPoint
184
                                                    .getY());
185
                                        feature.setGeometry(featureStore.getDefaultFeatureType().getDefaultGeometry(),geometry);
186
                                        featureStore.update(feature);
187
//                                    vea.modifyRow(row.getIndex(), fea,
188
//                                                    getName(),EditionEvent.GRAPHIC);
189
//                                    selectedRowAux.add(new DefaultRowEdited(fea,IRowEdited.STATUS_MODIFIED,row.getIndex()));
190
                            }
191

    
192
                            featureStore.getCommandsRecord().endComplex(getName());
193
//                            vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS, selectedRowAux);
194
                            //clearSelection();
195
                            //selectedRow.addAll(selectedRowAux);
196
                    } catch (ReadException e) {
197
                            NotificationManager.addError(e.getMessage(),e);
198
                    } catch (DataException e) {
199
                                // TODO Auto-generated catch block
200
                                e.printStackTrace();
201
                        }
202
                    PluginServices.getMDIManager().restoreCursor();
203
            }
204
    }
205

    
206
    /**
207
     * M?todo para dibujar la lo necesario para el estado en el que nos
208
     * encontremos.
209
     *
210
     * @param g Graphics sobre el que dibujar.
211
     * @param x par?metro x del punto que se pase para dibujar.
212
     * @param y par?metro x del punto que se pase para dibujar.
213
     */
214
    public void drawOperation(Graphics g, double x, double y) {
215
        RotateCADToolState actualState = _fsm.getState();
216
        String status = actualState.getName();
217
        VectorialLayerEdited vle=getVLE();
218
        //ArrayList selectedRow=getSelectedRows();
219

    
220
       // drawHandlers(g, selectedRow,
221
       //          getCadToolAdapter().getMapControl().getViewPort()
222
       //              .getAffineTransform());
223
        if (status.equals("Rotate.AngleOrPoint")) {
224
                        double w;
225
                        double h;
226
                        w = x - firstPoint.getX();
227
                        h = y - firstPoint.getY();
228
                        ViewPort vp=vle.getLayer().getMapContext().getViewPort();
229
                        Point2D point = vp.fromMapPoint(firstPoint.getX(),
230
                                                                firstPoint.getY());
231
                        AffineTransform at = AffineTransform.getRotateInstance(Math.atan2(
232
                                                                w, h) - (Math.PI / 2), (int) point.getX(),
233
                                                        (int) point.getY());
234

    
235
                        Image imgSel = vle.getSelectionImage();
236
            ((Graphics2D)g).drawImage(imgSel, at, null);
237
            drawLine((Graphics2D) g, firstPoint, new Point2D.Double(x, y),DefaultCADTool.axisReferencesSymbol);
238
                        ///AffineTransform at = AffineTransform.getRotateInstance(+Math.atan2(
239
                        ///                        w, h) - (Math.PI / 2), (int) point.getX(),
240
                        ///                (int) point.getY());
241
                        ///Image img = getCadToolAdapter().getVectorialAdapter().getImage();
242

    
243
                        ///((Graphics2D) g).drawImage(img, at, null);
244

    
245
                        ///drawLine((Graphics2D) g, firstPoint, new Point2D.Double(x, y));
246

    
247
/*
248
                             for (int i = 0; i < selectedRow.size(); i++) {
249
                                           // IGeometry geometry =
250
                                                                // getCadToolAdapter().getVectorialAdapter().getShape(i);
251
                                IRowEdited edRow = (IRowEdited) selectedRow.get(i);
252
                                IFeature feat = (IFeature) edRow.getLinkedRow();
253
                                IGeometry geometry = feat.getGeometry().cloneGeometry();
254
                                // Rotamos la geometry
255
                                UtilFunctions.rotateGeom(geometry, -Math.atan2(w, h) + Math.PI
256
                                                / 2, firstPoint.getX(), firstPoint.getY());
257

258
                                geometry.draw((Graphics2D) g, getCadToolAdapter()
259
                                                .getMapControl().getViewPort(), CADTool.drawingSymbol);
260
                                GeneralPathX elShape = new GeneralPathX(
261
                                                GeneralPathX.WIND_EVEN_ODD, 2);
262
                                elShape.moveTo(firstPoint.getX(), firstPoint.getY());
263
                                elShape.lineTo(x, y);
264
                                ShapeFactory.createPolyline2D(elShape).draw((Graphics2D) g,
265
                                                getCadToolAdapter().getMapControl().getViewPort(),
266
                                                CADTool.drawingSymbol);
267

268
                        }
269
                        */
270
                }else{
271
                        if (!vle.getLayer().isVisible())
272
                                return;
273
                         Image imgSel = vle.getSelectionImage();
274
             if (imgSel!=null)
275
                     g.drawImage(imgSel, 0, 0, null);
276
             Image imgHand = vle.getHandlersImage();
277
             if (imgHand!=null)
278
                     g.drawImage(imgHand, 0, 0, null);
279
                }
280
    }
281

    
282
    /**
283
         * Add a diferent option.
284
         *
285
         * @param s
286
         *            Diferent option.
287
         */
288
    public void addOption(String s) {
289
    }
290

    
291
    /*
292
         * (non-Javadoc)
293
         *
294
         * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
295
         */
296
    public void addValue(double d) {
297
            RotateCADToolState actualState = (RotateCADToolState) _fsm.getPreviousState();
298
        String status = actualState.getName();
299
        VectorialLayerEdited vle=getVLE();
300
        FeatureStore featureStore=null;
301
                try {
302
                        featureStore = vle.getFeatureStore();
303
                } catch (ReadException e1) {
304
                        // TODO Auto-generated catch block
305
                        e1.printStackTrace();
306
                }
307
        FeatureCollection selection=(FeatureCollection)featureStore.getSelection();//getSelectedRows();
308

    
309
        if (status.equals("Rotate.AngleOrPoint")) {
310
                        try {
311

    
312
                                featureStore.getCommandsRecord().startComplex();
313
                                ///ArrayList selectedRowAux=new ArrayList();
314
                                Iterator iterator=selection.iterator();
315
                                while (iterator.hasNext()) {
316
                                        Feature feature = (Feature) iterator.next();
317

    
318
//                                }
319
//                                for (int i = 0; i < selectedRow.size(); i++) {
320
//                                        DefaultRowEdited row=(DefaultRowEdited) selectedRow.get(i);
321
//                                        DefaultFeature fea = (DefaultFeature) row.getLinkedRow().cloneRow();
322
                                                // Rotamos la geometry
323
                                                AffineTransform at = new AffineTransform();
324
                                                at.rotate(Math.toRadians(d),
325
                                                            firstPoint.getX(), firstPoint.getY());
326
                                                Geometry geometry=((Geometry)feature.getDefaultGeometry()).cloneGeometry();
327
                                                feature.editing();
328
                                                geometry.transform(at);
329
                                                feature.setGeometry(featureStore.getDefaultFeatureType().getDefaultGeometry(),geometry);
330
                                                featureStore.update(feature);
331
//                                            vea.modifyRow(row.getIndex(), fea,getName(),EditionEvent.GRAPHIC);
332
                                            ///selectedRowAux.add(new DefaultRowEdited(fea,IRowEdited.STATUS_MODIFIED,index));
333
                                }
334
                                featureStore.getCommandsRecord().endComplex(getName());
335
                                clearSelection();
336
                                ///selectedRow=selectedRowAux;
337
                        } catch (ReadException e) {
338
                                NotificationManager.addError(e.getMessage(),e);
339
                        } catch (DataException e) {
340
                                // TODO Auto-generated catch block
341
                                e.printStackTrace();
342
                        }
343
                }
344
    }
345

    
346
        public String getName() {
347
                return PluginServices.getText(this,"rotate_");
348
        }
349

    
350
        public String toString() {
351
                return "_rotate";
352
        }
353

    
354
}