Statistics
| Revision:

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

History | View | Annotate | Download (10.2 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.io.IOException;
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.core.DefaultFeature;
54
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
55
import com.iver.cit.gvsig.fmap.core.IGeometry;
56
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
57
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
58
import com.iver.cit.gvsig.fmap.edition.UtilFunctions;
59
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
60
import com.iver.cit.gvsig.fmap.layers.FBitSet;
61
import com.iver.cit.gvsig.gui.cad.CADTool;
62
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
63
import com.iver.cit.gvsig.gui.cad.tools.smc.RotateCADToolContext;
64
import com.iver.cit.gvsig.gui.cad.tools.smc.RotateCADToolContext.RotateCADToolState;
65
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
66

    
67

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

    
78
    /**
79
     * Crea un nuevo PolylineCADTool.
80
     */
81
    public RotateCADTool() {
82
    }
83

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

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

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

    
106
    /* (non-Javadoc)
107
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
108
     */
109
    public void transition(String s) {
110
        _fsm.addOption(s);
111
    }
112

    
113
    /**
114
     * DOCUMENT ME!
115
     */
116
    public void selection() {
117
        FBitSet selection = CADExtension.getCADToolAdapter()
118
                                        .getVectorialAdapter().getSelection();
119

    
120
        if (selection.cardinality() == 0) {
121
            CADExtension.setCADTool("selection");
122
            ((SelectionCADTool) CADExtension.getCADToolAdapter().getCadTool()).setNextTool(
123
                "rotate");
124
        }
125
    }
126

    
127
    /**
128
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
129
     * editableFeatureSource que ya estar? creado.
130
     *
131
     * @param x par?metro x del punto que se pase en esta transici?n.
132
     * @param y par?metro y del punto que se pase en esta transici?n.
133
     */
134
    public void addPoint(double x, double y,InputEvent event) {
135
        RotateCADToolState actualState = (RotateCADToolState) _fsm.getPreviousState();
136
        String status = actualState.getName();
137
        VectorialEditableAdapter vea = getCadToolAdapter().getVectorialAdapter();
138
        FBitSet selection = vea.getSelection();
139

    
140
        if (status.equals("Rotate.PointMain")) {
141
                firstPoint = new Point2D.Double(x, y);
142
                    } else if (status.equals("Rotate.AngleOrPoint")) {
143
                            PluginServices.getMDIManager().setWaitCursor();
144
                            lastPoint = new Point2D.Double(x,y);
145

    
146
                            double w;
147
                            double h;
148
                            w = lastPoint.getX() - firstPoint.getX();
149
                            h = lastPoint.getY() - firstPoint.getY();
150

    
151
                            try {
152
                                    getCadToolAdapter().getVectorialAdapter().startComplexRow();
153

    
154
                                    for (int i = selection.nextSetBit(0); i >= 0;
155
                                                    i = selection.nextSetBit(i + 1)) {
156
                                            DefaultFeature fea = (DefaultFeature)getCadToolAdapter().getVectorialAdapter().getRow(i).cloneRow();
157
                                                // Rotamos la geometry
158
                                                UtilFunctions.rotateGeom(fea.getGeometry(), -Math.atan2(w, h) + (Math.PI / 2),
159
                                                            firstPoint.getX(), firstPoint.getY());
160

    
161
                                            /* fea.getGeometry().rotate(-Math.atan2(w, h) + (Math.PI / 2),
162
                                                    firstPoint.getX(), firstPoint.getY()); */
163
                                            getCadToolAdapter().getVectorialAdapter().modifyRow(i, fea,getName());
164
                                    }
165

    
166
                                    getCadToolAdapter().getVectorialAdapter().endComplexRow();
167
                            } catch (DriverIOException e) {
168
                                    e.printStackTrace();
169
                            } catch (IOException e1) {
170
                                    e1.printStackTrace();
171
                            }
172

    
173
                            PluginServices.getMDIManager().restoreCursor();
174
                    }
175
    }
176

    
177
    /**
178
     * M?todo para dibujar la lo necesario para el estado en el que nos
179
     * encontremos.
180
     *
181
     * @param g Graphics sobre el que dibujar.
182
     * @param x par?metro x del punto que se pase para dibujar.
183
     * @param y par?metro x del punto que se pase para dibujar.
184
     */
185
    public void drawOperation(Graphics g, double x, double y) {
186
        RotateCADToolState actualState = ((RotateCADToolContext) _fsm).getState();
187
        String status = actualState.getName();
188
        VectorialEditableAdapter vea = getCadToolAdapter().getVectorialAdapter();
189
        FBitSet selection = vea.getSelection();
190

    
191
       /* try {
192
            drawHandlers(g, selection,
193
                getCadToolAdapter().getMapControl().getViewPort()
194
                    .getAffineTransform());
195
        } catch (DriverIOException e) {
196
            e.printStackTrace();
197
        }
198
*/
199
        VectorialLayerEdited vle = (VectorialLayerEdited) CADExtension
200
                .getEditionManager().getActiveLayerEdited();
201
            ArrayList selectedRow = vle.getSelectedRow();
202
            drawHandlers(g, selectedRow,
203
                 getCadToolAdapter().getMapControl().getViewPort()
204
                     .getAffineTransform());
205
        if (status.equals("Rotate.AngleOrPoint")) {
206
                        /*Point2D point = getCadToolAdapter().getMapControl().getViewPort()
207
                                                                .fromMapPoint(firstPoint.getX(),
208
                                        firstPoint.getY());*/
209
                        double w;
210
                        double h;
211
                        w = x - firstPoint.getX();
212
                        h = y - firstPoint.getY();
213

    
214
                        ///AffineTransform at = AffineTransform.getRotateInstance(+Math.atan2(
215
                        ///                        w, h) - (Math.PI / 2), (int) point.getX(),
216
                        ///                (int) point.getY());
217
                        ///Image img = getCadToolAdapter().getVectorialAdapter().getImage();
218

    
219
                        ///((Graphics2D) g).drawImage(img, at, null);
220

    
221
                        ///drawLine((Graphics2D) g, firstPoint, new Point2D.Double(x, y));
222

    
223

    
224
                           try {
225
                                   for (int i = selection.nextSetBit(0);
226
                                   i >= 0;
227
                                   i = selection.nextSetBit(i + 1)){
228
                                           IGeometry geometry = getCadToolAdapter().getVectorialAdapter().getShape(i);
229
                                                                // Rotamos la geometry
230
                                                                UtilFunctions.rotateGeom(geometry, -Math.atan2(w,h)+Math.PI/2,firstPoint.getX(),firstPoint.getY());
231

    
232
                                           // geometry.rotate(-Math.atan2(w,h)+Math.PI/2,firstPoint.getX(),firstPoint.getY());
233
                                           geometry.draw((Graphics2D) g,
234
                                                           getCadToolAdapter().getMapControl().getViewPort(),
235
                                                           CADTool.drawingSymbol);
236
                                           GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
237
                                                           2);
238
                                           elShape.moveTo(firstPoint.getX(), firstPoint.getY());
239
                                           elShape.lineTo(x, y);
240
                                           ShapeFactory.createPolyline2D(
241
                                                           elShape).draw((Graphics2D) g,
242
                                                                           getCadToolAdapter().getMapControl().getViewPort(),
243
                                                                           CADTool.drawingSymbol);
244

    
245
                           }
246
                           } catch (DriverIOException e) {
247
                                   e.printStackTrace();
248
                           }
249

    
250
                }
251
    }
