Statistics
| Revision:

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

History | View | Annotate | Download (10.5 KB)

1 4434 caballero
/* 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 4637 caballero
import java.awt.Image;
45 4434 caballero
import java.awt.event.InputEvent;
46
import java.awt.geom.Point2D;
47 23086 vcaballero
import java.util.Iterator;
48 4434 caballero
49 12739 caballero
import org.cresques.cts.ICoordTrans;
50 24500 jmvivo
import org.gvsig.fmap.dal.exception.DataException;
51
import org.gvsig.fmap.dal.exception.ReadException;
52 24490 jmvivo
import org.gvsig.fmap.dal.feature.EditableFeature;
53
import org.gvsig.fmap.dal.feature.Feature;
54
import org.gvsig.fmap.dal.feature.FeatureSet;
55
import org.gvsig.fmap.dal.feature.FeatureStore;
56 21668 vcaballero
import org.gvsig.fmap.geom.Geometry;
57
import org.gvsig.fmap.geom.util.UtilFunctions;
58
import org.gvsig.fmap.mapcontext.ViewPort;
59 12739 caballero
60 4434 caballero
import com.iver.andami.PluginServices;
61 12739 caballero
import com.iver.andami.messages.NotificationManager;
62 4434 caballero
import com.iver.cit.gvsig.CADExtension;
63
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
64 5735 caballero
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
65 4434 caballero
import com.iver.cit.gvsig.gui.cad.tools.smc.MoveCADToolContext;
66
import com.iver.cit.gvsig.gui.cad.tools.smc.MoveCADToolContext.MoveCADToolState;
67 4637 caballero
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
68 4434 caballero
69
70
/**
71
 * DOCUMENT ME!
72
 *
73
 * @author Vicente Caballero Navarro
74
 */
75
public class MoveCADTool extends DefaultCADTool {
76 20894 vcaballero
    protected MoveCADToolContext _fsm;
77
    protected Point2D firstPoint;
78
    protected Point2D lastPoint;
79 4434 caballero
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 5735 caballero
        _fsm.addValue(d);
106 4434 caballero
    }
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 5735 caballero
    public void transition(String s) throws CommandException {
112 4892 caballero
            if (!super.changeCommand(s)){
113
                    _fsm.addOption(s);
114
            }
115 4434 caballero
    }
116
117
    /**
118
     * DOCUMENT ME!
119
     */
120
    public void selection() {
121 23842 jjdelcerro
            FeatureSet selection=null;
122 22986 vcaballero
            try {
123 23842 jjdelcerro
                    selection = (FeatureSet)getVLE().getFeatureStore().getSelection();
124 24263 vcaballero
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 22986 vcaballero
            } catch (ReadException e) {
131
                    // TODO Auto-generated catch block
132
                    e.printStackTrace();
133 24263 vcaballero
            } catch (DataException e) {
134
                    // TODO Auto-generated catch block
135
                    e.printStackTrace();
136 22986 vcaballero
            }
137 4434 caballero
    }
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 24429 vcaballero
        MoveCADToolState actualState = (MoveCADToolState) _fsm
148
                                .getPreviousState();
149 4434 caballero
        String status = actualState.getName();
150 4637 caballero
        VectorialLayerEdited vle=getVLE();
151 23086 vcaballero
        FeatureStore featureStore=null;
152
                try {
153
                        featureStore = vle.getFeatureStore();
154
                } catch (ReadException e1) {
155
                        // TODO Auto-generated catch block
156
                        e1.printStackTrace();
157
                }
158 24429 vcaballero
                 try {
159 23842 jjdelcerro
        FeatureSet selection=(FeatureSet)featureStore.getSelection();//getSelectedRows();
160 23086 vcaballero
//        ArrayList selectedRowAux=new ArrayList();
161 12739 caballero
        ICoordTrans ct=getVLE().getLayer().getCoordTrans();
162
        if (status.equals("Move.FirstPointToMove")) {
163 4434 caballero
            firstPoint = new Point2D.Double(x, y);
164
        } else if (status.equals("Move.SecondPointToMove")) {
165
            PluginServices.getMDIManager().setWaitCursor();
166 4637 caballero
           lastPoint = new Point2D.Double(x, y);
167 4434 caballero
168 24429 vcaballero
           featureStore.beginEditingGroup(getName());
169
170
171 23086 vcaballero
                    Iterator iterator=selection.iterator();
172
                    while (iterator.hasNext()) {
173
                                        Feature feature = (Feature) iterator.next();
174
175
//                                }
176
//              for (int i = 0; i < selection.size(); i++) {
177
//                                IRowEdited edRow = (IRowEdited) selectedRow.get(i);
178
//                                IFeature feat = (IFeature) edRow.getLinkedRow().cloneRow();
179
                                Geometry ig = ((Geometry)feature.getDefaultGeometry()).cloneGeometry();
180 24429 vcaballero
                                EditableFeature eFeature=feature.getEditable();
181 4458 caballero
                                if (ig == null)
182
                                        continue;
183 12739 caballero
                                if (ct!=null) {
184
                                        lastPoint=ct.getInverted().convert(lastPoint,null);
185
                                        firstPoint=ct.getInverted().convert(firstPoint,null);
186
187
                                }
188
//                                if (ct!=null)
189
//                                        ig.reProject(ct);
190
                                // Movemos la geometr?a
191 4458 caballero
                    UtilFunctions.moveGeom(ig, lastPoint.getX() -
192 4434 caballero
                            firstPoint.getX(), lastPoint.getY() - firstPoint.getY());
193
194 12739 caballero
//                  if (ct!=null)
195
//                        ig.reProject(ct.getInverted());
196 24429 vcaballero
                    eFeature.setGeometry(featureStore.getDefaultFeatureType().getDefaultGeometryAttributeName(),ig);
197
                    featureStore.update(eFeature);
198 23086 vcaballero
//                    vea.modifyRow(edRow.getIndex(),feat,getName(),EditionEvent.GRAPHIC);
199
//                    selectedRowAux.add(new DefaultRowEdited(feat,IRowEdited.STATUS_MODIFIED,edRow.getIndex()));
200 4637 caballero
              }
201 24429 vcaballero
                featureStore.endEditingGroup();
202 23086 vcaballero
//                vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS, selectedRowAux);
203 5985 caballero
                //clearSelection();
204
                      //selectedRow.addAll(selectedRowAux);
205 24429 vcaballero
        }
206
                 } catch (ReadException e) {
207 12739 caballero
                                NotificationManager.addError(e.getMessage(),e);
208 23086 vcaballero
                        } catch (DataException e) {
209 23535 vcaballero
                                NotificationManager.addError(e.getMessage(),e);
210 20894 vcaballero
                        }
