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