Statistics
| Revision:

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

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
                                // TODO Auto-generated catch block
209
                                e.printStackTrace();
210
                        } catch (DataException e) {
211
                                // TODO Auto-generated catch block
212
                                e.printStackTrace();
213
                        }
214
            PluginServices.getMDIManager().restoreCursor();
215
        }
216
    }
217

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

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

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

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

    
307
        public String toString() {
308
                return "_move";
309
        }
310

    
311
}