Statistics
| Revision:

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

History | View | Annotate | Download (7.47 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

    
46
import org.gvsig.fmap.dal.exception.DataException;
47
import org.gvsig.fmap.dal.exception.ReadException;
48
import org.gvsig.fmap.dal.feature.DisposableIterator;
49
import org.gvsig.fmap.dal.feature.EditableFeature;
50
import org.gvsig.fmap.dal.feature.Feature;
51
import org.gvsig.fmap.dal.feature.FeatureSet;
52
import org.gvsig.fmap.dal.feature.FeatureStore;
53
import org.gvsig.fmap.geom.exception.CreateGeometryException;
54
import org.gvsig.fmap.geom.operation.GeometryOperationException;
55
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
56
import org.gvsig.fmap.geom.operation.tojts.ToJTS;
57
import org.gvsig.fmap.geom.util.Converter;
58

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

    
68

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

    
77
    /**
78
     * Crea un nuevo JoinCADTool.
79
     */
80
    public JoinCADTool() {
81
    }
82

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

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

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

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

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

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

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

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

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

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

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

    
233
    }
234

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

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