Statistics
| Revision:

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

History | View | Annotate | Download (8.34 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.geom.Point2D;
46

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

    
54

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

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

    
70
    }
71

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

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

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

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

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

    
112
        String status = actualState.getName();
113

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

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

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

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

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

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

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

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

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

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

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

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

    
230
}