Statistics
| Revision:

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

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

    
51
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
52
import com.iver.andami.PluginServices;
53
import com.iver.cit.gvsig.CADExtension;
54
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
55
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileWriteException;
56
import com.iver.cit.gvsig.exceptions.validate.ValidateRowException;
57
import com.iver.cit.gvsig.fmap.ViewPort;
58
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
59
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
60
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
61
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
62
import com.iver.cit.gvsig.fmap.edition.UtilFunctions;
63
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
64
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
65
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
66
import com.iver.cit.gvsig.gui.cad.tools.smc.RotateCADToolContext;
67
import com.iver.cit.gvsig.gui.cad.tools.smc.RotateCADToolContext.RotateCADToolState;
68
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
69

    
70

    
71
/**
72
 * DOCUMENT ME!
73
 *
74
 * @author Vicente Caballero Navarro
75
 */
76
public class RotateCADTool extends DefaultCADTool {
77
    private RotateCADToolContext _fsm;
78
    private Point2D firstPoint;
79
    private Point2D lastPoint;
80

    
81
    /**
82
     * Crea un nuevo PolylineCADTool.
83
     */
84
    public RotateCADTool() {
85
    }
86

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

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

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

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

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

    
130
    /**
131
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
132
     * editableFeatureSource que ya estar? creado.
133
     *
134
     * @param x par?metro x del punto que se pase en esta transici?n.
135
     * @param y par?metro y del punto que se pase en esta transici?n.
136
     */
137
    public void addPoint(double x, double y,InputEvent event) {
138
        RotateCADToolState actualState = (RotateCADToolState) _fsm.getPreviousState();
139
        String status = actualState.getName();
140
        ArrayList selectedRow=getSelectedRows();
141
        ArrayList selectedRowAux=new ArrayList();
142
        VectorialLayerEdited vle=getVLE();
143
        VectorialEditableAdapter vea = vle.getVEA();
144
        if (status.equals("Rotate.PointMain")) {
145
                firstPoint = new Point2D.Double(x, y);
146
                    } else if (status.equals("Rotate.AngleOrPoint")) {
147
                            PluginServices.getMDIManager().setWaitCursor();
148
                            lastPoint = new Point2D.Double(x,y);
149

    
150
                            double w;
151
                            double h;
152
                            w = lastPoint.getX() - firstPoint.getX();
153
                            h = lastPoint.getY() - firstPoint.getY();
154

    
155
                            try {
156
                                    vea.startComplexRow();
157
                            for (int i = 0; i < selectedRow.size(); i++) {
158
                                        DefaultRowEdited row=(DefaultRowEdited) selectedRow.get(i);
159
                                        DefaultFeature fea = (DefaultFeature) row.getLinkedRow().cloneRow();
160
                                        // Rotamos la geometry
161
                                        UtilFunctions.rotateGeom(fea.getGeometry(), -Math.atan2(w,
162
                                                        h)
163
                                                        + (Math.PI / 2), firstPoint.getX(), firstPoint
164
                                                        .getY());
165

    
166
                                        vea.modifyRow(row.getIndex(), fea,
167
                                                        getName(),EditionEvent.GRAPHIC);
168
                                        selectedRowAux.add(new DefaultRowEdited(fea,IRowEdited.STATUS_MODIFIED,row.getIndex()));
169
                                }
170

    
171
                                vea.endComplexRow(getName());
172
                                vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS, selectedRowAux);
173
                                //clearSelection();
174
                                //selectedRow.addAll(selectedRowAux);
175
                        } catch (ValidateRowException e) {
176
                                // TODO Auto-generated catch block
177
                                e.printStackTrace();
178
                        } catch (ExpansionFileWriteException e) {
179
                                // TODO Auto-generated catch block
180
                                e.printStackTrace();
181
                        } catch (ReadDriverException e) {
182
                                // TODO Auto-generated catch block
183
                                e.printStackTrace();
184
                        } catch (ExpansionFileReadException e) {
185
                                // TODO Auto-generated catch block
186
                                e.printStackTrace();
187
                        }
188

    
189
                            PluginServices.getMDIManager().restoreCursor();
190
                    }
191
    }
192

    
193
    /**
194
     * M?todo para dibujar la lo necesario para el estado en el que nos
195
     * encontremos.
196
     *
197
     * @param g Graphics sobre el que dibujar.
198
     * @param x par?metro x del punto que se pase para dibujar.
199
     * @param y par?metro x del punto que se pase para dibujar.
200
     */
