Statistics
| Revision:

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

History | View | Annotate | Download (9.79 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.event.InputEvent;
46
import java.awt.geom.Point2D;
47
import java.awt.geom.Point2D.Double;
48
import java.io.IOException;
49

    
50
import com.iver.andami.PluginServices;
51
import com.iver.cit.gvsig.CADExtension;
52
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
53
import com.iver.cit.gvsig.fmap.core.IGeometry;
54
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
55
import com.iver.cit.gvsig.fmap.edition.UtilFunctions;
56
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
57
import com.iver.cit.gvsig.fmap.layers.FBitSet;
58
import com.iver.cit.gvsig.gui.cad.CADTool;
59
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
60
import com.iver.cit.gvsig.gui.cad.tools.smc.ScaleCADToolContext;
61
import com.iver.cit.gvsig.gui.cad.tools.smc.ScaleCADToolContext.ScaleCADToolState;
62

    
63

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

    
79
    /**
80
     * Crea un nuevo PolylineCADTool.
81
     */
82
    public ScaleCADTool() {
83
    }
84

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

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

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

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

    
114
    /**
115
     * DOCUMENT ME!
116
     */
117
    public void selection() {
118
        FBitSet selection = CADExtension.getCADToolAdapter()
119
                                        .getVectorialAdapter().getSelection();
120

    
121
        if (selection.cardinality() == 0) {
122
            CADExtension.setCADTool("selection");
123
            ((SelectionCADTool) CADExtension.getCADToolAdapter().getCadTool()).setNextTool(
124
                "scale");
125
        }
126
    }
127

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

    
141

    
142
        if (status.equals("Scale.PointMain")) {
143
                                firstPoint = new Point2D.Double(x, y);
144
                            scalePoint = firstPoint;
145
                } else if (status.equals("Scale.ScaleFactorOrReference")) {
146
                        PluginServices.getMDIManager().setWaitCursor();
147
                        lastPoint = new Point2D.Double(x, y);
148

    
149
                        double w;
150
                        double h;
151
                        w = lastPoint.getX() - firstPoint.getX();
152
                        h = lastPoint.getY() - firstPoint.getY();
153

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

    
163
                        PluginServices.getMDIManager().restoreCursor();
164
                } else if (status.equals("Scale.PointOriginOrScaleFactor")) {
165
                        orr = new Point2D.Double(x, y);
166
                } else if (status.equals("Scale.EndPointReference")) {
167
                        frr = new Point2D.Double(x, y);
168
                } else if (status.equals("Scale.OriginPointScale")) {
169
                        ore = new Point2D.Double(x, y);
170
                        firstPoint = ore;
171
                } else if (status.equals("Scale.EndPointScale")) {
172
                        fre = new Point2D.Double(x, y);
173

    
174
                        double distrr = orr.distance(frr);
175
                        double distre = ore.distance(fre);
176
                        double escalado = distre / distrr;
177

    
178
                        try {
179
                                scale(escalado);
180
                        } catch (DriverIOException e) {
181
                                e.printStackTrace();
182
                        } catch (IOException e) {
183
                                e.printStackTrace();
184
                        }
185

    
186
                }
187

    
188
    }
189

    
190
    /**
191
     * M?todo para dibujar la lo necesario para el estado en el que nos
192
     * encontremos.
193
     *
194
     * @param g Graphics sobre el que dibujar.
195
     * @param x par?metro x del punto que se pase para dibujar.
196
     * @param y par?metro x del punto que se pase para dibujar.
197
     */
