Statistics
| Revision:

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

History | View | Annotate | Download (9 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.PathIterator;
47
import java.awt.geom.Point2D;
48
import java.io.IOException;
49
import java.util.ArrayList;
50

    
51
import com.iver.andami.PluginServices;
52
import com.iver.cit.gvsig.CADExtension;
53
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
54
import com.iver.cit.gvsig.fmap.core.FShape;
55
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
56
import com.iver.cit.gvsig.fmap.core.IFeature;
57
import com.iver.cit.gvsig.fmap.core.IGeometry;
58
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
59
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
60
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
61
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
62
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
63
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
64
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
65
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
66
import com.iver.cit.gvsig.gui.cad.tools.smc.InternalPolygonCADToolContext;
67
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
68

    
69

    
70
/**
71
 * DOCUMENT ME!
72
 *
73
 * @author Vicente Caballero Navarro
74
 */
75
public class InternalPolygonCADTool extends DefaultCADTool {
76
    private InternalPolygonCADToolContext _fsm;
77
   private ArrayList points=new ArrayList();
78
    /**
79
     * Crea un nuevo PolylineCADTool.
80
     */
81
    public InternalPolygonCADTool() {
82
    }
83

    
84
    /**
85
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
86
     * carga previa a la utilizaci?n de la herramienta.
87
     */
88
    public void init() {
89
        _fsm = new InternalPolygonCADToolContext(this);
90
        points.clear();
91
    }
92

    
93
    /* (non-Javadoc)
94
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
95
     */
96
    public void transition(double x, double y, InputEvent event) {
97
        _fsm.addPoint(x, y, event);
98
    }
99

    
100
    /* (non-Javadoc)
101
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
102
     */
103
    public void transition(double d) {
104
        _fsm.addValue(d);
105
    }
106

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

    
116
    /**
117
     * DOCUMENT ME!
118
     */
119
    public void selection() {
120
            ArrayList selectedRow=getSelectedRows();
121
        if (selectedRow.size() == 0 && !CADExtension.getCADTool().getClass().getName().equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
122
            CADExtension.setCADTool("_selection",false);
123
            ((SelectionCADTool) CADExtension.getCADTool()).setNextTool(
124
                "_internalpolygon");
125
        }
126
    }
127

    
128
    /**
129
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
130
     * editableFeatureSource que ya estar? creado.
131
     *
132
     * @param x par?metro x del punto que se pase en esta transici?n.
133
     * @param y par?metro y del punto que se pase en esta transici?n.
134
     */
135
    public void addPoint(double x, double y,InputEvent event) {
136
            VectorialLayerEdited vle=getVLE();
137
        ArrayList selectedRows=vle.getSelectedRow();
138
        if (selectedRows.size()==1){
139
                        points.add(new Point2D.Double(x,y));
140
        }
141
        }
142

    
143
        /**
144
     * M?todo para dibujar la lo necesario para el estado en el que nos
145
     * encontremos.
146
     *
147
     * @param g Graphics sobre el que dibujar.
148
     * @param x par?metro x del punto que se pase para dibujar.
149
     * @param y par?metro x del punto que se pase para dibujar.
150
     */
151
    public void drawOperation(Graphics g, double x, double y) {
152
            Point2D[] ps=(Point2D[])points.toArray(new Point2D[0]);
153
            GeneralPathX gpx=new GeneralPathX();
154
            GeneralPathX gpx1=new GeneralPathX();
155

    
156
            if (ps.length>0){
157
            for (int i=0;i<ps.length;i++){
158
                    if (i==0){
159
                            gpx.moveTo(ps[i].getX(),ps[i].getY());
160
                            gpx1.moveTo(ps[i].getX(),ps[i].getY());
161
                    }else{
162
                            gpx.lineTo(ps[i].getX(),ps[i].getY());
163
                            gpx1.lineTo(ps[i].getX(),ps[i].getY());
164
                    }
165

    
166
            }
167
            gpx.lineTo(x,y);
168
            gpx.closePath();
169
            gpx1.closePath();
170
            IGeometry geom=ShapeFactory.createPolygon2D(gpx);
171
            IGeometry geom1=ShapeFactory.createPolygon2D(gpx1);
172
            geom1.draw((Graphics2D)g,CADExtension.getEditionManager().getMapControl().getViewPort(),DefaultCADTool.drawingSymbol);
173
            geom.draw((Graphics2D)g,CADExtension.getEditionManager().getMapControl().getViewPort(),DefaultCADTool.modifySymbol);
174
            }
175
    }
176

    
177
    /**
178
     * Add a diferent option.
179
     *
180
     * @param s Diferent option.
181
     */
182
    public void addOption(String s) {
183
            VectorialLayerEdited vle=getVLE();
184
            ArrayList selectedRows=vle.getSelectedRow();
185
            VectorialEditableAdapter vea = vle.getVEA();
186
            IRowEdited row=null;
187
            if (s.equals(PluginServices.getText(this,"end")) || s.equals("e")|| s.equals("E")){
188
                    if (points.size()>0){
189
                            row =  (DefaultRowEdited) selectedRows.get(0);
190
                            IFeature feat = (IFeature) row.getLinkedRow().cloneRow();
191

    
192
                            IGeometry geometry=feat.getGeometry();
193
                            geometry=createNewPolygon(geometry,(Point2D[])points.toArray(new Point2D[0]));
194
                            DefaultFeature df=new DefaultFeature(geometry,feat.getAttributes(), feat.getID());
195
                            DefaultRowEdited dre=new DefaultRowEdited(df,DefaultRowEdited.STATUS_MODIFIED,row.getIndex());
196
                            try {
197
                                        vea.modifyRow(dre.getIndex(),dre.getLinkedRow(),getName(),EditionEvent.GRAPHIC);
198
                                } catch (IOException e) {
199
                                        e.printStackTrace();
200
                                } catch (DriverIOException e) {
201
                                        e.printStackTrace();
202
                                }
203
                                selectedRows.clear();
204
                            selectedRows.add(dre);
205
                    }
206
                    points.clear();
207

    
208

    
209
            }else if (s.equals(PluginServices.getText(this,"cancel"))|| s.equals("c")|| s.equals("C")){
210
                    points.clear();
211
            }
212
    }
213
    /* (non-Javadoc)
214
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
215
     */
216
    public void addValue(double d) {
217
    }
218
    private IGeometry createNewPolygon(IGeometry gp,Point2D[] ps) {
219
        GeneralPathX newGp = new GeneralPathX();
220
        double[] theData = new double[6];
221

    
222
        PathIterator theIterator;
223
        int theType;
224
        int numParts = 0;
225

    
226

    
227
        theIterator = gp.getPathIterator(null); //, flatness);
228
        while (!theIterator.isDone()) {
229
            theType = theIterator.currentSegment(theData);
230
            switch (theType) {
231

    
232
                case PathIterator.SEG_MOVETO:
233
                    numParts++;
234
                    newGp.moveTo(theData[0], theData[1]);
235
                    break;
236

    
237
                case PathIterator.SEG_LINETO:
238
                    newGp.lineTo(theData[0], theData[1]);
239
                    break;
240

    
241
                case PathIterator.SEG_QUADTO:
242
                    newGp.quadTo(theData[0], theData[1], theData[2], theData[3]);
243
                    break;
244

    
245
                case PathIterator.SEG_CUBICTO:
246
                    newGp.curveTo(theData[0], theData[1], theData[2], theData[3], theData[4], theData[5]);
247
                    break;
248

    
249
                case PathIterator.SEG_CLOSE:
250
                        newGp.closePath();
251
                    break;
252
            } //end switch
253

    
254
            theIterator.next();
255
        } //end while loop
256
        newGp.moveTo(ps[ps.length-1].getX(),ps[ps.length-1].getY());
257
     for (int i=ps.length-1;i>=0;i--){
258
             newGp.lineTo(ps[i].getX(),ps[i].getY());
259
     }
260
     newGp.lineTo(ps[ps.length-1].getX(),ps[ps.length-1].getY());
261
     return ShapeFactory.createPolygon2D(newGp);
262
    }
263
    public String getName() {
264
                return PluginServices.getText(this,"internal_polygon_");
265
        }
266

    
267
        public String toString() {
268
                return "_internalpolygon";
269
        }
270

    
271
        public boolean isApplicable(int shapeType) {
272
                switch (shapeType) {
273
                case FShape.POINT:
274
                case FShape.LINE:
275
                case FShape.MULTIPOINT:
276
                        return false;
277
                }
278
                return true;
279
        }
280

    
281

    
282
}