svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extEditing / src / org / gvsig / editing / StopEditing.java @ 37534
History | View | Annotate | Download (14 KB)
1 |
package org.gvsig.editing; |
---|---|
2 |
|
3 |
import java.awt.Component; |
4 |
import java.util.ArrayList; |
5 |
|
6 |
import javax.swing.JOptionPane; |
7 |
|
8 |
import org.cresques.cts.IProjection; |
9 |
import org.gvsig.andami.PluginServices; |
10 |
import org.gvsig.andami.PluginsLocator; |
11 |
import org.gvsig.andami.messages.NotificationManager; |
12 |
import org.gvsig.andami.plugins.Extension; |
13 |
import org.gvsig.andami.plugins.IExtension; |
14 |
import org.gvsig.andami.plugins.status.IExtensionStatus; |
15 |
import org.gvsig.andami.plugins.status.IUnsavedData; |
16 |
import org.gvsig.andami.plugins.status.UnsavedData; |
17 |
import org.gvsig.app.project.Project; |
18 |
import org.gvsig.app.project.ProjectManager; |
19 |
import org.gvsig.app.project.documents.view.DefaultViewDocument; |
20 |
import org.gvsig.app.project.documents.view.ViewDocument; |
21 |
import org.gvsig.app.project.documents.view.ViewManager; |
22 |
import org.gvsig.app.project.documents.view.gui.DefaultViewPanel; |
23 |
import org.gvsig.editing.gui.cad.CADToolAdapter; |
24 |
import org.gvsig.editing.layers.VectorialLayerEdited; |
25 |
import org.gvsig.exportto.app.extension.ExporttoLayerExtension; |
26 |
import org.gvsig.fmap.dal.exception.DataException; |
27 |
import org.gvsig.fmap.dal.exception.ReadException; |
28 |
import org.gvsig.fmap.dal.exception.WriteException; |
29 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
30 |
import org.gvsig.fmap.mapcontext.MapContext; |
31 |
import org.gvsig.fmap.mapcontext.exceptions.CancelEditingLayerException; |
32 |
import org.gvsig.fmap.mapcontext.exceptions.StartEditionLayerException; |
33 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
34 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
35 |
import org.gvsig.fmap.mapcontext.layers.LayersIterator; |
36 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
37 |
import org.gvsig.fmap.mapcontrol.MapControl; |
38 |
import org.gvsig.utils.swing.threads.IMonitorableTask; |
39 |
|
40 |
/**
|
41 |
* @author Francisco Jos?
|
42 |
*
|
43 |
* Cuando un tema se pone en edici?n, puede que su driver implemente
|
44 |
* ISpatialWriter. En ese caso, es capaz de guardarse sobre s? mismo. Si
|
45 |
* no lo implementa, esta opci?n estar? deshabilitada y la ?nica
|
46 |
* posibilidad de guardar este tema ser? "Guardando como..."
|
47 |
*/
|
48 |
public class StopEditing extends Extension { |
49 |
private DefaultViewPanel vista;
|
50 |
|
51 |
/**
|
52 |
* @see org.gvsig.andami.plugins.IExtension#initialize()
|
53 |
*/
|
54 |
public void initialize() { |
55 |
} |
56 |
|
57 |
/**
|
58 |
* @see org.gvsig.andami.plugins.IExtension#execute(java.lang.String)
|
59 |
*/
|
60 |
public void execute(String s) { |
61 |
org.gvsig.andami.ui.mdiManager.IWindow f = PluginServices |
62 |
.getMDIManager().getActiveWindow(); |
63 |
|
64 |
vista = (DefaultViewPanel) f; |
65 |
boolean isStop = false; |
66 |
ViewDocument model = vista.getModel(); |
67 |
MapContext mapa = model.getMapContext(); |
68 |
FLayers layers = mapa.getLayers(); |
69 |
EditionManager edMan = CADExtension.getEditionManager(); |
70 |
if (s.equals("STOPEDITING")) { |
71 |
vista.getMapControl().getCanceldraw().setCanceled(true);
|
72 |
FLayer[] actives = layers.getActives();
|
73 |
// TODO: Comprobar que solo hay una activa, o al menos
|
74 |
// que solo hay una en edici?n que est? activa, etc, etc
|
75 |
for (int i = 0; i < actives.length; i++) { |
76 |
if (actives[i] instanceof FLyrVect && actives[i].isEditing()) { |
77 |
FLyrVect lv = (FLyrVect) actives[i]; |
78 |
MapControl mapControl = vista.getMapControl(); |
79 |
VectorialLayerEdited lyrEd = (VectorialLayerEdited) edMan |
80 |
.getActiveLayerEdited(); |
81 |
lv.getFeatureStore().deleteObserver(lyrEd); |
82 |
isStop = stopEditing(lv, mapControl); |
83 |
if (isStop) {
|
84 |
lv.removeLayerListener(edMan); |
85 |
mapControl.getMapContext().removeLayerDrawListener( |
86 |
lyrEd); |
87 |
// if (lv instanceof FLyrAnnotation){
|
88 |
// FLyrAnnotation lva=(FLyrAnnotation)lv;
|
89 |
// lva.setMapping(lva.getMapping());
|
90 |
// }
|
91 |
} else {
|
92 |
lv.getFeatureStore().addObserver(lyrEd); |
93 |
|
94 |
} |
95 |
} |
96 |
} |
97 |
if (isStop) {
|
98 |
vista.getMapControl().setTool("zoomIn");
|
99 |
vista.hideConsole(); |
100 |
vista.repaintMap(); |
101 |
CADExtension.clearView(); |
102 |
|
103 |
} |
104 |
} |
105 |
PluginServices.getMainFrame().enableControls(); |
106 |
} |
107 |
|
108 |
/**
|
109 |
* @see org.gvsig.andami.plugins.IExtension#isEnabled()
|
110 |
*/
|
111 |
public boolean isEnabled() { |
112 |
FLayer[] lyrs = EditionUtilities.getActiveAndEditedLayers();
|
113 |
if (lyrs == null) { |
114 |
return false; |
115 |
} |
116 |
FLyrVect lyrVect = (FLyrVect) lyrs[0];
|
117 |
if (lyrVect.isEditing()) {
|
118 |
return true; |
119 |
} |
120 |
return false; |
121 |
} |
122 |
|
123 |
/**
|
124 |
* DOCUMENT ME!
|
125 |
*/
|
126 |
public boolean stopEditing(FLyrVect layer, MapControl mapControl) { |
127 |
int resp = JOptionPane.CANCEL_OPTION; |
128 |
|
129 |
try {
|
130 |
if (layer.isWritable()) {
|
131 |
Object[] options = { |
132 |
PluginServices.getText(this, "guardar"), |
133 |
PluginServices.getText(this,
|
134 |
"finish_editing_without_saving_changes"),
|
135 |
PluginServices.getText(this, "continue_editing") }; |
136 |
|
137 |
resp = JOptionPane
|
138 |
.showOptionDialog( |
139 |
(Component) PluginServices.getMainFrame(),
|
140 |
PluginServices.getText(this,
|
141 |
"realmente_desea_guardar_la_capa")
|
142 |
+ " : " + layer.getName(),
|
143 |
PluginServices.getText(this, "stop_edition"), |
144 |
JOptionPane.YES_NO_CANCEL_OPTION,
|
145 |
JOptionPane.QUESTION_MESSAGE, null, options, |
146 |
options[2]);
|
147 |
if (resp == JOptionPane.YES_OPTION) { // SAVE |
148 |
saveLayer(layer); |
149 |
layer.setEditing(false);
|
150 |
} else if (resp == JOptionPane.NO_OPTION) { // CANCEL EDITING |
151 |
cancelEdition(layer); |
152 |
layer.setEditing(false);
|
153 |
return true; |
154 |
} |
155 |
return true; |
156 |
} |
157 |
Object[] options = { |
158 |
PluginServices.getText(this, "export"), |
159 |
PluginServices.getText(this,
|
160 |
"finish_editing_without_saving_changes"),
|
161 |
PluginServices.getText(this, "continue_editing") }; |
162 |
|
163 |
resp = JOptionPane
|
164 |
.showOptionDialog( |
165 |
(Component) PluginServices.getMainFrame(),
|
166 |
PluginServices |
167 |
.getText( |
168 |
this,
|
169 |
"no_existe_writer_para_este_formato_de_capa_o_no_tiene_permisos_de_escritura_los_datos_que_desea_hacer")
|
170 |
+ " : " + layer.getName(), PluginServices
|
171 |
.getText(this, "stop_edition"), |
172 |
JOptionPane.YES_NO_CANCEL_OPTION,
|
173 |
JOptionPane.QUESTION_MESSAGE, null, options, |
174 |
options[2]);
|
175 |
if (resp == JOptionPane.NO_OPTION) { // CANCEL EDITING |
176 |
cancelEdition(layer); |
177 |
layer.setEditing(false);
|
178 |
return true; |
179 |
} else if (resp == JOptionPane.YES_OPTION) { |
180 |
int status = exportLayer(layer);
|
181 |
if (status == JOptionPane.OK_OPTION) { |
182 |
cancelEdition(layer); |
183 |
layer.setEditing(false);
|
184 |
} |
185 |
} |
186 |
|
187 |
} catch (StartEditionLayerException e) {
|
188 |
NotificationManager.addError(e); |
189 |
} catch (ReadException e) {
|
190 |
NotificationManager.addError(e); |
191 |
} catch (CancelEditingLayerException e) {
|
192 |
NotificationManager.addError(e); |
193 |
} |
194 |
return false; |
195 |
|
196 |
} |
197 |
|
198 |
private void saveLayer(FLyrVect layer) throws ReadException { |
199 |
FeatureStore featureStore = layer.getFeatureStore(); |
200 |
try {
|
201 |
featureStore.finishEditing(); |
202 |
} catch (WriteException e) {
|
203 |
throw new ReadException(featureStore.getName(), e); |
204 |
} catch (DataException e) {
|
205 |
throw new ReadException(featureStore.getName(), e); |
206 |
} |
207 |
} |
208 |
|
209 |
private void cancelEdition(FLyrVect layer) |
210 |
throws CancelEditingLayerException {
|
211 |
FeatureStore featureStore = null;
|
212 |
try {
|
213 |
featureStore = layer.getFeatureStore(); |
214 |
|
215 |
featureStore.cancelEditing(); |
216 |
} catch (ReadException e) {
|
217 |
throw new CancelEditingLayerException(layer.getName(), e); |
218 |
} catch (DataException e) {
|
219 |
throw new CancelEditingLayerException(layer.getName(), e); |
220 |
} |
221 |
} |
222 |
|
223 |
private int exportLayer(FLyrVect layer) throws ReadException { |
224 |
ViewDocument model = vista.getModel(); |
225 |
MapContext mapContext = model.getMapContext(); |
226 |
IProjection projection = mapContext.getProjection(); |
227 |
|
228 |
ExporttoLayerExtension extension = (ExporttoLayerExtension) PluginsLocator |
229 |
.getManager().getExtension(ExporttoLayerExtension.class); |
230 |
return extension.showExportto(layer, projection, mapContext);
|
231 |
} |
232 |
|
233 |
/**
|
234 |
* @see org.gvsig.andami.plugins.IExtension#isVisible()
|
235 |
*/
|
236 |
public boolean isVisible() { |
237 |
if (EditionUtilities.getEditionStatus() == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE) {
|
238 |
return true; |
239 |
} |
240 |
return false; |
241 |
|
242 |
} |
243 |
|
244 |
public IExtensionStatus getStatus() {
|
245 |
return new StopEditingStatus(); |
246 |
} |
247 |
|
248 |
/**
|
249 |
* Show the dialogs to save the layer without ask if don't like to save.
|
250 |
*
|
251 |
* @param layer
|
252 |
* Layer to save.
|
253 |
*/
|
254 |
public boolean executeSaveLayer(FLyrVect layer) { |
255 |
CADToolAdapter cadtoolAdapter = CADExtension.getCADToolAdapter(layer); |
256 |
EditionManager edMan = cadtoolAdapter.getEditionManager(); |
257 |
VectorialLayerEdited lyrEd = (VectorialLayerEdited) edMan |
258 |
.getLayerEdited(layer); |
259 |
boolean isStop = false; |
260 |
try {
|
261 |
lyrEd.clearSelection(); |
262 |
|
263 |
if (layer.isWritable()) {
|
264 |
saveLayer(layer); |
265 |
layer.setEditing(false);
|
266 |
// if (layer.isSpatiallyIndexed())
|
267 |
// {
|
268 |
// if(layer.getISpatialIndex() != null)
|
269 |
// {
|
270 |
// PluginServices.
|
271 |
// cancelableBackgroundExecution(new
|
272 |
// CreateSpatialIndexMonitorableTask((FLyrVect)layer));
|
273 |
// }
|
274 |
// }
|
275 |
|
276 |
isStop = true;
|
277 |
} else {
|
278 |
// Si no existe writer para la capa que tenemos en edici?n
|
279 |
int resp = JOptionPane |
280 |
.showConfirmDialog( |
281 |
(Component) PluginServices.getMainFrame(),
|
282 |
PluginServices |
283 |
.getText( |
284 |
this,
|
285 |
"no_existe_writer_para_este_formato_de_capa_o_no_tiene_permisos_de_escritura_los_datos_no_se_guardaran_desea_continuar")
|
286 |
+ " : " + layer.getName(),
|
287 |
PluginServices |
288 |
.getText(this, "cancelar_edicion"), |
289 |
JOptionPane.YES_NO_OPTION);
|
290 |
if (resp == JOptionPane.YES_OPTION) { // CANCEL EDITING |
291 |
try {
|
292 |
cancelEdition(layer); |
293 |
layer.setEditing(false);
|
294 |
// if (!(layer.getSource().getDriver() instanceof
|
295 |
// IndexedShpDriver)){
|
296 |
// VectorialLayerEdited
|
297 |
// vle=(VectorialLayerEdited)CADExtension.getEditionManager().getLayerEdited(layer);
|
298 |
// layer.setLegend((IVectorLegend)vle.getLegend());
|
299 |
// }
|
300 |
} catch (CancelEditingLayerException e) {
|
301 |
PluginServices.getLogger().error(e.getMessage(), e); |
302 |
return isStop;
|
303 |
} |
304 |
isStop = true;
|
305 |
} |
306 |
|
307 |
} |
308 |
if (isStop) {
|
309 |
layer.removeLayerListener(edMan); |
310 |
// if (layer instanceof FLyrAnnotation){
|
311 |
// FLyrAnnotation lva=(FLyrAnnotation)layer;
|
312 |
// lva.setMapping(lva.getMapping());
|
313 |
// }
|
314 |
org.gvsig.andami.ui.mdiManager.IWindow f = PluginServices |
315 |
.getMDIManager().getActiveWindow(); |
316 |
if (f instanceof DefaultViewPanel) { |
317 |
vista = (DefaultViewPanel) f; |
318 |
FLayer auxLayer = vista.getMapControl().getMapContext() |
319 |
.getLayers().getLayer(layer.getName()); |
320 |
if (auxLayer != null && auxLayer.equals(layer)) { |
321 |
vista.getMapControl().setTool("zoomIn");
|
322 |
vista.hideConsole(); |
323 |
vista.repaintMap(); |
324 |
CADExtension.clearView(); |
325 |
} |
326 |
} |
327 |
} |
328 |
} catch (ReadException e1) {
|
329 |
NotificationManager.showMessageError(e1.getMessage(), e1); |
330 |
} catch (StartEditionLayerException e) {
|
331 |
NotificationManager.showMessageError(e.getMessage(), e); |
332 |
} catch (DataException e) {
|
333 |
NotificationManager.showMessageError(e.getMessage(), e); |
334 |
} |
335 |
return isStop;
|
336 |
} |
337 |
|
338 |
private class UnsavedLayer extends UnsavedData { |
339 |
|
340 |
private FLayer layer;
|
341 |
|
342 |
public UnsavedLayer(IExtension extension) {
|
343 |
super(extension);
|
344 |
} |
345 |
|
346 |
public String getDescription() { |
347 |
return PluginServices.getText(this, "editing_layer_unsaved"); |
348 |
} |
349 |
|
350 |
public String getResourceName() { |
351 |
return layer.getName();
|
352 |
} |
353 |
|
354 |
public boolean saveData() { |
355 |
return executeSaveLayer((FLyrVect) layer);
|
356 |
} |
357 |
|
358 |
public void setLayer(FLayer layer) { |
359 |
this.layer = layer;
|
360 |
|
361 |
} |
362 |
|
363 |
public String getIcon() { |
364 |
return layer.getTocImageIcon();
|
365 |
} |
366 |
|
367 |
} |
368 |
|
369 |
/**
|
370 |
* <p>
|
371 |
* This class provides the status of extensions. If this extension has some
|
372 |
* unsaved editing layer (and save them), and methods to check if the
|
373 |
* extension has some associated background tasks.
|
374 |
*
|
375 |
* @author Vicente Caballero Navarro
|
376 |
*
|
377 |
*/
|
378 |
private class StopEditingStatus implements IExtensionStatus { |
379 |
/**
|
380 |
* This method is used to check if this extension has some unsaved
|
381 |
* editing layer.
|
382 |
*
|
383 |
* @return true if the extension has some unsaved editing layer, false
|
384 |
* otherwise.
|
385 |
*/
|
386 |
public boolean hasUnsavedData() { |
387 |
Project project = ProjectManager.getInstance().getCurrentProject(); |
388 |
DefaultViewDocument[] views = project.getDocuments(
|
389 |
ViewManager.TYPENAME).toArray(new DefaultViewDocument[0]); |
390 |
for (int i = 0; i < views.length; i++) { |
391 |
FLayers layers = views[i].getMapContext().getLayers(); |
392 |
LayersIterator iter = getEditingLayer(layers); |
393 |
if (iter.hasNext()) {
|
394 |
return true; |
395 |
} |
396 |
} |
397 |
return false; |
398 |
} |
399 |
|
400 |
/**
|
401 |
* This method is used to check if the extension has some associated
|
402 |
* background process which is currently running.
|
403 |
*
|
404 |
* @return true if the extension has some associated background process,
|
405 |
* false otherwise.
|
406 |
*/
|
407 |
public boolean hasRunningProcesses() { |
408 |
return false; |
409 |
} |
410 |
|
411 |
/**
|
412 |
* <p>
|
413 |
* Gets an array of the traceable background tasks associated with this
|
414 |
* extension. These tasks may be tracked, canceled, etc.
|
415 |
* </p>
|
416 |
*
|
417 |
* @return An array of the associated background tasks, or null in case
|
418 |
* there is no associated background tasks.
|
419 |
*/
|
420 |
public IMonitorableTask[] getRunningProcesses() { |
421 |
return null; |
422 |
} |
423 |
|
424 |
/**
|
425 |
* <p>
|
426 |
* Gets an array of the UnsavedData objects, which contain information
|
427 |
* about the unsaved editing layers and allows to save it.
|
428 |
* </p>
|
429 |
*
|
430 |
* @return An array of the associated unsaved editing layers, or null in
|
431 |
* case the extension has not unsaved editing layers.
|
432 |
*/
|
433 |
public IUnsavedData[] getUnsavedData() { |
434 |
Project project = ProjectManager.getInstance().getCurrentProject(); |
435 |
DefaultViewDocument[] views = project.getDocuments(
|
436 |
ViewManager.TYPENAME).toArray(new DefaultViewDocument[0]); |
437 |
ArrayList unsavedLayers = new ArrayList(); |
438 |
for (int i = 0; i < views.length; i++) { |
439 |
FLayers layers = views[i].getMapContext().getLayers(); |
440 |
LayersIterator iter = getEditingLayer(layers); |
441 |
while (iter.hasNext()) {
|
442 |
UnsavedLayer ul = new UnsavedLayer(StopEditing.this);
|
443 |
ul.setLayer(iter.nextLayer()); |
444 |
unsavedLayers.add(ul); |
445 |
} |
446 |
} |
447 |
return (IUnsavedData[]) unsavedLayers.toArray(new IUnsavedData[0]); |
448 |
} |
449 |
} |
450 |
|
451 |
private LayersIterator getEditingLayer(FLayers layers) {
|
452 |
return new LayersIterator(layers) { |
453 |
public boolean evaluate(FLayer layer) { |
454 |
return layer.isEditing();
|
455 |
} |
456 |
}; |
457 |
} |
458 |
} |