Statistics
| Revision:

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

History | View | Annotate | Download (9.17 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.io.IOException;
48
import java.util.ArrayList;
49

    
50
import com.iver.andami.PluginServices;
51
import com.iver.cit.gvsig.CADExtension;
52
import com.iver.cit.gvsig.fmap.ViewPort;
53
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
54
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
55
import com.iver.cit.gvsig.fmap.core.Handler;
56
import com.iver.cit.gvsig.fmap.core.IGeometry;
57
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
58
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
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.CADTool;
65
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
66
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
67
import com.iver.cit.gvsig.gui.cad.tools.smc.SymmetryCADToolContext;
68
import com.iver.cit.gvsig.gui.cad.tools.smc.SymmetryCADToolContext.SymmetryCADToolState;
69
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
70

    
71

    
72
/**
73
 * Herramienta para crear una geometr?a sim?trica a otra, con la posibilidad de
74
 * borrar la original.
75
 *
76
 * @author Vicente Caballero Navarro
77
 */
78
public class SymmetryCADTool extends DefaultCADTool {
79
    private SymmetryCADToolContext _fsm;
80
    private Point2D firstPoint;
81
    private Point2D secondPoint;
82

    
83
    /**
84
     * Crea un nuevo SymmetryCADTool.
85
     */
86
    public SymmetryCADTool() {
87
    }
88

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

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

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

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

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

    
132
    /**
133
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
134
     * editableFeatureSource que ya estar? creado.
135
     *
136
     * @param x par?metro x del punto que se pase en esta transici?n.
137
     * @param y par?metro y del punto que se pase en esta transici?n.
138
     */
139
    public void addPoint(double x, double y,InputEvent event) {
140
        SymmetryCADToolState actualState = (SymmetryCADToolState) _fsm.getPreviousState();
141
        String status = actualState.getName();
142
        if (status.equals("Symmetry.FirstPoint")) {
143
                firstPoint = new Point2D.Double(x, y);
144
            } else if (status.equals("Symmetry.SecondPoint")) {
145
                    secondPoint = new Point2D.Double(x,y);
146
            }
147
    }
148

    
149
    /**
150
     * M?todo para dibujar la lo necesario para el estado en el que nos
151
     * encontremos.
152
     *
153
     * @param g Graphics sobre el que dibujar.
154
     * @param x par?metro x del punto que se pase para dibujar.
155
     * @param y par?metro x del punto que se pase para dibujar.
156
     */
157
    public void drawOperation(Graphics g, double x, double y) {
158
        SymmetryCADToolState actualState = ((SymmetryCADToolContext) _fsm).getState();
159
        String status = actualState.getName();
160

    
161
        if (status.equals("Symmetry.SecondPoint")) {
162
                Point2D pAux=new Point2D.Double(x,y);
163
                drawSymmetry(g,pAux);
164
                }else if (status.equals("Symmetry.CutOrCopy")){
165
                        drawSymmetry(g,secondPoint);
166
                }
167
    }
168
private void drawSymmetry(Graphics g,Point2D pAux) {
169
        ArrayList selectedRow=getSelectedRows();
170
    VectorialLayerEdited vle=getVLE();
171
        ViewPort vp=vle.getLayer().getFMap().getViewPort();
172

    
173
        GeneralPathX gpx=new GeneralPathX();
174
        gpx.moveTo(firstPoint.getX(),firstPoint.getY());
175
        gpx.lineTo(pAux.getX(),pAux.getY());
176
        ShapeFactory.createPolyline2D(gpx).draw((Graphics2D)g,vp,CADTool.modifySymbol);
177
        for (int i = 0; i < selectedRow.size(); i++) {
178
                DefaultRowEdited row=(DefaultRowEdited) selectedRow.get(i);
179
                DefaultFeature fea = (DefaultFeature) row.getLinkedRow();
180

    
181
                IGeometry geom = fea.getGeometry().cloneGeometry();
182
                Handler[] handlers = geom.getHandlers(IGeometry.SELECTHANDLER);
183

    
184
                        for (int j = 0; j < handlers.length; j++) {
185
                                Handler h = (Handler) handlers[j];
186
                                Point2D p = h.getPoint();
187
                                Point2D[] ps = UtilFunctions.getPerpendicular(firstPoint,
188
                                                pAux, p);
189
                                Point2D inter = UtilFunctions.getIntersection(ps[0],
190
                                                ps[1], firstPoint, pAux);
191
                                if (inter!=null) {
192
                                        Point2D dif = new Point2D.Double(inter.getX() -
193
                                                        p.getX(), inter.getY() - p.getY());
194
                                        h.set(inter.getX() + dif.getX(),
195
                                                        inter.getY() + dif.getY());
196
                                }
197
                        }
198
                        geom.draw((Graphics2D)g,vp,CADTool.drawingSymbol);
199
        }
200
}
201

    
202
    /**
203
         * Add a diferent option.
204
         *
205
         * @param s
206
         *            Diferent option.
207
         */
208
    public void addOption(String s) {
209
             SymmetryCADToolState actualState = (SymmetryCADToolState) _fsm
210
                                .getPreviousState();
211
                String status = actualState.getName();
212
                ArrayList selectedRow = getSelectedRows();
213
                ArrayList selectedRowAux=new ArrayList();
214
                VectorialLayerEdited vle = getVLE();
215
                VectorialEditableAdapter vea = vle.getVEA();
216
                if (status.equals("Symmetry.CutOrCopy")) {
217
                        PluginServices.getMDIManager().setWaitCursor();
218
                        try {
219
                                vea.startComplexRow();
220
                                for (int i = 0; i < selectedRow.size(); i++) {
221
                                        DefaultRowEdited row = (DefaultRowEdited) selectedRow
222
                                                        .get(i);
223
                                        DefaultFeature fea = (DefaultFeature) row.getLinkedRow()
224
                                                        .cloneRow();
225

    
226
                                        IGeometry geom = fea.getGeometry();
227
                                        Handler[] handlers = geom
228
                                                        .getHandlers(IGeometry.SELECTHANDLER);
229

    
230
                                        for (int j = 0; j < handlers.length; j++) {
231
                                                Handler h = (Handler) handlers[j];
232
                                                Point2D p = h.getPoint();
233
                                                Point2D[] ps = UtilFunctions.getPerpendicular(
234
                                                                firstPoint, secondPoint, p);
235
                                                Point2D inter = UtilFunctions.getIntersection(ps[0],
236
                                                                ps[1], firstPoint, secondPoint);
237
                                                Point2D dif = new Point2D.Double(inter.getX()
238
                                                                - p.getX(), inter.getY() - p.getY());
239
                                                h.set(inter.getX() + dif.getX(), inter.getY()
240
                                                                + dif.getY());
241
                                        }
242

    
243
                                        if (s.equals(PluginServices.getText(this, "cut"))
244
                                                        || s.equals("s") || s.equals("S")) {
245
                                                vea.modifyRow(row.getIndex(), fea,
246
                                                                getName(), EditionEvent.GRAPHIC);
247

    
248
                                                selectedRowAux.add(new DefaultRowEdited(fea,
249
                                                                IRowEdited.STATUS_MODIFIED, row.getIndex()));
250
                                        } else {
251
                                                int index=addGeometry(geom,fea.getAttributes());
252
                                                selectedRowAux.add(new DefaultRowEdited(fea,
253
                                                                IRowEdited.STATUS_ADDED, index));
254
                                        }
255

    
256
                                }
257

    
258
                                vea.endComplexRow();
259
                                clearSelection();
260
                                selectedRow.addAll(selectedRowAux);
261
                        } catch (DriverIOException e) {
262
                                e.printStackTrace();
263
                        } catch (IOException e1) {
264
                                e1.printStackTrace();
265
                        }
266
                        PluginServices.getMDIManager().restoreCursor();
267
                }
268
    }
269

    
270
    /*
271
         * (non-Javadoc)
272
         *
273
         * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
274
         */
275
    public void addValue(double d) {
276

    
277
    }
278

    
279
        public String getName() {
280
                return PluginServices.getText(this,"symmetry_");
281
        }
282

    
283
        public String toString() {
284
                return "_symmetry";
285
        }
286

    
287
}