Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / ScaleCADTool.java @ 10626

History | View | Annotate | Download (12.5 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.util.ArrayList;
52

    
53
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
54
import com.iver.andami.PluginServices;
55
import com.iver.cit.gvsig.CADExtension;
56
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
57
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileWriteException;
58
import com.iver.cit.gvsig.exceptions.validate.ValidateRowException;
59
import com.iver.cit.gvsig.fmap.ViewPort;
60
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
61
import com.iver.cit.gvsig.fmap.core.IGeometry;
62
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
63
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
64
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
65
import com.iver.cit.gvsig.fmap.edition.UtilFunctions;
66
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
67
import com.iver.cit.gvsig.gui.cad.CADTool;
68
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
69
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
70
import com.iver.cit.gvsig.gui.cad.tools.smc.ScaleCADToolContext;
71
import com.iver.cit.gvsig.gui.cad.tools.smc.ScaleCADToolContext.ScaleCADToolState;
72
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
73

    
74

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

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

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

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

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

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

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

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

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

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

    
162
                        try {
163
                                double size=getCadToolAdapter().getMapControl().getViewPort().toMapDistance(getCadToolAdapter().getMapControl().getWidth());
164
                                scale(firstPoint.distance(lastPoint)/(size/40));
165
                        } catch (ValidateRowException e) {
166
                                // TODO Auto-generated catch block
167
                                e.printStackTrace();
168
                        } catch (ExpansionFileWriteException e) {
169
                                // TODO Auto-generated catch block
170
                                e.printStackTrace();
171
                        } catch (ReadDriverException e) {
172
                                // TODO Auto-generated catch block
173
                                e.printStackTrace();
174
                        } catch (ExpansionFileReadException e) {
175
                                // TODO Auto-generated catch block
176
                                e.printStackTrace();
177
                        }
178

    
179
                        PluginServices.getMDIManager().restoreCursor();
180
                } else if (status.equals("Scale.PointOriginOrScaleFactor")) {
181
                        orr = new Point2D.Double(x, y);
182
                } else if (status.equals("Scale.EndPointReference")) {
183
                        frr = new Point2D.Double(x, y);
184
                } else if (status.equals("Scale.OriginPointScale")) {
185
                        ore = new Point2D.Double(x, y);
186
                        firstPoint = ore;
187
                } else if (status.equals("Scale.EndPointScale")) {
188
                        fre = new Point2D.Double(x, y);
189

    
190
                        double distrr = orr.distance(frr);
191
                        double distre = ore.distance(fre);
192
                        double escalado = distre / distrr;
193

    
194
                        try {
195
                                scale(escalado);
196
                        } catch (ValidateRowException e) {
197
                                // TODO Auto-generated catch block
198
                                e.printStackTrace();
199
                        } catch (ExpansionFileWriteException e) {
200
                                // TODO Auto-generated catch block
201
                                e.printStackTrace();
202
                        } catch (ReadDriverException e) {
203
                                // TODO Auto-generated catch block
204
                                e.printStackTrace();
205
                        } catch (ExpansionFileReadException e) {
206
                                // TODO Auto-generated catch block
207
                                e.printStackTrace();
208
                        }
209
                }
210

    
211
    }
212

    
213
    /**
214
     * M?todo para dibujar la lo necesario para el estado en el que nos
215
     * encontremos.
216
     *
217
     * @param g Graphics sobre el que dibujar.
218
     * @param x par?metro x del punto que se pase para dibujar.
219
     * @param y par?metro x del punto que se pase para dibujar.
220
     */
