Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / StretchCADTool.java @ 29616

History | View | Annotate | Download (13.1 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 org.gvsig.editing.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.Point2D;
48
import java.awt.geom.Rectangle2D;
49

    
50
import org.gvsig.andami.PluginServices;
51
import org.gvsig.andami.messages.NotificationManager;
52
import org.gvsig.editing.CADExtension;
53
import org.gvsig.editing.gui.cad.DefaultCADTool;
54
import org.gvsig.editing.gui.cad.exception.CommandException;
55
import org.gvsig.editing.gui.cad.tools.smc.StretchCADToolContext;
56
import org.gvsig.editing.gui.cad.tools.smc.StretchCADToolContext.StretchCADToolState;
57
import org.gvsig.editing.layers.VectorialLayerEdited;
58
import org.gvsig.fmap.dal.exception.DataException;
59
import org.gvsig.fmap.dal.exception.ReadException;
60
import org.gvsig.fmap.dal.feature.DisposableIterator;
61
import org.gvsig.fmap.dal.feature.EditableFeature;
62
import org.gvsig.fmap.dal.feature.Feature;
63
import org.gvsig.fmap.dal.feature.FeatureSelection;
64
import org.gvsig.fmap.dal.feature.FeatureSet;
65
import org.gvsig.fmap.dal.feature.FeatureStore;
66
import org.gvsig.fmap.geom.Geometry;
67
import org.gvsig.fmap.geom.handler.Handler;
68
import org.gvsig.fmap.geom.operation.Draw;
69
import org.gvsig.fmap.geom.operation.DrawOperationContext;
70
import org.gvsig.fmap.geom.operation.GeometryOperationException;
71
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
72
import org.gvsig.fmap.geom.primitive.GeneralPathX;
73
import org.gvsig.fmap.mapcontext.rendering.symbols.FGraphicUtilities;
74

    
75

    
76
/**
77
 * Herramienta para estirar los handlers que seleccionemos previamente.
78
 *
79
 * @author Vicente Caballero Navarro
80
 */
81
public class StretchCADTool extends DefaultCADTool {
82
        protected StretchCADToolContext _fsm;
83
        protected Point2D selfirstPoint;
84
        protected Point2D sellastPoint;
85
        protected Point2D movefirstPoint;
86
        protected Point2D movelastPoint;
87
        protected Rectangle2D rect = null;
88

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

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

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

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

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

    
135
        /**
136
         * DOCUMENT ME!
137
         */
138
        public void selection() {
139
                FeatureSet selection = null;
140
                try {
141
                        selection = (FeatureSet) getVLE().getFeatureStore().getSelection();
142

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

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

    
177
                if (status.equals("Stretch.SelFirstPoint")) {
178
                        selfirstPoint = new Point2D.Double(x, y);
179
                } else if (status.equals("Stretch.SelLastPoint")) {
180
                        sellastPoint = new Point2D.Double(x, y);
181

    
182
                        double x1;
183
                        double y1;
184
                        double w1;
185
                        double h1;
186

    
187
                        if (selfirstPoint.getX() < sellastPoint.getX()) {
188
                                x1 = selfirstPoint.getX();
189
                                w1 = sellastPoint.getX() - selfirstPoint.getX();
190
                        } else {
191
                                x1 = sellastPoint.getX();
192
                                w1 = selfirstPoint.getX() - sellastPoint.getX();
193
                        }
194

    
195
                        if (selfirstPoint.getY() < sellastPoint.getY()) {
196
                                y1 = selfirstPoint.getY();
197
                                h1 = sellastPoint.getY() - selfirstPoint.getY();
198
                        } else {
199
                                y1 = sellastPoint.getY();
200
                                h1 = selfirstPoint.getY() - sellastPoint.getY();
201
                        }
202

    
203
                        rect = new Rectangle2D.Double(x1, y1, w1, h1);
204
                } else if (status.equals("Stretch.MoveFirstPoint")) {
205
                        movefirstPoint = new Point2D.Double(x, y);
206
                } else if (status.equals("Stretch.MoveLastPoint")) {
207
                        VectorialLayerEdited vle = getVLE();
208
                        FeatureStore featureStore = null;
209
                        DisposableIterator iterator = null;
210
                        try {
211
                                featureStore = vle.getFeatureStore();
212

    
213
                                // VectorialEditableAdapter vea=vle.getVEA();
214
                                featureStore.beginEditingGroup(getName());
215
                                // ArrayList selectedRow=getSelectedRows();
216
//                                ArrayList selectedRowAux = new ArrayList();
217
//                                PluginServices.getMDIManager().setWaitCursor();
218
                                movelastPoint = new Point2D.Double(x, y);
219

    
220
                                Handler[] handlers = null;
221

    
222
                                // for (int i = selectedGeometries.nextSetBit(0); i >= 0;
223
                                // i = selectedGeometries.nextSetBit(i + 1)) {
224
                                iterator = ((FeatureSelection) featureStore
225
                                                .getSelection()).iterator();
226
                                while (iterator.hasNext()) {
227
                                        Feature feature = (Feature) iterator.next();
228

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

    
238
                                        handlers = geometry.getHandlers(Geometry.STRETCHINGHANDLER);
239

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

    
259
//                                PluginServices.getMDIManager().restoreCursor();
260
                        } catch (DataException e) {
261
                                NotificationManager.addError(e.getMessage(), e);
262
                        } finally {
263
                                if (iterator != null) {
264
                                        iterator.dispose();
265
                                }
266
                        }
267
                }
268
        }
269

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

    
286
                // ArrayList selectedRow = getSelectedRows();
287
                DisposableIterator iterator = null;
288
                try {
289
                        iterator = ((FeatureSelection) getVLE().getFeatureStore()
290
                                        .getSelection()).iterator();
291
                        if (status.equals("Stretch.SelLastPoint")) {
292
                                GeneralPathX elShape = new GeneralPathX(
293
                                                GeneralPathX.WIND_EVEN_ODD, 4);
294
                                elShape.moveTo(selfirstPoint.getX(), selfirstPoint.getY());
295
                                elShape.lineTo(x, selfirstPoint.getY());
296
                                elShape.lineTo(x, y);
297
                                elShape.lineTo(selfirstPoint.getX(), y);
298
                                elShape.lineTo(selfirstPoint.getX(), selfirstPoint.getY());
299

    
300
                                DrawOperationContext doc = new DrawOperationContext();
301
                                doc.setGraphics((Graphics2D) g);
302
                                doc.setViewPort(getCadToolAdapter().getMapControl()
303
                                                .getViewPort());
304
                                doc.setSymbol(DefaultCADTool.axisReferencesSymbol);
305
                                try {
306
                                        createCurve(elShape).invokeOperation(Draw.CODE, doc);
307
                                } catch (GeometryOperationNotSupportedException e) {
308
                                        e.printStackTrace();
309
                                } catch (GeometryOperationException e) {
310
                                        e.printStackTrace();
311
                                }
312
                                // geomFactory.createPolyline2D(elShape).draw((Graphics2D) g,
313
                                // getCadToolAdapter().getMapControl().getViewPort(),
314
                                // DefaultCADTool.axisReferencesSymbol);
315
                        } else if (status.equals("Stretch.MoveFirstPoint")) {
316

    
317
                                Handler[] handlers = null;
318
                                while (iterator.hasNext()) {
319
                                        Feature feature = (Feature) iterator.next();
320

    
321
                                        // }
322
                                        // for (int i = 0;i<selectedRow.size();i++) {
323
                                        // IRowEdited edRow = (IRowEdited) selectedRow.get(i);
324
                                        // DefaultFeature fea = (DefaultFeature)
325
                                        // edRow.getLinkedRow().cloneRow();
326
                                        Geometry geometry = null;
327
                                        geometry = (feature.getDefaultGeometry()).cloneGeometry();
328

    
329
                                        handlers = geometry.getHandlers(Geometry.STRETCHINGHANDLER);
330

    
331
                                        for (int j = 0; j < handlers.length; j++) {
332
                                                if (rect.contains(handlers[j].getPoint())) {
333
                                                        FGraphicUtilities
334
                                                                        .DrawHandlers((Graphics2D) g,
335
                                                                                        getCadToolAdapter().getMapControl()
336
                                                                                                        .getViewPort()
337
                                                                                                        .getAffineTransform(),
338
                                                                                        new Handler[] { handlers[j] },
339
                                                                                        DefaultCADTool.handlerSymbol);
340
                                                }
341
                                        }
342
                                }
343
                        } else if (status.equals("Stretch.MoveLastPoint")) {
344
                                Handler[] handlers = null;
345
                                while (iterator.hasNext()) {
346
                                        Feature feature = (Feature) iterator.next();
347
                                        // for (int i = 0;i<selectedRow.size();i++) {
348
                                        // IRowEdited edRow = (IRowEdited) selectedRow.get(i);
349
                                        // DefaultFeature fea = (DefaultFeature)
350
                                        // edRow.getLinkedRow().cloneRow();
351
                                        Geometry geometry = null;
352
                                        geometry = (feature.getDefaultGeometry()).cloneGeometry();
353

    
354
                                        handlers = geometry.getHandlers(Geometry.STRETCHINGHANDLER);
355

    
356
                                        for (int j = 0; j < handlers.length; j++) {
357
                                                if (rect.contains(handlers[j].getPoint())) {
358
                                                        handlers[j].move(x - movefirstPoint.getX(), y
359
                                                                        - movefirstPoint.getY());
360
                                                }
361
                                        }
362
                                        DrawOperationContext doc = new DrawOperationContext();
363
                                        doc.setGraphics((Graphics2D) g);
364
                                        doc.setViewPort(getCadToolAdapter().getMapControl()
365
                                                        .getViewPort());
366
                                        doc.setSymbol(DefaultCADTool.axisReferencesSymbol);
367
                                        try {
368
                                                geometry.cloneGeometry()
369
                                                                .invokeOperation(Draw.CODE, doc);
370
                                        } catch (GeometryOperationNotSupportedException e) {
371
                                                e.printStackTrace();
372
                                        } catch (GeometryOperationException e) {
373
                                                e.printStackTrace();
374
                                        }
375
                                        // geometry.draw((Graphics2D) g,
376
                                        // getCadToolAdapter().getMapControl().getViewPort(),
377
                                        // DefaultCADTool.axisReferencesSymbol);
378
                                }
379
                        } else {
380
                                VectorialLayerEdited vle = getVLE();
381
                                if (!vle.getLayer().isVisible()) {
382
                                        return;
383
                                }
384
                                try {
385
                                        Image imgSel = vle.getSelectionImage();
386
                                        if (imgSel != null) {
387
                                                g.drawImage(imgSel, 0, 0, null);
388
                                        }
389
                                        Image imgHand = vle.getHandlersImage();
390
                                        if (imgHand != null) {
391
                                                g.drawImage(imgHand, 0, 0, null);
392
                                        }
393
                                } catch (Exception e) {
394
                                }
395
                        }
396

    
397
                } catch (DataException e) {
398
                        // TODO Auto-generated catch block
399
                        e.printStackTrace();
400
                } finally {
401
                        if (iterator != null) {
402
                                iterator.dispose();
403
                        }
404
                }
405

    
406
        }
407

    
408
        /**
409
         * Add a diferent option.
410
         *
411
         * @param s
412
         *            Diferent option.
413
         */
414
        public void addOption(String s) {
415
        }
416

    
417
        /*
418
         * (non-Javadoc)
419
         *
420
         * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
421
         */
422
        public void addValue(double d) {
423
        }
424

    
425
        public String getName() {
426
                return PluginServices.getText(this, "stretch_");
427
        }
428

    
429
        public String toString() {
430
                return "_stretch";
431
        }
432

    
433
        public boolean isApplicable(int shapeType) {
434
                switch (shapeType) {
435
                case Geometry.TYPES.POINT:
436
                case Geometry.TYPES.MULTIPOINT:
437
                        return false;
438
                }
439
                return true;
440
        }
441
}