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