Statistics
| Revision:

svn-gvsig-desktop / branches / gvSIG_CAD_Layout_version / applications / appgvSIG / src / com / iver / cit / gvsig / gui / cad / tools / ScaleCadTool.java @ 1763

History | View | Annotate | Download (6.32 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.CadTool;
51
import com.iver.cit.gvsig.gui.cad.automaton.Escalar;
52

    
53
import com.iver.fsac.Automaton;
54

    
55
import java.awt.Graphics;
56
import java.awt.Graphics2D;
57
import java.awt.geom.Point2D;
58

    
59
import java.io.IOException;
60

    
61

    
62
/**
63
 * Herramienta para escalar las geometr?as seleccionadas.
64
 *
65
 * @author Vicente Caballero Navarro
66
 */
67
public class ScaleCadTool extends AbstractCadTool {
68
        private static Status[] STATUS = {
69
                        new Status("Precise punto base"),
70
                        new Status("Precise factor de escala[2]"),
71
                };
72
        private Escalar scaleStatus = new Escalar();
73
        private Point2D firstPoint;
74
        private Point2D lastPoint;
75

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

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

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

    
104
                        try {
105
                                //ViewPort vp = getCadToolAdapter().getMapControl().getViewPort(); 
106
                                editingSource.startComplexGeometry();
107

    
108
                                for (int i = 0; i < editingSource.getGeometryCount(); i++) {
109
                                        if (selectedGeometries.get(i)) {
110
                                                IGeometry geometry = editingSource.getGeometry(i);
111
                                                geometry.scale(firstPoint,
112
                                                        firstPoint.distance(lastPoint),
113
                                                        firstPoint.distance(lastPoint));
114
                                                editingSource.modifyGeometry(i, geometry);
115
                                        }
116
                                }
117

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

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

    
142
                        ret = ret | scaleStatus.transition("cancel");
143
                }else if (status == 4) {
144
                        try {
145
                                for (int i = 0; i < editingSource.getGeometryCount(); i++) {
146
                                        if (selectedGeometries.get(i)) {
147
                                                IGeometry geometry = editingSource.getGeometry(i);
148
                                                geometry.scale(firstPoint, 2, 2);
149
                                                editingSource.modifyGeometry(i, geometry);
150
                                        }
151
                                }
152
                        } catch (DriverIOException e) {
153
                                e.printStackTrace();
154
                        } catch (IOException e1) {
155
                                e1.printStackTrace();
156
                        }
157

    
158
                        ret = ret | scaleStatus.transition("cancel");
159
                }
160

    
161

    
162
                return ret;
163
        }
164

    
165
        /**
166
         * @see com.iver.cit.gvsig.gui.cad.CadTool#drawOperation(java.awt.Graphics,
167
         *                 com.iver.cit.gvsig.fmap.edition.EditableFeatureSource,
168
         *                 com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
169
         */
170
        public void drawOperation(Graphics g, EditableFeatureSource efs,
171
                FBitSet selectedGeometries, double x, double y) {
172
                int status = scaleStatus.getStatus();
173

    
174
                if (status == 1) {
175
                        double w;
176
                        double h;
177
                        w = x - firstPoint.getX();
178
                        h = y - firstPoint.getY();
179

    
180
                        try {
181
                                for (int i = 0; i < efs.getGeometryCount(); i++) {
182
                                        if (selectedGeometries.get(i)) {
183
                                                IGeometry geometry = efs.getGeometry(i);
184

    
185
                                                Point2D currentPoint = new Point2D.Double(x, y);
186

    
187
                                                geometry.scale(firstPoint,
188
                                                        firstPoint.distance(currentPoint),
189
                                                        firstPoint.distance(currentPoint));
190
                                                geometry.draw((Graphics2D) g,
191
                                                        getCadToolAdapter().getMapControl().getViewPort(),
192
                                                        CadTool.modifySymbol);
193
                                                drawLine((Graphics2D) g, firstPoint, currentPoint);
194
                                        }
195
                                }
196
                        } catch (DriverIOException e) {
197
                                e.printStackTrace();
198
                        } catch (IOException e) {
199
                                e.printStackTrace();
200
                        }
201
                }
202
        }
203

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

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

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

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