Statistics
| Revision:

root / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / StretchCADTool.java @ 27149

History | View | Annotate | Download (10.6 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.andami.messages.NotificationManager;
53
import com.iver.cit.gvsig.CADExtension;
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
                            NotificationManager.addError(e.getMessage(),e);
214
                    } catch (ExpansionFileWriteException e) {
215
                            NotificationManager.addError(e.getMessage(),e);
216
                    } catch (ReadDriverException e) {
217
                            NotificationManager.addError(e.getMessage(),e);
218
                    } 
219
            }
220
    }
221

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

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

    
254
                                handlers = geometry.getHandlers(IGeometry.STRETCHINGHANDLER);
255

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

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

    
271
                                handlers = geometry.getHandlers(IGeometry.STRETCHINGHANDLER);
272

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

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

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

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