Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.editing.app / org.gvsig.editing.app.mainplugin / src / main / java / org / gvsig / editing / gui / cad / tools / StretchCADTool.java @ 40557

History | View | Annotate | Download (14.6 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.editing.gui.cad.tools;
25

    
26
import java.awt.event.InputEvent;
27
import java.awt.geom.Point2D;
28
import java.awt.geom.Rectangle2D;
29

    
30
import org.gvsig.andami.PluginServices;
31
import org.gvsig.andami.messages.NotificationManager;
32
import org.gvsig.editing.CADExtension;
33
import org.gvsig.editing.gui.cad.DefaultCADTool;
34
import org.gvsig.editing.gui.cad.exception.CommandException;
35
import org.gvsig.editing.gui.cad.tools.smc.StretchCADToolContext;
36
import org.gvsig.editing.gui.cad.tools.smc.StretchCADToolContext.StretchCADToolState;
37
import org.gvsig.editing.layers.VectorialLayerEdited;
38
import org.gvsig.fmap.dal.exception.DataException;
39
import org.gvsig.fmap.dal.exception.ReadException;
40
import org.gvsig.fmap.dal.feature.EditableFeature;
41
import org.gvsig.fmap.dal.feature.Feature;
42
import org.gvsig.fmap.dal.feature.FeatureSelection;
43
import org.gvsig.fmap.dal.feature.FeatureSet;
44
import org.gvsig.fmap.dal.feature.FeatureStore;
45
import org.gvsig.fmap.geom.Geometry;
46
import org.gvsig.fmap.geom.handler.Handler;
47
import org.gvsig.fmap.geom.primitive.GeneralPathX;
48
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
49
import org.gvsig.tools.dispose.DisposableIterator;
50

    
51
/**
52
 * Herramienta para estirar los handlers que seleccionemos previamente.
53
 * 
54
 * @author Vicente Caballero Navarro
55
 */
56
public class StretchCADTool extends DefaultCADTool {
57

    
58
    protected StretchCADToolContext _fsm;
59
    protected Point2D selfirstPoint;
60
    protected Point2D sellastPoint;
61
    protected Point2D movefirstPoint;
62
    protected Point2D movelastPoint;
63
    protected Rectangle2D rect = null;
64

    
65
    /**
66
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
67
     * carga previa a la utilizaci?n de la herramienta.
68
     */
69
    public void init() {
70
        _fsm = new StretchCADToolContext(this);
71
    }
72

    
73
    /*
74
     * (non-Javadoc)
75
     * 
76
     * @see
77
     * com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
78
     * .layers.FBitSet,
79
     * double, double)
80
     */
81
    public void transition(double x, double y, InputEvent event) {
82
        _fsm.addPoint(x, y, event);
83
    }
84

    
85
    /*
86
     * (non-Javadoc)
87
     * 
88
     * @see
89
     * com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
90
     * .layers.FBitSet,
91
     * double)
92
     */
93
    public void transition(double d) {
94
        _fsm.addValue(d);
95
    }
96

    
97
    /*
98
     * (non-Javadoc)
99
     * 
100
     * @see
101
     * com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
102
     * .layers.FBitSet,
103
     * java.lang.String)
104
     */
105
    public void transition(String s) throws CommandException {
106
        if (!super.changeCommand(s)) {
107
            _fsm.addOption(s);
108
        }
109
    }
110

    
111
    /**
112
     * DOCUMENT ME!
113
     */
114
    public void selection() {
115
        FeatureSet selection = null;
116
        try {
117
            selection = (FeatureSet) getVLE().getFeatureStore().getSelection();
118

    
119
            if (selection.getSize() == 0
120
                && !SelectionCADTool.isInstance(CADExtension.getCADTool(), true)) {
121
                CADExtension.setCADTool("_selection", false);
122
                ((SelectionCADTool) CADExtension.getCADTool())
123
                    .setNextTool("_stretch");
124
            }
125
        } catch (ReadException e) {
126
            // TODO Auto-generated catch block
127
            e.printStackTrace();
128
        } catch (DataException e) {
129
            // TODO Auto-generated catch block
130
            e.printStackTrace();
131
        }
132
    }
133

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

    
148
        if (status.equals("Stretch.SelFirstPoint")) {
149
            selfirstPoint = new Point2D.Double(x, y);
150
        } else
151
            if (status.equals("Stretch.SelLastPoint")) {
152
                sellastPoint = new Point2D.Double(x, y);
153

    
154
                double x1;
155
                double y1;
156
                double w1;
157
                double h1;
158

    
159
                if (selfirstPoint.getX() < sellastPoint.getX()) {
160
                    x1 = selfirstPoint.getX();
161
                    w1 = sellastPoint.getX() - selfirstPoint.getX();
162
                } else {
163
                    x1 = sellastPoint.getX();
164
                    w1 = selfirstPoint.getX() - sellastPoint.getX();
165
                }
166

    
167
                if (selfirstPoint.getY() < sellastPoint.getY()) {
168
                    y1 = selfirstPoint.getY();
169
                    h1 = sellastPoint.getY() - selfirstPoint.getY();
170
                } else {
171
                    y1 = sellastPoint.getY();
172
                    h1 = selfirstPoint.getY() - sellastPoint.getY();
173
                }
174

    
175
                rect = new Rectangle2D.Double(x1, y1, w1, h1);
176
            } else
177
                if (status.equals("Stretch.MoveFirstPoint")) {
178
                    movefirstPoint = new Point2D.Double(x, y);
179
                } else
180
                    if (status.equals("Stretch.MoveLastPoint")) {
181
                        VectorialLayerEdited vle = getVLE();
182
                        FeatureStore featureStore = null;
183
                        DisposableIterator iterator = null;
184
                        try {
185
                            featureStore = vle.getFeatureStore();
186

    
187
                            // VectorialEditableAdapter vea=vle.getVEA();
188
                            featureStore.beginEditingGroup(getName());
189
                            // ArrayList selectedRow=getSelectedRows();
190
                            // ArrayList selectedRowAux = new ArrayList();
191
                            // PluginServices.getMDIManager().setWaitCursor();
192
                            movelastPoint = new Point2D.Double(x, y);
193

    
194
                            Handler[] handlers = null;
195

    
196
                            // for (int i = selectedGeometries.nextSetBit(0); i
197
                            // >= 0;
198
                            // i = selectedGeometries.nextSetBit(i + 1)) {
199
                            iterator =
200
                                ((FeatureSelection) featureStore.getSelection())
201
                                    .iterator();
202
                            while (iterator.hasNext()) {
203
                                Feature feature = (Feature) iterator.next();
204

    
205
                                // }
206
                                // for (int i =0;i<selectedRow.size(); i++) {
207
                                // IRowEdited edRow = (IRowEdited)
208
                                // selectedRow.get(i);
209
                                // DefaultFeature fea = (DefaultFeature)
210
                                // edRow.getLinkedRow().cloneRow();
211
                                Geometry geometry = null;
212
                                geometry =
213
                                    (feature.getDefaultGeometry())
214
                                        .cloneGeometry();
215

    
216
                                handlers =
217
                                    geometry
218
                                        .getHandlers(Geometry.STRETCHINGHANDLER);
219

    
220
                                for (int j = 0; j < handlers.length; j++) {
221
                                    if (rect.contains(handlers[j].getPoint())) {
222
                                        handlers[j].move(movelastPoint.getX()
223
                                            - movefirstPoint.getX(),
224
                                            movelastPoint.getY()
225
                                                - movefirstPoint.getY());
226
                                    }
227
                                }
228
                                super.updateGeometry(featureStore, feature, geometry);
229
//                                EditableFeature eFeature =
230
//                                    feature.getEditable();
231
//                                eFeature.setGeometry(featureStore
232
//                                    .getDefaultFeatureType()
233
//                                    .getDefaultGeometryAttributeName(),
234
//                                    geometry);
235
//                                // vea.modifyRow(edRow.getIndex(),fea,getName(),EditionEvent.GRAPHIC);
236
//                                featureStore.update(eFeature);
237
//                                // selectedRowAux.add(feature);
238
                            }
239
                            featureStore.endEditingGroup();
240
                            // vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS,
241
                            // selectedRowAux);
242

    
243
                            // PluginServices.getMDIManager().restoreCursor();
244
                        } catch (DataException e) {
245
                            NotificationManager.addError(e.getMessage(), e);
246
                        } finally {
247
                            if (iterator != null) {
248
                                iterator.dispose();
249
                            }
250
                        }
251
                    }
252
    }
253

    
254
    /**
255
     * M?todo para dibujar la lo necesario para el estado en el que nos
256
     * encontremos.
257
     * 
258
     * @param g
259
     *            Graphics sobre el que dibujar.
260
     * @param x
261
     *            par?metro x del punto que se pase para dibujar.
262
     * @param y
263
     *            par?metro x del punto que se pase para dibujar.
264
     */
265
    public void drawOperation(MapControlDrawer renderer, double x, double y) {
266
        StretchCADToolState actualState = (_fsm).getState();
267
        String status = actualState.getName();
268

    
269
        // ArrayList selectedRow = getSelectedRows();
270
        DisposableIterator iterator = null;
271
        try {
272
            iterator =
273
                ((FeatureSelection) getVLE().getFeatureStore().getSelection())
274
                    .iterator();
275
            if (status.equals("Stretch.SelLastPoint")) {
276
                GeneralPathX elShape =
277
                    new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 4);
278
                elShape.moveTo(selfirstPoint.getX(), selfirstPoint.getY());
279
                elShape.lineTo(x, selfirstPoint.getY());
280
                elShape.lineTo(x, y);
281
                elShape.lineTo(selfirstPoint.getX(), y);
282
                elShape.lineTo(selfirstPoint.getX(), selfirstPoint.getY());
283

    
284
                renderer.draw(createCurve(elShape),
285
                    mapControlManager.getAxisReferenceSymbol());
286
            } else
287
                if (status.equals("Stretch.MoveFirstPoint")) {
288

    
289
                    Handler[] handlers = null;
290
                    while (iterator.hasNext()) {
291
                        Feature feature = (Feature) iterator.next();
292

    
293
                        Geometry geometry = null;
294
                        geometry =
295
                            (feature.getDefaultGeometry()).cloneGeometry();
296

    
297
                        handlers =
298
                            geometry.getHandlers(Geometry.STRETCHINGHANDLER);
299

    
300
                        for (int j = 0; j < handlers.length; j++) {
301
                            if (rect.contains(handlers[j].getPoint())) {
302
                                renderer.drawHandlers(handlers,
303
                                    getCadToolAdapter().getMapControl()
304
                                        .getViewPort().getAffineTransform(),
305
                                    mapControlManager.getHandlerSymbol());
306
                            }
307
                        }
308
                    }
309
                } else
310
                    if (status.equals("Stretch.MoveLastPoint")) {
311
                        Handler[] handlers = null;
312
                        while (iterator.hasNext()) {
313
                            Feature feature = (Feature) iterator.next();
314
                            // for (int i = 0;i<selectedRow.size();i++) {
315
                            // IRowEdited edRow = (IRowEdited)
316
                            // selectedRow.get(i);
317
                            // DefaultFeature fea = (DefaultFeature)
318
                            // edRow.getLinkedRow().cloneRow();
319
                            Geometry geometry = null;
320
                            geometry =
321
                                (feature.getDefaultGeometry()).cloneGeometry();
322

    
323
                            handlers =
324
                                geometry
325
                                    .getHandlers(Geometry.STRETCHINGHANDLER);
326

    
327
                            for (int j = 0; j < handlers.length; j++) {
328
                                if (rect.contains(handlers[j].getPoint())) {
329
                                    handlers[j].move(x - movefirstPoint.getX(),
330
                                        y - movefirstPoint.getY());
331
                                }
332
                            }
333
                            renderer.draw(geometry,
334
                                mapControlManager.getAxisReferenceSymbol());
335
                        }
336
                    }
337
        } catch (DataException e) {
338
            // TODO Auto-generated catch block
339
            e.printStackTrace();
340
        } finally {
341
            if (iterator != null) {
342
                iterator.dispose();
343
            }
344
        }
345

    
346
    }
347

    
348
    /**
349
     * Add a diferent option.
350
     * 
351
     * @param s
352
     *            Diferent option.
353
     */
354
    public void addOption(String s) {
355
    }
356

    
357
    /*
358
     * (non-Javadoc)
359
     * 
360
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
361
     */
362
    public void addValue(double d) {
363
    }
364

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

    
369
    public String toString() {
370
        return "_stretch";
371
    }
372

    
373
    @Override
374
    protected int[] getSupportedGeometryTypes() {
375
        return new int[] { CURVE, SURFACE, AGGREGATE, MULTICURVE,
376
            MULTISURFACE, CIRCLE, ARC, ELLIPSE, SPLINE, ELLIPTICARC };
377
    }
378
}