Statistics
| Revision:

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

History | View | Annotate | Download (14.6 KB)

1 37138 cordinyana
/* gvSIG. Geographic Information System of the Valencian Government
2 6044 caballero
 *
3 37138 cordinyana
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6 6044 caballero
 * 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 2
9
 * of the License, or (at your option) any later version.
10 37138 cordinyana
 *
11 6044 caballero
 * 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 37138 cordinyana
 *
16 6044 caballero
 * 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 37138 cordinyana
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21 6044 caballero
 */
22 29616 jpiera
package org.gvsig.editing.gui.cad.tools;
23 6044 caballero
24
import java.awt.event.InputEvent;
25
import java.awt.geom.Point2D;
26
import java.awt.geom.Rectangle2D;
27
28 29616 jpiera
import org.gvsig.andami.PluginServices;
29
import org.gvsig.andami.messages.NotificationManager;
30
import org.gvsig.editing.CADExtension;
31
import org.gvsig.editing.gui.cad.DefaultCADTool;
32
import org.gvsig.editing.gui.cad.exception.CommandException;
33
import org.gvsig.editing.gui.cad.tools.smc.StretchCADToolContext;
34
import org.gvsig.editing.gui.cad.tools.smc.StretchCADToolContext.StretchCADToolState;
35
import org.gvsig.editing.layers.VectorialLayerEdited;
36 24500 jmvivo
import org.gvsig.fmap.dal.exception.DataException;
37
import org.gvsig.fmap.dal.exception.ReadException;
38 24490 jmvivo
import org.gvsig.fmap.dal.feature.EditableFeature;
39
import org.gvsig.fmap.dal.feature.Feature;
40
import org.gvsig.fmap.dal.feature.FeatureSelection;
41
import org.gvsig.fmap.dal.feature.FeatureSet;
42
import org.gvsig.fmap.dal.feature.FeatureStore;
43 21668 vcaballero
import org.gvsig.fmap.geom.Geometry;
44
import org.gvsig.fmap.geom.handler.Handler;
45
import org.gvsig.fmap.geom.primitive.GeneralPathX;
46 30335 jpiera
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
47 37138 cordinyana
import org.gvsig.tools.dispose.DisposableIterator;
48 21668 vcaballero
49 6044 caballero
/**
50
 * Herramienta para estirar los handlers que seleccionemos previamente.
51 37138 cordinyana
 *
52 6044 caballero
 * @author Vicente Caballero Navarro
53
 */
