Statistics
| Revision:

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

History | View | Annotate | Download (9.03 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.Image;
44
import java.awt.event.InputEvent;
45
import java.awt.geom.Point2D;
46

    
47
import org.cresques.cts.ICoordTrans;
48
import org.gvsig.andami.PluginServices;
49
import org.gvsig.andami.messages.NotificationManager;
50
import org.gvsig.editing.CADExtension;
51
import org.gvsig.editing.gui.cad.DefaultCADTool;
52
import org.gvsig.editing.gui.cad.exception.CommandException;
53
import org.gvsig.editing.gui.cad.tools.smc.MoveCADToolContext;
54
import org.gvsig.editing.gui.cad.tools.smc.MoveCADToolContext.MoveCADToolState;
55
import org.gvsig.editing.layers.VectorialLayerEdited;
56
import org.gvsig.fmap.dal.exception.DataException;
57
import org.gvsig.fmap.dal.exception.ReadException;
58
import org.gvsig.fmap.dal.feature.DisposableIterator;
59
import org.gvsig.fmap.dal.feature.EditableFeature;
60
import org.gvsig.fmap.dal.feature.Feature;
61
import org.gvsig.fmap.dal.feature.FeatureSet;
62
import org.gvsig.fmap.dal.feature.FeatureStore;
63
import org.gvsig.fmap.geom.Geometry;
64
import org.gvsig.fmap.geom.util.UtilFunctions;
65
import org.gvsig.fmap.mapcontext.ViewPort;
66
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
67

    
68

    
69

    
70
/**
71
 * DOCUMENT ME!
72
 *
73
 * @author Vicente Caballero Navarro
74
 */
75
public class MoveCADTool extends DefaultCADTool {
76
    protected MoveCADToolContext _fsm;
77
    protected Point2D firstPoint;
78
    protected Point2D lastPoint;
79

    
80
    /**
81
     * Crea un nuevo PolylineCADTool.
82
     */
83
    public MoveCADTool() {
84
    }
85

    
86
    /**
87
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
88
     * carga previa a la utilizaci?n de la herramienta.
89
     */
90
    public void init() {
91
        _fsm = new MoveCADToolContext(this);
92
    }
93

    
94
    /* (non-Javadoc)
95
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
96
     */
97
    public void transition(double x, double y, InputEvent event) {
98
        _fsm.addPoint(x, y, event);
99
    }
100

    
101
    /* (non-Javadoc)
102
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
103
     */
104
    public void transition(double d) {
105
        _fsm.addValue(d);
106
    }
107

    
108
    /* (non-Javadoc)
109
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
110
     */
111
    public void transition(String s) throws CommandException {
112
            if (!super.changeCommand(s)){
113
                    _fsm.addOption(s);
114
            }
115
    }
116

    
117
    /**
118
     * DOCUMENT ME!
119
     */
120
    public void selection() {
121
            FeatureSet selection=null;
122
            try {
123
                    selection = (FeatureSet)getVLE().getFeatureStore().getSelection();
124

    
125
                    if (selection.getSize() == 0 && !CADExtension.getCADTool().getClass().getName().equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
126
                            CADExtension.setCADTool("_selection",false);
127
                            ((SelectionCADTool) CADExtension.getCADTool()).setNextTool(
128
                            "_move");
129
                    }
130
            } catch (ReadException e) {
131
                    // TODO Auto-generated catch block
132
                    e.printStackTrace();
133
            } catch (DataException e) {
134
                    // TODO Auto-generated catch block
135
                    e.printStackTrace();
136
            }
137
    }
138

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

    
169
                                featureStore.beginEditingGroup(getName());
170
                                try {
171

    
172
                                        iterator = selection.iterator();
173
                                        while (iterator.hasNext()) {
174
                                                Feature feature = (Feature) iterator.next();
175

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

    
193
                                                }
194
                                                // if (ct!=null)
195
                                                // ig.reProject(ct);
196
                                                // Movemos la geometr?a
197
                                                UtilFunctions.moveGeom(ig, lastPoint.getX()
198
                                                                - firstPoint.getX(), lastPoint.getY()
199
                                                                - firstPoint.getY());
200

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

    
231
    /**
232
     * M?todo para dibujar lo necesario para el estado en el que nos
233
     * encontremos.
234
     *
235
     * @param g Graphics sobre el que dibujar.
236
     * @param x par?metro x del punto que se pase para dibujar.
237
     * @param y par?metro x del punto que se pase para dibujar.
238
     */
239
    public void drawOperation(MapControlDrawer renderer, double x, double y) {
240
        MoveCADToolState actualState = (_fsm).getState();
241
        String status = actualState.getName();
242
        VectorialLayerEdited vle=getVLE();
243
        //ArrayList selectedRow=getSelectedRows();
244
                /*drawHandlers(g, selectedRow,
245
                     getCadToolAdapter().getMapControl().getViewPort()
246
                         .getAffineTransform());
247
        */
248
        if (status.equals("Move.SecondPointToMove")) {
249
                ViewPort vp=vle.getLayer().getMapContext().getViewPort();
250
            int dx = vp.fromMapDistance(x - firstPoint.getX());
251
            int dy = -vp.fromMapDistance(y - firstPoint.getY());
252
            Image img = vle.getSelectionImage();
253
            renderer.drawImage(img, dx, dy);
254
        }else{
255
                if (!vle.getLayer().isVisible()) {
256
                                return;
257
                        }
258
                try{
259
                        Image imgSel = vle.getSelectionImage();
260
                        renderer.drawImage(imgSel, 0, 0);
261
                                
262
                        Image imgHand = vle.getHandlersImage();
263
                        renderer.drawImage(imgHand, 0, 0);                                
264
                }catch (Exception e) {
265
                }
266
        }
267
    }
268

    
269
    /**
270
     * Add a diferent option.
271
     *
272
     * @param s Diferent option.
273
     */
274
    public void addOption(String s) {
275
    }
276

    
277
    /* (non-Javadoc)
278
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
279
     */
280
    public void addValue(double d) {
281
    }
282

    
283
        public String getName() {
284
                return PluginServices.getText(this,"move_");
285
        }
286

    
287
        public String toString() {
288
                return "_move";
289
        }
290

    
291
}