Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_RELEASE / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / RotateCADTool.java @ 11432

History | View | Annotate | Download (10.3 KB)

1 3883 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
import java.awt.Graphics2D;
45 4637 caballero
import java.awt.Image;
46 4313 fjp
import java.awt.event.InputEvent;
47 3883 caballero
import java.awt.geom.AffineTransform;
48
import java.awt.geom.Point2D;
49
import java.io.IOException;
50 4458 caballero
import java.util.ArrayList;
51 3883 caballero
52
import com.iver.andami.PluginServices;
53
import com.iver.cit.gvsig.CADExtension;
54 4637 caballero
import com.iver.cit.gvsig.fmap.ViewPort;
55 3883 caballero
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
56
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
57 4584 caballero
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
58 5184 caballero
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
59 5884 caballero
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
60 3904 fjp
import com.iver.cit.gvsig.fmap.edition.UtilFunctions;
61 4637 caballero
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
62 3883 caballero
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
63 5735 caballero
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
64 3883 caballero
import com.iver.cit.gvsig.gui.cad.tools.smc.RotateCADToolContext;
65
import com.iver.cit.gvsig.gui.cad.tools.smc.RotateCADToolContext.RotateCADToolState;
66 4637 caballero
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
67 3883 caballero
68
69
/**
70
 * DOCUMENT ME!
71
 *
72
 * @author Vicente Caballero Navarro
73
 */
