Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1003 / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / SymmetryCADTool.java @ 12271

History | View | Annotate | Download (9.27 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.DefaultCADTool;
65
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
66
import com.iver.cit.gvsig.gui.cad.tools.smc.SymmetryCADToolContext;
67
import com.iver.cit.gvsig.gui.cad.tools.smc.SymmetryCADToolContext.SymmetryCADToolState;
68
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
69

    
70

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
256
                                }
257

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

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

    
278
    }
279

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

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

    
288
}