Statistics
| Revision:

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

History | View | Annotate | Download (10.9 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 4458 caballero
import java.util.ArrayList;
50 3883 caballero
51 10626 caballero
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
52 3883 caballero
import com.iver.andami.PluginServices;
53 12739 caballero
import com.iver.andami.messages.NotificationManager;
54 3883 caballero
import com.iver.cit.gvsig.CADExtension;
55 10626 caballero
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileWriteException;
56
import com.iver.cit.gvsig.exceptions.validate.ValidateRowException;
57 4637 caballero
import com.iver.cit.gvsig.fmap.ViewPort;
58 3883 caballero
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
59 4584 caballero
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
60 5184 caballero
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
61 5884 caballero
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
62 3904 fjp
import com.iver.cit.gvsig.fmap.edition.UtilFunctions;
63 4637 caballero
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
64 3883 caballero
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
65 5735 caballero
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
66 3883 caballero
import com.iver.cit.gvsig.gui.cad.tools.smc.RotateCADToolContext;
67
import com.iver.cit.gvsig.gui.cad.tools.smc.RotateCADToolContext.RotateCADToolState;
68 4637 caballero
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
69 3883 caballero
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 4313 fjp
    public void transition(double x, double y, InputEvent event) {
99 4324 caballero
        _fsm.addPoint(x, y, event);
100 3883 caballero
    }
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 5735 caballero
    public void transition(String s) throws CommandException {
113 4892 caballero
            if (!super.changeCommand(s)){
114
                    _fsm.addOption(s);
115
            }
116 3883 caballero
    }
117
118
    /**
119
     * DOCUMENT ME!
120
     */
121
    public void selection() {
122 4584 caballero
       ArrayList selectedRows=getSelectedRows();
123
        if (selectedRows.size() == 0 && !CADExtension.getCADTool().getClass().getName().equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
124 5071 caballero
            CADExtension.setCADTool("_selection",false);
125 4584 caballero
            ((SelectionCADTool) CADExtension.getCADTool()).setNextTool(
126 5071 caballero
                "_rotate");
127 3883 caballero
        }
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 4365 caballero
    public void addPoint(double x, double y,InputEvent event) {
138 13868 jaume
            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 3883 caballero
150 13868 jaume
                    double w;
151
                    double h;
152
                    w = lastPoint.getX() - firstPoint.getX();
153
                    h = lastPoint.getY() - firstPoint.getY();
154 3883 caballero
155 13868 jaume
                    try {
156
                            vea.startComplexRow();
157 5884 caballero
                            for (int i = 0; i < selectedRow.size(); i++) {
158 13868 jaume
                                    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 3978 caballero
166 13868 jaume
                                    vea.modifyRow(row.getIndex(), fea,
167
                                                    getName(),EditionEvent.GRAPHIC);
168
                                    selectedRowAux.add(new DefaultRowEdited(fea,IRowEdited.STATUS_MODIFIED,row.getIndex()));
169
                            }
170 4637 caballero
171 13868 jaume
                            vea.endComplexRow(getName());
172
                            vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS, selectedRowAux);
173
                            //clearSelection();
174
                            //selectedRow.addAll(selectedRowAux);
175
                    } catch (ValidateRowException e) {
176
                            NotificationManager.addError(e.getMessage(),e);
177
                    } catch (ExpansionFileWriteException e) {
178
                            NotificationManager.addError(e.getMessage(),e);
179
                    } catch (ReadDriverException e) {
180
                            NotificationManager.addError(e.getMessage(),e);
181
                    }
182
                    PluginServices.getMDIManager().restoreCursor();
183
            }
184 3883 caballero
    }
185
186
    /**
187
     * M?todo para dibujar la lo necesario para el estado en el que nos
188
     * encontremos.
189
     *
190
     * @param g Graphics sobre el que dibujar.
191
     * @param x par?metro x del punto que se pase para dibujar.
192
     * @param y par?metro x del punto que se pase para dibujar.
193
     */
194
    public void drawOperation(Graphics g, double x, double y) {
195 9121 caballero
        RotateCADToolState actualState = _fsm.getState();
196 3883 caballero
        String status = actualState.getName();
197 4637 caballero
        VectorialLayerEdited vle=getVLE();
198
        //ArrayList selectedRow=getSelectedRows();
199 3883 caballero
200 4637 caballero
       // drawHandlers(g, selectedRow,
201
       //          getCadToolAdapter().getMapControl().getViewPort()
202
       //              .getAffineTransform());
203 3978 caballero
        if (status.equals("Rotate.AngleOrPoint")) {
204 3883 caballero
                        double w;
205
                        double h;
206
                        w = x - firstPoint.getX();
207
                        h = y - firstPoint.getY();
208 6881 cesar
                        ViewPort vp=vle.getLayer().getMapContext().getViewPort();
209 4637 caballero
                        Point2D point = vp.fromMapPoint(firstPoint.getX(),
210
                                                                firstPoint.getY());
211
                        AffineTransform at = AffineTransform.getRotateInstance(Math.atan2(
212
                                                                w, h) - (Math.PI / 2), (int) point.getX(),
213
                                                        (int) point.getY());
214 3883 caballero
215 4637 caballero
                        Image imgSel = vle.getSelectionImage();
216
            ((Graphics2D)g).drawImage(imgSel, at, null);
217 11437 caballero
            drawLine((Graphics2D) g, firstPoint, new Point2D.Double(x, y),DefaultCADTool.axisReferencesSymbol);
218 3883 caballero
                        ///AffineTransform at = AffineTransform.getRotateInstance(+Math.atan2(
219
                        ///                        w, h) - (Math.PI / 2), (int) point.getX(),
220
                        ///                (int) point.getY());
221
                        ///Image img = getCadToolAdapter().getVectorialAdapter().getImage();
222
223
                        ///((Graphics2D) g).drawImage(img, at, null);
224
225
                        ///drawLine((Graphics2D) g, firstPoint, new Point2D.Double(x, y));
226
227 4637 caballero
/*
228 4584 caballero
                             for (int i = 0; i < selectedRow.size(); i++) {
229
                                           // IGeometry geometry =
230
                                                                // getCadToolAdapter().getVectorialAdapter().getShape(i);
231
                                IRowEdited edRow = (IRowEdited) selectedRow.get(i);
232
                                IFeature feat = (IFeature) edRow.getLinkedRow();
233
                                IGeometry geometry = feat.getGeometry().cloneGeometry();
234
                                // Rotamos la geometry
235
                                UtilFunctions.rotateGeom(geometry, -Math.atan2(w, h) + Math.PI
236
                                                / 2, firstPoint.getX(), firstPoint.getY());
237 3883 caballero

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

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