Statistics
| Revision:

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

History | View | Annotate | Download (10.9 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.RectangleCADToolContext;
53
import org.gvsig.editing.gui.cad.tools.smc.RectangleCADToolContext.RectangleCADToolState;
54
import org.gvsig.editing.layers.VectorialLayerEdited;
55
import org.gvsig.fmap.geom.Geometry;
56
import org.gvsig.fmap.geom.operation.Draw;
57
import org.gvsig.fmap.geom.operation.DrawOperationContext;
58
import org.gvsig.fmap.geom.operation.GeometryOperationException;
59
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
60
import org.gvsig.fmap.geom.primitive.GeneralPathX;
61

    
62

    
63

    
64
/**
65
 * DOCUMENT ME!
66
 *
67
 * @author Vicente Caballero Navarro
68
 */
69
public class RectangleCADTool extends DefaultCADTool {
70
    protected RectangleCADToolContext _fsm;
71
    protected Point2D firstPoint;
72
    protected Point2D lastPoint;
73

    
74
    /**
75
     * Crea un nuevo LineCADTool.
76
     */
77
    public RectangleCADTool() {
78

    
79
    }
80

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

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

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

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

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

    
123
        String status = actualState.getName();
124

    
125
        if (status.equals("Rectangle.FirstPoint")) {
126
            firstPoint = new Point2D.Double(x, y);
127
        } else if (status == "Rectangle.SecondPointOrSquare") {
128
            lastPoint = new Point2D.Double(x, y);
129

    
130
            GeneralPathX elShape = new GeneralPathX();
131
            elShape.moveTo(firstPoint.getX(), firstPoint.getY());
132
            elShape.lineTo(lastPoint.getX(), firstPoint.getY());
133
            elShape.lineTo(lastPoint.getX(), lastPoint.getY());
134
            elShape.lineTo(firstPoint.getX(), lastPoint.getY());
135
            //elShape.lineTo(firstPoint.getX(), firstPoint.getY());
136
            elShape.closePath();
137
            int type=getCadToolAdapter().getActiveLayerType();
138
            if (type==Geometry.TYPES.SURFACE){
139
                    insertAndSelectGeometry(createSurface(elShape));
140
            }else{
141
                    insertAndSelectGeometry(createCurve(elShape));
142
            }
143
            firstPoint = (Point2D) lastPoint.clone();
144
        } else if (status == "Rectangle.SecondPointSquare") {
145
            lastPoint = new Point2D.Double(x, y);
146

    
147
            GeneralPathX elShape = new GeneralPathX();
148
            elShape.moveTo(firstPoint.getX(), firstPoint.getY());
149
            elShape.lineTo(lastPoint.getX(), firstPoint.getY());
150

    
151
            if (((lastPoint.getY() <= firstPoint.getY()) &&
152
                    (lastPoint.getX() <= firstPoint.getX())) ||
153
                    ((lastPoint.getY() > firstPoint.getY()) &&
154
                    (lastPoint.getX() > firstPoint.getX()))) {
155
                elShape.lineTo(lastPoint.getX(),
156
                    firstPoint.getY() + (lastPoint.getX() - firstPoint.getX()));
157
                elShape.lineTo(firstPoint.getX(),
158
                    firstPoint.getY() + (lastPoint.getX() - firstPoint.getX()));
159
            } else {
160
                elShape.lineTo(lastPoint.getX(),
161
                    firstPoint.getY() - (lastPoint.getX() - firstPoint.getX()));
162
                elShape.lineTo(firstPoint.getX(),
163
                    firstPoint.getY() - (lastPoint.getX() - firstPoint.getX()));
164
            }
165

    
166
            //elShape.lineTo(firstPoint.getX(), firstPoint.getY());
167
            elShape.closePath();
168
            int type=getCadToolAdapter().getActiveLayerType();
169
            if (type==Geometry.TYPES.SURFACE){
170
                    insertAndSelectGeometry(createSurface(elShape));
171
            }else{
172
                    insertAndSelectGeometry(createCurve(elShape));
173
            }
174
            firstPoint = (Point2D) lastPoint.clone();
175
        }
176
    }
177

    
178
    /**
179
     * M?todo para dibujar la lo necesario para el estado en el que nos
180
     * encontremos.
181
     *
182
     * @param g Graphics sobre el que dibujar.
183
     * @param selectedGeometries BitSet con las geometr?as seleccionadas.
184
     * @param x par?metro x del punto que se pase para dibujar.
185
     * @param y par?metro x del punto que se pase para dibujar.
186
     */
187
    public void drawOperation(Graphics g, double x,
188
        double y) {
189
        RectangleCADToolState actualState = _fsm.getState();
190
        String status = actualState.getName();
191

    
192
        if (status == "Rectangle.SecondPointOrSquare") {
193
            GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
194
                    4);
195
            elShape.moveTo(firstPoint.getX(), firstPoint.getY());
196
            elShape.lineTo(x, firstPoint.getY());
197
            elShape.lineTo(x, y);
198
            elShape.lineTo(firstPoint.getX(), y);
199
            elShape.lineTo(firstPoint.getX(), firstPoint.getY());
200
            DrawOperationContext doc=new DrawOperationContext();
201
                        doc.setGraphics((Graphics2D)g);
202
                        doc.setViewPort(getCadToolAdapter().getMapControl().getViewPort());
203
                        doc.setSymbol(DefaultCADTool.geometrySelectSymbol);
204
                try {
205
                        createCurve(elShape).cloneGeometry().invokeOperation(Draw.CODE,doc);
206
                        } catch (GeometryOperationNotSupportedException e) {
207
                                e.printStackTrace();
208
                        } catch (GeometryOperationException e) {
209
                                e.printStackTrace();
210
                        }
211
//            geomFactory.createPolyline2D(elShape).draw((Graphics2D) g,
212
//                getCadToolAdapter().getMapControl().getViewPort(),
213
//                DefaultCADTool.geometrySelectSymbol);
214
        } else if (status == "Rectangle.SecondPointSquare") {
215
            GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
216
                    4);
217
            elShape.moveTo(firstPoint.getX(), firstPoint.getY());
218
            elShape.lineTo(x, firstPoint.getY());
219

    
220
            if (((y <= firstPoint.getY()) && (x <= firstPoint.getX())) ||
221
                    ((y > firstPoint.getY()) && (x > firstPoint.getX()))) {
222
                elShape.lineTo(x, firstPoint.getY() + (x - firstPoint.getX()));
223
                elShape.lineTo(firstPoint.getX(),
224
                    firstPoint.getY() + (x - firstPoint.getX()));
225
                elShape.lineTo(firstPoint.getX(), firstPoint.getY());
226
            } else {
227
                elShape.lineTo(x, firstPoint.getY() - (x - firstPoint.getX()));
228
                elShape.lineTo(firstPoint.getX(),
229
                    firstPoint.getY() - (x - firstPoint.getX()));
230
                elShape.lineTo(firstPoint.getX(), firstPoint.getY());
231
            }
232
            DrawOperationContext doc=new DrawOperationContext();
233
                        doc.setGraphics((Graphics2D)g);
234
                        doc.setViewPort(getCadToolAdapter().getMapControl().getViewPort());
235
                        doc.setSymbol(DefaultCADTool.geometrySelectSymbol);
236
                try {
237
                        createCurve(elShape).cloneGeometry().invokeOperation(Draw.CODE,doc);
238
                        } catch (GeometryOperationNotSupportedException e) {
239
                                e.printStackTrace();
240
                        } catch (GeometryOperationException e) {
241
                                e.printStackTrace();
242
                        }
243
//            geomFactory.createPolyline2D(elShape).draw((Graphics2D) g,
244
//                getCadToolAdapter().getMapControl().getViewPort(),
245
//                DefaultCADTool.geometrySelectSymbol);
246
        }else{
247
                VectorialLayerEdited vle=getVLE();
248
                if (!vle.getLayer().isVisible())
249
                        return;
250
                try{
251
                        Image imgSel = vle.getSelectionImage();
252
                        if (imgSel!=null)
253
                                g.drawImage(imgSel, 0, 0, null);
254
                        Image imgHand = vle.getHandlersImage();
255
                        if (imgHand!=null)
256
                                g.drawImage(imgHand, 0, 0, null);
257
                }catch (Exception e) {
258
                }
259
        }
260
    }
261

    
262
    /**
263
     * Add a diferent option.
264
     *
265
     * @param sel DOCUMENT ME!
266
     * @param s Diferent option.
267
     */
268
    public void addOption(String s) {
269
        RectangleCADToolState actualState = (RectangleCADToolState) _fsm.getPreviousState();
270
        String status = actualState.getName();
271

    
272
        if (status == "Rectangle.SecondPointOrSquare") {
273
            if (s.equalsIgnoreCase(PluginServices.getText(this,"RectangleCADTool.square"))) {
274
                //Opci?n correcta
275
            }
276
        }
277
    }
278

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

    
285
        public String getName() {
286
                return PluginServices.getText(this,"rectangle_");
287
        }
288

    
289
        public String toString() {
290
                return "_rectangle";
291
        }
292
        public boolean isApplicable(int shapeType) {
293
                switch (shapeType) {
294
                case Geometry.TYPES.POINT:
295
                case Geometry.TYPES.MULTIPOINT:
296
                        return false;
297
                }
298
                return true;
299
        }
300
}