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