Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / MoveCADTool.java @ 23535

History | View | Annotate | Download (10.9 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 com.iver.cit.gvsig.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.util.ArrayList;
49
import java.util.Iterator;
50

    
51
import org.cresques.cts.ICoordTrans;
52
import org.gvsig.fmap.data.DataException;
53
import org.gvsig.fmap.data.ReadException;
54
import org.gvsig.fmap.data.feature.Feature;
55
import org.gvsig.fmap.data.feature.FeatureCollection;
56
import org.gvsig.fmap.data.feature.FeatureStore;
57
import org.gvsig.fmap.data.feature.IsNotFeatureSettingException;
58
import org.gvsig.fmap.geom.Geometry;
59
import org.gvsig.fmap.geom.operation.Draw;
60
import org.gvsig.fmap.geom.operation.DrawOperationContext;
61
import org.gvsig.fmap.geom.operation.GeometryOperationException;
62
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
63
import org.gvsig.fmap.geom.util.UtilFunctions;
64
import org.gvsig.fmap.mapcontext.ViewPort;
65

    
66
import com.iver.andami.PluginServices;
67
import com.iver.andami.messages.NotificationManager;
68
import com.iver.cit.gvsig.CADExtension;
69
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
70
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
71
import com.iver.cit.gvsig.gui.cad.tools.smc.MoveCADToolContext;
72
import com.iver.cit.gvsig.gui.cad.tools.smc.MoveCADToolContext.MoveCADToolState;
73
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
74

    
75

    
76
/**
77
 * DOCUMENT ME!
78
 *
79
 * @author Vicente Caballero Navarro
80
 */
81
public class MoveCADTool extends DefaultCADTool {
82
    protected MoveCADToolContext _fsm;
83
    protected Point2D firstPoint;
84
    protected Point2D lastPoint;
85

    
86
    /**
87
     * Crea un nuevo PolylineCADTool.
88
     */
89
    public MoveCADTool() {
90
    }
91

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

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

    
107
    /* (non-Javadoc)
108
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
109
     */
110
    public void transition(double d) {
111
        _fsm.addValue(d);
112
    }
113

    
114
    /* (non-Javadoc)
115
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
116
     */
117
    public void transition(String s) throws CommandException {
118
            if (!super.changeCommand(s)){
119
                    _fsm.addOption(s);
120
            }
121
    }
122

    
123
    /**
124
     * DOCUMENT ME!
125
     */
126
    public void selection() {
127
            FeatureCollection selection=null;
128
            try {
129
                    selection = (FeatureCollection)getVLE().getFeatureStore().getSelection();
130
            } catch (ReadException e) {
131
                    // TODO Auto-generated catch block
132
                    e.printStackTrace();
133
            }
134
        if (selection.size() == 0 && !CADExtension.getCADTool().getClass().getName().equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
135
            CADExtension.setCADTool("_selection",false);
136
            ((SelectionCADTool) CADExtension.getCADTool()).setNextTool(
137
                "_move");
138
        }
139
    }
140

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

    
169
            try {
170
                    Iterator iterator=selection.iterator();
171
                    while (iterator.hasNext()) {
172
                                        Feature feature = (Feature) iterator.next();
173

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

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

    
193
//                  if (ct!=null)
194
//                        ig.reProject(ct.getInverted());
195
                    feature.setGeometry(featureStore.getDefaultFeatureType().getDefaultGeometry(),ig);
196
                    featureStore.update(feature);
197
//                    vea.modifyRow(edRow.getIndex(),feat,getName(),EditionEvent.GRAPHIC);
198
//                    selectedRowAux.add(new DefaultRowEdited(feat,IRowEdited.STATUS_MODIFIED,edRow.getIndex()));
199
              }
200
                featureStore.getCommandsRecord().endComplex(getName());
201
//                vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS, selectedRowAux);
202
                //clearSelection();
203
                      //selectedRow.addAll(selectedRowAux);
204

    
205
            } catch (ReadException e) {
206
                                NotificationManager.addError(e.getMessage(),e);
207
                        } catch (IsNotFeatureSettingException e) {
208
                                NotificationManager.addError(e.getMessage(),e);
209
                        } catch (DataException e) {
210
                                NotificationManager.addError(e.getMessage(),e);
211
                        }
212
            PluginServices.getMDIManager().restoreCursor();
213
        }
214
    }
