Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extEditing / src / com / iver / cit / gvsig / gui / cad / tools / JoinCADTool.java @ 26242

History | View | Annotate | Download (7.36 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.event.InputEvent;
45
import java.util.ArrayList;
46
import java.util.Iterator;
47

    
48
import org.gvsig.fmap.dal.exception.DataException;
49
import org.gvsig.fmap.dal.exception.ReadException;
50
import org.gvsig.fmap.dal.feature.EditableFeature;
51
import org.gvsig.fmap.dal.feature.Feature;
52
import org.gvsig.fmap.dal.feature.FeatureSet;
53
import org.gvsig.fmap.dal.feature.FeatureStore;
54
import org.gvsig.fmap.dal.feature.impl.DefaultFeature;
55
import org.gvsig.fmap.geom.operation.GeometryOperationException;
56
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
57
import org.gvsig.fmap.geom.operation.tojts.ToJTS;
58
import org.gvsig.fmap.geom.util.Converter;
59
import org.gvsig.fmap.geom.util.UtilFunctions;
60

    
61

    
62
import com.iver.andami.PluginServices;
63
import com.iver.andami.messages.NotificationManager;
64
import com.iver.cit.gvsig.CADExtension;
65
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
66
import com.iver.cit.gvsig.gui.cad.exception.CommandException;
67
import com.iver.cit.gvsig.gui.cad.tools.smc.JoinCADToolContext;
68
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
69
import com.vividsolutions.jts.geom.Geometry;
70

    
71

    
72
/**
73
 * DOCUMENT ME!
74
 *
75
 * @author Vicente Caballero Navarro
76
 */
77
public class JoinCADTool extends DefaultCADTool {
78
    private JoinCADToolContext _fsm;
79

    
80
    /**
81
     * Crea un nuevo JoinCADTool.
82
     */
83
    public JoinCADTool() {
84
    }
85

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

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

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

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

    
117
    /**
118
     * DOCUMENT ME!
119
     */
120
    public void selection() {
121
            FeatureSet selection = null;
122
                try {
123
                        selection = (FeatureSet) getVLE().getFeatureStore().getSelection();
124

    
125
                        if (selection.getSize() == 0
126
                                        && !CADExtension
127
                                                        .getCADTool()
128
                                                        .getClass()
129
                                                        .getName()
130
                                                        .equals(
131
                                                                        "com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
132
                                CADExtension.setCADTool("_selection", false);
133
                                ((SelectionCADTool) CADExtension.getCADTool())
134
                                                .setNextTool("_join");
135
                        }
136
                } catch (ReadException e) {
137
                        // TODO Auto-generated catch block
138
                        e.printStackTrace();
139
                } catch (DataException e) {
140
                        // TODO Auto-generated catch block
141
                        e.printStackTrace();
142
                }
143
    }
144

    
145
    /**
146
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
147
     * editableFeatureSource que ya estar? creado.
148
     *
149
     * @param x par?metro x del punto que se pase en esta transici?n.
150
     * @param y par?metro y del punto que se pase en esta transici?n.
151
     */
152
    public void addPoint(double x, double y,InputEvent event) {
153
    }
154

    
155
    /**
156
     * M?todo para dibujar la lo necesario para el estado en el que nos
157
     * encontremos.
158
     *
159
     * @param g Graphics sobre el que dibujar.
160
     * @param x par?metro x del punto que se pase para dibujar.
161
     * @param y par?metro x del punto que se pase para dibujar.
162
     */
163
    public void drawOperation(Graphics g, double x, double y) {
164
    }
165
    public void join() {
166
            VectorialLayerEdited vle = getVLE();
167
                FeatureStore featureStore = null;
168
                try {
169
                        featureStore = vle.getFeatureStore();
170
                } catch (ReadException e1) {
171
                        NotificationManager.addError(e1.getMessage(),e1);
172
                }
173
                try {
174
                FeatureSet selection = (FeatureSet) featureStore.getSelection();
175
                // getSelectedRows();
176
                    featureStore.beginEditingGroup(getName());
177
                    Geometry geomTotal=null;
178
                    Iterator<Feature> features=selection.iterator();
179
                    boolean first=true;
180
                    Feature firstFeature=null;
181
                    while (features.hasNext()) {
182
                                Feature feature = (Feature) features.next();
183
                                if (first){
184
                                        firstFeature=feature;
185
                                        first=false;
186
                                }
187

    
188
                                org.gvsig.fmap.geom.Geometry geom = ((org.gvsig.fmap.geom.Geometry) feature
189
                                                .getDefaultGeometry()).cloneGeometry();
190
                                featureStore.delete(feature);
191
                                if (geomTotal==null){
192
                                    geomTotal=(Geometry)geom.invokeOperation(ToJTS.CODE, null);
193
                            }else{
194
                                    Geometry geomJTS=(Geometry)geom.invokeOperation(ToJTS.CODE, null);
195
                                    geomTotal=geomTotal.union(geomJTS);
196
                            }
197
                        }
198

    
199
                    EditableFeature eFeature = featureStore.createNewFeature(firstFeature
200
                                        .getType(), firstFeature);
201
                        eFeature.setGeometry(featureStore.getDefaultFeatureType()
202
                                        .getDefaultGeometryAttributeName(), Converter.jtsToGeometry(geomTotal));
203
                        featureStore.insert(eFeature);
204
                        refresh();
205
                } catch (DataException e) {
206
                        NotificationManager.addError(e.getMessage(),e);
207
                } catch (GeometryOperationNotSupportedException e) {
208
                        NotificationManager.addError(e.getMessage(),e);
209
                } catch (GeometryOperationException e) {
210
                        NotificationManager.addError(e.getMessage(),e);
211
                }
212
    }
213
       /**
214
         * Add a diferent option.
215
         *
216
         * @param s
217
         *            Diferent option.
218
         */
219
    public void addOption(String s) {
220
    }
221

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

    
229
    }
230

    
231
        public String getName() {
232
                return PluginServices.getText(this,"join_");
233
        }
234

    
235
        public String toString() {
236
                return "_join";
237
        }
238
        public boolean isApplicable(int shapeType) {
239
                switch (shapeType) {
240
                case org.gvsig.fmap.geom.Geometry.TYPES.POINT:
241
                        return false;
242
        }
243
                return true;
244
        }
245
}