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