Statistics
| Revision:

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

History | View | Annotate | Download (8.47 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

    
48
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
49
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
50
import com.iver.cit.gvsig.gui.cad.CADTool;
51
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
52
import com.iver.cit.gvsig.gui.cad.tools.smc.RectangleCADToolContext;
53
import com.iver.cit.gvsig.gui.cad.tools.smc.RectangleCADToolContext.RectangleCADToolState;
54

    
55

    
56
/**
57
 * DOCUMENT ME!
58
 *
59
 * @author Vicente Caballero Navarro
60
 */
61
public class RectangleCADTool extends DefaultCADTool {
62
    private RectangleCADToolContext _fsm;
63
    private Point2D firstPoint;
64
    private Point2D lastPoint;
65

    
66
    /**
67
     * Crea un nuevo LineCADTool.
68
     */
69
    public RectangleCADTool() {
70

    
71
    }
72

    
73
    /**
74
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
75
     * carga previa a la utilizaci?n de la herramienta.
76
     */
77
    public void init() {
78
            _fsm = new RectangleCADToolContext(this);
79
    }
80

    
81
    /* (non-Javadoc)
82
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
83
     */
84
    public void transition(double x, double y, InputEvent event) {
85
        _fsm.addPoint(x, y, event);
86
    }
87

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

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

    
102
    /**
103
     * Equivale al transition del prototipo pero sin pasarle como par? metro el
104
     * editableFeatureSource que ya estar? creado.
105
     *
106
     * @param sel Bitset con las geometr?as que est?n seleccionadas.
107
     * @param x par?metro x del punto que se pase en esta transici?n.
108
     * @param y par?metro y del punto que se pase en esta transici?n.
109
     */
110
    public void addPoint(double x, double y,InputEvent event) {
111
        RectangleCADToolState actualState = (RectangleCADToolState) _fsm.getPreviousState();
112

    
113
        String status = actualState.getName();
114

    
115
        if (status.equals("Rectangle.FirstPoint")) {
116
            firstPoint = new Point2D.Double(x, y);
117
        } else if (status == "Rectangle.SecondPointOrSquare") {
118
            lastPoint = new Point2D.Double(x, y);
119

    
120
            GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
121
                    2);
122
            elShape.moveTo(firstPoint.getX(), firstPoint.getY());
123
            elShape.lineTo(lastPoint.getX(), firstPoint.getY());
124
            elShape.lineTo(lastPoint.getX(), lastPoint.getY());
125
            elShape.lineTo(firstPoint.getX(), lastPoint.getY());
126
            elShape.lineTo(firstPoint.getX(), firstPoint.getY());
127
            addGeometry(ShapeFactory.createPolyline2D(elShape));
128
            firstPoint = (Point2D) lastPoint.clone();
129
        } else if (status == "Rectangle.SecondPointSquare") {
130
            lastPoint = new Point2D.Double(x, y);
131

    
132
            GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
133
                    2);
134
            elShape.moveTo(firstPoint.getX(), firstPoint.getY());
135
            elShape.lineTo(lastPoint.getX(), firstPoint.getY());
136

    
137
            if (((lastPoint.getY() <= firstPoint.getY()) &&
138
                    (lastPoint.getX() <= firstPoint.getX())) ||
139
                    ((lastPoint.getY() > firstPoint.getY()) &&
140
                    (lastPoint.getX() > firstPoint.getX()))) {
141
                elShape.lineTo(lastPoint.getX(),
142
                    firstPoint.getY() + (lastPoint.getX() - firstPoint.getX()));
143
                elShape.lineTo(firstPoint.getX(),
144
                    firstPoint.getY() + (lastPoint.getX() - firstPoint.getX()));
145
            } else {
146
                elShape.lineTo(lastPoint.getX(),
147
                    firstPoint.getY() - (lastPoint.getX() - firstPoint.getX()));
148
                elShape.lineTo(firstPoint.getX(),
149
                    firstPoint.getY() - (lastPoint.getX() - firstPoint.getX()));
150
            }
151

    
152
            elShape.lineTo(firstPoint.getX(), firstPoint.getY());
153
            addGeometry(ShapeFactory.createPolyline2D(elShape));
154
            firstPoint = (Point2D) lastPoint.clone();
155
        }
156
    }
157

    
158
    /**
159
     * M?todo para dibujar la lo necesario para el estado en el que nos
160
     * encontremos.
161
     *
162
     * @param g Graphics sobre el que dibujar.
163
     * @param selectedGeometries BitSet con las geometr?as seleccionadas.
164
     * @param x par?metro x del punto que se pase para dibujar.
165
     * @param y par?metro x del punto que se pase para dibujar.
166
     */
167
    public void drawOperation(Graphics g, double x,
168
        double y) {
169
        RectangleCADToolState actualState = _fsm.getState();
170
        String status = actualState.getName();
171

    
172
        if (status == "Rectangle.SecondPointOrSquare") {
173
            GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
174
                    4);
175
            elShape.moveTo(firstPoint.getX(), firstPoint.getY());
176
            elShape.lineTo(x, firstPoint.getY());
177
            elShape.lineTo(x, y);
178
            elShape.lineTo(firstPoint.getX(), y);
179
            elShape.lineTo(firstPoint.getX(), firstPoint.getY());
180
            ShapeFactory.createPolyline2D(elShape).draw((Graphics2D) g,
181
                getCadToolAdapter().getMapControl().getViewPort(),
182
                CADTool.drawingSymbol);
183
        } else if (status == "Rectangle.SecondPointSquare") {
184
            GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
185
                    4);
186
            elShape.moveTo(firstPoint.getX(), firstPoint.getY());
187
            elShape.lineTo(x, firstPoint.getY());
188

    
189
            if (((y <= firstPoint.getY()) && (x <= firstPoint.getX())) ||
190
                    ((y > firstPoint.getY()) && (x > firstPoint.getX()))) {
191
                elShape.lineTo(x, firstPoint.getY() + (x - firstPoint.getX()));
192
                elShape.lineTo(firstPoint.getX(),
193
                    firstPoint.getY() + (x - firstPoint.getX()));
194
                elShape.lineTo(firstPoint.getX(), firstPoint.getY());
195
            } else {
196
                elShape.lineTo(x, firstPoint.getY() - (x - firstPoint.getX()));
197
                elShape.lineTo(firstPoint.getX(),
198
                    firstPoint.getY() - (x - firstPoint.getX()));
199
                elShape.lineTo(firstPoint.getX(), firstPoint.getY());
200
            }
201

    
202
            ShapeFactory.createPolyline2D(elShape).draw((Graphics2D) g,
203
                getCadToolAdapter().getMapControl().getViewPort(),
204
                CADTool.drawingSymbol);
205
        }
206
    }
207

    
208
    /**
209
     * Add a diferent option.
210
     *
211
     * @param sel DOCUMENT ME!
212
     * @param s Diferent option.
213
     */
214
    public void addOption(String s) {
215
        RectangleCADToolState actualState = (RectangleCADToolState) _fsm.getPreviousState();
216
        String status = actualState.getName();
217

    
218
        if (status == "Rectangle.SecondPointOrSquare") {
219
            if (s.equals("C") || s.equals("c")) {
220
                //Opci?n correcta
221
            }
222
        }
223
    }
224

    
225
    /* (non-Javadoc)
226
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
227
     */
228
    public void addValue(double d) {
229
    }
230

    
231
        public String getName() {
232
                return "RECTANGULO";
233
        }
234

    
235
}