Statistics
| Revision:

svn-gvsig-desktop / branches / pilotoDWG / applications / appgvSIG / src / com / iver / cit / gvsig / gui / cad / tools / StretchingCadTool.java @ 1535

History | View | Annotate | Download (7.75 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.geom.Point2D;
46
import java.awt.geom.Rectangle2D;
47
import java.io.IOException;
48

    
49
import com.iver.andami.PluginServices;
50
import com.iver.andami.persistence.generate.PluginsStatus;
51
import com.iver.cit.gvsig.fmap.core.FGeometry;
52
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
53
import com.iver.cit.gvsig.fmap.core.Handler;
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.EditableFeatureSource;
58
import com.iver.cit.gvsig.fmap.edition.cad.Status;
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.automaton.Estirar;
62
import com.iver.fsac.Automaton;
63

    
64

    
65
/**
66
 * DOCUMENT ME!
67
 *
68
 * @author Vicente Caballero Navarro
69
 */
70
public class StretchingCadTool extends AbstractCadTool {
71
        private static Status[] STATUS = {
72
                        new Status("Seleccione primer punto de zona a estirar"),
73
                        new Status("Seleccione segundo punto de zona a estirar"),
74
                        new Status("Precise punto de estiramiento"),
75
                        new Status("Precise segundo punto")
76
                };
77
        private Estirar stretchingStatus = new Estirar();
78
        private Point2D selfirstPoint;
79
        private Point2D sellastPoint;
80
        private Point2D movefirstPoint;
81
        private Point2D movelastPoint;
82
        private Rectangle2D rect = null;
83

    
84
        /**
85
         * @see com.iver.cit.gvsig.gui.cad.CadTool#transition(java.lang.String,
86
         *                 com.iver.cit.gvsig.fmap.edition.EditableFeatureSource,
87
         *                 com.iver.cit.gvsig.fmap.layers.FBitSet, double[])
88
         */
89
        public int transition(String text, final EditableFeatureSource editingSource,
90
                final FBitSet selectedGeometries, final double[] ds) {
91
                int ret = stretchingStatus.transition(text);
92

    
93
                int status = stretchingStatus.getStatus();
94

    
95
                if (status == 0) {
96
                        if (selectedGeometries.cardinality() == 0){
97
                                getCadToolAdapter().pushCadTool(new SelectionCadTool());
98
                        }
99
                        if (ds.length != 0) {
100
                                sellastPoint = new Point2D.Double(ds[0], ds[1]);
101

    
102
                                double x;
103
                                double y;
104
                                double w;
105
                                double h;
106

    
107
                                if (selfirstPoint.getX() < sellastPoint.getX()) {
108
                                        x = selfirstPoint.getX();
109
                                        w = sellastPoint.getX() - selfirstPoint.getX();
110
                                } else {
111
                                        x = sellastPoint.getX();
112
                                        w = selfirstPoint.getX() - sellastPoint.getX();
113
                                }
114

    
115
                                if (selfirstPoint.getY() < sellastPoint.getY()) {
116
                                        y = selfirstPoint.getY();
117
                                        h = sellastPoint.getY() - selfirstPoint.getY();
118
                                } else {
119
                                        y = sellastPoint.getY();
120
                                        h = selfirstPoint.getY() - sellastPoint.getY();
121
                                }
122

    
123
                                rect = new Rectangle2D.Double(x, y, w, h);
124
                        }
125
                } else if (status == 1) {
126
                        if (ds.length != 0) {
127
                                selfirstPoint = new Point2D.Double(ds[0], ds[1]);
128
                        }
129
                } else if (status == 2) {
130
                } else if (status == 3) {
131
                        movefirstPoint = new Point2D.Double(ds[0], ds[1]);
132
                } else if (status == 4) {
133
                        PluginServices.getMDIManager().setWaitCursor();
134
                                        movelastPoint = new Point2D.Double(ds[0], ds[1]);
135

    
136
                                        Handler[] handlers = null;
137

    
138
                                        for (int i = selectedGeometries.nextSetBit(0); i >= 0;
139
                                                        i = selectedGeometries.nextSetBit(i + 1)) {
140
                                                IGeometry geometry = null;
141

    
142
                                                try {
143
                                                        geometry = editingSource.getGeometry(i);
144
                                                } catch (IOException e) {
145
                                                        e.printStackTrace();
146
                                                } catch (DriverIOException e) {
147
                                                        e.printStackTrace();
148
                                                }
149

    
150
                                                handlers = geometry.getHandlers(FGeometry.STRETCHINGHANDLER);
151

    
152
                                                for (int j = 0; j < handlers.length; j++) {
153
                                                        if (rect.contains(handlers[j].getPoint())) {
154
                                                                handlers[j].move(movelastPoint.getX() -
155
                                                                        movefirstPoint.getX(),
156
                                                                        movelastPoint.getY() - movefirstPoint.getY());
157
                                                        }
158
                                                }
159

    
160
                                                try {
161
                                                        editingSource.modifyGeometry(i, geometry);
162
                                                } catch (IOException e1) {
163
                                                        e1.printStackTrace();
164
                                                } catch (DriverIOException e) {
165
                                                        e.printStackTrace();
166
                                                }
167

    
168
                                        }
169
                                        PluginServices.getMDIManager().restoreCursor();
170
                        ret = ret | stretchingStatus.transition("cancel");
171

    
172
                }
173

    
174
                return ret;
175
        }
176

    
177
        /**
178
         * @see com.iver.cit.gvsig.gui.cad.CadTool#drawOperation(java.awt.Graphics,
179
         *                 com.iver.cit.gvsig.fmap.edition.EditableFeatureSource,
180
         *                 com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
181
         */
182
        public void drawOperation(Graphics g, EditableFeatureSource efs,
183
                FBitSet selectedGeometries, double x, double y) {
184
                double dx;
185
                double dy;
186
                double dw;
187
                double dh;
188
                int status = stretchingStatus.getStatus();
189

    
190
                if (status == 1) {
191
                        GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
192
                                        4);
193
                        elShape.moveTo(selfirstPoint.getX(), selfirstPoint.getY());
194
                        elShape.lineTo(x, selfirstPoint.getY());
195
                        elShape.lineTo(x, y);
196
                        elShape.lineTo(selfirstPoint.getX(), y);
197
                        elShape.lineTo(selfirstPoint.getX(), selfirstPoint.getY());
198
                        ShapeFactory.createPolyline2D(elShape).draw((Graphics2D) g,
199
                                getCadToolAdapter().getMapControl().getViewPort(),
200
                                CadTool.modifySymbol);
201
                } else if (status == 3) {
202
                        //        drawLine((Graphics2D)g, movefirstPoint, new Point2D.Double(x, y));
203
                        //}else if (status == 4){
204
                        if (selfirstPoint.getX() < x) {
205
                                dx = selfirstPoint.getX();
206
                                dw = x - selfirstPoint.getX();
207
                        } else {
208
                                dx = x;
209
                                dw = selfirstPoint.getX() - x;
210
                        }
211

    
212
                        if (selfirstPoint.getY() < sellastPoint.getY()) {
213
                                dy = selfirstPoint.getY();
214
                                dh = y - selfirstPoint.getY();
215
                        } else {
216
                                dy = y;
217
                                dh = selfirstPoint.getY() - y;
218
                        }
219

    
220
                        Rectangle2D rec = new Rectangle2D.Double(dx, dy, dw, dh);
221
                        Handler[] handlers = null;
222

    
223
                        for (int i = selectedGeometries.nextSetBit(0); i >= 0;
224
                                        i = selectedGeometries.nextSetBit(i + 1)) {
225
                                IGeometry geometry = null;
226

    
227
                                try {
228
                                        geometry = efs.getGeometry(i);
229
                                } catch (IOException e) {
230
                                        e.printStackTrace();
231
                                } catch (DriverIOException e) {
232
                                        e.printStackTrace();
233
                                }
234

    
235
                                handlers = geometry.getHandlers(FGeometry.STRETCHINGHANDLER);
236

    
237
                                for (int j = 0; j < handlers.length; j++) {
238
                                        if (rect.contains(handlers[j].getPoint())) {
239
                                                handlers[j].move(x - movefirstPoint.getX(),
240
                                                        y - movefirstPoint.getY());
241
                                        }
242
                                }
243

    
244
                                geometry.draw((Graphics2D) g,
245
                                        getCadToolAdapter().getMapControl().getViewPort(),
246
                                        CadTool.modifySymbol);
247
                        }
248
                }
249
        }
250

    
251
        /**
252
         * @see com.iver.cit.gvsig.gui.cad.CadTool#getQuestion()
253
         */
254
        public String getQuestion() {
255
                return STATUS[stretchingStatus.getStatus()].getQuestion();
256
        }
257

    
258
        /**
259
         * @see com.iver.cit.gvsig.gui.cad.CadTool#initializeStatus()
260
         */
261
        public void initializeStatus() {
262
                stretchingStatus.initialize();
263
        }
264

    
265
        /**
266
         * @see com.iver.cit.gvsig.gui.cad.CadTool#getAutomaton()
267
         */
268
        public Automaton getAutomaton() {
269
                return stretchingStatus;
270
        }
271
}