Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / StretchCADTool.java @ 10626

History | View | Annotate | Download (10.5 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.event.InputEvent;
46
import java.awt.geom.Point2D;
47
import java.awt.geom.Rectangle2D;
48
import java.util.ArrayList;
49

    
50
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
51
import com.iver.andami.PluginServices;
52
import com.iver.cit.gvsig.CADExtension;
53
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
54
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileWriteException;
55
import com.iver.cit.gvsig.exceptions.validate.ValidateRowException;
56
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
57
import com.iver.cit.gvsig.fmap.core.FShape;
58
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
59
import com.iver.cit.gvsig.fmap.core.Handler;
60
import com.iver.cit.gvsig.fmap.core.IGeometry;
61
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
62
import com.iver.cit.gvsig.fmap.core.v02.FGraphicUtilities;
63
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
64
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
65
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
66
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
67
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
68
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
69
import com.iver.cit.gvsig.gui.cad.tools.smc.StretchCADToolContext;
70
import com.iver.cit.gvsig.gui.cad.tools.smc.StretchCADToolContext.StretchCADToolState;
71
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
72

    
73

    
74
/**
75
 * Herramienta para estirar los handlers que seleccionemos previamente.
76
 *
77
 * @author Vicente Caballero Navarro
78
 */
79
public class StretchCADTool extends DefaultCADTool {
80
    private StretchCADToolContext _fsm;
81
    private Point2D selfirstPoint;
82
        private Point2D sellastPoint;
83
        private Point2D movefirstPoint;
84
        private Point2D movelastPoint;
85
        private Rectangle2D rect = null;
86

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

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

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

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

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

    
124
    /**
125
     * DOCUMENT ME!
126
     */
127
    public void selection() {
128
        ArrayList rowSelected=getSelectedRows();
129
        if (rowSelected.size() == 0 && !CADExtension.getCADTool().getClass().getName().equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
130
            CADExtension.setCADTool("_selection",false);
131
            ((SelectionCADTool) CADExtension.getCADTool()).setNextTool(
132
                "_stretch");
133
        }
134
    }
135

    
136
    /**
137
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
138
     * editableFeatureSource que ya estar? creado.
139
     *
140
     * @param x par?metro x del punto que se pase en esta transici?n.
141
     * @param y par?metro y del punto que se pase en esta transici?n.
142
     */
143
    public void addPoint(double x, double y,InputEvent event) {
144
        StretchCADToolState actualState = (StretchCADToolState) _fsm.getPreviousState();
145
        String status = actualState.getName();
146

    
147
        if (status.equals("Stretch.SelFirstPoint")) {
148
                selfirstPoint = new Point2D.Double(x, y);
149
                } else if (status.equals("Stretch.SelLastPoint")) {
150
                        sellastPoint = new Point2D.Double(x,y);
151

    
152
                        double x1;
153
                        double y1;
154
                        double w1;
155
                        double h1;
156

    
157
                        if (selfirstPoint.getX() < sellastPoint.getX()) {
158
                                x1 = selfirstPoint.getX();
159
                                w1 = sellastPoint.getX() - selfirstPoint.getX();
160
                        } else {
161
                                x1 = sellastPoint.getX();
162
                                w1 = selfirstPoint.getX() - sellastPoint.getX();
163
                        }
164

    
165
                        if (selfirstPoint.getY() < sellastPoint.getY()) {
166
                                y1 = selfirstPoint.getY();
167
                                h1 = sellastPoint.getY() - selfirstPoint.getY();
168
                        } else {
169
                                y1 = sellastPoint.getY();
170
                                h1 = selfirstPoint.getY() - sellastPoint.getY();
171
                        }
172

    
173
                        rect = new Rectangle2D.Double(x1, y1, w1, h1);
174
                } else if (status.equals("Stretch.MoveFirstPoint")) {
175
                        movefirstPoint = new Point2D.Double(x, y);
176
                } else if (status.equals("Stretch.MoveLastPoint")) {
177
                        VectorialLayerEdited vle=getVLE();
178
                    VectorialEditableAdapter vea=vle.getVEA();
179
                    vea.startComplexRow();
180
                    ArrayList selectedRow=getSelectedRows();
181
                    ArrayList selectedRowAux=new ArrayList();
182
                    PluginServices.getMDIManager().setWaitCursor();
183
                        movelastPoint = new Point2D.Double(x, y);
184

    
185
                        Handler[] handlers = null;
186

    
187
                        //for (int i = selectedGeometries.nextSetBit(0); i >= 0;
188
                        //                i = selectedGeometries.nextSetBit(i + 1)) {
189
                        try {
190
                        for (int i =0;i<selectedRow.size(); i++) {
191
                                IRowEdited edRow = (IRowEdited) selectedRow.get(i);
192
                            DefaultFeature fea = (DefaultFeature) edRow.getLinkedRow().cloneRow();
193
                                IGeometry geometry = null;
194
                                geometry = fea.getGeometry();
195

    
196
                                handlers = geometry.getHandlers(IGeometry.STRETCHINGHANDLER);
197

    
198
                                for (int j = 0; j < handlers.length; j++) {
199
                                        if (rect.contains(handlers[j].getPoint())) {
200
                                                handlers[j].move(movelastPoint.getX() -
201
                                                        movefirstPoint.getX(),
202
                                                        movelastPoint.getY() - movefirstPoint.getY());
203
                                        }
204
                                }
205
                                vea.modifyRow(edRow.getIndex(),fea,getName(),EditionEvent.GRAPHIC);
206
                                selectedRowAux.add(new DefaultRowEdited(fea,IRowEdited.STATUS_MODIFIED,edRow.getIndex()));
207
                        }
208
                        vea.endComplexRow(getName());
209
                    vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS, selectedRowAux);
210

    
211
                        PluginServices.getMDIManager().restoreCursor();
212
                } catch (ValidateRowException e) {
213
                        // TODO Auto-generated catch block
214
                        e.printStackTrace();
215
                } catch (ExpansionFileWriteException e) {
216
                        // TODO Auto-generated catch block
217
                        e.printStackTrace();
218
                } catch (ReadDriverException e) {
219
                        // TODO Auto-generated catch block
220
                        e.printStackTrace();
221
                } catch (ExpansionFileReadException e) {
222
                        // TODO Auto-generated catch block
223
                        e.printStackTrace();
224
                }
225
                }
226
    }
227

    
228
    /**
229
     * M?todo para dibujar la lo necesario para el estado en el que nos
230
     * encontremos.
231
     *
232
     * @param g Graphics sobre el que dibujar.
233
     * @param x par?metro x del punto que se pase para dibujar.
234
     * @param y par?metro x del punto que se pase para dibujar.
235
     */
236
    public void drawOperation(Graphics g, double x, double y) {
237
                StretchCADToolState actualState = ((StretchCADToolContext) _fsm).getState();
238
                String status = actualState.getName();
239
                ArrayList selectedRow = getSelectedRows();
240

    
241
                if (status.equals("Stretch.SelLastPoint")) {
242
                        GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
243
                                        4);
244
                        elShape.moveTo(selfirstPoint.getX(), selfirstPoint.getY());
245
                        elShape.lineTo(x, selfirstPoint.getY());
246
                        elShape.lineTo(x, y);
247
                        elShape.lineTo(selfirstPoint.getX(), y);
248
                        elShape.lineTo(selfirstPoint.getX(), selfirstPoint.getY());
249
                        ShapeFactory.createPolyline2D(elShape).draw((Graphics2D) g,
250
                                getCadToolAdapter().getMapControl().getViewPort(),
251
                                modifySymbol);
252
                } else if (status.equals("Stretch.MoveFirstPoint")) {
253
                        Handler[] handlers = null;
254
                        for (int i = 0;i<selectedRow.size();i++) {
255
                                IRowEdited edRow = (IRowEdited) selectedRow.get(i);
256
                            DefaultFeature fea = (DefaultFeature) edRow.getLinkedRow().cloneRow();
257
                                IGeometry geometry = null;
258
                                        geometry = fea.getGeometry();
259

    
260
                                handlers = geometry.getHandlers(IGeometry.STRETCHINGHANDLER);
261

    
262
                                for (int j = 0; j < handlers.length; j++) {
263
                                        if (rect.contains(handlers[j].getPoint())) {
264
                                                FGraphicUtilities.DrawHandlers((Graphics2D) g,getCadToolAdapter().getMapControl().getViewPort().getAffineTransform(),new Handler[] {handlers[j]});
265
                                        }
266
                                }
267
                        }
268
                } else if (status.equals("Stretch.MoveLastPoint")) {
269
                        Handler[] handlers = null;
270

    
271
                        for (int i = 0;i<selectedRow.size();i++) {
272
                                IRowEdited edRow = (IRowEdited) selectedRow.get(i);
273
                            DefaultFeature fea = (DefaultFeature) edRow.getLinkedRow().cloneRow();
274
                                IGeometry geometry = null;
275
                                        geometry = fea.getGeometry();
276

    
277
                                handlers = geometry.getHandlers(IGeometry.STRETCHINGHANDLER);
278

    
279
                                for (int j = 0; j < handlers.length; j++) {
280
                                        if (rect.contains(handlers[j].getPoint())) {
281
                                                handlers[j].move(x - movefirstPoint.getX(),
282
                                                        y - movefirstPoint.getY());
283
                                        }
284
                                }
285
                                geometry.draw((Graphics2D) g,
286
                                        getCadToolAdapter().getMapControl().getViewPort(),
287
                                        modifySymbol);
288
                        }
289
                }
290
        }
291

    
292
    /**
293
         * Add a diferent option.
294
         *
295
         * @param s
296
         *            Diferent option.
297
         */
298
    public void addOption(String s) {
299
    }
300

    
301
    /* (non-Javadoc)
302
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
303
     */
304
    public void addValue(double d) {
305
    }
306

    
307

    
308
        public String getName() {
309
                return PluginServices.getText(this,"stretch_");
310
        }
311

    
312
        public String toString() {
313
                return "_stretch";
314
        }
315
        public boolean isApplicable(int shapeType) {
316
                switch (shapeType) {
317
                case FShape.POINT:
318
                case FShape.MULTIPOINT:
319
                        return false;
320
                }
321
                return true;
322
        }
323
}