Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extEditing / src / com / iver / cit / gvsig / gui / cad / tools / StretchCADTool.java @ 26329

History | View | Annotate | Download (12.9 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.Rectangle2D;
48
import java.util.ArrayList;
49
import java.util.Iterator;
50

    
51
import org.gvsig.fmap.dal.exception.DataException;
52
import org.gvsig.fmap.dal.exception.ReadException;
53
import org.gvsig.fmap.dal.feature.EditableFeature;
54
import org.gvsig.fmap.dal.feature.Feature;
55
import org.gvsig.fmap.dal.feature.FeatureSelection;
56
import org.gvsig.fmap.dal.feature.FeatureSet;
57
import org.gvsig.fmap.dal.feature.FeatureStore;
58
import org.gvsig.fmap.geom.Geometry;
59
import org.gvsig.fmap.geom.GeometryFactory;
60
import org.gvsig.fmap.geom.GeometryLocator;
61
import org.gvsig.fmap.geom.GeometryManager;
62
import org.gvsig.fmap.geom.handler.Handler;
63
import org.gvsig.fmap.geom.operation.Draw;
64
import org.gvsig.fmap.geom.operation.DrawOperationContext;
65
import org.gvsig.fmap.geom.operation.GeometryOperationException;
66
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
67
import org.gvsig.fmap.geom.primitive.GeneralPathX;
68
import org.gvsig.fmap.mapcontext.rendering.symbols.FGraphicUtilities;
69

    
70
import com.iver.andami.PluginServices;
71
import com.iver.andami.messages.NotificationManager;
72
import com.iver.cit.gvsig.CADExtension;
73
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
74
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
75
import com.iver.cit.gvsig.gui.cad.tools.smc.StretchCADToolContext;
76
import com.iver.cit.gvsig.gui.cad.tools.smc.StretchCADToolContext.StretchCADToolState;
77
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
78

    
79
/**
80
 * Herramienta para estirar los handlers que seleccionemos previamente.
81
 *
82
 * @author Vicente Caballero Navarro
83
 */
84
public class StretchCADTool extends DefaultCADTool {
85
        private StretchCADToolContext _fsm;
86
        private Point2D selfirstPoint;
87
        private Point2D sellastPoint;
88
        private Point2D movefirstPoint;
89
        private Point2D movelastPoint;
90
        private Rectangle2D rect = null;
91
        private GeometryFactory geomFactory = GeometryLocator.getGeometryManager()
92
                        .getGeometryFactory();
93

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

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

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

    
118
        /*
119
         * (non-Javadoc)
120
         *
121
         * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
122
         *      double)
123
         */
124
        public void transition(double d) {
125
                _fsm.addValue(d);
126
        }
127

    
128
        /*
129
         * (non-Javadoc)
130
         *
131
         * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
132
         *      java.lang.String)
133
         */
134
        public void transition(String s) throws CommandException {
135
                if (!super.changeCommand(s)) {
136
                        _fsm.addOption(s);
137
                }
138
        }
139

    
140
        /**
141
         * DOCUMENT ME!
142
         */
143
        public void selection() {
144
                FeatureSet selection = null;
145
                try {
146
                        selection = (FeatureSet) getVLE().getFeatureStore().getSelection();
147

    
148
                        if (selection.getSize() == 0
149
                                        && !CADExtension
150
                                                        .getCADTool()
151
                                                        .getClass()
152
                                                        .getName()
153
                                                        .equals(
154
                                                                        "com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
155
                                CADExtension.setCADTool("_selection", false);
156
                                ((SelectionCADTool) CADExtension.getCADTool())
157
                                                .setNextTool("_stretch");
158
                        }
159
                } catch (ReadException e) {
160
                        // TODO Auto-generated catch block
161
                        e.printStackTrace();
162
                } catch (DataException e) {
163
                        // TODO Auto-generated catch block
164
                        e.printStackTrace();
165
                }
166
        }
167

    
168
        /**
169
         * Equivale al transition del prototipo pero sin pasarle como par?metro el
170
         * editableFeatureSource que ya estar? creado.
171
         *
172
         * @param x
173
         *            par?metro x del punto que se pase en esta transici?n.
174
         * @param y
175
         *            par?metro y del punto que se pase en esta transici?n.
176
         */
177
        public void addPoint(double x, double y, InputEvent event) {
178
                StretchCADToolState actualState = (StretchCADToolState) _fsm
179
                                .getPreviousState();
180
                String status = actualState.getName();
181

    
182
                if (status.equals("Stretch.SelFirstPoint")) {
183
                        selfirstPoint = new Point2D.Double(x, y);
184
                } else if (status.equals("Stretch.SelLastPoint")) {
185
                        sellastPoint = new Point2D.Double(x, y);
186

    
187
                        double x1;
188
                        double y1;
189
                        double w1;
190
                        double h1;
191

    
192
                        if (selfirstPoint.getX() < sellastPoint.getX()) {
193
                                x1 = selfirstPoint.getX();
194
                                w1 = sellastPoint.getX() - selfirstPoint.getX();
195
                        } else {
196
                                x1 = sellastPoint.getX();
197
                                w1 = selfirstPoint.getX() - sellastPoint.getX();
198
                        }
199

    
200
                        if (selfirstPoint.getY() < sellastPoint.getY()) {
201
                                y1 = selfirstPoint.getY();
202
                                h1 = sellastPoint.getY() - selfirstPoint.getY();
203
                        } else {
204
                                y1 = sellastPoint.getY();
205
                                h1 = selfirstPoint.getY() - sellastPoint.getY();
206
                        }
207

    
208
                        rect = new Rectangle2D.Double(x1, y1, w1, h1);
209
                } else if (status.equals("Stretch.MoveFirstPoint")) {
210
                        movefirstPoint = new Point2D.Double(x, y);
211
                } else if (status.equals("Stretch.MoveLastPoint")) {
212
                        VectorialLayerEdited vle = getVLE();
213
                        FeatureStore featureStore = null;
214
                        try {
215
                                featureStore = vle.getFeatureStore();
216

    
217
                                // VectorialEditableAdapter vea=vle.getVEA();
218
                                featureStore.beginEditingGroup(getName());
219
                                // ArrayList selectedRow=getSelectedRows();
220
                                ArrayList selectedRowAux = new ArrayList();
221
                                PluginServices.getMDIManager().setWaitCursor();
222
                                movelastPoint = new Point2D.Double(x, y);
223

    
224
                                Handler[] handlers = null;
225

    
226
                                // for (int i = selectedGeometries.nextSetBit(0); i >= 0;
227
                                // i = selectedGeometries.nextSetBit(i + 1)) {
228
                                Iterator iterator = ((FeatureSelection) featureStore
229
                                                .getSelection()).iterator();
230
                                while (iterator.hasNext()) {
231
                                        Feature feature = (Feature) iterator.next();
232

    
233
                                        // }
234
                                        // for (int i =0;i<selectedRow.size(); i++) {
235
                                        // IRowEdited edRow = (IRowEdited) selectedRow.get(i);
236
                                        // DefaultFeature fea = (DefaultFeature)
237
                                        // edRow.getLinkedRow().cloneRow();
238
                                        Geometry geometry = null;
239
                                        geometry = ((Geometry) feature.getDefaultGeometry())
240
                                                        .cloneGeometry();
241

    
242
                                        handlers = geometry.getHandlers(Geometry.STRETCHINGHANDLER);
243

    
244
                                        for (int j = 0; j < handlers.length; j++) {
245
                                                if (rect.contains(handlers[j].getPoint())) {
246
                                                        handlers[j].move(movelastPoint.getX()
247
                                                                        - movefirstPoint.getX(), movelastPoint
248
                                                                        .getY()
249
                                                                        - movefirstPoint.getY());
250
                                                }
251
                                        }
252
                                        EditableFeature eFeature = feature.getEditable();
253
                                        eFeature.setGeometry(featureStore.getDefaultFeatureType()
254
                                                        .getDefaultGeometryAttributeName(), geometry);
255
                                        // vea.modifyRow(edRow.getIndex(),fea,getName(),EditionEvent.GRAPHIC);
256
                                        featureStore.update(eFeature);
257
                                        selectedRowAux.add(feature);
258
                                }
259
                                featureStore.endEditingGroup();
260
                                // vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS,
261
                                // selectedRowAux);
262

    
263
                                PluginServices.getMDIManager().restoreCursor();
264
                        } catch (ReadException e) {
265
                                NotificationManager.addError(e.getMessage(), e);
266
                        } catch (DataException e) {
267
                                NotificationManager.addError(e.getMessage(), e);
268
                        }
269
                }
270
        }
271

    
272
        /**
273
         * M?todo para dibujar la lo necesario para el estado en el que nos
274
         * encontremos.
275
         *
276
         * @param g
277
         *            Graphics sobre el que dibujar.
278
         * @param x
279
         *            par?metro x del punto que se pase para dibujar.
280
         * @param y
281
         *            par?metro x del punto que se pase para dibujar.
282
         */
283
        public void drawOperation(Graphics g, double x, double y) {
284
                StretchCADToolState actualState = ((StretchCADToolContext) _fsm)
285
                                .getState();
286
                String status = actualState.getName();
287

    
288
                // ArrayList selectedRow = getSelectedRows();
289
                Iterator iterator = null;
290
                try {
291
                        iterator = ((FeatureSelection) getVLE().getFeatureStore()
292
                                        .getSelection()).iterator();
293
                } catch (ReadException e1) {
294
                        // TODO Auto-generated catch block
295
                        e1.printStackTrace();
296
                } catch (DataException e) {
297
                        // TODO Auto-generated catch block
298
                        e.printStackTrace();
299
                }
300
                if (status.equals("Stretch.SelLastPoint")) {
301
                        GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
302
                                        4);
303
                        elShape.moveTo(selfirstPoint.getX(), selfirstPoint.getY());
304
                        elShape.lineTo(x, selfirstPoint.getY());
305
                        elShape.lineTo(x, y);
306
                        elShape.lineTo(selfirstPoint.getX(), y);
307
                        elShape.lineTo(selfirstPoint.getX(), selfirstPoint.getY());
308

    
309
                        DrawOperationContext doc = new DrawOperationContext();
310
                        doc.setGraphics((Graphics2D) g);
311
                        doc.setViewPort(getCadToolAdapter().getMapControl().getViewPort());
312
                        doc.setSymbol(DefaultCADTool.axisReferencesSymbol);
313
                        try {
314
                                geomFactory.createPolyline2D(elShape).invokeOperation(
315
                                                Draw.CODE, doc);
316
                        } catch (GeometryOperationNotSupportedException e) {
317
                                e.printStackTrace();
318
                        } catch (GeometryOperationException e) {
319
                                e.printStackTrace();
320
                        }
321
                        // geomFactory.createPolyline2D(elShape).draw((Graphics2D) g,
322
                        // getCadToolAdapter().getMapControl().getViewPort(),
323
                        // DefaultCADTool.axisReferencesSymbol);
324
                } else if (status.equals("Stretch.MoveFirstPoint")) {
325

    
326
                        Handler[] handlers = null;
327
                        while (iterator.hasNext()) {
328
                                Feature feature = (Feature) iterator.next();
329

    
330
                                // }
331
                                // for (int i = 0;i<selectedRow.size();i++) {
332
                                // IRowEdited edRow = (IRowEdited) selectedRow.get(i);
333
                                // DefaultFeature fea = (DefaultFeature)
334
                                // edRow.getLinkedRow().cloneRow();
335
                                Geometry geometry = null;
336
                                geometry = ((Geometry) feature.getDefaultGeometry())
337
                                                .cloneGeometry();
338

    
339
                                handlers = geometry.getHandlers(Geometry.STRETCHINGHANDLER);
340

    
341
                                for (int j = 0; j < handlers.length; j++) {
342
                                        if (rect.contains(handlers[j].getPoint())) {
343
                                                FGraphicUtilities.DrawHandlers((Graphics2D) g,
344
                                                                getCadToolAdapter().getMapControl()
345
                                                                                .getViewPort().getAffineTransform(),
346
                                                                new Handler[] { handlers[j] },
347
                                                                DefaultCADTool.handlerSymbol);
348
                                        }
349
                                }
350
                        }
351
                } else if (status.equals("Stretch.MoveLastPoint")) {
352
                        Handler[] handlers = null;
353
                        while (iterator.hasNext()) {
354
                                Feature feature = (Feature) iterator.next();
355
                                // for (int i = 0;i<selectedRow.size();i++) {
356
                                // IRowEdited edRow = (IRowEdited) selectedRow.get(i);
357
                                // DefaultFeature fea = (DefaultFeature)
358
                                // edRow.getLinkedRow().cloneRow();
359
                                Geometry geometry = null;
360
                                geometry = ((Geometry) feature.getDefaultGeometry())
361
                                                .cloneGeometry();
362

    
363
                                handlers = geometry.getHandlers(Geometry.STRETCHINGHANDLER);
364

    
365
                                for (int j = 0; j < handlers.length; j++) {
366
                                        if (rect.contains(handlers[j].getPoint())) {
367
                                                handlers[j].move(x - movefirstPoint.getX(), y
368
                                                                - movefirstPoint.getY());
369
                                        }
370
                                }
371
                                DrawOperationContext doc = new DrawOperationContext();
372
                                doc.setGraphics((Graphics2D) g);
373
                                doc.setViewPort(getCadToolAdapter().getMapControl()
374
                                                .getViewPort());
375
                                doc.setSymbol(DefaultCADTool.axisReferencesSymbol);
376
                                try {
377
                                        geometry.cloneGeometry().invokeOperation(Draw.CODE, doc);
378
                                } catch (GeometryOperationNotSupportedException e) {
379
                                        e.printStackTrace();
380
                                } catch (GeometryOperationException e) {
381
                                        e.printStackTrace();
382
                                }
383
                                // geometry.draw((Graphics2D) g,
384
                                // getCadToolAdapter().getMapControl().getViewPort(),
385
                                // DefaultCADTool.axisReferencesSymbol);
386
                        }
387
                }
388
        }
389

    
390
        /**
391
         * Add a diferent option.
392
         *
393
         * @param s
394
         *            Diferent option.
395
         */
396
        public void addOption(String s) {
397
        }
398

    
399
        /*
400
         * (non-Javadoc)
401
         *
402
         * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
403
         */
404
        public void addValue(double d) {
405
        }
406

    
407
        public String getName() {
408
                return PluginServices.getText(this, "stretch_");
409
        }
410

    
411
        public String toString() {
412
                return "_stretch";
413
        }
414

    
415
        public boolean isApplicable(int shapeType) {
416
                switch (shapeType) {
417
                case Geometry.TYPES.POINT:
418
                case Geometry.TYPES.MULTIPOINT:
419
                        return false;
420
                }
421
                return true;
422
        }
423
}