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