root / branches / v2_0_0_prep / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / JoinCADTool.java @ 38564
History | View | Annotate | Download (8.09 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
package org.gvsig.editing.gui.cad.tools; |
23 |
|
24 |
import java.awt.event.InputEvent; |
25 |
|
26 |
import org.slf4j.Logger; |
27 |
import org.slf4j.LoggerFactory; |
28 |
|
29 |
import org.gvsig.andami.PluginServices; |
30 |
import org.gvsig.andami.messages.NotificationManager; |
31 |
import org.gvsig.editing.CADExtension; |
32 |
import org.gvsig.editing.gui.cad.DefaultCADTool; |
33 |
import org.gvsig.editing.gui.cad.exception.CommandException; |
34 |
import org.gvsig.editing.gui.cad.tools.smc.JoinCADToolContext; |
35 |
import org.gvsig.editing.layers.VectorialLayerEdited; |
36 |
import org.gvsig.fmap.dal.exception.DataException; |
37 |
import org.gvsig.fmap.dal.exception.ReadException; |
38 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
39 |
import org.gvsig.fmap.dal.feature.Feature; |
40 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
41 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
42 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
43 |
import org.gvsig.fmap.dal.feature.exception.NeedEditingModeException; |
44 |
import org.gvsig.fmap.geom.Geometry; |
45 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
46 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
47 |
import org.gvsig.fmap.mapcontrol.MapControlDrawer; |
48 |
import org.gvsig.tools.dispose.DisposableIterator; |
49 |
|
50 |
/**
|
51 |
* DOCUMENT ME!
|
52 |
*
|
53 |
* @author Vicente Caballero Navarro
|
54 |
*/
|
55 |
public class JoinCADTool extends DefaultCADTool { |
56 |
|
57 |
private static final Logger LOG = LoggerFactory |
58 |
.getLogger(JoinCADTool.class); |
59 |
|
60 |
private JoinCADToolContext _fsm;
|
61 |
|
62 |
/**
|
63 |
* M?todo de inicio, para poner el c?digo de todo lo que se requiera de una
|
64 |
* carga previa a la utilizaci?n de la herramienta.
|
65 |
*/
|
66 |
public void init() { |
67 |
_fsm = new JoinCADToolContext(this); |
68 |
} |
69 |
|
70 |
/*
|
71 |
* (non-Javadoc)
|
72 |
*
|
73 |
* @see
|
74 |
* com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
|
75 |
* .layers.FBitSet, double, double)
|
76 |
*/
|
77 |
public void transition(double x, double y, InputEvent event) { |
78 |
_fsm.addPoint(x, y, event); |
79 |
} |
80 |
|
81 |
/*
|
82 |
* (non-Javadoc)
|
83 |
*
|
84 |
* @see
|
85 |
* com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
|
86 |
* .layers.FBitSet, double)
|
87 |
*/
|
88 |
public void transition(double d) { |
89 |
_fsm.addValue(d); |
90 |
} |
91 |
|
92 |
/*
|
93 |
* (non-Javadoc)
|
94 |
*
|
95 |
* @see
|
96 |
* com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
|
97 |
* .layers.FBitSet, java.lang.String)
|
98 |
*/
|
99 |
public void transition(String s) throws CommandException { |
100 |
if (!super.changeCommand(s)) { |
101 |
_fsm.addOption(s); |
102 |
} |
103 |
} |
104 |
|
105 |
/**
|
106 |
* DOCUMENT ME!
|
107 |
*/
|
108 |
public void selection() { |
109 |
FeatureSet selection = null;
|
110 |
try {
|
111 |
selection = (FeatureSet) getVLE().getFeatureStore().getSelection(); |
112 |
|
113 |
if (selection.getSize() == 0 |
114 |
&& !CADExtension |
115 |
.getCADTool() |
116 |
.getClass() |
117 |
.getName() |
118 |
.equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
|
119 |
CADExtension.setCADTool("_selection", false); |
120 |
((SelectionCADTool) CADExtension.getCADTool()) |
121 |
.setNextTool("_join");
|
122 |
} |
123 |
} catch (ReadException e) {
|
124 |
// TODO Auto-generated catch block
|
125 |
e.printStackTrace(); |
126 |
} catch (DataException e) {
|
127 |
// TODO Auto-generated catch block
|
128 |
e.printStackTrace(); |
129 |
} |
130 |
} |
131 |
|
132 |
/**
|
133 |
* Equivale al transition del prototipo pero sin pasarle como par?metro el
|
134 |
* editableFeatureSource que ya estar? creado.
|
135 |
*
|
136 |
* @param x
|
137 |
* par?metro x del punto que se pase en esta transici?n.
|
138 |
* @param y
|
139 |
* par?metro y del punto que se pase en esta transici?n.
|
140 |
*/
|
141 |
public void addPoint(double x, double y, InputEvent event) { |
142 |
|
143 |
} |
144 |
|
145 |
/**
|
146 |
* M?todo para dibujar la lo necesario para el estado en el que nos
|
147 |
* encontremos.
|
148 |
*
|
149 |
* @param g
|
150 |
* Graphics sobre el que dibujar.
|
151 |
* @param x
|
152 |
* par?metro x del punto que se pase para dibujar.
|
153 |
* @param y
|
154 |
* par?metro x del punto que se pase para dibujar.
|
155 |
*/
|
156 |
public void drawOperation(MapControlDrawer renderer, double x, double y) { |
157 |
|
158 |
} |
159 |
|
160 |
public void join() { |
161 |
VectorialLayerEdited vle = getVLE(); |
162 |
FeatureStore featureStore = null;
|
163 |
try {
|
164 |
featureStore = vle.getFeatureStore(); |
165 |
} catch (ReadException e1) {
|
166 |
NotificationManager.addError(e1.getMessage(), e1); |
167 |
} |
168 |
DisposableIterator iterator = null;
|
169 |
try {
|
170 |
featureStore.beginEditingGroup(getName()); |
171 |
|
172 |
FeatureSelection selection = featureStore.createFeatureSelection(); |
173 |
selection.select((FeatureSet) featureStore.getSelection()); |
174 |
|
175 |
Geometry geomTotal = null;
|
176 |
iterator = selection.fastIterator(); |
177 |
boolean first = true; |
178 |
Feature firstFeature = null;
|
179 |
while (iterator.hasNext()) {
|
180 |
Feature feature = (Feature) iterator.next(); |
181 |
if (first) {
|
182 |
firstFeature = feature; |
183 |
first = false;
|
184 |
} |
185 |
|
186 |
Geometry currentGeometry = |
187 |
feature.getDefaultGeometry().cloneGeometry(); |
188 |
featureStore.delete(feature); |
189 |
if (geomTotal == null) { |
190 |
geomTotal = currentGeometry; |
191 |
} else {
|
192 |
geomTotal = geomTotal.union(currentGeometry); |
193 |
} |
194 |
} |
195 |
|
196 |
EditableFeature editableFeature = |
197 |
featureStore.createNewFeature(firstFeature.getType(), |
198 |
firstFeature); |
199 |
editableFeature.setGeometry(featureStore.getDefaultFeatureType() |
200 |
.getDefaultGeometryAttributeName(), geomTotal); |
201 |
featureStore.insert(editableFeature); |
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 |
} finally {
|
210 |
if (iterator != null) { |
211 |
iterator.dispose(); |
212 |
} |
213 |
try {
|
214 |
featureStore.endEditingGroup(); |
215 |
} catch (NeedEditingModeException e) {
|
216 |
LOG.error("Exception endEditingGroup", e);
|
217 |
} |
218 |
} |
219 |
} |
220 |
|
221 |
/**
|
222 |
* Add a diferent option.
|
223 |
*
|
224 |
* @param s
|
225 |
* Diferent option.
|
226 |
*/
|
227 |
public void addOption(String s) { |
228 |
} |
229 |
|
230 |
/*
|
231 |
* (non-Javadoc)
|
232 |
*
|
233 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
|
234 |
*/
|
235 |
public void addValue(double d) { |
236 |
|
237 |
} |
238 |
|
239 |
public String getName() { |
240 |
return PluginServices.getText(this, "join_"); |
241 |
} |
242 |
|
243 |
public String toString() { |
244 |
return "_join"; |
245 |
} |
246 |
|
247 |
@Override
|
248 |
protected int[] getSupportedGeometryTypes() { |
249 |
return new int[] { CURVE, SURFACE }; |
250 |
} |
251 |
} |