211 24429 vcaballero
         PluginServices.getMDIManager().restoreCursor();
212 4434 caballero
    }
213
214
    /**
215 23535 vcaballero
     * M?todo para dibujar lo necesario para el estado en el que nos
216 4434 caballero
     * encontremos.
217
     *
218
     * @param g Graphics sobre el que dibujar.
219
     * @param x par?metro x del punto que se pase para dibujar.
220
     * @param y par?metro x del punto que se pase para dibujar.
221
     */
222
    public void drawOperation(Graphics g, double x, double y) {
223
        MoveCADToolState actualState = ((MoveCADToolContext) _fsm).getState();
224
        String status = actualState.getName();
225 4637 caballero
        VectorialLayerEdited vle=getVLE();
226
        //ArrayList selectedRow=getSelectedRows();
227
                /*drawHandlers(g, selectedRow,
228 4458 caballero
                     getCadToolAdapter().getMapControl().getViewPort()
229
                         .getAffineTransform());
230 4637 caballero
        */
231 4434 caballero
        if (status.equals("Move.SecondPointToMove")) {
232 6881 cesar
                ViewPort vp=vle.getLayer().getMapContext().getViewPort();
233 4637 caballero
            int dx = vp.fromMapDistance(x - firstPoint.getX());
234
            int dy = -vp.fromMapDistance(y - firstPoint.getY());
235
            Image img = vle.getSelectionImage();
236
            g.drawImage(img, dx, dy, null);
237 23086 vcaballero
//                try {
238
//                                Iterator iterator=vle.getFeatureStore().getSelection().iterator();
239
//                                while (iterator.hasNext()) {
240
//                                        Feature feature = (Feature) iterator.next();
241
//                                        Geometry ig = ((Geometry)feature.getDefaultGeometry()).cloneGeometry();
242
//                                    if (ig == null)
243
//                                            continue;
244
//                                    // Movemos la geometr?a
245
//                        UtilFunctions.moveGeom(ig, x - firstPoint.getX(), y - firstPoint.getY());
246
//
247
//                        DrawOperationContext doc=new DrawOperationContext();
248
//                                    doc.setGraphics((Graphics2D)g);
249
//                                    doc.setViewPort(vp);
250
//                                    doc.setSymbol(DefaultCADTool.geometrySelectSymbol);
251
//                            try {
252
//                                                ig.invokeOperation(Draw.CODE,doc);
253
//                                        } catch (GeometryOperationNotSupportedException e) {
254
//                                                e.printStackTrace();
255
//                                        } catch (GeometryOperationException e) {
256
//                                                e.printStackTrace();
257
//                                        }
258
//                    }
259
//                        } catch (ReadException e) {
260
//                                // TODO Auto-generated catch block
261
//                                e.printStackTrace();
262
//                        }
263
            /*for (int i = 0; i < selectedRow.size(); i++) {
264 4458 caballero
                            IRowEdited edRow = (IRowEdited) selectedRow.get(i);
265
                            IFeature feat = (IFeature) edRow.getLinkedRow();
266
                            IGeometry ig = feat.getGeometry().cloneGeometry();
267
                            if (ig == null)
268
                                    continue;
269
                            // Movemos la geometr?a
270
                UtilFunctions.moveGeom(ig, x - firstPoint.getX(), y - firstPoint.getY());
271
                ig.draw((Graphics2D) g,
272
                    getCadToolAdapter().getMapControl().getViewPort(),
273
                    CADTool.drawingSymbol);
274 4637 caballero
                    }*/
275
        }else{
276 7547 caballero
                if (!vle.getLayer().isVisible())
277
                                return;
278 4637 caballero
                 Image imgSel = vle.getSelectionImage();
279
             g.drawImage(imgSel, 0, 0, null);
280
             Image imgHand = vle.getHandlersImage();
281
             g.drawImage(imgHand, 0, 0, null);
282 4434 caballero
        }
283
    }
284
285
    /**
286
     * Add a diferent option.
287
     *
288
     * @param s Diferent option.
289
     */
290
    public void addOption(String s) {
291
    }
292
293
    /* (non-Javadoc)
294
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
295
     */
296
    public void addValue(double d) {
297
    }
298
299
        public String getName() {
300 4584 caballero
                return PluginServices.getText(this,"move_");
301 4434 caballero
        }
302 4892 caballero
303
        public String toString() {
304
                return "_move";
305
        }
306 5170 caballero
307 4434 caballero
}