Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2059 / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / MoveCADTool.java @ 39296

History | View | Annotate | Download (9.66 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22
package org.gvsig.editing.gui.cad.tools;
23

    
24
import java.awt.event.InputEvent;
25
import java.awt.geom.Point2D;
26

    
27
import org.cresques.cts.ICoordTrans;
28

    
29
import org.gvsig.andami.PluginServices;
30
import org.gvsig.andami.messages.NotificationManager;
31
import org.gvsig.editing.CADExtension;
32
import org.gvsig.editing.gui.cad.DefaultCADTool;
33
import org.gvsig.editing.gui.cad.exception.CommandException;
34
import org.gvsig.editing.gui.cad.tools.smc.MoveCADToolContext;
35
import org.gvsig.editing.gui.cad.tools.smc.MoveCADToolContext.MoveCADToolState;
36
import org.gvsig.editing.layers.VectorialLayerEdited;
37
import org.gvsig.fmap.dal.exception.DataException;
38
import org.gvsig.fmap.dal.exception.ReadException;
39
import org.gvsig.fmap.dal.feature.EditableFeature;
40
import org.gvsig.fmap.dal.feature.Feature;
41
import org.gvsig.fmap.dal.feature.FeatureSet;
42
import org.gvsig.fmap.dal.feature.FeatureStore;
43
import org.gvsig.fmap.geom.Geometry;
44
import org.gvsig.fmap.geom.type.GeometryType;
45
import org.gvsig.fmap.geom.util.UtilFunctions;
46
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
47
import org.gvsig.tools.dispose.DisposableIterator;
48

    
49
/**
50
 * DOCUMENT ME!
51
 * 
52
 * @author Vicente Caballero Navarro
53
 */
54
public class MoveCADTool extends DefaultCADTool {
55

    
56
    protected MoveCADToolContext _fsm;
57
    protected Point2D firstPoint;
58
    protected Point2D lastPoint;
59

    
60
    /**
61
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
62
     * carga previa a la utilizaci?n de la herramienta.
63
     */
64
    public void init() {
65
        _fsm = new MoveCADToolContext(this);
66
    }
67

    
68
    /*
69
     * (non-Javadoc)
70
     * 
71
     * @see
72
     * com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
73
     * .layers.FBitSet, double, double)
74
     */
75
    public void transition(double x, double y, InputEvent event) {
76
        _fsm.addPoint(x, y, event);
77
    }
78

    
79
    /*
80
     * (non-Javadoc)
81
     * 
82
     * @see
83
     * com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
84
     * .layers.FBitSet, double)
85
     */
86
    public void transition(double d) {
87
        _fsm.addValue(d);
88
    }
89

    
90
    /*
91
     * (non-Javadoc)
92
     * 
93
     * @see
94
     * com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
95
     * .layers.FBitSet, java.lang.String)
96
     */
97
    public void transition(String s) throws CommandException {
98
        if (!super.changeCommand(s)) {
99
            _fsm.addOption(s);
100
        }
101
    }
102

    
103
    /**
104
     * DOCUMENT ME!
105
     */
106
    public void selection() {
107
        FeatureSet selection = null;
108
        try {
109
            selection = (FeatureSet) getVLE().getFeatureStore().getSelection();
110

    
111
            if (selection.getSize() == 0
112
                && !CADExtension
113
                    .getCADTool()
114
                    .getClass()
115
                    .getName()
116
                    .equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
117
                CADExtension.setCADTool("_selection", false);
118
                ((SelectionCADTool) CADExtension.getCADTool())
119
                    .setNextTool("_move");
120
            }
121
        } catch (ReadException e) {
122
            // TODO Auto-generated catch block
123
            e.printStackTrace();
124
        } catch (DataException e) {
125
            // TODO Auto-generated catch block
126
            e.printStackTrace();
127
        }
128
    }
129

    
130
    /**
131
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
132
     * editableFeatureSource que ya estar? creado.
133
     * 
134
     * @param x
135
     *            par?metro x del punto que se pase en esta transici?n.
136
     * @param y
137
     *            par?metro y del punto que se pase en esta transici?n.
138
     */
139
    public void addPoint(double x, double y, InputEvent event) {
140
        MoveCADToolState actualState =
141
            (MoveCADToolState) _fsm.getPreviousState();
142
        String status = actualState.getName();
143
        VectorialLayerEdited vle = getVLE();
144
        FeatureStore featureStore = null;
145
        try {
146
            featureStore = vle.getFeatureStore();
147
        } catch (ReadException e1) {
148
            // TODO Auto-generated catch block
149
            e1.printStackTrace();
150
        }
151
        DisposableIterator iterator = null;
152
        try {
153
            FeatureSet selection = (FeatureSet) featureStore.getSelection();// getSelectedRows();
154
            // ArrayList selectedRowAux=new ArrayList();
155
            ICoordTrans ct = getVLE().getLayer().getCoordTrans();
156
            if (status.equals("Move.FirstPointToMove")) {
157
                firstPoint = new Point2D.Double(x, y);
158
            } else
159
                if (status.equals("Move.SecondPointToMove")) {
160
                    PluginServices.getMDIManager().setWaitCursor();
161
                    lastPoint = new Point2D.Double(x, y);
162

    
163
                    featureStore.beginEditingGroup(getName());
164
                    try {
165

    
166
                        iterator = selection.iterator();
167
                        while (iterator.hasNext()) {
168
                            Feature feature = (Feature) iterator.next();
169

    
170
                            // }
171
                            // for (int i = 0; i < selection.size(); i++) {
172
                            // IRowEdited edRow = (IRowEdited)
173
                            // selectedRow.get(i);
174
                            // IFeature feat = (IFeature)
175
                            // edRow.getLinkedRow().cloneRow();
176
                            Geometry ig =
177
                                (feature.getDefaultGeometry()).cloneGeometry();
178
                            EditableFeature eFeature = feature.getEditable();
179
                            if (ig == null) {
180
                                continue;
181
                            }
182
                            if (ct != null) {
183
                                lastPoint =
184
                                    ct.getInverted().convert(lastPoint, null);
185
                                firstPoint =
186
                                    ct.getInverted().convert(firstPoint, null);
187

    
188
                            }
189
                            // if (ct!=null)
190
                            // ig.reProject(ct);
191
                            // Movemos la geometr?a
192
                            ig.move(lastPoint.getX() - firstPoint.getX(),
193
                                lastPoint.getY() - firstPoint.getY());
194

    
195
                            // if (ct!=null)
196
                            // ig.reProject(ct.getInverted());
197
                            eFeature.setGeometry(featureStore
198
                                .getDefaultFeatureType()
199
                                .getDefaultGeometryAttributeName(), ig);
200
                            featureStore.update(eFeature);
201
                            // vea.modifyRow(edRow.getIndex(),feat,getName(),EditionEvent.GRAPHIC);
202
                            // selectedRowAux.add(new
203
                            // DefaultRowEdited(feat,IRowEdited.STATUS_MODIFIED,edRow.getIndex()));
204
                        }
205
                    } finally {
206
                        featureStore.endEditingGroup();
207
                    }
208
                    // vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS,
209
                    // selectedRowAux);
210
                    // clearSelection();
211
                    // selectedRow.addAll(selectedRowAux);
212
                }
213
        } catch (ReadException e) {
214
            NotificationManager.addError(e.getMessage(), e);
215
        } catch (DataException e) {
216
            NotificationManager.addError(e.getMessage(), e);
217
        } finally {
218
            if (iterator != null) {
219
                iterator.dispose();
220
            }
221
        }
222
        PluginServices.getMDIManager().restoreCursor();
223
    }
224

    
225
    /**
226
     * M?todo para dibujar lo necesario para el estado en el que nos
227
     * encontremos.
228
     * 
229
     * @param g
230
     *            Graphics sobre el que dibujar.
231
     * @param x
232
     *            par?metro x del punto que se pase para dibujar.
233
     * @param y
234
     *            par?metro x del punto que se pase para dibujar.
235
     */
236
    public void drawOperation(MapControlDrawer renderer, double x, double y) {
237
        MoveCADToolState actualState = (_fsm).getState();
238
        String status = actualState.getName();
239
        VectorialLayerEdited vle = getVLE();
240

    
241
        if (status.equals("Move.SecondPointToMove")) {
242
            drawSelectedGeometries(renderer, firstPoint, x, y);
243
        }
244
    }
245

    
246
    /**
247
     * Add a diferent option.
248
     * 
249
     * @param s
250
     *            Diferent option.
251
     */
252
    public void addOption(String s) {
253
    }
254

    
255
    /*
256
     * (non-Javadoc)
257
     * 
258
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
259
     */
260
    public void addValue(double d) {
261
    }
262

    
263
    public String getName() {
264
        return PluginServices.getText(this, "move_");
265
    }
266

    
267
    public String toString() {
268
        return "_move";
269
    }
270

    
271
    @Override
272
    public boolean isApplicable(GeometryType geometryType) {
273
        return true;
274
    }
275

    
276
    @Override
277
    protected int[] getSupportedGeometryTypes() {
278
        return null;
279
    }
280

    
281
}