Statistics
| Revision:

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

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

    
52
import com.iver.andami.PluginServices;
53
import com.iver.cit.gvsig.CADExtension;
54
import com.iver.cit.gvsig.fmap.ViewPort;
55
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
56
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
57
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
58
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
59
import com.iver.cit.gvsig.fmap.edition.UtilFunctions;
60
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
61
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
62
import com.iver.cit.gvsig.gui.cad.tools.smc.RotateCADToolContext;
63
import com.iver.cit.gvsig.gui.cad.tools.smc.RotateCADToolContext.RotateCADToolState;
64
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
65

    
66

    
67
/**
68
 * DOCUMENT ME!
69
 *
70
 * @author Vicente Caballero Navarro
71
 */
72
public class RotateCADTool extends DefaultCADTool {
73
    private RotateCADToolContext _fsm;
74
    private Point2D firstPoint;
75
    private Point2D lastPoint;
76

    
77
    /**
78
     * Crea un nuevo PolylineCADTool.
79
     */
80
    public RotateCADTool() {
81
    }
82

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

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

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

    
105
    /* (non-Javadoc)
106
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
107
     */
108
    public void transition(String s) {
109
        _fsm.addOption(s);
110
    }
111

    
112
    /**
113
     * DOCUMENT ME!
114
     */
115
    public void selection() {
116
       ArrayList selectedRows=getSelectedRows();
117
        if (selectedRows.size() == 0 && !CADExtension.getCADTool().getClass().getName().equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
118
            CADExtension.setCADTool("selection");
119
            ((SelectionCADTool) CADExtension.getCADTool()).setNextTool(
120
                "rotate");
121
        }
122
    }
123

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

    
143
                            double w;
144
                            double h;
145
                            w = lastPoint.getX() - firstPoint.getX();
146
                            h = lastPoint.getY() - firstPoint.getY();
147

    
148
                            try {
149
                                    vea.startComplexRow();
150
                                    ///ArrayList selectedRowAux=new ArrayList();
151
                                for (int i = 0; i < selectedRow.size(); i++) {
152
                                        DefaultRowEdited row=(DefaultRowEdited) selectedRow.get(i);
153
                                        DefaultFeature fea = (DefaultFeature) row.getLinkedRow().cloneRow();
154
                                        // Rotamos la geometry
155
                                        UtilFunctions.rotateGeom(fea.getGeometry(), -Math.atan2(w,
156
                                                        h)
157
                                                        + (Math.PI / 2), firstPoint.getX(), firstPoint
158
                                                        .getY());
159

    
160
                                        int index=vea.modifyRow(row.getIndex(), fea,
161
                                                        getName());
162
                                        ///selectedRowAux.add(new DefaultRowEdited(fea,IRowEdited.STATUS_MODIFIED,index));
163
                                }
164

    
165
                                vea.endComplexRow();
166
                                clearSelection();
167
                                ///selectedRow=selectedRowAux;
168
                        } catch (DriverIOException e) {
169
                                e.printStackTrace();
170
                        } catch (IOException e1) {
171
                                e1.printStackTrace();
172
                        }
173

    
174
                            PluginServices.getMDIManager().restoreCursor();
175
                    }
176
    }
177

    
178
    /**
179
     * M?todo para dibujar la lo necesario para el estado en el que nos
180
     * encontremos.
181
     *
182
     * @param g Graphics sobre el que dibujar.
183
     * @param x par?metro x del punto que se pase para dibujar.
184
     * @param y par?metro x del punto que se pase para dibujar.
185
     */
