Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / CircleCADTool.java @ 29616

History | View | Annotate | Download (8.61 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 org.gvsig.editing.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.Point2D;
48

    
49
import org.gvsig.andami.PluginServices;
50
import org.gvsig.editing.gui.cad.DefaultCADTool;
51
import org.gvsig.editing.gui.cad.exception.CommandException;
52
import org.gvsig.editing.gui.cad.tools.smc.CircleCADToolContext;
53
import org.gvsig.editing.gui.cad.tools.smc.CircleCADToolContext.CircleCADToolState;
54
import org.gvsig.editing.layers.VectorialLayerEdited;
55
import org.gvsig.fmap.dal.exception.DataException;
56
import org.gvsig.fmap.dal.feature.FeatureSelection;
57
import org.gvsig.fmap.dal.feature.FeatureStore;
58
import org.gvsig.fmap.geom.Geometry;
59
import org.gvsig.fmap.geom.operation.Draw;
60
import org.gvsig.fmap.geom.operation.DrawOperationContext;
61
import org.gvsig.fmap.geom.operation.GeometryOperationException;
62
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
63

    
64

    
65

    
66
/**
67
 * DOCUMENT ME!
68
 *
69
 * @author Vicente Caballero Navarro
70
 */
71
public class CircleCADTool extends DefaultCADTool {
72
    protected CircleCADToolContext _fsm;
73
    protected Point2D center;
74
    protected Point2D firstPoint;
75
    protected Point2D secondPoint;
76
    protected Point2D thirdPoint;
77

    
78
    /**
79
     * Crea un nuevo LineCADTool.
80
     */
81
    public CircleCADTool() {
82
    }
83

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

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

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

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

    
115
    /**
116
     * Equivale al transition del prototipo pero sin pasarle como par? metro el
117
     * editableFeatureSource que ya estar? creado.
118
     *
119
     * @param sel Bitset con las geometr?as que est?n seleccionadas.
120
     * @param x par?metro x del punto que se pase en esta transici?n.
121
     * @param y par?metro y del punto que se pase en esta transici?n.
122
     */
123
    public void addPoint(double x, double y,InputEvent event) {
124
        CircleCADToolState actualState = (CircleCADToolState) _fsm.getPreviousState();
125
        String status = actualState.getName();
126

    
127
        if (status.equals("Circle.CenterPointOr3p")) {
128
            center = new Point2D.Double(x, y);
129
        } else if (status == "Circle.PointOrRadius") {
130
                insertAndSelectGeometry(createCircle(createPoint(center),
131
                                createPoint(x, y)));
132
        } else if (status == "Circle.FirstPoint") {
133
            firstPoint = new Point2D.Double(x, y);
134
        } else if (status == "Circle.SecondPoint") {
135
            secondPoint = new Point2D.Double(x, y);
136
        } else if (status == "Circle.ThirdPoint") {
137
                thirdPoint = new Point2D.Double(x, y);
138
                insertAndSelectGeometry(createCircle(createPoint(firstPoint),
139
                                createPoint(secondPoint),
140
                                createPoint(thirdPoint)));
141
        }
142
    }
143

    
144
    /**
145
     * M?todo para dibujar la lo necesario para el estado en el que nos
146
     * encontremos.
147
     *
148
     * @param g Graphics sobre el que dibujar.
149
     * @param selectedGeometries BitSet con las geometr?as seleccionadas.
150
     * @param x par?metro x del punto que se pase para dibujar.
151
     * @param y par?metro x del punto que se pase para dibujar.
152
     */
153
    public void drawOperation(Graphics g,double x,
154
        double y) {
155
        CircleCADToolState actualState = _fsm.getState();
156
        String status = actualState.getName();
157

    
158
        if ((status == "Circle.CenterPointOr3p")) { // || (status == "5")) {
159

    
160
            if (firstPoint != null) {
161
                drawLine((Graphics2D) g, firstPoint, new Point2D.Double(x, y),DefaultCADTool.geometrySelectSymbol);
162
            }
163
        }
164

    
165
        if (status == "Circle.PointOrRadius") {
166
            Point2D currentPoint = new Point2D.Double(x, y);
167
            DrawOperationContext doc=new DrawOperationContext();
168
                        doc.setGraphics((Graphics2D)g);
169
                        doc.setViewPort(getCadToolAdapter().getMapControl().getViewPort());
170
                        doc.setSymbol(DefaultCADTool.axisReferencesSymbol);
171
                try {
172
                        createCircle(createPoint(center), createPoint(currentPoint)).invokeOperation(Draw.CODE,doc);
173
                        } catch (GeometryOperationNotSupportedException e) {
174
                                e.printStackTrace();
175
                        } catch (GeometryOperationException e) {
176
                                e.printStackTrace();
177
                        }
178
        } else if (status == "Circle.SecondPoint") {
179
            drawLine((Graphics2D) g, firstPoint, new Point2D.Double(x, y),DefaultCADTool.geometrySelectSymbol);
180
        } else if (status == "Circle.ThirdPoint") {
181
            Point2D currentPoint = new Point2D.Double(x, y);
182
            Geometry geom = createCircle(createPoint(firstPoint),
183
                            createPoint(secondPoint),
184
                    createPoint(currentPoint));
185

    
186
            if (geom != null) {
187
                    DrawOperationContext doc=new DrawOperationContext();
188
                            doc.setGraphics((Graphics2D)g);
189
                            doc.setViewPort(getCadToolAdapter().getMapControl().getViewPort());
190
                            doc.setSymbol(DefaultCADTool.axisReferencesSymbol);
191
                    try {
192
                                        geom.invokeOperation(Draw.CODE,doc);
193
                                } catch (GeometryOperationNotSupportedException e) {
194
                                        e.printStackTrace();
195
                                } catch (GeometryOperationException e) {
196
                                        e.printStackTrace();
197
                                }
198
            }
199
        }else{
200
                VectorialLayerEdited vle=getVLE();
201
                if (!vle.getLayer().isVisible())
202
                        return;
203
                try{
204
                        Image imgSel = vle.getSelectionImage();
205
                        if (imgSel!=null)
206
                                g.drawImage(imgSel, 0, 0, null);
207
                        Image imgHand = vle.getHandlersImage();
208
                        if (imgHand!=null)
209
                                g.drawImage(imgHand, 0, 0, null);
210
                }catch (Exception e) {
211
                }
212
        }
213
    }
214

    
215
    /**
216
     * Add a diferent option.
217
     *
218
     * @param sel DOCUMENT ME!
219
     * @param s Diferent option.
220
     */
221
    public void addOption(String s) {
222
        CircleCADToolState actualState = (CircleCADToolState) _fsm.getPreviousState();
223
        String status = actualState.getName();
224

    
225
        if (status == "Circle.CenterPointOr3p") {
226
            if (s.equalsIgnoreCase(PluginServices.getText(this,"CircleCADTool.3p"))) {
227
                //Opci?n correcta.
228
            }
229
        }
230
    }
231

    
232
    /* (non-Javadoc)
233
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
234
     */
235
    public void addValue(double d) {
236
        CircleCADToolState actualState = (CircleCADToolState) _fsm.getPreviousState();
237
        String status = actualState.getName();
238

    
239
        if (status == "Circle.PointOrRadius") {
240
                insertAndSelectGeometry(createCircle(createPoint(center), d));
241
        }
242
    }
243

    
244
        public String getName() {
245
                return PluginServices.getText(this,"circle_");
246
        }
247

    
248
        public String toString() {
249
                return "_circle";
250
        }
251

    
252
        public boolean isApplicable(int shapeType) {
253
                switch (shapeType) {
254
                case Geometry.TYPES.POINT:
255
                case Geometry.TYPES.MULTIPOINT:
256
                        return false;
257
                }
258
                return true;
259
        }
260

    
261
}