Statistics
| Revision:

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

History | View | Annotate | Download (8.91 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.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, event);
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 && !CADExtension.getCADToolAdapter().getCadTool().getClass().getName().equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
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,InputEvent event) {
133
        RotateCADToolState actualState = (RotateCADToolState) _fsm.getPreviousState();
134
        String status = actualState.getName();
135
        ArrayList selectedRow=getSelectedRow();
136

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

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

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

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

    
157
                                            getCadToolAdapter().getVectorialAdapter().modifyRow(i, fea,getName());
158
                                    }
159

    
160
                                    getCadToolAdapter().getVectorialAdapter().endComplexRow();
161
                            } catch (DriverIOException e) {
162
                                    e.printStackTrace();
163
                            } catch (IOException e1) {
164
                                    e1.printStackTrace();
165
                            }
166

    
167
                            PluginServices.getMDIManager().restoreCursor();
168
                    }
169
    }
170

    
171
    /**
172
     * M?todo para dibujar la lo necesario para el estado en el que nos
173
     * encontremos.
174
     *
175
     * @param g Graphics sobre el que dibujar.
176
     * @param x par?metro x del punto que se pase para dibujar.
177
     * @param y par?metro x del punto que se pase para dibujar.
178
     */
179
    public void drawOperation(Graphics g, double x, double y) {
180
        RotateCADToolState actualState = ((RotateCADToolContext) _fsm).getState();
181
        String status = actualState.getName();
182
        ArrayList selectedRow=getSelectedRow();
183

    
184
        drawHandlers(g, selectedRow,
185
                 getCadToolAdapter().getMapControl().getViewPort()
186
                     .getAffineTransform());
187
        if (status.equals("Rotate.AngleOrPoint")) {
188
                        double w;
189
                        double h;
190
                        w = x - firstPoint.getX();
191
                        h = y - firstPoint.getY();
192

    
193
                        ///AffineTransform at = AffineTransform.getRotateInstance(+Math.atan2(
194
                        ///                        w, h) - (Math.PI / 2), (int) point.getX(),
195
                        ///                (int) point.getY());
196
                        ///Image img = getCadToolAdapter().getVectorialAdapter().getImage();
197

    
198
                        ///((Graphics2D) g).drawImage(img, at, null);
199

    
200
                        ///drawLine((Graphics2D) g, firstPoint, new Point2D.Double(x, y));
201

    
202

    
203
                           try {
204
                                   for (int i = 0; i < selectedRow.size(); i++) {
205
                                           IGeometry geometry = getCadToolAdapter().getVectorialAdapter().getShape(i);
206
                                                                // Rotamos la geometry
207
                                                                UtilFunctions.rotateGeom(geometry, -Math.atan2(w,h)+Math.PI/2,firstPoint.getX(),firstPoint.getY());
208

    
209
                                           geometry.draw((Graphics2D) g,
210
                                                           getCadToolAdapter().getMapControl().getViewPort(),
211
                                                           CADTool.drawingSymbol);
212
                                           GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
213
                                                           2);
214
                                           elShape.moveTo(firstPoint.getX(), firstPoint.getY());
215
                                           elShape.lineTo(x, y);
216
                                           ShapeFactory.createPolyline2D(
217
                                                           elShape).draw((Graphics2D) g,
218
                                                                           getCadToolAdapter().getMapControl().getViewPort(),
219
                                                                           CADTool.drawingSymbol);
220

    
221
                           }
222
                           } catch (DriverIOException e) {
223
                                   e.printStackTrace();
224
                           }
225

    
226
                }
227
    }
228

    
229
    /**
230
     * Add a diferent option.
231
     *
232
     * @param s Diferent option.
233
     */
234
    public void addOption(String s) {
235
    }
236

    
237
    /* (non-Javadoc)
238
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
239
     */
240
    public void addValue(double d) {
241
            RotateCADToolState actualState = (RotateCADToolState) _fsm.getPreviousState();
242
        String status = actualState.getName();
243
        ArrayList selectedRow=getSelectedRow();
244

    
245
            if (status.equals("Rotate.AngleOrPoint")) {
246
                        try {
247
                                for (int i = 0; i < selectedRow.size(); i++) {
248
                                                DefaultFeature fea = (DefaultFeature)getCadToolAdapter().getVectorialAdapter().getRow(i).cloneRow();
249
                                                // Rotamos la geometry
250
                                                AffineTransform at = new AffineTransform();
251
                                                at.rotate(Math.toRadians(d),
252
                                                            firstPoint.getX(), firstPoint.getY());
253
                                                fea.getGeometry().transform(at);
254
                                            getCadToolAdapter().getVectorialAdapter().modifyRow(i, fea,getName());
255

    
256
                                }
257
                        } catch (DriverIOException e) {
258
                                e.printStackTrace();
259
                        } catch (IOException e1) {
260
                                e1.printStackTrace();
261
                        }
262

    
263
                }
264
    }
265

    
266
        public String getName() {
267
                return "ROTAR";
268
        }
269
}