54
public class StretchCADTool extends DefaultCADTool {
55
56 37138 cordinyana
    protected StretchCADToolContext _fsm;
57
    protected Point2D selfirstPoint;
58
    protected Point2D sellastPoint;
59
    protected Point2D movefirstPoint;
60
    protected Point2D movelastPoint;
61
    protected Rectangle2D rect = null;
62 6044 caballero
63 37138 cordinyana
    /**
64
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
65
     * carga previa a la utilizaci?n de la herramienta.
66
     */
67
    public void init() {
68
        _fsm = new StretchCADToolContext(this);
69
    }
70 6044 caballero
71 37138 cordinyana
    /*
72
     * (non-Javadoc)
73
     *
74
     * @see
75
     * com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
76
     * .layers.FBitSet,
77
     * double, double)
78
     */
79
    public void transition(double x, double y, InputEvent event) {
80
        _fsm.addPoint(x, y, event);
81
    }
82 6044 caballero
83 37138 cordinyana
    /*
84
     * (non-Javadoc)
85
     *
86
     * @see
87
     * com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
88
     * .layers.FBitSet,
89
     * double)
90
     */
91
    public void transition(double d) {
92
        _fsm.addValue(d);
93
    }
94 6044 caballero
95 37138 cordinyana
    /*
96
     * (non-Javadoc)
97
     *
98
     * @see
99
     * com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
100
     * .layers.FBitSet,
101
     * java.lang.String)
102
     */
103
    public void transition(String s) throws CommandException {
104
        if (!super.changeCommand(s)) {
105
            _fsm.addOption(s);
106
        }
107
    }
108 24263 vcaballero
109 37138 cordinyana
    /**
110
     * DOCUMENT ME!
111
     */
112
    public void selection() {
113
        FeatureSet selection = null;
114
        try {
115
            selection = (FeatureSet) getVLE().getFeatureStore().getSelection();
116 6044 caballero
117 37138 cordinyana
            if (selection.getSize() == 0
118
                && !CADExtension
119
                    .getCADTool()
120
                    .getClass()
121
                    .getName()
122
                    .equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
123
                CADExtension.setCADTool("_selection", false);
124
                ((SelectionCADTool) CADExtension.getCADTool())
125
                    .setNextTool("_stretch");
126
            }
127
        } catch (ReadException e) {
128
            // TODO Auto-generated catch block
129
            e.printStackTrace();
130
        } catch (DataException e) {
131
            // TODO Auto-generated catch block
132
            e.printStackTrace();
133
        }
134
    }
135 6044 caballero
136 37138 cordinyana
    /**
137
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
138
     * editableFeatureSource que ya estar? creado.
139
     *
140
     * @param x
141
     *            par?metro x del punto que se pase en esta transici?n.
142
     * @param y
143
     *            par?metro y del punto que se pase en esta transici?n.
144
     */
145
    public void addPoint(double x, double y, InputEvent event) {
146
        StretchCADToolState actualState =
147
            (StretchCADToolState) _fsm.getPreviousState();
148
        String status = actualState.getName();
149 6044 caballero
150 37138 cordinyana
        if (status.equals("Stretch.SelFirstPoint")) {
151
            selfirstPoint = new Point2D.Double(x, y);
152
        } else
153
            if (status.equals("Stretch.SelLastPoint")) {
154
                sellastPoint = new Point2D.Double(x, y);
155 6044 caballero
156 37138 cordinyana
                double x1;
157
                double y1;
158
                double w1;
159
                double h1;
160 6044 caballero
161 37138 cordinyana
                if (selfirstPoint.getX() < sellastPoint.getX()) {
162
                    x1 = selfirstPoint.getX();
163
                    w1 = sellastPoint.getX() - selfirstPoint.getX();
164
                } else {
165
                    x1 = sellastPoint.getX();
166
                    w1 = selfirstPoint.getX() - sellastPoint.getX();
167
                }
168 6044 caballero
169 37138 cordinyana
                if (selfirstPoint.getY() < sellastPoint.getY()) {
170
                    y1 = selfirstPoint.getY();
171
                    h1 = sellastPoint.getY() - selfirstPoint.getY();
172
                } else {
173
                    y1 = sellastPoint.getY();
174
                    h1 = selfirstPoint.getY() - sellastPoint.getY();
175
                }
176 6044 caballero
177 37138 cordinyana
                rect = new Rectangle2D.Double(x1, y1, w1, h1);
178
            } else
179
                if (status.equals("Stretch.MoveFirstPoint")) {
180
                    movefirstPoint = new Point2D.Double(x, y);
181
                } else
182
                    if (status.equals("Stretch.MoveLastPoint")) {
183
                        VectorialLayerEdited vle = getVLE();
184
                        FeatureStore featureStore = null;
185
                        DisposableIterator iterator = null;
186
                        try {
187
                            featureStore = vle.getFeatureStore();
188 6044 caballero
189 37138 cordinyana
                            // VectorialEditableAdapter vea=vle.getVEA();
190
                            featureStore.beginEditingGroup(getName());
191
                            // ArrayList selectedRow=getSelectedRows();
192
                            // ArrayList selectedRowAux = new ArrayList();
193
                            // PluginServices.getMDIManager().setWaitCursor();
194
                            movelastPoint = new Point2D.Double(x, y);
195 24429 vcaballero
196 37138 cordinyana
                            Handler[] handlers = null;
197 23424 vcaballero
198 37138 cordinyana
                            // for (int i = selectedGeometries.nextSetBit(0); i
199
                            // >= 0;
200
                            // i = selectedGeometries.nextSetBit(i + 1)) {
201
                            iterator =
202
                                ((FeatureSelection) featureStore.getSelection())
203
                                    .iterator();
204
                            while (iterator.hasNext()) {
205
                                Feature feature = (Feature) iterator.next();
206 6044 caballero
207 37138 cordinyana
                                // }
208
                                // for (int i =0;i<selectedRow.size(); i++) {
209
                                // IRowEdited edRow = (IRowEdited)
210
                                // selectedRow.get(i);
211
                                // DefaultFeature fea = (DefaultFeature)
212
                                // edRow.getLinkedRow().cloneRow();
213
                                Geometry geometry = null;
214
                                geometry =
215
                                    (feature.getDefaultGeometry())
216
                                        .cloneGeometry();
217 6044 caballero
218 37138 cordinyana
                                handlers =
219
                                    geometry
220
                                        .getHandlers(Geometry.STRETCHINGHANDLER);
221 6044 caballero
222 37138 cordinyana
                                for (int j = 0; j < handlers.length; j++) {
223
                                    if (rect.contains(handlers[j].getPoint())) {
224
                                        handlers[j].move(movelastPoint.getX()
225
                                            - movefirstPoint.getX(),
226
                                            movelastPoint.getY()
227
                                                - movefirstPoint.getY());
228
                                    }
229
                                }
230
                                EditableFeature eFeature =
231
                                    feature.getEditable();
232
                                eFeature.setGeometry(featureStore
233
                                    .getDefaultFeatureType()
234
                                    .getDefaultGeometryAttributeName(),
235
                                    geometry);
236
                                // vea.modifyRow(edRow.getIndex(),fea,getName(),EditionEvent.GRAPHIC);
237
                                featureStore.update(eFeature);
238
                                // selectedRowAux.add(feature);
239
                            }
240
                            featureStore.endEditingGroup();
241
                            // vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS,
242
                            // selectedRowAux);
243 6044 caballero
244 37138 cordinyana
                            // PluginServices.getMDIManager().restoreCursor();
245
                        } catch (DataException e) {
246
                            NotificationManager.addError(e.getMessage(), e);
247
                        } finally {
248
                            if (iterator != null) {
249
                                iterator.dispose();
250
                            }
251
                        }
252
                    }
253
    }
254 6044 caballero
255 37138 cordinyana
    /**
256
     * M?todo para dibujar la lo necesario para el estado en el que nos
257
     * encontremos.
258
     *
259
     * @param g
260
     *            Graphics sobre el que dibujar.
261
     * @param x
262
     *            par?metro x del punto que se pase para dibujar.
263
     * @param y
264
     *            par?metro x del punto que se pase para dibujar.
265
     */
266
    public void drawOperation(MapControlDrawer renderer, double x, double y) {
267
        StretchCADToolState actualState = (_fsm).getState();
268
        String status = actualState.getName();
269 23424 vcaballero
270 37138 cordinyana
        // ArrayList selectedRow = getSelectedRows();
271
        DisposableIterator iterator = null;
272
        try {
273
            iterator =
274
                ((FeatureSelection) getVLE().getFeatureStore().getSelection())
275
                    .iterator();
276
            if (status.equals("Stretch.SelLastPoint")) {
277
                GeneralPathX elShape =
278
                    new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, 4);
279
                elShape.moveTo(selfirstPoint.getX(), selfirstPoint.getY());
280
                elShape.lineTo(x, selfirstPoint.getY());
281
                elShape.lineTo(x, y);
282
                elShape.lineTo(selfirstPoint.getX(), y);
283
                elShape.lineTo(selfirstPoint.getX(), selfirstPoint.getY());
284 27525 jmvivo
285 37138 cordinyana
                renderer.draw(createCurve(elShape),
286
                    mapControlManager.getAxisReferenceSymbol());
287
            } else
288
                if (status.equals("Stretch.MoveFirstPoint")) {
289 27525 jmvivo
290 37138 cordinyana
                    Handler[] handlers = null;
291
                    while (iterator.hasNext()) {
292
                        Feature feature = (Feature) iterator.next();
293 27525 jmvivo
294 37138 cordinyana
                        Geometry geometry = null;
295
                        geometry =
296
                            (feature.getDefaultGeometry()).cloneGeometry();
297 27525 jmvivo
298 37138 cordinyana
                        handlers =
299
                            geometry.getHandlers(Geometry.STRETCHINGHANDLER);
300 27525 jmvivo
301 37138 cordinyana
                        for (int j = 0; j < handlers.length; j++) {
302
                            if (rect.contains(handlers[j].getPoint())) {
303
                                renderer.drawHandlers(handlers,
304
                                    getCadToolAdapter().getMapControl()
305
                                        .getViewPort().getAffineTransform(),
306
                                    mapControlManager.getHandlerSymbol());
307
                            }
308
                        }
309
                    }
310
                } else
311
                    if (status.equals("Stretch.MoveLastPoint")) {
312
                        Handler[] handlers = null;
313
                        while (iterator.hasNext()) {
314
                            Feature feature = (Feature) iterator.next();
315
                            // for (int i = 0;i<selectedRow.size();i++) {
316
                            // IRowEdited edRow = (IRowEdited)
317
                            // selectedRow.get(i);
318
                            // DefaultFeature fea = (DefaultFeature)
319
                            // edRow.getLinkedRow().cloneRow();
320
                            Geometry geometry = null;
321
                            geometry =
322
                                (feature.getDefaultGeometry()).cloneGeometry();
323 27525 jmvivo
324 37138 cordinyana
                            handlers =
325
                                geometry
326
                                    .getHandlers(Geometry.STRETCHINGHANDLER);
327 27525 jmvivo
328 37138 cordinyana
                            for (int j = 0; j < handlers.length; j++) {
329
                                if (rect.contains(handlers[j].getPoint())) {
330
                                    handlers[j].move(x - movefirstPoint.getX(),
331
                                        y - movefirstPoint.getY());
332
                                }
333
                            }
334
                            renderer.draw(geometry,
335
                                mapControlManager.getAxisReferenceSymbol());
336
                        }
337
                    }
338
        } catch (DataException e) {
339
            // TODO Auto-generated catch block
340
            e.printStackTrace();
341
        } finally {
342
            if (iterator != null) {
343
                iterator.dispose();
344
            }
345
        }
346 6044 caballero
347 37138 cordinyana
    }
348 6044 caballero
349 37138 cordinyana
    /**
350
     * Add a diferent option.
351
     *
352
     * @param s
353
     *            Diferent option.
354
     */
355
    public void addOption(String s) {
356
    }
357 6044 caballero
358 37138 cordinyana
    /*
359
     * (non-Javadoc)
360
     *
361
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
362
     */
363
    public void addValue(double d) {
364
    }
365 6044 caballero
366 37138 cordinyana
    public String getName() {
367
        return PluginServices.getText(this, "stretch_");
368
    }
369 24429 vcaballero
370 37138 cordinyana
    public String toString() {
371
        return "_stretch";
372
    }
373
374 37328 cordinyana
    @Override
375
    protected int[] getSupportedGeometryTypes() {
376
        return new int[] { CURVE, SURFACE, AGGREGATE, MULTICURVE,
377
            MULTISURFACE, CIRCLE, ARC, ELLIPSE, SPLINE, ELLIPTICARC };
378 37138 cordinyana
    }
379 6044 caballero
}