201
    public void drawOperation(Graphics g, double x, double y) {
202
        RotateCADToolState actualState = _fsm.getState();
203
        String status = actualState.getName();
204
        VectorialLayerEdited vle=getVLE();
205
        //ArrayList selectedRow=getSelectedRows();
206

    
207
       // drawHandlers(g, selectedRow,
208
       //          getCadToolAdapter().getMapControl().getViewPort()
209
       //              .getAffineTransform());
210
        if (status.equals("Rotate.AngleOrPoint")) {
211
                        double w;
212
                        double h;
213
                        w = x - firstPoint.getX();
214
                        h = y - firstPoint.getY();
215
                        ViewPort vp=vle.getLayer().getMapContext().getViewPort();
216
                        Point2D point = vp.fromMapPoint(firstPoint.getX(),
217
                                                                firstPoint.getY());
218
                        AffineTransform at = AffineTransform.getRotateInstance(Math.atan2(
219
                                                                w, h) - (Math.PI / 2), (int) point.getX(),
220
                                                        (int) point.getY());
221

    
222
                        Image imgSel = vle.getSelectionImage();
223
            ((Graphics2D)g).drawImage(imgSel, at, null);
224
            drawLine((Graphics2D) g, firstPoint, new Point2D.Double(x, y));
225
                        ///AffineTransform at = AffineTransform.getRotateInstance(+Math.atan2(
226
                        ///                        w, h) - (Math.PI / 2), (int) point.getX(),
227
                        ///                (int) point.getY());
228
                        ///Image img = getCadToolAdapter().getVectorialAdapter().getImage();
229

    
230
                        ///((Graphics2D) g).drawImage(img, at, null);
231

    
232
                        ///drawLine((Graphics2D) g, firstPoint, new Point2D.Double(x, y));
233

    
234
/*
235
                             for (int i = 0; i < selectedRow.size(); i++) {
236
                                           // IGeometry geometry =
237
                                                                // getCadToolAdapter().getVectorialAdapter().getShape(i);
238
                                IRowEdited edRow = (IRowEdited) selectedRow.get(i);
239
                                IFeature feat = (IFeature) edRow.getLinkedRow();
240
                                IGeometry geometry = feat.getGeometry().cloneGeometry();
241
                                // Rotamos la geometry
242
                                UtilFunctions.rotateGeom(geometry, -Math.atan2(w, h) + Math.PI
243
                                                / 2, firstPoint.getX(), firstPoint.getY());
244

245
                                geometry.draw((Graphics2D) g, getCadToolAdapter()
246
                                                .getMapControl().getViewPort(), CADTool.drawingSymbol);
247
                                GeneralPathX elShape = new GeneralPathX(
248
                                                GeneralPathX.WIND_EVEN_ODD, 2);
249
                                elShape.moveTo(firstPoint.getX(), firstPoint.getY());
250
                                elShape.lineTo(x, y);
251
                                ShapeFactory.createPolyline2D(elShape).draw((Graphics2D) g,
252
                                                getCadToolAdapter().getMapControl().getViewPort(),
253
                                                CADTool.drawingSymbol);
254

255
                        }
256
                        */
257
                }else{
258
                        if (!vle.getLayer().isVisible())
259
                                return;
260
                         Image imgSel = vle.getSelectionImage();
261
             g.drawImage(imgSel, 0, 0, null);
262
             Image imgHand = vle.getHandlersImage();
263
             g.drawImage(imgHand, 0, 0, null);
264
                }
265
    }
266

    
267
    /**
268
         * Add a diferent option.
269
         *
270
         * @param s
271
         *            Diferent option.
272
         */
273
    public void addOption(String s) {
274
    }
275

    
276
    /*
277
         * (non-Javadoc)
278
         *
279
         * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
280
         */
281
    public void addValue(double d) {
282
            RotateCADToolState actualState = (RotateCADToolState) _fsm.getPreviousState();
283
        String status = actualState.getName();
284
        ArrayList selectedRow=getSelectedRows();
285
        VectorialLayerEdited vle=getVLE();
286
        VectorialEditableAdapter vea = vle.getVEA();
287
            if (status.equals("Rotate.AngleOrPoint")) {
288
                        try {
289

    
290
                                vea.startComplexRow();
291
                                ///ArrayList selectedRowAux=new ArrayList();
292
                                for (int i = 0; i < selectedRow.size(); i++) {
293
                                        DefaultRowEdited row=(DefaultRowEdited) selectedRow.get(i);
294
                                        DefaultFeature fea = (DefaultFeature) row.getLinkedRow().cloneRow();
295
                                                // Rotamos la geometry
296
                                                AffineTransform at = new AffineTransform();
297
                                                at.rotate(Math.toRadians(d),
298
                                                            firstPoint.getX(), firstPoint.getY());
299
                                                fea.getGeometry().transform(at);
300
                                            vea.modifyRow(row.getIndex(), fea,getName(),EditionEvent.GRAPHIC);
301
                                            ///selectedRowAux.add(new DefaultRowEdited(fea,IRowEdited.STATUS_MODIFIED,index));
302
                                }
303
                                vea.endComplexRow(getName());
304
                                clearSelection();
305
                                ///selectedRow=selectedRowAux;
306
                        } catch (ValidateRowException e) {
307
                                // TODO Auto-generated catch block
308
                                e.printStackTrace();
309
                        } catch (ExpansionFileWriteException e) {
310
                                // TODO Auto-generated catch block
311
                                e.printStackTrace();
312
                        } catch (ReadDriverException e) {
313
                                // TODO Auto-generated catch block
314
                                e.printStackTrace();
315
                        } catch (ExpansionFileReadException e) {
316
                                // TODO Auto-generated catch block
317
                                e.printStackTrace();
318
                        }
319

    
320
                }
321
    }
322

    
323
        public String getName() {
324
                return PluginServices.getText(this,"rotate_");
325
        }
326

    
327
        public String toString() {
328
                return "_rotate";
329
        }
330

    
331
}