Statistics
| Revision:

root / branches / pilotoDWG / applications / appgvSIG / src / com / iver / cit / gvsig / gui / cad / tools / ScaleCadTool.java @ 1695

History | View | Annotate | Download (5.86 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"),
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
                }
144

    
145
                return ret;
146
        }
147

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

    
157
                if (status == 1) {
158
                        double w;
159
                        double h;
160
                        w = x - firstPoint.getX();
161
                        h = y - firstPoint.getY();
162

    
163
                        try {
164
                                for (int i = 0; i < efs.getGeometryCount(); i++) {
165
                                        if (selectedGeometries.get(i)) {
166
                                                IGeometry geometry = efs.getGeometry(i);
167

    
168
                                                Point2D currentPoint = new Point2D.Double(x, y);
169

    
170
                                                geometry.scale(firstPoint,
171
                                                        firstPoint.distance(currentPoint),
172
                                                        firstPoint.distance(currentPoint));
173
                                                geometry.draw((Graphics2D) g,
174
                                                        getCadToolAdapter().getMapControl().getViewPort(),
175
                                                        CadTool.modifySymbol);
176
                                                drawLine((Graphics2D) g, firstPoint, currentPoint);
177
                                        }
178
                                }
179
                        } catch (DriverIOException e) {
180
                                e.printStackTrace();
181
                        } catch (IOException e) {
182
                                e.printStackTrace();
183
                        }
184
                }
185
        }
186

    
187
        /**
188
         * @see com.iver.cit.gvsig.gui.cad.CadTool#getQuestion()
189
         */
190
        public String getQuestion() {
191
                return STATUS[scaleStatus.getStatus()].getQuestion();
192
        }
193

    
194
        /**
195
         * @see com.iver.cit.gvsig.gui.cad.CadTool#initializeStatus()
196
         */
197
        public void initializeStatus() {
198
                scaleStatus.initialize();
199
        }
200

    
201
        /**
202
         * @see com.iver.cit.gvsig.gui.cad.CadTool#getAutomaton()
203
         */
204
        public Automaton getAutomaton() {
205
                return scaleStatus;
206
        }
207

    
208
        /**
209
         * @see com.iver.cit.gvsig.gui.cad.CadTool#getName()
210
         */
211
        public String getName() {
212
                return "ESCALAR";
213
        }
214
}