Statistics
| Revision:

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

History | View | Annotate | Download (8.87 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.CADTool;
53
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
54
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
55
import com.iver.cit.gvsig.gui.cad.tools.smc.RectangleCADToolContext;
56
import com.iver.cit.gvsig.gui.cad.tools.smc.RectangleCADToolContext.RectangleCADToolState;
57

    
58

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

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

    
74
    }
75

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

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

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

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

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

    
118
        String status = actualState.getName();
119

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

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

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

    
141
            if (((lastPoint.getY() <= firstPoint.getY()) &&
142
                    (lastPoint.getX() <= firstPoint.getX())) ||
143
                    ((lastPoint.getY() > firstPoint.getY()) &&
144
                    (lastPoint.getX() > firstPoint.getX()))) {
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
            } else {
150
                elShape.lineTo(lastPoint.getX(),
151
                    firstPoint.getY() - (lastPoint.getX() - firstPoint.getX()));
152
                elShape.lineTo(firstPoint.getX(),
153
                    firstPoint.getY() - (lastPoint.getX() - firstPoint.getX()));
154
            }
155

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

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

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

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

    
207
            ShapeFactory.createPolyline2D(elShape).draw((Graphics2D) g,
208
                getCadToolAdapter().getMapControl().getViewPort(),
209
                CADTool.drawingSymbol);
210
        }
211
    }
212

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

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

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

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

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