Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / ScaleCADTool.java @ 23424

History | View | Annotate | Download (13.6 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
import java.util.Iterator;
53

    
54
import org.gvsig.fmap.data.DataException;
55
import org.gvsig.fmap.data.ReadException;
56
import org.gvsig.fmap.data.feature.Feature;
57
import org.gvsig.fmap.data.feature.FeatureCollection;
58
import org.gvsig.fmap.data.feature.FeatureStore;
59
import org.gvsig.fmap.geom.Geometry;
60
import org.gvsig.fmap.geom.operation.Draw;
61
import org.gvsig.fmap.geom.operation.DrawOperationContext;
62
import org.gvsig.fmap.geom.operation.GeometryOperationException;
63
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
64
import org.gvsig.fmap.geom.util.UtilFunctions;
65
import org.gvsig.fmap.mapcontext.ViewPort;
66

    
67
import com.iver.andami.PluginServices;
68
import com.iver.andami.messages.NotificationManager;
69
import com.iver.cit.gvsig.CADExtension;
70
import com.iver.cit.gvsig.gui.cad.CADTool;
71
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
72
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
73
import com.iver.cit.gvsig.gui.cad.tools.smc.ScaleCADToolContext;
74
import com.iver.cit.gvsig.gui.cad.tools.smc.ScaleCADToolContext.ScaleCADToolState;
75
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
76

    
77

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

    
93
    /**
94
     * Crea un nuevo PolylineCADTool.
95
     */
96
    public ScaleCADTool() {
97
    }
98

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

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

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

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

    
130
    /**
131
     * DOCUMENT ME!
132
     */
133
    public void selection() {
134
            FeatureCollection selection=null;
135
            try {
136
                    selection = (FeatureCollection)getVLE().getFeatureStore().getSelection();
137
            } catch (ReadException e) {
138
                    // TODO Auto-generated catch block
139
                    e.printStackTrace();
140
            }
141
        if (selection.size() == 0 && !CADExtension.getCADTool().getClass().getName().equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
142
            CADExtension.setCADTool("_selection",false);
143
            ((SelectionCADTool) CADExtension.getCADTool()).setNextTool(
144
                "_scale");
145
        }
146
    }
147

    
148
    /**
149
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
150
     * editableFeatureSource que ya estar? creado.
151
     *
152
     * @param x par?metro x del punto que se pase en esta transici?n.
153
     * @param y par?metro y del punto que se pase en esta transici?n.
154
     */
155
    public void addPoint(double x, double y,InputEvent event) {
156
        ScaleCADToolState actualState = (ScaleCADToolState) _fsm.getPreviousState();
157
        String status = actualState.getName();
158

    
159
        if (status.equals("Scale.PointMain")) {
160
                                firstPoint = new Point2D.Double(x, y);
161
                            scalePoint = firstPoint;
162
                } else if (status.equals("Scale.ScaleFactorOrReference")) {
163
                        PluginServices.getMDIManager().setWaitCursor();
164
                        lastPoint = new Point2D.Double(x, y);
165

    
166
                        //double w;
167
                        //double h;
168
                        //w = lastPoint.getX() - firstPoint.getX();
169
                        //h = lastPoint.getY() - firstPoint.getY();
170

    
171
                        try {
172
                                double size=getCadToolAdapter().getMapControl().getViewPort().toMapDistance(getCadToolAdapter().getMapControl().getWidth());
173
                                scale(firstPoint.distance(lastPoint)/(size/40));
174
                        } catch (ReadException e) {
175
                                NotificationManager.addError(e.getMessage(),e);
176
                        }
177

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

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

    
193
                        try {
194
                                scale(escalado);
195
                        } catch (ReadException e) {
196
                                NotificationManager.addError(e.getMessage(),e);
197
                        }
198
                }
199

    
200
    }
201

    
202
    /**
203
     * M?todo para dibujar la lo necesario para el estado en el que nos
204
     * encontremos.
205
     *
206
     * @param g Graphics sobre el que dibujar.
207
     * @param x par?metro x del punto que se pase para dibujar.
208
     * @param y par?metro x del punto que se pase para dibujar.
209
     */
210
    public void drawOperation(Graphics g, double x, double y) {
211
                ScaleCADToolState actualState = _fsm.getState();
212
                String status = actualState.getName();
213
                FeatureCollection selection=null;
214
                try {
215
                        selection = (FeatureCollection)getVLE().getFeatureStore().getSelection();
216
                } catch (ReadException e1) {
217
                        // TODO Auto-generated catch block
218
                        e1.printStackTrace();
219
                }
220
//                ArrayList selectedRow = getSelectedRows();
221
                Point2D currentPoint = new Point2D.Double(x, y);
222
                int selectionCount=selection.size();
223
                if (status.equals("Scale.ScaleFactorOrReference")) {
224
                        VectorialLayerEdited vle = getVLE();
225
                        ViewPort vp = vle.getLayer().getMapContext().getViewPort();
226
                        Point2D point = vp.fromMapPoint(firstPoint.getX(), firstPoint
227
                                        .getY());
228
                        double size = getCadToolAdapter().getMapControl().getViewPort()
229
                                        .toMapDistance(
230
                                                        getCadToolAdapter().getMapControl().getWidth());
231
                        double scale = firstPoint.distance(currentPoint) / (size / 40);
232
                        drawLine((Graphics2D) g, firstPoint, currentPoint,DefaultCADTool.axisReferencesSymbol);
233

    
234
                        if (selectionCount < CADTool.TOPGEOMETRY) {
235
                                Iterator iterator=selection.iterator();
236
                                while (iterator.hasNext()) {
237
                                        Feature feature = (Feature) iterator.next();
238

    
239
//                                }
240
//                                for (int i = 0; i < selectionCount; i++) {
241
//                                        DefaultFeature fea = (DefaultFeature) ((DefaultRowEdited) selectedRow
242
//                                                        .get(i)).getLinkedRow();
243
                                        Geometry geometry = ((Geometry)feature.getDefaultGeometry()).cloneGeometry();
244

    
245
                                        UtilFunctions.scaleGeom(geometry, firstPoint, scale, scale);
246

    
247
                                        DrawOperationContext doc=new DrawOperationContext();
248
                                    doc.setGraphics((Graphics2D)g);
249
                                    doc.setViewPort(getCadToolAdapter().getMapControl().getViewPort());
250
                                    doc.setSymbol(DefaultCADTool.axisReferencesSymbol);
251
                            try {
252
                                                geometry.invokeOperation(Draw.CODE,doc);
253
                                        } catch (GeometryOperationNotSupportedException e) {
254
                                                e.printStackTrace();
255
                                        } catch (GeometryOperationException e) {
256
                                                e.printStackTrace();
257
                                        }
258

    
259
//                                        geometry.draw((Graphics2D) g, getCadToolAdapter()
260
//                                                        .getMapControl().getViewPort(),
261
//                                                        DefaultCADTool.axisReferencesSymbol);
262

    
263
                                }
264

    
265
                        } else {
266
                                AffineTransform at = new AffineTransform();
267
                                at.setToTranslation(point.getX(), point.getY());
268
                                at.scale(scale, scale);
269
                                at.translate(-point.getX(), -point.getY());
270
                                Image imgSel = vle.getSelectionImage();
271
                                try {
272
                                        ((Graphics2D) g).drawImage(imgSel, at, null);
273
                                } catch (ImagingOpException e) {
274
                                }
275
                        }
276
                        PluginServices.getMainFrame().getStatusBar().setMessage("5",
277
                                        "Factor = " + scale);
278
                } else if (status.equals("Scale.EndPointScale")) {
279
                        VectorialLayerEdited vle = getVLE();
280
                        ViewPort vp = vle.getLayer().getMapContext().getViewPort();
281
                        Point2D point = vp.fromMapPoint(scalePoint.getX(), scalePoint
282
                                        .getY());
283

    
284
                        double distrr = orr.distance(frr);
285
                        double distre = ore.distance(currentPoint);
286
                        double escalado = distre / distrr;
287
                        if (selectionCount < CADTool.TOPGEOMETRY) {
288
                                Iterator iterator=selection.iterator();
289
                                while (iterator.hasNext()) {
290
                                        Feature feature = (Feature) iterator.next();
291
//                                for (int i = 0; i < selectedRow.size(); i++) {
292
//                                                DefaultFeature fea = (DefaultFeature) ((DefaultRowEdited) selectedRow
293
//                                                                .get(i)).getLinkedRow();
294
                                                Geometry geometry = ((Geometry)feature.getDefaultGeometry()).cloneGeometry();
295
                                                UtilFunctions.scaleGeom(geometry, scalePoint, escalado,
296
                                                                escalado);
297

    
298
                                                DrawOperationContext doc=new DrawOperationContext();
299
                                            doc.setGraphics((Graphics2D)g);
300
                                            doc.setViewPort(getCadToolAdapter().getMapControl().getViewPort());
301
                                            doc.setSymbol(DefaultCADTool.axisReferencesSymbol);
302
                                    try {
303
                                                        geometry.invokeOperation(Draw.CODE,doc);
304
                                                } catch (GeometryOperationNotSupportedException e) {
305
                                                        e.printStackTrace();
306
                                                } catch (GeometryOperationException e) {
307
                                                        e.printStackTrace();
308
                                                }
309

    
310
//                                                geometry.draw((Graphics2D) g, getCadToolAdapter()
311
//                                                                .getMapControl().getViewPort(),
312
//                                                                DefaultCADTool.axisReferencesSymbol);
313

    
314
                                        }
315
                                } else {
316
                                        AffineTransform at = new AffineTransform();
317
                                        at.setToTranslation(point.getX(), point.getY());
318
                                        at.scale(escalado, escalado);
319
                                        at.translate(-point.getX(), -point.getY());
320
                                        Image imgSel = vle.getSelectionImage();
321
                                        try {
322
                                                ((Graphics2D) g).drawImage(imgSel, at, null);
323
                                        } catch (ImagingOpException e) {
324
                                        }
325
                                }
326
                                drawLine((Graphics2D) g, firstPoint, new Point2D.Double(x, y),DefaultCADTool.axisReferencesSymbol);
327
                        }
328
        }
329

    
330
    /**
331
         * Add a diferent option.
332
         *
333
         * @param s
334
         *            Diferent option.
335
         */
336
    public void addOption(String s) {
337
            ScaleCADToolState actualState = (ScaleCADToolState) _fsm.getPreviousState();
338
            String status = actualState.getName();
339
            if (status.equals("Scale.ScaleFactorOrReference")) {
340
                    /*        try {
341
                                scale(2);
342
                        } catch (DriverIOException e) {
343
                                // TODO Auto-generated catch block
344
                                e.printStackTrace();
345
                        } catch (IOException e) {
346
                                // TODO Auto-generated catch block
347
                                e.printStackTrace();
348
                        }
349
                     */
350
            }
351
    }
352

    
353
    /* (non-Javadoc)
354
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
355
     */
356
    public void addValue(double d) {
357
            ScaleCADToolState actualState = (ScaleCADToolState) _fsm.getPreviousState();
358
            String status = actualState.getName();
359
            if (status.equals("Scale.ScaleFactorOrReference")) {
360
                    try {
361
                            scale(d);
362
                    } catch (ReadException e) {
363
                            NotificationManager.addError(e.getMessage(),e);
364
                    }
365
            }
366
    }
367
    private void scale(double scaleFactor) throws ReadException{
368
            VectorialLayerEdited vle=getVLE();
369
            FeatureStore featureStore=vle.getFeatureStore();
370

    
371
            featureStore.getCommandsRecord().startComplex();
372
            Iterator iterator =featureStore.getSelection().iterator();
373
            ArrayList selectedRowAux=new ArrayList();
374
            while (iterator.hasNext()) {
375
                        Feature feature = (Feature) iterator.next();
376

    
377
//                }
378
//            for (int i = 0; i < selectedRow.size(); i++) {
379
//                    IRowEdited edRow = (IRowEdited) selectedRow.get(i);
380
//                    DefaultFeature fea = (DefaultFeature) edRow.getLinkedRow().cloneRow();
381
                        Geometry geometry=((Geometry)feature.getDefaultGeometry()).cloneGeometry();
382
                        UtilFunctions.scaleGeom(geometry, scalePoint, scaleFactor, scaleFactor);
383
                    try {
384
                                feature.editing();
385
                                feature.setGeometry(featureStore.getDefaultFeatureType().getDefaultGeometry(), geometry);
386
                                featureStore.update(feature);
387
                    } catch (DataException e) {
388
                                // TODO Auto-generated catch block
389
                                e.printStackTrace();
390
                        }
391
//                        vea.modifyRow(edRow.getIndex(), fea,getName(),EditionEvent.GRAPHIC);
392
                    selectedRowAux.add(feature);
393
            }
394
            featureStore.getCommandsRecord().endComplex(getName());
395
//            vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS, selectedRowAux);
396
            //clearSelection();
397
            //selectedRow.addAll(selectedRowAux);
398
    }
399

    
400
    public String getName() {
401
                return PluginServices.getText(this,"scale_");
402
        }
403

    
404
        public String toString() {
405
                return "_scale";
406
        }
407

    
408
}