186
    public void drawOperation(Graphics g, double x, double y) {
187
        RotateCADToolState actualState = ((RotateCADToolContext) _fsm).getState();
188
        String status = actualState.getName();
189
        VectorialLayerEdited vle=getVLE();
190
        //ArrayList selectedRow=getSelectedRows();
191

    
192
       // drawHandlers(g, selectedRow,
193
       //          getCadToolAdapter().getMapControl().getViewPort()
194
       //              .getAffineTransform());
195
        if (status.equals("Rotate.AngleOrPoint")) {
196
                        double w;
197
                        double h;
198
                        w = x - firstPoint.getX();
199
                        h = y - firstPoint.getY();
200
                        ViewPort vp=vle.getLayer().getFMap().getViewPort();
201
                        Point2D point = vp.fromMapPoint(firstPoint.getX(),
202
                                                                firstPoint.getY());
203
                        AffineTransform at = AffineTransform.getRotateInstance(Math.atan2(
204
                                                                w, h) - (Math.PI / 2), (int) point.getX(),
205
                                                        (int) point.getY());
206

    
207
                        Image imgSel = vle.getSelectionImage();
208
            ((Graphics2D)g).drawImage(imgSel, at, null);
209
            drawLine((Graphics2D) g, firstPoint, new Point2D.Double(x, y));
210
                        ///AffineTransform at = AffineTransform.getRotateInstance(+Math.atan2(
211
                        ///                        w, h) - (Math.PI / 2), (int) point.getX(),
212
                        ///                (int) point.getY());
213
                        ///Image img = getCadToolAdapter().getVectorialAdapter().getImage();
214

    
215
                        ///((Graphics2D) g).drawImage(img, at, null);
216

    
217
                        ///drawLine((Graphics2D) g, firstPoint, new Point2D.Double(x, y));
218

    
219
/*
220
                             for (int i = 0; i < selectedRow.size(); i++) {
221
                                           // IGeometry geometry =
222
                                                                // getCadToolAdapter().getVectorialAdapter().getShape(i);
223
                                IRowEdited edRow = (IRowEdited) selectedRow.get(i);
224
                                IFeature feat = (IFeature) edRow.getLinkedRow();
225
                                IGeometry geometry = feat.getGeometry().cloneGeometry();
226
                                // Rotamos la geometry
227
                                UtilFunctions.rotateGeom(geometry, -Math.atan2(w, h) + Math.PI
228
                                                / 2, firstPoint.getX(), firstPoint.getY());
229

230
                                geometry.draw((Graphics2D) g, getCadToolAdapter()
231
                                                .getMapControl().getViewPort(), CADTool.drawingSymbol);
232
                                GeneralPathX elShape = new GeneralPathX(
233
                                                GeneralPathX.WIND_EVEN_ODD, 2);
234
                                elShape.moveTo(firstPoint.getX(), firstPoint.getY());
235
                                elShape.lineTo(x, y);
236
                                ShapeFactory.createPolyline2D(elShape).draw((Graphics2D) g,
237
                                                getCadToolAdapter().getMapControl().getViewPort(),
238
                                                CADTool.drawingSymbol);
239

240
                        }
241
                        */
242
                }else{
243
                         Image imgSel = vle.getSelectionImage();
244
             g.drawImage(imgSel, 0, 0, null);
245
             Image imgHand = vle.getHandlersImage();
246
             g.drawImage(imgHand, 0, 0, null);
247
                }
248
    }
249

    
250
    /**
251
         * Add a diferent option.
252
         *
253
         * @param s
254
         *            Diferent option.
255
         */
256
    public void addOption(String s) {
257
    }
258

    
259
    /*
260
         * (non-Javadoc)
261
         *
262
         * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
263
         */
264
    public void addValue(double d) {
265
            RotateCADToolState actualState = (RotateCADToolState) _fsm.getPreviousState();
266
        String status = actualState.getName();
267
        ArrayList selectedRow=getSelectedRows();
268
        VectorialLayerEdited vle=getVLE();
269
        VectorialEditableAdapter vea = vle.getVEA();
270
            if (status.equals("Rotate.AngleOrPoint")) {
271
                        try {
272

    
273
                                vea.startComplexRow();
274
                                ///ArrayList selectedRowAux=new ArrayList();
275
                                for (int i = 0; i < selectedRow.size(); i++) {
276
                                        DefaultRowEdited row=(DefaultRowEdited) selectedRow.get(i);
277
                                        DefaultFeature fea = (DefaultFeature) row.getLinkedRow().cloneRow();
278
                                                // Rotamos la geometry
279
                                                AffineTransform at = new AffineTransform();
280
                                                at.rotate(Math.toRadians(d),
281
                                                            firstPoint.getX(), firstPoint.getY());
282
                                                fea.getGeometry().transform(at);
283
                                            int index=vea.modifyRow(row.getIndex(), fea,getName());
284
                                            ///selectedRowAux.add(new DefaultRowEdited(fea,IRowEdited.STATUS_MODIFIED,index));
285
                                }
286
                                vea.endComplexRow();
287
                                clearSelection();
288
                                ///selectedRow=selectedRowAux;
289
                        } catch (DriverIOException e) {
290
                                e.printStackTrace();
291
                        } catch (IOException e1) {
292
                                e1.printStackTrace();
293
                        }
294

    
295
                }
296
    }
297

    
298
        public String getName() {
299
                return PluginServices.getText(this,"rotate_");
300
        }
301
}