Statistics
| Revision:

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

History | View | Annotate | Download (9.76 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.Image;
44
import java.awt.event.InputEvent;
45
import java.awt.geom.Point2D;
46

    
47
import org.gvsig.andami.PluginServices;
48
import org.gvsig.editing.gui.cad.DefaultCADTool;
49
import org.gvsig.editing.gui.cad.exception.CommandException;
50
import org.gvsig.editing.gui.cad.tools.smc.RectangleCADToolContext;
51
import org.gvsig.editing.gui.cad.tools.smc.RectangleCADToolContext.RectangleCADToolState;
52
import org.gvsig.editing.layers.VectorialLayerEdited;
53
import org.gvsig.fmap.geom.Geometry;
54
import org.gvsig.fmap.geom.operation.Draw;
55
import org.gvsig.fmap.geom.operation.DrawOperationContext;
56
import org.gvsig.fmap.geom.operation.GeometryOperationException;
57
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
58
import org.gvsig.fmap.geom.primitive.GeneralPathX;
59
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
60

    
61

    
62

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

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

    
78
    }
79

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

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

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

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

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

    
122
        String status = actualState.getName();
123

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

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

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

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

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

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

    
191
        if (status == "Rectangle.SecondPointOrSquare") {
192
            GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
193
                    4);
194
            elShape.moveTo(firstPoint.getX(), firstPoint.getY());
195
            elShape.lineTo(x, firstPoint.getY());
196
            elShape.lineTo(x, y);
197
            elShape.lineTo(firstPoint.getX(), y);
198
            elShape.lineTo(firstPoint.getX(), firstPoint.getY());
199
       
200
                renderer.draw(createCurve(elShape), mapControlManager.getGeometrySelectionSymbol());
201
        } else if (status == "Rectangle.SecondPointSquare") {
202
            GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
203
                    4);
204
            elShape.moveTo(firstPoint.getX(), firstPoint.getY());
205
            elShape.lineTo(x, firstPoint.getY());
206

    
207
            if (((y <= firstPoint.getY()) && (x <= firstPoint.getX())) ||
208
                    ((y > firstPoint.getY()) && (x > firstPoint.getX()))) {
209
                elShape.lineTo(x, firstPoint.getY() + (x - firstPoint.getX()));
210
                elShape.lineTo(firstPoint.getX(),
211
                    firstPoint.getY() + (x - firstPoint.getX()));
212
                elShape.lineTo(firstPoint.getX(), firstPoint.getY());
213
            } else {
214
                elShape.lineTo(x, firstPoint.getY() - (x - firstPoint.getX()));
215
                elShape.lineTo(firstPoint.getX(),
216
                    firstPoint.getY() - (x - firstPoint.getX()));
217
                elShape.lineTo(firstPoint.getX(), firstPoint.getY());
218
            }
219
                       renderer.draw(createCurve(elShape), mapControlManager.getGeometrySelectionSymbol());
220
        }else{
221
                VectorialLayerEdited vle=getVLE();
222
                if (!vle.getLayer().isVisible())
223
                        return;
224
                try{
225
                        Image imgSel = vle.getSelectionImage();
226
                        renderer.drawImage(imgSel, 0, 0);
227
                        
228
                        Image imgHand = vle.getHandlersImage();
229
                        renderer.drawImage(imgHand, 0, 0);
230
                }catch (Exception e) {
231
                }
232
        }
233
    }
234

    
235
    /**
236
     * Add a diferent option.
237
     *
238
     * @param sel DOCUMENT ME!
239
     * @param s Diferent option.
240
     */
241
    public void addOption(String s) {
242
        RectangleCADToolState actualState = (RectangleCADToolState) _fsm.getPreviousState();
243
        String status = actualState.getName();
244

    
245
        if (status == "Rectangle.SecondPointOrSquare") {
246
            if (s.equalsIgnoreCase(PluginServices.getText(this,"RectangleCADTool.square"))) {
247
                //Opci?n correcta
248
            }
249
        }
250
    }
251

    
252
    /* (non-Javadoc)
253
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
254
     */
255
    public void addValue(double d) {
256
    }
257

    
258
        public String getName() {
259
                return PluginServices.getText(this,"rectangle_");
260
        }
261

    
262
        public String toString() {
263
                return "_rectangle";
264
        }
265
        public boolean isApplicable(int shapeType) {
266
                switch (shapeType) {
267
                case Geometry.TYPES.POINT:
268
                case Geometry.TYPES.MULTIPOINT:
269
                        return false;
270
                }
271
                return true;
272
        }
273
}