198
    public void drawOperation(Graphics g, double x, double y) {
199
        ScaleCADToolState actualState = ((ScaleCADToolContext) _fsm).getState();
200
        String status = actualState.getName();
201
        VectorialEditableAdapter vea = getCadToolAdapter().getVectorialAdapter();
202
        FBitSet selection = vea.getSelection();
203
        Point2D currentPoint = new Point2D.Double(x, y);
204

    
205

    
206
        if (status.equals("Scale.ScaleFactorOrReference")) {
207
                try {
208
                                for (int i = 0; i < vea.getRowCount(); i++) {
209
                                        if (selection.get(i)) {
210
                                                IGeometry geometry = vea.getShape(i);
211
                                                double size=getCadToolAdapter().getMapControl().getViewPort().toMapDistance(getCadToolAdapter().getMapControl().getWidth());
212
                                                UtilFunctions.scaleGeom(geometry, firstPoint,
213
                                                                firstPoint.distance(currentPoint)/(size/40),
214
                                                                firstPoint.distance(currentPoint)/(size/40));
215
                                                geometry.draw((Graphics2D) g,
216
                                                        getCadToolAdapter().getMapControl().getViewPort(),
217
                                                        CADTool.modifySymbol);
218
                                                drawLine((Graphics2D) g, firstPoint, currentPoint);
219
                                                PluginServices.getMainFrame().getStatusBar().setMessage("5","Factor = "+firstPoint.distance(currentPoint)/(size/40));
220
                                        }
221
                                }
222
                        } catch (DriverIOException e) {
223
                                e.printStackTrace();
224
                        } catch (IOException e) {
225
                                e.printStackTrace();
226
                        }
227
                } else if (status.equals("Scale.EndPointScale")) {
228
                        try {
229
                                for (int i = 0; i < vea.getRowCount(); i++) {
230
                                        if (selection.get(i)) {
231
                                                IGeometry geometry = vea.getShape(i);
232

    
233

    
234
                                                double distrr = orr.distance(frr);
235
                                                double distre = ore.distance(currentPoint);
236
                                                double escalado = distre / distrr;
237

    
238
                                                UtilFunctions.scaleGeom(geometry, scalePoint, escalado, escalado);
239
                                                // geometry.scale(scalePoint, escalado, escalado);
240
                                                geometry.draw((Graphics2D) g,
241
                                                        getCadToolAdapter().getMapControl().getViewPort(),
242
                                                        CADTool.modifySymbol);
243
                                                drawLine((Graphics2D) g, firstPoint,
244
                                                        new Point2D.Double(x, y));
245
                                        }
246
                                }
247
                        } catch (DriverIOException e) {
248
                                e.printStackTrace();
249
                        } catch (IOException e1) {
250
                                e1.printStackTrace();
251
                        }
252
                }
253
    }
254

    
255
    /**
256
     * Add a diferent option.
257
     *
258
     * @param s Diferent option.
259
     */
260
    public void addOption(String s) {
261
            ScaleCADToolState actualState = (ScaleCADToolState) _fsm.getPreviousState();
262
        String status = actualState.getName();
263
       if (status.equals("Scale.ScaleFactorOrReference")) {
264
                        try {
265
                                scale(2);
266
                        } catch (DriverIOException e) {
267
                                // TODO Auto-generated catch block
268
                                e.printStackTrace();
269
                        } catch (IOException e) {
270
                                // TODO Auto-generated catch block
271
                                e.printStackTrace();
272
                        }
273
                }
274
    }
275

    
276
    /* (non-Javadoc)
277
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
278
     */
279
    public void addValue(double d) {
280
            ScaleCADToolState actualState = (ScaleCADToolState) _fsm.getPreviousState();
281
        String status = actualState.getName();
282
        if (status.equals("Scale.ScaleFactorOrReference")) {
283
                            try {
284
                                    scale(d);
285
                            } catch (DriverIOException e) {
286
                                    e.printStackTrace();
287
                            } catch (IOException e1) {
288
                                    e1.printStackTrace();
289
                            }
290

    
291
            }
292
    }
293
    private void scale(double scaleFactor) throws DriverIOException, IOException {
294
                    VectorialEditableAdapter vea=getCadToolAdapter().getVectorialAdapter();
295
                    vea.startComplexRow();
296
                    FBitSet selection=vea.getSelection();
297
                    for (int i = 0; i < vea.getRowCount(); i++) {
298
                            if (selection.get(i)) {
299
                                    DefaultFeature df=(DefaultFeature)vea.getRow(i).cloneRow();
300
                                    UtilFunctions.scaleGeom(df.getGeometry(), scalePoint, scaleFactor, scaleFactor);
301
                                    // df.getGeometry().scale(scalePoint, scaleFactor, scaleFactor);
302
                                    vea.modifyRow(i, df,getName());
303
                            }
304
                    }
305
                    vea.endComplexRow();
306
            }
307

    
308
        public String getName() {
309
                return "ESCALAR";
310
        }
311
}