Statistics
| Revision:

root / branches / gvSIG_CAD / applications / appgvSIG / src / com / iver / cit / gvsig / gui / cad / tools / RotateCadTool.java @ 3513

History | View | Annotate | Download (6.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 com.iver.andami.PluginServices;
44

    
45
import com.iver.cit.gvsig.fmap.core.IGeometry;
46
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
47
import com.iver.cit.gvsig.fmap.edition.EditableFeatureSource;
48
import com.iver.cit.gvsig.fmap.edition.cad.Status;
49
import com.iver.cit.gvsig.fmap.layers.FBitSet;
50
import com.iver.cit.gvsig.gui.cad.automaton.Girar;
51

    
52
import com.iver.fsac.Automaton;
53

    
54
import java.awt.Graphics;
55
import java.awt.Graphics2D;
56
import java.awt.Image;
57
import java.awt.geom.AffineTransform;
58
import java.awt.geom.Point2D;
59

    
60
import java.io.IOException;
61

    
62

    
63
/**
64
 * Herramienta para rotar las geometr?as seleccionadas.
65
 *
66
 * @author Vicente Caballero Navarro
67
 */
68
public class RotateCadTool extends AbstractCadTool {
69
        private static Status[] STATUS = {
70
                        new Status("Precise punto base(x,y):"),
71
                        new Status("Precise angulo de rotaci?n(a):"),
72
                };
73
        private Girar rotateStatus = new Girar();
74
        private Point2D firstPoint;
75
        private Point2D lastPoint;
76

    
77
        /**
78
         * @see com.iver.cit.gvsig.gui.cad.CadTool#transition(java.lang.String,
79
         *                 com.iver.cit.gvsig.fmap.edition.EditableFeatureSource,
80
         *                 com.iver.cit.gvsig.fmap.layers.FBitSet, double[])
81
         */
82
        public int transition(String text,
83
                final EditableFeatureSource editingSource,
84
                final FBitSet selectedGeometries, final double[] ds) {
85
                int ret = rotateStatus.transition(text);
86
                int status = rotateStatus.getStatus();
87

    
88
                if (status == 0) {
89
                        if (selectedGeometries.cardinality() == 0) {
90
                                getCadToolAdapter().pushCadTool(new SelectionCadTool());
91
                        }
92
                } else if (status == 1) {
93
                        if (ds.length != 0) {
94
                                firstPoint = new Point2D.Double(ds[0], ds[1]);
95
                        }
96
                } else if (status == 2) {
97
                        PluginServices.getMDIManager().setWaitCursor();
98
                        lastPoint = new Point2D.Double(ds[0], ds[1]);
99

    
100
                        double w;
101
                        double h;
102
                        w = lastPoint.getX() - firstPoint.getX();
103
                        h = lastPoint.getY() - firstPoint.getY();
104

    
105
                        try {
106
                                editingSource.startComplexGeometry();
107

    
108
                                for (int i = selectedGeometries.nextSetBit(0); i >= 0;
109
                                                i = selectedGeometries.nextSetBit(i + 1)) {
110
                                        IGeometry geometry = editingSource.getGeometry(i);
111
                                        geometry.rotate(-Math.atan2(w, h) + (Math.PI / 2),
112
                                                firstPoint.getX(), firstPoint.getY());
113
                                        editingSource.modifyGeometry(i, geometry);
114
                                }
115

    
116
                                editingSource.endComplexGeometry();
117
                        } catch (DriverIOException e) {
118
                                e.printStackTrace();
119
                        } catch (IOException e1) {
120
                                e1.printStackTrace();
121
                        }
122

    
123
                        PluginServices.getMDIManager().restoreCursor();
124
                        ret = ret | rotateStatus.transition("cancel");
125
                } else if (status == 3) {
126
                        try {
127
                                for (int i = 0; i < editingSource.getGeometryCount(); i++) {
128
                                        if (selectedGeometries.get(i)) {
129
                                                IGeometry geometry = editingSource.getGeometry(i);
130
                                                geometry.rotate(Math.toRadians(ds[0]),
131
                                                        firstPoint.getX(), firstPoint.getY());
132
                                                editingSource.modifyGeometry(i, geometry);
133
                                        }
134
                                }
135
                        } catch (DriverIOException e) {
136
                                e.printStackTrace();
137
                        } catch (IOException e1) {
138
                                e1.printStackTrace();
139
                        }
140

    
141
                        ret = ret | rotateStatus.transition("cancel");
142
                }
143

    
144
                return ret;
145
        }
146

    
147
        /**
148
         * @see com.iver.cit.gvsig.gui.cad.CadTool#drawOperation(java.awt.Graphics,
149
         *                 com.iver.cit.gvsig.fmap.edition.EditableFeatureSource,
150
         *                 com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
151
         */
152
        public void drawOperation(Graphics g, EditableFeatureSource efs,
153
                FBitSet selectedGeometries, double x, double y) {
154
                int status = rotateStatus.getStatus();
155

    
156
                if (status == 1) {
157
                        Point2D point = getCadToolAdapter().getMapControl().getViewPort()
158
                                                                .fromMapPoint(firstPoint.getX(),
159
                                        firstPoint.getY());
160
                        double w;
161
                        double h;
162
                        w = x - firstPoint.getX();
163
                        h = y - firstPoint.getY();
164

    
165
                        AffineTransform at = AffineTransform.getRotateInstance(+Math.atan2(
166
                                                w, h) - (Math.PI / 2), (int) point.getX(),
167
                                        (int) point.getY());
168
                        Image img = getCadToolAdapter().getEditableFeatureSource().getImage();
169

    
170
                        ((Graphics2D) g).drawImage(img, at, null);
171

    
172
                        drawLine((Graphics2D) g, firstPoint, new Point2D.Double(x, y));
173

    
174
                        /*
175
                           try {
176
                                   for (int i = selectedGeometries.nextSetBit(0);
177
                                   i >= 0;
178
                                   i = selectedGeometries.nextSetBit(i + 1)){
179
                                           IGeometry geometry = efs.getGeometry(i);
180
                        
181
                                           geometry.rotate(-Math.atan2(w,h)+Math.PI/2,firstPoint.getX(),firstPoint.getY());
182
                                           geometry.draw((Graphics2D) g,
183
                                                           getCadToolAdapter().getMapControl().getViewPort(),
184
                                                           CadTool.drawingSymbol);
185
                                           GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
186
                                                           2);
187
                                           elShape.moveTo(firstPoint.getX(), firstPoint.getY());
188
                                           elShape.lineTo(x, y);
189
                                           ShapeFactory.createPolyline2D(
190
                                                           elShape).draw((Graphics2D) g,
191
                                                                           getCadToolAdapter().getMapControl().getViewPort(),
192
                                                                           CadTool.drawingSymbol);
193
                        
194
                           }
195
                           } catch (DriverIOException e) {
196
                                   e.printStackTrace();
197
                           } catch (IOException e) {
198
                                   e.printStackTrace();
199
                           }
200
                         */
201
                }
202
        }
203

    
204
        /**
205
         * @see com.iver.cit.gvsig.gui.cad.CadTool#getQuestion()
206
         */
207
        public String getQuestion() {
208
                return STATUS[rotateStatus.getStatus()].getQuestion();
209
        }
210

    
211
        /**
212
         * @see com.iver.cit.gvsig.gui.cad.CadTool#initializeStatus()
213
         */
214
        public void initializeStatus() {
215
                rotateStatus.initialize();
216
        }
217

    
218
        /**
219
         * @see com.iver.cit.gvsig.gui.cad.CadTool#getAutomaton()
220
         */
221
        public Automaton getAutomaton() {
222
                return rotateStatus;
223
        }
224

    
225
        /**
226
         * @see com.iver.cit.gvsig.gui.cad.CadTool#getName()
227
         */
228
        public String getName() {
229
                return "GIRAR";
230
        }
231
}