Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1008 / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / StretchCADTool.java @ 12520

History | View | Annotate | Download (10.1 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.io.IOException;
49
import java.util.ArrayList;
50

    
51
import com.iver.andami.PluginServices;
52
import com.iver.cit.gvsig.CADExtension;
53
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
54
import com.iver.cit.gvsig.fmap.core.FShape;
55
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
56
import com.iver.cit.gvsig.fmap.core.Handler;
57
import com.iver.cit.gvsig.fmap.core.IGeometry;
58
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
59
import com.iver.cit.gvsig.fmap.core.v02.FGraphicUtilities;
60
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
61
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
62
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
63
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
64
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
65
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
66
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
67
import com.iver.cit.gvsig.gui.cad.tools.smc.StretchCADToolContext;
68
import com.iver.cit.gvsig.gui.cad.tools.smc.StretchCADToolContext.StretchCADToolState;
69
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
70

    
71

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

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

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

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

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

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

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

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

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

    
150
                        double x1;
151
                        double y1;
152
                        double w1;
153
                        double h1;
154

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

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

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

    
183
                        Handler[] handlers = null;
184

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

    
194
                                handlers = geometry.getHandlers(IGeometry.STRETCHINGHANDLER);
195

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

    
209
                        PluginServices.getMDIManager().restoreCursor();
210
                } catch (IOException e) {
211
                        // TODO Auto-generated catch block
212
                        e.printStackTrace();
213
                } catch (DriverIOException e) {
214
                        // TODO Auto-generated catch block
215
                        e.printStackTrace();
216
                }
217
                }
218
    }
219

    
220
    /**
221
     * M?todo para dibujar la lo necesario para el estado en el que nos
222
     * encontremos.
223
     *
224
     * @param g Graphics sobre el que dibujar.
225
     * @param x par?metro x del punto que se pase para dibujar.
226
     * @param y par?metro x del punto que se pase para dibujar.
227
     */
228
    public void drawOperation(Graphics g, double x, double y) {
229
                StretchCADToolState actualState = ((StretchCADToolContext) _fsm).getState();
230
                String status = actualState.getName();
231
                ArrayList selectedRow = getSelectedRows();
232

    
233
                if (status.equals("Stretch.SelLastPoint")) {
234
                        GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
235
                                        4);
236
                        elShape.moveTo(selfirstPoint.getX(), selfirstPoint.getY());
237
                        elShape.lineTo(x, selfirstPoint.getY());
238
                        elShape.lineTo(x, y);
239
                        elShape.lineTo(selfirstPoint.getX(), y);
240
                        elShape.lineTo(selfirstPoint.getX(), selfirstPoint.getY());
241
                        ShapeFactory.createPolyline2D(elShape).draw((Graphics2D) g,
242
                                getCadToolAdapter().getMapControl().getViewPort(),
243
                                DefaultCADTool.axisReferencesSymbol);
244
                } else if (status.equals("Stretch.MoveFirstPoint")) {
245
                        Handler[] handlers = null;
246
                        for (int i = 0;i<selectedRow.size();i++) {
247
                                IRowEdited edRow = (IRowEdited) selectedRow.get(i);
248
                            DefaultFeature fea = (DefaultFeature) edRow.getLinkedRow().cloneRow();
249
                                IGeometry geometry = null;
250
                                        geometry = fea.getGeometry();
251

    
252
                                handlers = geometry.getHandlers(IGeometry.STRETCHINGHANDLER);
253

    
254
                                for (int j = 0; j < handlers.length; j++) {
255
                                        if (rect.contains(handlers[j].getPoint())) {
256
                                                FGraphicUtilities.DrawHandlers((Graphics2D) g,getCadToolAdapter().getMapControl().getViewPort().getAffineTransform(),new Handler[] {handlers[j]},DefaultCADTool.handlerSymbol);
257
                                        }
258
                                }
259
                        }
260
                } else if (status.equals("Stretch.MoveLastPoint")) {
261
                        Handler[] handlers = null;
262

    
263
                        for (int i = 0;i<selectedRow.size();i++) {
264
                                IRowEdited edRow = (IRowEdited) selectedRow.get(i);
265
                            DefaultFeature fea = (DefaultFeature) edRow.getLinkedRow().cloneRow();
266
                                IGeometry geometry = null;
267
                                        geometry = fea.getGeometry();
268

    
269
                                handlers = geometry.getHandlers(IGeometry.STRETCHINGHANDLER);
270

    
271
                                for (int j = 0; j < handlers.length; j++) {
272
                                        if (rect.contains(handlers[j].getPoint())) {
273
                                                handlers[j].move(x - movefirstPoint.getX(),
274
                                                        y - movefirstPoint.getY());
275
                                        }
276
                                }
277
                                geometry.draw((Graphics2D) g,
278
                                        getCadToolAdapter().getMapControl().getViewPort(),
279
                                        DefaultCADTool.axisReferencesSymbol);
280
                        }
281
                }
282
        }
283

    
284
    /**
285
         * Add a diferent option.
286
         *
287
         * @param s
288
         *            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

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

    
304
        public String toString() {
305
                return "_stretch";
306
        }
307
        public boolean isApplicable(int shapeType) {
308
                switch (shapeType) {
309
                case FShape.POINT:
310
                case FShape.MULTIPOINT:
311
                        return false;
312
                }
313
                return true;
314
        }
315
}