Statistics
| Revision:

root / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / ScaleCADTool.java @ 3883

History | View | Annotate | Download (10.7 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.Image;
46
import java.awt.geom.AffineTransform;
47
import java.awt.geom.Point2D;
48
import java.awt.geom.Point2D.Double;
49
import java.io.IOException;
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.IFeature;
56
import com.iver.cit.gvsig.fmap.core.IGeometry;
57
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
58
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
59
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
60
import com.iver.cit.gvsig.fmap.layers.FBitSet;
61
import com.iver.cit.gvsig.gui.cad.CADTool;
62
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
63
import com.iver.cit.gvsig.gui.cad.tools.smc.ScaleCADToolContext;
64
import com.iver.cit.gvsig.gui.cad.tools.smc.ScaleCADToolContext.ScaleCADToolState;
65

    
66

    
67
/**
68
 * DOCUMENT ME!
69
 *
70
 * @author Vicente Caballero Navarro
71
 */
72
public class ScaleCADTool extends DefaultCADTool {
73
    private ScaleCADToolContext _fsm;
74
    private Point2D firstPoint;
75
    private Point2D lastPoint;
76
        private Point2D scalePoint;
77
        private Double orr;
78
        private Double frr;
79
        private Double ore;
80
        private Double fre;
81

    
82
    /**
83
     * Crea un nuevo PolylineCADTool.
84
     */
85
    public ScaleCADTool() {
86
    }
87

    
88
    /**
89
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
90
     * carga previa a la utilizaci?n de la herramienta.
91
     */
92
    public void init() {
93
        _fsm = new ScaleCADToolContext(this);
94
    }
95

    
96
    /* (non-Javadoc)
97
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
98
     */
99
    public void transition(double x, double y) {
100
        _fsm.addPoint(x, y);
101
    }
102

    
103
    /* (non-Javadoc)
104
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
105
     */
106
    public void transition(double d) {
107
        _fsm.addValue(d);
108
    }
109

    
110
    /* (non-Javadoc)
111
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
112
     */
113
    public void transition(String s) {
114
        _fsm.addOption(s);
115
    }
116

    
117
    /**
118
     * DOCUMENT ME!
119
     */
120
    public void selection() {
121
        FBitSet selection = CADExtension.getCADToolAdapter()
122
                                        .getVectorialAdapter().getSelection();
123

    
124
        if (selection.cardinality() == 0) {
125
            CADExtension.setCADTool("selection");
126
            ((SelectionCADTool) CADExtension.getCADToolAdapter().getCadTool()).setNextTool(
127
                "scale");
128
        }
129
    }
130

    
131
    /**
132
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
133
     * editableFeatureSource que ya estar? creado.
134
     *
135
     * @param x par?metro x del punto que se pase en esta transici?n.
136
     * @param y par?metro y del punto que se pase en esta transici?n.
137
     */
138
    public void addPoint(double x, double y) {
139
        ScaleCADToolState actualState = (ScaleCADToolState) _fsm.getPreviousState();
140
        String status = actualState.getName();
141
        VectorialEditableAdapter vea = getCadToolAdapter().getVectorialAdapter();
142
        FBitSet selection = vea.getSelection();
143

    
144

    
145
        if (status.equals("ExecuteMap.Initial")) {
146
                                firstPoint = new Point2D.Double(x, y);
147
                            scalePoint = firstPoint;
148
                } else if (status.equals("ExecuteMap.First")) {
149
                        PluginServices.getMDIManager().setWaitCursor();
150
                        lastPoint = new Point2D.Double(x, y);
151

    
152
                        double w;
153
                        double h;
154
                        w = lastPoint.getX() - firstPoint.getX();
155
                        h = lastPoint.getY() - firstPoint.getY();
156

    
157
                        try {
158
                                double size=getCadToolAdapter().getMapControl().getViewPort().toMapDistance(getCadToolAdapter().getMapControl().getWidth());
159
                                scale(firstPoint.distance(lastPoint)/(size/40));
160
                        } catch (DriverIOException e) {
161
                                e.printStackTrace();
162
                        } catch (IOException e1) {
163
                                e1.printStackTrace();
164
                        }
165

    
166
                        PluginServices.getMDIManager().restoreCursor();
167
                } else if (status.equals("ExecuteMap.Second")) {
168
                        orr = new Point2D.Double(x, y);
169
                } else if (status.equals("ExecuteMap.Third")) {
170
                        frr = new Point2D.Double(x, y);
171
                } else if (status.equals("ExecuteMap.Fourth")) {
172
                        ore = new Point2D.Double(x, y);
173
                        firstPoint = ore;
174
                } else if (status.equals("ExecuteMap.Fiveth")) {
175
                        fre = new Point2D.Double(x, y);
176

    
177
                        double distrr = orr.distance(frr);
178
                        double distre = ore.distance(fre);
179
                        double escalado = distre / distrr;
180

    
181
                        try {
182
                                scale(escalado);
183
                        } catch (DriverIOException e) {
184
                                e.printStackTrace();
185
                        } catch (IOException e) {
186
                                e.printStackTrace();
187
                        }
188

    
189
                }
190

    
191

    
192

    
193

    
194
        if (status.equals("ExecuteMap.Initial")) {
195
                firstPoint = new Point2D.Double(x, y);
196
                    } else if (status.equals("ExecuteMap.First")) {
197
                            PluginServices.getMDIManager().setWaitCursor();
198
                            lastPoint = new Point2D.Double(x,y);
199

    
200
                            double w;
201
                            double h;
202
                            w = lastPoint.getX() - firstPoint.getX();
203
                            h = lastPoint.getY() - firstPoint.getY();
204

    
205
                            try {
206
                                    getCadToolAdapter().getVectorialAdapter().startComplexRow();
207

    
208
                                    for (int i = selection.nextSetBit(0); i >= 0;
209
                                                    i = selection.nextSetBit(i + 1)) {
210
                                            DefaultFeature fea = (DefaultFeature)getCadToolAdapter().getVectorialAdapter().getRow(i).cloneRow();
211
                                            fea.getGeometry().rotate(-Math.atan2(w, h) + (Math.PI / 2),
212
                                                    firstPoint.getX(), firstPoint.getY());
213
                                            getCadToolAdapter().getVectorialAdapter().modifyRow(i, fea);
214
                                    }
215

    
216
                                    getCadToolAdapter().getVectorialAdapter().endComplexRow();
217
                            } catch (DriverIOException e) {
218
                                    e.printStackTrace();
219
                            } catch (IOException e1) {
220
                                    e1.printStackTrace();
221
                            }
222

    
223
                            PluginServices.getMDIManager().restoreCursor();
224
                    }
225
    }
226

    
227
    /**
228
     * M?todo para dibujar la lo necesario para el estado en el que nos
229
     * encontremos.
230
     *
231
     * @param g Graphics sobre el que dibujar.
232
     * @param x par?metro x del punto que se pase para dibujar.
233
     * @param y par?metro x del punto que se pase para dibujar.
234
     */
235
    public void drawOperation(Graphics g, double x, double y) {
236
        ScaleCADToolState actualState = ((ScaleCADToolContext) _fsm).getState();
237
        String status = actualState.getName();
238
        VectorialEditableAdapter vea = getCadToolAdapter().getVectorialAdapter();
239
        FBitSet selection = vea.getSelection();
240
        Point2D currentPoint = new Point2D.Double(x, y);
241

    
242

    
243
        if (status.equals("ExecuteMap.First")) {
244
                try {
245
                                for (int i = 0; i < vea.getRowCount(); i++) {
246
                                        if (selection.get(i)) {
247
                                                IGeometry geometry = vea.getShape(i);
248
                                                double size=getCadToolAdapter().getMapControl().getViewPort().toMapDistance(getCadToolAdapter().getMapControl().getWidth());
249
                                                geometry.scale(firstPoint,
250
                                                        firstPoint.distance(currentPoint)/(size/40),
251
                                                        firstPoint.distance(currentPoint)/(size/40));
252
                                                geometry.draw((Graphics2D) g,
253
                                                        getCadToolAdapter().getMapControl().getViewPort(),
254
                                                        CADTool.modifySymbol);
255
                                                drawLine((Graphics2D) g, firstPoint, currentPoint);
256
                                                PluginServices.getMainFrame().getStatusBar().setMessage("5","Factor = "+firstPoint.distance(currentPoint)/(size/40));
257
                                        }
258
                                }
259
                        } catch (DriverIOException e) {
260
                                e.printStackTrace();
261
                        } catch (IOException e) {
262
                                e.printStackTrace();
263
                        }
264
                } else if (status.equals("ExecuteMap.Fiveth")) {
265
                        try {
266
                                for (int i = 0; i < vea.getRowCount(); i++) {
267
                                        if (selection.get(i)) {
268
                                                IGeometry geometry = vea.getShape(i);
269

    
270

    
271
                                                double distrr = orr.distance(frr);
272
                                                double distre = ore.distance(currentPoint);
273
                                                double escalado = distre / distrr;
274

    
275
                                                geometry.scale(scalePoint, escalado, escalado);
276
                                                geometry.draw((Graphics2D) g,
277
                                                        getCadToolAdapter().getMapControl().getViewPort(),
278
                                                        CADTool.modifySymbol);
279
                                                drawLine((Graphics2D) g, firstPoint,
280
                                                        new Point2D.Double(x, y));
281
                                        }
282
                                }
283
                        } catch (DriverIOException e) {
284
                                e.printStackTrace();
285
                        } catch (IOException e1) {
286
                                e1.printStackTrace();
287
                        }
288
                }
289
    }
290

    
291
    /**
292
     * Add a diferent option.
293
     *
294
     * @param s Diferent option.
295
     */
296
    public void addOption(String s) {
297
            ScaleCADToolState actualState = (ScaleCADToolState) _fsm.getPreviousState();
298
        String status = actualState.getName();
299
       if (status.equals("ExecuteMap.First")) {
300
                        try {
301
                                scale(2);
302
                        } catch (DriverIOException e) {
303
                                // TODO Auto-generated catch block
304
                                e.printStackTrace();
305
                        } catch (IOException e) {
306
                                // TODO Auto-generated catch block
307
                                e.printStackTrace();
308
                        }
309
                }
310
    }
311

    
312
    /* (non-Javadoc)
313
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
314
     */
315
    public void addValue(double d) {
316
            ScaleCADToolState actualState = (ScaleCADToolState) _fsm.getPreviousState();
317
        String status = actualState.getName();
318
        if (status.equals("ExecuteMap.First")) {
319
                            try {
320
                                    scale(d);
321
                            } catch (DriverIOException e) {
322
                                    e.printStackTrace();
323
                            } catch (IOException e1) {
324
                                    e1.printStackTrace();
325
                            }
326

    
327
            }
328
    }
329
    private void scale(double scaleFactor) throws DriverIOException, IOException {
330
                    VectorialEditableAdapter vea=getCadToolAdapter().getVectorialAdapter();
331
                    vea.startComplexRow();
332
                    FBitSet selection=vea.getSelection();
333
                    for (int i = 0; i < vea.getRowCount(); i++) {
334
                            if (selection.get(i)) {
335
                                    DefaultFeature df=vea.getRow(i).cloneRow();
336
                                    df.getGeometry().scale(scalePoint, scaleFactor, scaleFactor);
337
                                    vea.modifyRow(i, df);
338
                            }
339
                    }
340
                    vea.endComplexRow();
341
            }
342
}