252

    
253
    /**
254
     * Add a diferent option.
255
     *
256
     * @param s Diferent option.
257
     */
258
    public void addOption(String s) {
259
    }
260

    
261
    /* (non-Javadoc)
262
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
263
     */
264
    public void addValue(double d) {
265
            RotateCADToolState actualState = (RotateCADToolState) _fsm.getPreviousState();
266
        String status = actualState.getName();
267
        VectorialEditableAdapter vea = getCadToolAdapter().getVectorialAdapter();
268
        FBitSet selection = vea.getSelection();
269

    
270
            if (status.equals("Rotate.AngleOrPoint")) {
271
                        try {
272
                                for (int i = 0; i < getCadToolAdapter().getVectorialAdapter().getRowCount(); i++) {
273
                                        if (selection.get(i)) {
274
                                                DefaultFeature fea = (DefaultFeature)getCadToolAdapter().getVectorialAdapter().getRow(i).cloneRow();
275
                                                // Rotamos la geometry
276
                                                AffineTransform at = new AffineTransform();
277
                                                at.rotate(Math.toRadians(d),
278
                                                            firstPoint.getX(), firstPoint.getY());
279
                                                fea.getGeometry().transform(at);
280
                                            // fea.getGeometry().rotate(Math.toRadians(d),
281
                                            //         firstPoint.getX(), firstPoint.getY());
282
                                            getCadToolAdapter().getVectorialAdapter().modifyRow(i, fea,getName());
283
                                        }
284
                                }
285
                        } catch (DriverIOException e) {
286
                                e.printStackTrace();
287
                        } catch (IOException e1) {
288
                                e1.printStackTrace();
289
                        }
290

    
291
                }
292
    }
293

    
294
        public String getName() {
295
                return "ROTAR";
296
        }
297
}