221
    public void drawOperation(Graphics g, double x, double y) {
222
                ScaleCADToolState actualState = _fsm.getState();
223
                String status = actualState.getName();
224
                ArrayList selectedRow = getSelectedRows();
225
                Point2D currentPoint = new Point2D.Double(x, y);
226

    
227
                if (status.equals("Scale.ScaleFactorOrReference")) {
228
                        VectorialLayerEdited vle = getVLE();
229
                        ViewPort vp = vle.getLayer().getMapContext().getViewPort();
230
                        Point2D point = vp.fromMapPoint(firstPoint.getX(), firstPoint
231
                                        .getY());
232
                        double size = getCadToolAdapter().getMapControl().getViewPort()
233
                                        .toMapDistance(
234
                                                        getCadToolAdapter().getMapControl().getWidth());
235
                        double scale = firstPoint.distance(currentPoint) / (size / 40);
236
                        drawLine((Graphics2D) g, firstPoint, currentPoint);
237
                        if (selectedRow.size() < CADTool.TOPGEOMETRY) {
238
                                for (int i = 0; i < selectedRow.size(); i++) {
239
                                        DefaultFeature fea = (DefaultFeature) ((DefaultRowEdited) selectedRow
240
                                                        .get(i)).getLinkedRow();
241
                                        IGeometry geometry = fea.getGeometry().cloneGeometry();
242

    
243
                                        UtilFunctions.scaleGeom(geometry, firstPoint, scale, scale);
244
                                        geometry.draw((Graphics2D) g, getCadToolAdapter()
245
                                                        .getMapControl().getViewPort(),
246
                                                        CADTool.modifySymbol);
247

    
248
                                }
249

    
250
                        } else {
251
                                AffineTransform at = new AffineTransform();
252
                                at.setToTranslation(point.getX(), point.getY());
253
                                at.scale(scale, scale);
254
                                at.translate(-point.getX(), -point.getY());
255
                                Image imgSel = vle.getSelectionImage();
256
                                try {
257
                                        ((Graphics2D) g).drawImage(imgSel, at, null);
258
                                } catch (ImagingOpException e) {
259
                                }
260
                        }
261
                        PluginServices.getMainFrame().getStatusBar().setMessage("5",
262
                                        "Factor = " + scale);
263
                } else if (status.equals("Scale.EndPointScale")) {
264
                        VectorialLayerEdited vle = getVLE();
265
                        ViewPort vp = vle.getLayer().getMapContext().getViewPort();
266
                        Point2D point = vp.fromMapPoint(scalePoint.getX(), scalePoint
267
                                        .getY());
268

    
269
                        double distrr = orr.distance(frr);
270
                        double distre = ore.distance(currentPoint);
271
                        double escalado = distre / distrr;
272
                        if (selectedRow.size() < CADTool.TOPGEOMETRY) {
273
                                for (int i = 0; i < selectedRow.size(); i++) {
274
                                                DefaultFeature fea = (DefaultFeature) ((DefaultRowEdited) selectedRow
275
                                                                .get(i)).getLinkedRow();
276
                                                IGeometry geometry = fea.getGeometry().cloneGeometry();
277
                                                UtilFunctions.scaleGeom(geometry, scalePoint, escalado,
278
                                                                escalado);
279
                                                geometry.draw((Graphics2D) g, getCadToolAdapter()
280
                                                                .getMapControl().getViewPort(),
281
                                                                CADTool.modifySymbol);
282

    
283
                                        }
284
                                } else {
285
                                        AffineTransform at = new AffineTransform();
286
                                        at.setToTranslation(point.getX(), point.getY());
287
                                        at.scale(escalado, escalado);
288
                                        at.translate(-point.getX(), -point.getY());
289
                                        Image imgSel = vle.getSelectionImage();
290
                                        try {
291
                                                ((Graphics2D) g).drawImage(imgSel, at, null);
292
                                        } catch (ImagingOpException e) {
293
                                        }
294
                                }
295
                                drawLine((Graphics2D) g, firstPoint, new Point2D.Double(x, y));
296
                        }
297
        }
298

    
299
    /**
300
         * Add a diferent option.
301
         *
302
         * @param s
303
         *            Diferent option.
304
         */
305
    public void addOption(String s) {
306
            ScaleCADToolState actualState = (ScaleCADToolState) _fsm.getPreviousState();
307
        String status = actualState.getName();
308
       if (status.equals("Scale.ScaleFactorOrReference")) {
309
                /*        try {
310
                                scale(2);
311
                        } catch (DriverIOException e) {
312
                                // TODO Auto-generated catch block
313
                                e.printStackTrace();
314
                        } catch (IOException e) {
315
                                // TODO Auto-generated catch block
316
                                e.printStackTrace();
317
                        }
318
                */
319
                }
320
        }
321

    
322
    /* (non-Javadoc)
323
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
324
     */
325
    public void addValue(double d) {
326
            ScaleCADToolState actualState = (ScaleCADToolState) _fsm.getPreviousState();
327
        String status = actualState.getName();
328
        if (status.equals("Scale.ScaleFactorOrReference")) {
329
                            try {
330
                                    scale(d);
331
                            } catch (ValidateRowException e) {
332
                                        // TODO Auto-generated catch block
333
                                        e.printStackTrace();
334
                                } catch (ExpansionFileWriteException e) {
335
                                        // TODO Auto-generated catch block
336
                                        e.printStackTrace();
337
                                } catch (ReadDriverException e) {
338
                                        // TODO Auto-generated catch block
339
                                        e.printStackTrace();
340
                                } catch (ExpansionFileReadException e) {
341
                                        // TODO Auto-generated catch block
342
                                        e.printStackTrace();
343
                                }
344

    
345
            }
346
    }
347
    private void scale(double scaleFactor) throws ValidateRowException, ExpansionFileWriteException, ReadDriverException, ExpansionFileReadException{
348
            VectorialLayerEdited vle=getVLE();
349
            VectorialEditableAdapter vea=vle.getVEA();
350
            vea.startComplexRow();
351
            ArrayList selectedRow=getSelectedRows();
352
            ArrayList selectedRowAux=new ArrayList();
353
            for (int i = 0; i < selectedRow.size(); i++) {
354
                    IRowEdited edRow = (IRowEdited) selectedRow.get(i);
355
                    DefaultFeature fea = (DefaultFeature) edRow.getLinkedRow().cloneRow();
356
                        UtilFunctions.scaleGeom(fea.getGeometry(), scalePoint, scaleFactor, scaleFactor);
357
                    vea.modifyRow(edRow.getIndex(), fea,getName(),EditionEvent.GRAPHIC);
358
                    selectedRowAux.add(new DefaultRowEdited(fea,IRowEdited.STATUS_MODIFIED,edRow.getIndex()));
359
            }
360
            vea.endComplexRow(getName());
361
            vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS, selectedRowAux);
362
            //clearSelection();
363
            //selectedRow.addAll(selectedRowAux);
364
    }
365

    
366
    public String getName() {
367
                return PluginServices.getText(this,"scale_");
368
        }
369

    
370
        public String toString() {
371
                return "_scale";
372
        }
373

    
374
}