svn-gvsig-desktop / trunk / extensions / extCAD / src / com / iver / cit / gvsig / StopEditing.java @ 11468
History | View | Annotate | Download (8.14 KB)
1 |
package com.iver.cit.gvsig; |
---|---|
2 |
|
3 |
import java.awt.Component; |
4 |
|
5 |
import javax.swing.JOptionPane; |
6 |
|
7 |
import com.hardcode.gdbms.driver.exceptions.InitializeWriterException; |
8 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
9 |
import com.iver.andami.PluginServices; |
10 |
import com.iver.andami.messages.NotificationManager; |
11 |
import com.iver.andami.plugins.Extension; |
12 |
import com.iver.cit.gvsig.exceptions.layers.CancelEditingLayerException; |
13 |
import com.iver.cit.gvsig.exceptions.layers.LegendLayerException; |
14 |
import com.iver.cit.gvsig.exceptions.layers.StartEditionLayerException; |
15 |
import com.iver.cit.gvsig.exceptions.layers.StopEditionLayerException; |
16 |
import com.iver.cit.gvsig.exceptions.table.CancelEditingTableException; |
17 |
import com.iver.cit.gvsig.exceptions.visitors.StopWriterVisitorException; |
18 |
import com.iver.cit.gvsig.fmap.MapContext; |
19 |
import com.iver.cit.gvsig.fmap.MapControl; |
20 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
21 |
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition; |
22 |
import com.iver.cit.gvsig.fmap.drivers.shp.IndexedShpDriver; |
23 |
import com.iver.cit.gvsig.fmap.edition.EditionEvent; |
24 |
import com.iver.cit.gvsig.fmap.edition.ISpatialWriter; |
25 |
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter; |
26 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
27 |
import com.iver.cit.gvsig.fmap.layers.FLayers; |
28 |
import com.iver.cit.gvsig.fmap.layers.FLyrAnnotation; |
29 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
30 |
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend; |
31 |
import com.iver.cit.gvsig.layers.VectorialLayerEdited; |
32 |
import com.iver.cit.gvsig.project.documents.table.gui.Table; |
33 |
import com.iver.cit.gvsig.project.documents.view.IProjectView; |
34 |
import com.iver.cit.gvsig.project.documents.view.gui.View; |
35 |
|
36 |
/**
|
37 |
* @author Francisco Jos?
|
38 |
*
|
39 |
* Cuando un tema se pone en edici?n, puede que su driver implemente
|
40 |
* ISpatialWriter. En ese caso, es capaz de guardarse sobre s? mismo. Si no lo
|
41 |
* implementa, esta opci?n estar? deshabilitada y la ?nica posibilidad de
|
42 |
* guardar este tema ser? "Guardando como..."
|
43 |
*/
|
44 |
public class StopEditing extends Extension { |
45 |
private View vista; |
46 |
|
47 |
/**
|
48 |
* @see com.iver.andami.plugins.IExtension#initialize()
|
49 |
*/
|
50 |
public void initialize() { |
51 |
} |
52 |
|
53 |
/**
|
54 |
* @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
|
55 |
*/
|
56 |
public void execute(String s) { |
57 |
com.iver.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager() |
58 |
.getActiveWindow(); |
59 |
|
60 |
vista = (View) f;
|
61 |
boolean isStop=false; |
62 |
IProjectView model = vista.getModel(); |
63 |
MapContext mapa = model.getMapContext(); |
64 |
FLayers layers = mapa.getLayers(); |
65 |
EditionManager edMan = CADExtension.getEditionManager(); |
66 |
if (s.equals("STOPEDITING")) { |
67 |
FLayer[] actives = layers.getActives();
|
68 |
// TODO: Comprobar que solo hay una activa, o al menos
|
69 |
// que solo hay una en edici?n que est? activa, etc, etc
|
70 |
for (int i = 0; i < actives.length; i++) { |
71 |
if (actives[i] instanceof FLyrVect && actives[i].isEditing()) { |
72 |
FLyrVect lv = (FLyrVect) actives[i]; |
73 |
MapControl mapControl = vista.getMapControl(); |
74 |
VectorialLayerEdited lyrEd = (VectorialLayerEdited) edMan.getActiveLayerEdited(); |
75 |
//lyrEd.clearSelection();
|
76 |
isStop=stopEditing(lv, mapControl); |
77 |
if (isStop){
|
78 |
lv.removeLayerListener(edMan); |
79 |
if (lv instanceof FLyrAnnotation){ |
80 |
FLyrAnnotation lva=(FLyrAnnotation)lv; |
81 |
lva.setMapping(lva.getMapping()); |
82 |
} |
83 |
} |
84 |
} |
85 |
} |
86 |
if (isStop) {
|
87 |
vista.getMapControl().setTool("zoomIn");
|
88 |
vista.hideConsole(); |
89 |
vista.repaintMap(); |
90 |
CADExtension.clearView(); |
91 |
|
92 |
} |
93 |
} |
94 |
PluginServices.getMainFrame().enableControls(); |
95 |
} |
96 |
|
97 |
/**
|
98 |
* @see com.iver.andami.plugins.IExtension#isEnabled()
|
99 |
*/
|
100 |
public boolean isEnabled() { |
101 |
FLayer[] lyrs = EditionUtilities.getActiveAndEditedLayers();
|
102 |
if (lyrs == null) |
103 |
return false; |
104 |
FLyrVect lyrVect = (FLyrVect) lyrs[0];
|
105 |
if (lyrVect.getSource() instanceof VectorialEditableAdapter) { |
106 |
return true; |
107 |
} |
108 |
return false; |
109 |
} |
110 |
/**
|
111 |
* DOCUMENT ME!
|
112 |
*/
|
113 |
public boolean stopEditing(FLyrVect layer, MapControl mapControl) { |
114 |
VectorialEditableAdapter vea = (VectorialEditableAdapter) layer |
115 |
.getSource(); |
116 |
int resp = JOptionPane.NO_OPTION; |
117 |
|
118 |
try {
|
119 |
if (layer.isWritable()) {
|
120 |
resp = JOptionPane.showConfirmDialog((Component) PluginServices |
121 |
.getMainFrame(), PluginServices.getText(this,
|
122 |
"realmente_desea_guardar_la_capa")
|
123 |
+ " : " + layer.getName(), PluginServices.getText(this, |
124 |
"guardar"), JOptionPane.YES_NO_OPTION); |
125 |
if (resp != JOptionPane.YES_OPTION) { // CANCEL EDITING |
126 |
cancelEdition(layer); |
127 |
} else { // GUARDAMOS EL TEMA |
128 |
saveLayer(layer); |
129 |
} |
130 |
|
131 |
vea.getCommandRecord().removeCommandListener(mapControl); |
132 |
layer.setEditing(false);
|
133 |
return true; |
134 |
} |
135 |
// Si no existe writer para la capa que tenemos en edici?n
|
136 |
resp = JOptionPane
|
137 |
.showConfirmDialog( |
138 |
(Component) PluginServices.getMainFrame(),
|
139 |
PluginServices |
140 |
.getText( |
141 |
this,
|
142 |
"no_existe_writer_para_este_formato_de_capa_o_no_tiene_permisos_de_escritura_los_datos_no_se_guardaran_desea_continuar")
|
143 |
+ " : " + layer.getName(),
|
144 |
PluginServices.getText(this, "cancelar_edicion"), |
145 |
JOptionPane.YES_NO_OPTION);
|
146 |
if (resp == JOptionPane.YES_OPTION) { // CANCEL EDITING |
147 |
cancelEdition(layer); |
148 |
vea.getCommandRecord().removeCommandListener(mapControl); |
149 |
if (!(layer.getSource().getDriver() instanceof IndexedShpDriver)){ |
150 |
VectorialLayerEdited vle=(VectorialLayerEdited)CADExtension.getEditionManager().getLayerEdited(layer); |
151 |
layer.setLegend((VectorialLegend)vle.getLegend()); |
152 |
} |
153 |
layer.setEditing(false);
|
154 |
return true; |
155 |
} |
156 |
|
157 |
} catch (LegendLayerException e) {
|
158 |
NotificationManager.addError(e); |
159 |
} catch (StartEditionLayerException e) {
|
160 |
NotificationManager.addError(e); |
161 |
} catch (ReadDriverException e) {
|
162 |
NotificationManager.addError(e); |
163 |
} catch (InitializeWriterException e) {
|
164 |
NotificationManager.addError(e); |
165 |
} catch (CancelEditingTableException e) {
|
166 |
NotificationManager.addError(e); |
167 |
} catch (StopWriterVisitorException e) {
|
168 |
NotificationManager.addError(e); |
169 |
} catch (CancelEditingLayerException e) {
|
170 |
NotificationManager.addError(e); |
171 |
} |
172 |
return false; |
173 |
|
174 |
} |
175 |
|
176 |
|
177 |
private void saveLayer(FLyrVect layer) throws ReadDriverException, InitializeWriterException, StopWriterVisitorException{ |
178 |
VectorialEditableAdapter vea = (VectorialEditableAdapter) layer |
179 |
.getSource(); |
180 |
|
181 |
ISpatialWriter writer = (ISpatialWriter) vea.getWriter(); |
182 |
com.iver.andami.ui.mdiManager.IWindow[] views = PluginServices
|
183 |
.getMDIManager().getAllWindows(); |
184 |
for (int j = 0; j < views.length; j++) { |
185 |
if (views[j] instanceof Table) { |
186 |
Table table = (Table) views[j]; |
187 |
if (table.getModel().getAssociatedTable() != null |
188 |
&& table.getModel().getAssociatedTable().equals(layer)) { |
189 |
table.stopEditingCell(); |
190 |
} |
191 |
} |
192 |
} |
193 |
vea.cleanSelectableDatasource(); |
194 |
layer.setRecordset(vea.getRecordset()); // Queremos que el recordset del layer
|
195 |
// refleje los cambios en los campos.
|
196 |
ILayerDefinition lyrDef = EditionUtilities.createLayerDefinition(layer); |
197 |
String aux="FIELDS:"; |
198 |
FieldDescription[] flds = lyrDef.getFieldsDesc();
|
199 |
for (int i=0; i < flds.length; i++) |
200 |
{ |
201 |
aux = aux + ", " + flds[i].getFieldAlias();
|
202 |
} |
203 |
System.err.println("Escribiendo la capa " + lyrDef.getName() + |
204 |
" con los campos " + aux);
|
205 |
writer.initialize(lyrDef); |
206 |
vea.stopEdition(writer, EditionEvent.GRAPHIC); |
207 |
|
208 |
} |
209 |
|
210 |
private void cancelEdition(FLyrVect layer) throws CancelEditingTableException, CancelEditingLayerException { |
211 |
com.iver.andami.ui.mdiManager.IWindow[] views = PluginServices
|
212 |
.getMDIManager().getAllWindows(); |
213 |
VectorialEditableAdapter vea = (VectorialEditableAdapter) layer |
214 |
.getSource(); |
215 |
vea.cancelEdition(EditionEvent.GRAPHIC); |
216 |
for (int j = 0; j < views.length; j++) { |
217 |
if (views[j] instanceof Table) { |
218 |
Table table = (Table) views[j]; |
219 |
if (table.getModel().getAssociatedTable() != null |
220 |
&& table.getModel().getAssociatedTable().equals(layer)) { |
221 |
table.cancelEditing(); |
222 |
} |
223 |
} |
224 |
} |
225 |
} |
226 |
/**
|
227 |
* @see com.iver.andami.plugins.IExtension#isVisible()
|
228 |
*/
|
229 |
public boolean isVisible() { |
230 |
if (EditionUtilities.getEditionStatus() == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE)
|
231 |
return true; |
232 |
return false; |
233 |
|
234 |
} |
235 |
} |