215

    
216
    /**
217
     * M?todo para dibujar lo necesario para el estado en el que nos
218
     * encontremos.
219
     *
220
     * @param g Graphics sobre el que dibujar.
221
     * @param x par?metro x del punto que se pase para dibujar.
222
     * @param y par?metro x del punto que se pase para dibujar.
223
     */
224
    public void drawOperation(Graphics g, double x, double y) {
225
        MoveCADToolState actualState = ((MoveCADToolContext) _fsm).getState();
226
        String status = actualState.getName();
227
        VectorialLayerEdited vle=getVLE();
228
        //ArrayList selectedRow=getSelectedRows();
229
                /*drawHandlers(g, selectedRow,
230
                     getCadToolAdapter().getMapControl().getViewPort()
231
                         .getAffineTransform());
232
        */
233
        if (status.equals("Move.SecondPointToMove")) {
234
                ViewPort vp=vle.getLayer().getMapContext().getViewPort();
235
            int dx = vp.fromMapDistance(x - firstPoint.getX());
236
            int dy = -vp.fromMapDistance(y - firstPoint.getY());
237
            Image img = vle.getSelectionImage();
238
            g.drawImage(img, dx, dy, null);
239
//                try {
240
//                                Iterator iterator=vle.getFeatureStore().getSelection().iterator();
241
//                                while (iterator.hasNext()) {
242
//                                        Feature feature = (Feature) iterator.next();
243
//                                        Geometry ig = ((Geometry)feature.getDefaultGeometry()).cloneGeometry();
244
//                                    if (ig == null)
245
//                                            continue;
246
//                                    // Movemos la geometr?a
247
//                        UtilFunctions.moveGeom(ig, x - firstPoint.getX(), y - firstPoint.getY());
248
//
249
//                        DrawOperationContext doc=new DrawOperationContext();
250
//                                    doc.setGraphics((Graphics2D)g);
251
//                                    doc.setViewPort(vp);
252
//                                    doc.setSymbol(DefaultCADTool.geometrySelectSymbol);
253
//                            try {
254
//                                                ig.invokeOperation(Draw.CODE,doc);
255
//                                        } catch (GeometryOperationNotSupportedException e) {
256
//                                                e.printStackTrace();
257
//                                        } catch (GeometryOperationException e) {
258
//                                                e.printStackTrace();
259
//                                        }
260
//                    }
261
//                        } catch (ReadException e) {
262
//                                // TODO Auto-generated catch block
263
//                                e.printStackTrace();
264
//                        }
265
            /*for (int i = 0; i < selectedRow.size(); i++) {
266
                            IRowEdited edRow = (IRowEdited) selectedRow.get(i);
267
                            IFeature feat = (IFeature) edRow.getLinkedRow();
268
                            IGeometry ig = feat.getGeometry().cloneGeometry();
269
                            if (ig == null)
270
                                    continue;
271
                            // Movemos la geometr?a
272
                UtilFunctions.moveGeom(ig, x - firstPoint.getX(), y - firstPoint.getY());
273
                ig.draw((Graphics2D) g,
274
                    getCadToolAdapter().getMapControl().getViewPort(),
275
                    CADTool.drawingSymbol);
276
                    }*/
277
        }else{
278
                if (!vle.getLayer().isVisible())
279
                                return;
280
                 Image imgSel = vle.getSelectionImage();
281
             g.drawImage(imgSel, 0, 0, null);
282
             Image imgHand = vle.getHandlersImage();
283
             g.drawImage(imgHand, 0, 0, null);
284
        }
285
    }
286

    
287
    /**
288
     * Add a diferent option.
289
     *
290
     * @param s Diferent option.
291
     */
292
    public void addOption(String s) {
293
    }
294

    
295
    /* (non-Javadoc)
296
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
297
     */
298
    public void addValue(double d) {
299
    }
300

    
301
        public String getName() {
302
                return PluginServices.getText(this,"move_");
303
        }
304

    
305
        public String toString() {
306
                return "_move";
307
        }
308

    
309
}