Statistics
| Revision:

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

History | View | Annotate | Download (9.77 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.util.ArrayList;
48

    
49
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
50
import com.iver.andami.PluginServices;
51
import com.iver.cit.gvsig.CADExtension;
52
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
53
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileWriteException;
54
import com.iver.cit.gvsig.exceptions.validate.ValidateRowException;
55
import com.iver.cit.gvsig.fmap.ViewPort;
56
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
57
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
58
import com.iver.cit.gvsig.fmap.core.Handler;
59
import com.iver.cit.gvsig.fmap.core.IGeometry;
60
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
61
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
62
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
63
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
64
import com.iver.cit.gvsig.fmap.edition.UtilFunctions;
65
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
66
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
67
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
68
import com.iver.cit.gvsig.gui.cad.tools.smc.SymmetryCADToolContext;
69
import com.iver.cit.gvsig.gui.cad.tools.smc.SymmetryCADToolContext.SymmetryCADToolState;
70
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
71

    
72

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
258
                                }
259

    
260
                                vea.endComplexRow(getName());
261
                                vle.setSelectionCache(VectorialLayerEdited.SAVEPREVIOUS, selectedRowAux);
262
                                //clearSelection();
263
                                //selectedRow.addAll(selectedRowAux);
264
                        } catch (ValidateRowException e) {
265
                                // TODO Auto-generated catch block
266
                                e.printStackTrace();
267
                        } catch (ExpansionFileWriteException e) {
268
                                // TODO Auto-generated catch block
269
                                e.printStackTrace();
270
                        } catch (ReadDriverException e) {
271
                                // TODO Auto-generated catch block
272
                                e.printStackTrace();
273
                        } catch (ExpansionFileReadException e) {
274
                                // TODO Auto-generated catch block
275
                                e.printStackTrace();
276
                        }
277
                        PluginServices.getMDIManager().restoreCursor();
278
                }
279
    }
280

    
281
    /*
282
         * (non-Javadoc)
283
         *
284
         * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
285
         */
286
    public void addValue(double d) {
287

    
288
    }
289

    
290
        public String getName() {
291
                return PluginServices.getText(this,"symmetry_");
292
        }
293

    
294
        public String toString() {
295
                return "_symmetry";
296
        }
297

    
298
}