Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_916 / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / ScaleCADTool.java @ 12327

History | View | Annotate | Download (11.3 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.event.InputEvent;
47
import java.awt.geom.AffineTransform;
48
import java.awt.geom.Point2D;
49
import java.awt.geom.Point2D.Double;
50
import java.awt.image.ImagingOpException;
51
import java.io.IOException;
52
import java.util.ArrayList;
53

    
54
import com.iver.andami.PluginServices;
55
import com.iver.cit.gvsig.CADExtension;
56
import com.iver.cit.gvsig.fmap.ViewPort;
57
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
58
import com.iver.cit.gvsig.fmap.core.IGeometry;
59
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
60
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
61
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
62
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
63
import com.iver.cit.gvsig.fmap.edition.UtilFunctions;
64
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
65
import com.iver.cit.gvsig.gui.cad.CADTool;
66
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
67
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
68
import com.iver.cit.gvsig.gui.cad.tools.smc.ScaleCADToolContext;
69
import com.iver.cit.gvsig.gui.cad.tools.smc.ScaleCADToolContext.ScaleCADToolState;
70
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
71

    
72

    
73
/**
74
 * DOCUMENT ME!
75
 *
76
 * @author Vicente Caballero Navarro
77
 */
78
public class ScaleCADTool extends DefaultCADTool {
79
    private ScaleCADToolContext _fsm;
80
    private Point2D firstPoint;
81
    private Point2D lastPoint;
82
        private Point2D scalePoint;
83
        private Double orr;
84
        private Double frr;
85
        private Double ore;
86
        private Double fre;
87

    
88
    /**
89
     * Crea un nuevo PolylineCADTool.
90
     */
91
    public ScaleCADTool() {
92
    }
93

    
94
    /**
95
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
96
     * carga previa a la utilizaci?n de la herramienta.
97
     */
98
    public void init() {
99
        _fsm = new ScaleCADToolContext(this);
100
    }
101

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

    
109
    /* (non-Javadoc)
110
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
111
     */
112
    public void transition(double d) {
113
        _fsm.addValue(d);
114
    }
115

    
116
    /* (non-Javadoc)
117
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
118
     */
119
    public void transition(String s) throws CommandException {
120
            if (!super.changeCommand(s)){
121
                    _fsm.addOption(s);
122
            }
123
    }
124

    
125
    /**
126
     * DOCUMENT ME!
127
     */
128
    public void selection() {
129
        ArrayList rowSelected=getSelectedRows();
130
        if (rowSelected.size() == 0 && !CADExtension.getCADTool().getClass().getName().equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
131
            CADExtension.setCADTool("_selection",false);
132
            ((SelectionCADTool) CADExtension.getCADTool()).setNextTool(
133
                "_scale");
134
        }
135
    }
136

    
137
    /**
138
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
139
     * editableFeatureSource que ya estar? creado.
140
     *
141
     * @param x par?metro x del punto que se pase en esta transici?n.
142
     * @param y par?metro y del punto que se pase en esta transici?n.
143
     */
144
    public void addPoint(double x, double y,InputEvent event) {
145
        ScaleCADToolState actualState = (ScaleCADToolState) _fsm.getPreviousState();
146
        String status = actualState.getName();
147

    
148
        if (status.equals("Scale.PointMain")) {
149
                                firstPoint = new Point2D.Double(x, y);
150
                            scalePoint = firstPoint;
151
                } else if (status.equals("Scale.ScaleFactorOrReference")) {
152
                        PluginServices.getMDIManager().setWaitCursor();
153
                        lastPoint = new Point2D.Double(x, y);
154

    
155
                        //double w;
156
                        //double h;
157
                        //w = lastPoint.getX() - firstPoint.getX();
158
                        //h = lastPoint.getY() - firstPoint.getY();
159

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

    
169
                        PluginServices.getMDIManager().restoreCursor();
170
                } else if (status.equals("Scale.PointOriginOrScaleFactor")) {
171
                        orr = new Point2D.Double(x, y);
172
                } else if (status.equals("Scale.EndPointReference")) {
173
                        frr = new Point2D.Double(x, y);
174
                } else if (status.equals("Scale.OriginPointScale")) {
175
                        ore = new Point2D.Double(x, y);
176
                        firstPoint = ore;
177
                } else if (status.equals("Scale.EndPointScale")) {
178
                        fre = new Point2D.Double(x, y);
179

    
180
                        double distrr = orr.distance(frr);
181
                        double distre = ore.distance(fre);
182
                        double escalado = distre / distrr;
183

    
184
                        try {
185
                                scale(escalado);
186
                        } catch (DriverIOException e) {
187
                                e.printStackTrace();
188
                        } catch (IOException e) {
189
                                e.printStackTrace();
190
                        }
191
                }
192

    
193
    }
194

    
195
    /**
196
     * M?todo para dibujar la lo necesario para el estado en el que nos
197
     * encontremos.
198
     *
199
     * @param g Graphics sobre el que dibujar.
200
     * @param x par?metro x del punto que se pase para dibujar.
201
     * @param y par?metro x del punto que se pase para dibujar.
202
     */
203
    public void drawOperation(Graphics g, double x, double y) {
204
                ScaleCADToolState actualState = ((ScaleCADToolContext) _fsm).getState();
205
                String status = actualState.getName();
206
                ArrayList selectedRow = getSelectedRows();
207
                Point2D currentPoint = new Point2D.Double(x, y);
208

    
209
                if (status.equals("Scale.ScaleFactorOrReference")) {
210
                        VectorialLayerEdited vle = getVLE();
211
                        ViewPort vp = vle.getLayer().getMapContext().getViewPort();
212
                        Point2D point = vp.fromMapPoint(firstPoint.getX(), firstPoint
213
                                        .getY());
214
                        double size = getCadToolAdapter().getMapControl().getViewPort()
215
                                        .toMapDistance(
216
                                                        getCadToolAdapter().getMapControl().getWidth());
217
                        double scale = firstPoint.distance(currentPoint) / (size / 40);
218
                        drawLine((Graphics2D) g, firstPoint, currentPoint);
219
                        if (selectedRow.size() < CADTool.TOPGEOMETRY) {
220
                                for (int i = 0; i < selectedRow.size(); i++) {
221
                                        DefaultFeature fea = (DefaultFeature) ((DefaultRowEdited) selectedRow
222
                                                        .get(i)).getLinkedRow();
223
                                        IGeometry geometry = fea.getGeometry().cloneGeometry();
224

    
225
                                        UtilFunctions.scaleGeom(geometry, firstPoint, scale, scale);
226
                                        geometry.draw((Graphics2D) g, getCadToolAdapter()
227
                                                        .getMapControl().getViewPort(),
228
                                                        DefaultCADTool.modifySymbol);
229

    
230
                                }
231

    
232
                        } else {
233
                                AffineTransform at = new AffineTransform();
234
                                at.setToTranslation(point.getX(), point.getY());
235
                                at.scale(scale, scale);
236
                                at.translate(-point.getX(), -point.getY());
237
                                Image imgSel = vle.getSelectionImage();
238
                                try {
239
                                        ((Graphics2D) g).drawImage(imgSel, at, null);
240
                                } catch (ImagingOpException e) {
241
                                }
242
                        }
243
                        PluginServices.getMainFrame().getStatusBar().setMessage("5",
244
                                        "Factor = " + scale);
245
                } else if (status.equals("Scale.EndPointScale")) {
246
                        VectorialLayerEdited vle = getVLE();
247
                        ViewPort vp = vle.getLayer().getMapContext().getViewPort();
248
                        Point2D point = vp.fromMapPoint(scalePoint.getX(), scalePoint
249
                                        .getY());
250

    
251
                        double distrr = orr.distance(frr);
252
                        double distre = ore.distance(currentPoint);
253
                        double escalado = distre / distrr;
254
                        if (selectedRow.size() < CADTool.TOPGEOMETRY) {
255
                                for (int i = 0; i < selectedRow.size(); i++) {
256
                                                DefaultFeature fea = (DefaultFeature) ((DefaultRowEdited) selectedRow
257
                                                                .get(i)).getLinkedRow();
258
                                                IGeometry geometry = fea.getGeometry().cloneGeometry();
259
                                                UtilFunctions.scaleGeom(geometry, scalePoint, escalado,
260
                                                                escalado);
261
                                                geometry.draw((Graphics2D) g, getCadToolAdapter()
262
                                                                .getMapControl().getViewPort(),
263
                                                                DefaultCADTool.modifySymbol);
264

    
265
                                        }
266
                                } else {
267
                                        AffineTransform at = new AffineTransform();
268
                                        at.setToTranslation(point.getX(), point.getY());
269
                                        at.scale(escalado, escalado);
270
                                        at.translate(-point.getX(), -point.getY());
271
                                        Image imgSel = vle.getSelectionImage();
272
                                        try {
273
                                                ((Graphics2D) g).drawImage(imgSel, at, null);
274
                                        } catch (ImagingOpException e) {
275
                                        }
276
                                }
277
                                drawLine((Graphics2D) g, firstPoint, new Point2D.Double(x, y));
278
                        }
279
        }
280

    
281
    /**
282
         * Add a diferent option.
283
         *
284
         * @param s
285
         *            Diferent option.
286
         */
287
    public void addOption(String s) {
288
            ScaleCADToolState actualState = (ScaleCADToolState) _fsm.getPreviousState();
289
        String status = actualState.getName();
290
       if (status.equals("Scale.ScaleFactorOrReference")) {
291
                /*        try {
292
                                scale(2);
293
                        } catch (DriverIOException e) {
294
                                // TODO Auto-generated catch block
295
                                e.printStackTrace();
296
                        } catch (IOException e) {
297
                                // TODO Auto-generated catch block
298
                                e.printStackTrace();
299
                        }
300
                */
301
                }
302
        }
303

    
304
    /* (non-Javadoc)
305
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
306
     */
307
    public void addValue(double d) {
308
            ScaleCADToolState actualState = (ScaleCADToolState) _fsm.getPreviousState();
309
        String status = actualState.getName();
310
        if (status.equals("Scale.ScaleFactorOrReference")) {
311
                            try {
312
                                    scale(d);
313
                            } catch (DriverIOException e) {
314
                                    e.printStackTrace();
315
                            } catch (IOException e1) {
316
                                    e1.printStackTrace();
317
                            }
318

    
319
            }
320
    }
321
    private void scale(double scaleFactor) throws DriverIOException, IOException {
322
            VectorialLayerEdited vle=getVLE();
323
            VectorialEditableAdapter vea=vle.getVEA();
324
            vea.startComplexRow();
325
            ArrayList selectedRow=getSelectedRows();
326
            ArrayList selectedRowAux=new ArrayList();
327
            for (int i = 0; i < selectedRow.size(); i++) {
328
                    IRowEdited edRow = (IRowEdited) selectedRow.get(i);
329
                    DefaultFeature fea = (DefaultFeature) edRow.getLinkedRow().cloneRow();
330
                        UtilFunctions.scaleGeom(fea.getGeometry(), scalePoint, scaleFactor, scaleFactor);
331
                    vea.modifyRow(edRow.getIndex(), fea,getName(),EditionEvent.GRAPHIC);
332
                    selectedRowAux.add(new DefaultRowEdited(fea,IRowEdited.STATUS_MODIFIED,edRow.getIndex()));
333
            }
334
            vea.endComplexRow(getName());
335
            vle.setSelectionCache(selectedRowAux);
336
            //clearSelection();
337
            //selectedRow.addAll(selectedRowAux);
338
    }
339

    
340
        public String getName() {
341
                return PluginServices.getText(this,"scale_");
342
        }
343

    
344
        public String toString() {
345
                return "_scale";
346
        }
347

    
348
}