Statistics
| Revision:

root / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / RotateCADTool.java @ 4313

History | View | Annotate | Download (9.77 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

    
50
import com.iver.andami.PluginServices;
51
import com.iver.cit.gvsig.CADExtension;
52
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
53
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
54
import com.iver.cit.gvsig.fmap.core.IGeometry;
55
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
56
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
57
import com.iver.cit.gvsig.fmap.edition.UtilFunctions;
58
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
59
import com.iver.cit.gvsig.fmap.layers.FBitSet;
60
import com.iver.cit.gvsig.gui.cad.CADTool;
61
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
62
import com.iver.cit.gvsig.gui.cad.tools.smc.RotateCADToolContext;
63
import com.iver.cit.gvsig.gui.cad.tools.smc.RotateCADToolContext.RotateCADToolState;
64

    
65

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

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

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

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

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

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

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

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

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

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

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

    
149
                            try {
150
                                    getCadToolAdapter().getVectorialAdapter().startComplexRow();
151

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

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

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

    
171
                            PluginServices.getMDIManager().restoreCursor();
172
                    }
173
    }
174

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

    
189
        try {
190
            drawHandlers(g, selection,
191
                getCadToolAdapter().getMapControl().getViewPort()
192
                    .getAffineTransform());
193
        } catch (DriverIOException e) {
194
            e.printStackTrace();
195
        }
196

    
197
        if (status.equals("Rotate.AngleOrPoint")) {
198
                        Point2D point = getCadToolAdapter().getMapControl().getViewPort()
199
                                                                .fromMapPoint(firstPoint.getX(),
200
                                        firstPoint.getY());
201
                        double w;
202
                        double h;
203
                        w = x - firstPoint.getX();
204
                        h = y - firstPoint.getY();
205

    
206
                        ///AffineTransform at = AffineTransform.getRotateInstance(+Math.atan2(
207
                        ///                        w, h) - (Math.PI / 2), (int) point.getX(),
208
                        ///                (int) point.getY());
209
                        ///Image img = getCadToolAdapter().getVectorialAdapter().getImage();
210

    
211
                        ///((Graphics2D) g).drawImage(img, at, null);
212

    
213
                        ///drawLine((Graphics2D) g, firstPoint, new Point2D.Double(x, y));
214

    
215

    
216
                           try {
217
                                   for (int i = selection.nextSetBit(0);
218
                                   i >= 0;
219
                                   i = selection.nextSetBit(i + 1)){
220
                                           IGeometry geometry = getCadToolAdapter().getVectorialAdapter().getShape(i);
221
                                                                // Rotamos la geometry
222
                                                                UtilFunctions.rotateGeom(geometry, -Math.atan2(w,h)+Math.PI/2,firstPoint.getX(),firstPoint.getY());
223

    
224
                                           // geometry.rotate(-Math.atan2(w,h)+Math.PI/2,firstPoint.getX(),firstPoint.getY());
225
                                           geometry.draw((Graphics2D) g,
226
                                                           getCadToolAdapter().getMapControl().getViewPort(),
227
                                                           CADTool.drawingSymbol);
228
                                           GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
229
                                                           2);
230
                                           elShape.moveTo(firstPoint.getX(), firstPoint.getY());
231
                                           elShape.lineTo(x, y);
232
                                           ShapeFactory.createPolyline2D(
233
                                                           elShape).draw((Graphics2D) g,
234
                                                                           getCadToolAdapter().getMapControl().getViewPort(),
235
                                                                           CADTool.drawingSymbol);
236

    
237
                           }
238
                           } catch (DriverIOException e) {
239
                                   e.printStackTrace();
240
                           }
241

    
242
                }
243
    }
244

    
245
    /**
246
     * Add a diferent option.
247
     *
248
     * @param s Diferent option.
249
     */
250
    public void addOption(String s) {
251
    }
252

    
253
    /* (non-Javadoc)
254
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
255
     */
256
    public void addValue(double d) {
257
            RotateCADToolState actualState = (RotateCADToolState) _fsm.getPreviousState();
258
        String status = actualState.getName();
259
        VectorialEditableAdapter vea = getCadToolAdapter().getVectorialAdapter();
260
        FBitSet selection = vea.getSelection();
261

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

    
283
                }
284
    }
285

    
286
        public String getName() {
287
                return "ROTAR";
288
        }
289
}