Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1000 / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / RectangleCADTool.java @ 11885

History | View | Annotate | Download (8.85 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.andami.PluginServices;
49
import com.iver.cit.gvsig.fmap.core.FShape;
50
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
51
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
52
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
53
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
54
import com.iver.cit.gvsig.gui.cad.tools.smc.RectangleCADToolContext;
55
import com.iver.cit.gvsig.gui.cad.tools.smc.RectangleCADToolContext.RectangleCADToolState;
56

    
57

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

    
68
    /**
69
     * Crea un nuevo LineCADTool.
70
     */
71
    public RectangleCADTool() {
72

    
73
    }
74

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

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

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

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

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

    
117
        String status = actualState.getName();
118

    
119
        if (status.equals("Rectangle.FirstPoint")) {
120
            firstPoint = new Point2D.Double(x, y);
121
        } else if (status == "Rectangle.SecondPointOrSquare") {
122
            lastPoint = new Point2D.Double(x, y);
123

    
124
            GeneralPathX elShape = new GeneralPathX();
125
            elShape.moveTo(firstPoint.getX(), firstPoint.getY());
126
            elShape.lineTo(lastPoint.getX(), firstPoint.getY());
127
            elShape.lineTo(lastPoint.getX(), lastPoint.getY());
128
            elShape.lineTo(firstPoint.getX(), lastPoint.getY());
129
            elShape.lineTo(firstPoint.getX(), firstPoint.getY());
130
            elShape.closePath();
131
            addGeometry(ShapeFactory.createPolyline2D(elShape));
132
            firstPoint = (Point2D) lastPoint.clone();
133
        } else if (status == "Rectangle.SecondPointSquare") {
134
            lastPoint = new Point2D.Double(x, y);
135

    
136
            GeneralPathX elShape = new GeneralPathX();
137
            elShape.moveTo(firstPoint.getX(), firstPoint.getY());
138
            elShape.lineTo(lastPoint.getX(), firstPoint.getY());
139

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

    
155
            //elShape.lineTo(firstPoint.getX(), firstPoint.getY());
156
            elShape.closePath();
157
            addGeometry(ShapeFactory.createPolyline2D(elShape));
158
            firstPoint = (Point2D) lastPoint.clone();
159
        }
160
    }
161

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

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

    
193
            if (((y <= firstPoint.getY()) && (x <= firstPoint.getX())) ||
194
                    ((y > firstPoint.getY()) && (x > firstPoint.getX()))) {
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
            } else {
200
                elShape.lineTo(x, firstPoint.getY() - (x - firstPoint.getX()));
201
                elShape.lineTo(firstPoint.getX(),
202
                    firstPoint.getY() - (x - firstPoint.getX()));
203
                elShape.lineTo(firstPoint.getX(), firstPoint.getY());
204
            }
205

    
206
            ShapeFactory.createPolyline2D(elShape).draw((Graphics2D) g,
207
                getCadToolAdapter().getMapControl().getViewPort(),
208
                DefaultCADTool.geometrySelectSymbol);
209
        }
210
    }
211

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

    
222
        if (status == "Rectangle.SecondPointOrSquare") {
223
            if (s.equals("C") || s.equals("c")) {
224
                //Opci?n correcta
225
            }
226
        }
227
    }
228

    
229
    /* (non-Javadoc)
230
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
231
     */
232
    public void addValue(double d) {
233
    }
234

    
235
        public String getName() {
236
                return PluginServices.getText(this,"rectangle_");
237
        }
238

    
239
        public String toString() {
240
                return "_rectangle";
241
        }
242
        public boolean isApplicable(int shapeType) {
243
                switch (shapeType) {
244
                case FShape.POINT:
245
                case FShape.MULTIPOINT:
246
                        return false;
247
                }
248
                return true;
249
        }
250
}