74
public class RotateCADTool extends DefaultCADTool {
75
    private RotateCADToolContext _fsm;
76
    private Point2D firstPoint;
77
    private Point2D lastPoint;
78
79
    /**
80
     * Crea un nuevo PolylineCADTool.
81
     */
82
    public RotateCADTool() {
83
    }
84
85
    /**
86
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
87
     * carga previa a la utilizaci?n de la herramienta.
88
     */
89
    public void init() {
90
        _fsm = new RotateCADToolContext(this);
91
    }
92
93
    /* (non-Javadoc)
94
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
95
     */
96 4313 fjp
    public void transition(double x, double y, InputEvent event) {
97 4324 caballero
        _fsm.addPoint(x, y, event);
98 3883 caballero
    }
99
100
    /* (non-Javadoc)
101
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
102
     */
103
    public void transition(double d) {
104
        _fsm.addValue(d);
105
    }
106
107
    /* (non-Javadoc)
108
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
109
     */
110 5735 caballero
    public void transition(String s) throws CommandException {
111 4892 caballero
            if (!super.changeCommand(s)){
112
                    _fsm.addOption(s);
113
            }
114 3883 caballero
    }
115
116
    /**
117
     * DOCUMENT ME!
118
     */
119
    public void selection() {
120 4584 caballero
       ArrayList selectedRows=getSelectedRows();
121
        if (selectedRows.size() == 0 && !CADExtension.getCADTool().getClass().getName().equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
122 5071 caballero
            CADExtension.setCADTool("_selection",false);
123 4584 caballero
            ((SelectionCADTool) CADExtension.getCADTool()).setNextTool(
124 5071 caballero
                "_rotate");
125 3883 caballero
        }
126
    }
127
128
    /**
129
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
130
     * editableFeatureSource que ya estar? creado.
131
     *
132
     * @param x par?metro x del punto que se pase en esta transici?n.
133
     * @param y par?metro y del punto que se pase en esta transici?n.
134
     */
135 4365 caballero
    public void addPoint(double x, double y,InputEvent event) {
136 3883 caballero
        RotateCADToolState actualState = (RotateCADToolState) _fsm.getPreviousState();
137
        String status = actualState.getName();
138 4584 caballero
        ArrayList selectedRow=getSelectedRows();
139 5884 caballero
        ArrayList selectedRowAux=new ArrayList();
140 4637 caballero
        VectorialLayerEdited vle=getVLE();
141
        VectorialEditableAdapter vea = vle.getVEA();
142 3978 caballero
        if (status.equals("Rotate.PointMain")) {
143 3883 caballero
                firstPoint = new Point2D.Double(x, y);
144 3978 caballero
                    } else if (status.equals("Rotate.AngleOrPoint")) {
145 3883 caballero
                            PluginServices.getMDIManager().setWaitCursor();
146
                            lastPoint = new Point2D.Double(x,y);
147
148
                            double w;
149
                            double h;
150
                            w = lastPoint.getX() - firstPoint.getX();
151
                            h = lastPoint.getY() - firstPoint.getY();
152
153
                            try {
154 4637 caballero
                                    vea.startComplexRow();
155 5884 caballero
                            for (int i = 0; i < selectedRow.size(); i++) {
156 4637 caballero
                                        DefaultRowEdited row=(DefaultRowEdited) selectedRow.get(i);
157
                                        DefaultFeature fea = (DefaultFeature) row.getLinkedRow().cloneRow();
158 4584 caballero
                                        // Rotamos la geometry
159
                                        UtilFunctions.rotateGeom(fea.getGeometry(), -Math.atan2(w,
160
                                                        h)
161
                                                        + (Math.PI / 2), firstPoint.getX(), firstPoint
162
                                                        .getY());
163 3978 caballero
164 5908 caballero
                                        vea.modifyRow(row.getIndex(), fea,
165 5184 caballero
                                                        getName(),EditionEvent.GRAPHIC);
166 5908 caballero
                                        selectedRowAux.add(new DefaultRowEdited(fea,IRowEdited.STATUS_MODIFIED,row.getIndex()));
167 4584 caballero
                                }
168 4637 caballero
169 6071 caballero
                                vea.endComplexRow(getName());
170 5985 caballero
                                vle.setSelectionCache(selectedRowAux);
171
                                //clearSelection();
172
                                //selectedRow.addAll(selectedRowAux);
173 4584 caballero
                        } catch (DriverIOException e) {
174
                                e.printStackTrace();
175
                        } catch (IOException e1) {
176
                                e1.printStackTrace();
177
                        }
178 3883 caballero
179
                            PluginServices.getMDIManager().restoreCursor();
180
                    }
181
    }
182
183
    /**
184
     * M?todo para dibujar la lo necesario para el estado en el que nos
185
     * encontremos.
186
     *
187
     * @param g Graphics sobre el que dibujar.
188
     * @param x par?metro x del punto que se pase para dibujar.
189
     * @param y par?metro x del punto que se pase para dibujar.
190
     */
191
    public void drawOperation(Graphics g, double x, double y) {
192
        RotateCADToolState actualState = ((RotateCADToolContext) _fsm).getState();
193
        String status = actualState.getName();
194 4637 caballero
        VectorialLayerEdited vle=getVLE();
195
        //ArrayList selectedRow=getSelectedRows();
196 3883 caballero
197 4637 caballero
       // drawHandlers(g, selectedRow,
198
       //          getCadToolAdapter().getMapControl().getViewPort()
199
       //              .getAffineTransform());
200 3978 caballero
        if (status.equals("Rotate.AngleOrPoint")) {
201 3883 caballero
                        double w;
202
                        double h;
203
                        w = x - firstPoint.getX();
204
                        h = y - firstPoint.getY();
205 6881 cesar
                        ViewPort vp=vle.getLayer().getMapContext().getViewPort();
206 4637 caballero
                        Point2D point = vp.fromMapPoint(firstPoint.getX(),
207
                                                                firstPoint.getY());
208
                        AffineTransform at = AffineTransform.getRotateInstance(Math.atan2(
209
                                                                w, h) - (Math.PI / 2), (int) point.getX(),
210
                                                        (int) point.getY());
211 3883 caballero
212 4637 caballero
                        Image imgSel = vle.getSelectionImage();
213
            ((Graphics2D)g).drawImage(imgSel, at, null);
214
            drawLine((Graphics2D) g, firstPoint, new Point2D.Double(x, y));
215 3883 caballero
                        ///AffineTransform at = AffineTransform.getRotateInstance(+Math.atan2(
216
                        ///                        w, h) - (Math.PI / 2), (int) point.getX(),
217
                        ///                (int) point.getY());
218
                        ///Image img = getCadToolAdapter().getVectorialAdapter().getImage();
219
220
                        ///((Graphics2D) g).drawImage(img, at, null);
221
222
                        ///drawLine((Graphics2D) g, firstPoint, new Point2D.Double(x, y));
223
224 4637 caballero
/*
225 4584 caballero
                             for (int i = 0; i < selectedRow.size(); i++) {
226
                                           // IGeometry geometry =
227
                                                                // getCadToolAdapter().getVectorialAdapter().getShape(i);
228
                                IRowEdited edRow = (IRowEdited) selectedRow.get(i);
229
                                IFeature feat = (IFeature) edRow.getLinkedRow();
230
                                IGeometry geometry = feat.getGeometry().cloneGeometry();
231
                                // Rotamos la geometry
232
                                UtilFunctions.rotateGeom(geometry, -Math.atan2(w, h) + Math.PI
233
                                                / 2, firstPoint.getX(), firstPoint.getY());
234 3883 caballero

235 4584 caballero
                                geometry.draw((Graphics2D) g, getCadToolAdapter()
236
                                                .getMapControl().getViewPort(), CADTool.drawingSymbol);
237
                                GeneralPathX elShape = new GeneralPathX(
238
                                                GeneralPathX.WIND_EVEN_ODD, 2);
239
                                elShape.moveTo(firstPoint.getX(), firstPoint.getY());
240
                                elShape.lineTo(x, y);
241
                                ShapeFactory.createPolyline2D(elShape).draw((Graphics2D) g,
242
                                                getCadToolAdapter().getMapControl().getViewPort(),
243
                                                CADTool.drawingSymbol);
244 3883 caballero

245 4584 caballero
                        }
246 4637 caballero
                        */
247
                }else{
248 7541 caballero
                        if (!vle.getLayer().isVisible())
249
                                return;
250 4637 caballero
                         Image imgSel = vle.getSelectionImage();
251
             g.drawImage(imgSel, 0, 0, null);
252
             Image imgHand = vle.getHandlersImage();
253
             g.drawImage(imgHand, 0, 0, null);
254 3883 caballero
                }
255
    }
256
257
    /**
258 4584 caballero
         * Add a diferent option.
259
         *
260
         * @param s
261
         *            Diferent option.
262
         */
263 3883 caballero
    public void addOption(String s) {
264
    }
265
266 4584 caballero
    /*
267
         * (non-Javadoc)
268
         *
269
         * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
270
         */
271 3883 caballero
    public void addValue(double d) {
272
            RotateCADToolState actualState = (RotateCADToolState) _fsm.getPreviousState();
273
        String status = actualState.getName();
274 4584 caballero
        ArrayList selectedRow=getSelectedRows();
275 4637 caballero
        VectorialLayerEdited vle=getVLE();
276
        VectorialEditableAdapter vea = vle.getVEA();
277 3978 caballero
            if (status.equals("Rotate.AngleOrPoint")) {
278 3883 caballero
                        try {
279 4637 caballero
280
                                vea.startComplexRow();
281
                                ///ArrayList selectedRowAux=new ArrayList();
282 4522 caballero
                                for (int i = 0; i < selectedRow.size(); i++) {
283 4637 caballero
                                        DefaultRowEdited row=(DefaultRowEdited) selectedRow.get(i);
284
                                        DefaultFeature fea = (DefaultFeature) row.getLinkedRow().cloneRow();
285 3904 fjp
                                                // Rotamos la geometry
286
                                                AffineTransform at = new AffineTransform();
287
                                                at.rotate(Math.toRadians(d),
288
                                                            firstPoint.getX(), firstPoint.getY());
289
                                                fea.getGeometry().transform(at);
290 5735 caballero
                                            vea.modifyRow(row.getIndex(), fea,getName(),EditionEvent.GRAPHIC);
291 4637 caballero
                                            ///selectedRowAux.add(new DefaultRowEdited(fea,IRowEdited.STATUS_MODIFIED,index));
292 3883 caballero
                                }
293 6071 caballero
                                vea.endComplexRow(getName());
294 4584 caballero
                                clearSelection();
295 4637 caballero
                                ///selectedRow=selectedRowAux;
296 3883 caballero
                        } catch (DriverIOException e) {
297
                                e.printStackTrace();
298
                        } catch (IOException e1) {
299
                                e1.printStackTrace();
300
                        }
301
302
                }
303
    }
304 4118 caballero
305
        public String getName() {
306 4584 caballero
                return PluginServices.getText(this,"rotate_");
307 4118 caballero
        }
308 4892 caballero
309
        public String toString() {
310
                return "_rotate";
311
        }
312 5170 caballero
313 3883 caballero
}