root / tags / v1_0_2_Build_904 / extensions / extCAD / src / com / iver / cit / gvsig / StopEditing.java @ 10724
History | View | Annotate | Download (7.31 KB)
1 | 4421 | fjp | package com.iver.cit.gvsig; |
---|---|---|---|
2 | |||
3 | 5807 | caballero | import java.awt.Component; |
4 | 5267 | fjp | import java.io.IOException; |
5 | |||
6 | import javax.swing.JOptionPane; |
||
7 | |||
8 | 6312 | caballero | import com.hardcode.gdbms.engine.instruction.FieldNotFoundException; |
9 | 4421 | fjp | import com.iver.andami.PluginServices; |
10 | 5267 | fjp | import com.iver.andami.messages.NotificationManager; |
11 | 4421 | fjp | import com.iver.andami.plugins.Extension; |
12 | 5558 | fjp | import com.iver.cit.gvsig.fmap.DriverException; |
13 | 6878 | cesar | import com.iver.cit.gvsig.fmap.MapContext; |
14 | 4421 | fjp | import com.iver.cit.gvsig.fmap.MapControl; |
15 | 6483 | fjp | import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
16 | 5558 | fjp | import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition; |
17 | 6472 | caballero | import com.iver.cit.gvsig.fmap.drivers.shp.IndexedShpDriver; |
18 | 5184 | caballero | import com.iver.cit.gvsig.fmap.edition.EditionEvent; |
19 | 4421 | fjp | import com.iver.cit.gvsig.fmap.edition.EditionException; |
20 | 4430 | fjp | import com.iver.cit.gvsig.fmap.edition.ISpatialWriter; |
21 | 4421 | fjp | 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 | 9394 | caballero | import com.iver.cit.gvsig.fmap.layers.FLyrAnnotation; |
25 | 4421 | fjp | import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
26 | 6312 | caballero | import com.iver.cit.gvsig.fmap.rendering.VectorialLegend; |
27 | 6002 | fjp | import com.iver.cit.gvsig.layers.VectorialLayerEdited; |
28 | 8765 | jjdelcerro | 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 | 4421 | fjp | |
32 | /**
|
||
33 | * @author Francisco Jos?
|
||
34 | 5482 | caballero | *
|
35 | 4430 | fjp | * 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 | 4421 | fjp | */
|
40 | 5005 | jorpiell | public class StopEditing extends Extension { |
41 | 4430 | fjp | private View vista; |
42 | |||
43 | 4421 | fjp | /**
|
44 | 5005 | jorpiell | * @see com.iver.andami.plugins.IExtension#initialize()
|
45 | 4430 | fjp | */
|
46 | 5005 | jorpiell | public void initialize() { |
47 | 4430 | fjp | } |
48 | 4421 | fjp | |
49 | 4430 | fjp | /**
|
50 | 5005 | jorpiell | * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
|
51 | 4430 | fjp | */
|
52 | public void execute(String s) { |
||
53 | 6877 | cesar | com.iver.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager() |
54 | 6880 | cesar | .getActiveWindow(); |
55 | 4421 | fjp | |
56 | 4430 | fjp | vista = (View) f;
|
57 | 5807 | caballero | boolean isStop=false; |
58 | 8765 | jjdelcerro | IProjectView model = vista.getModel(); |
59 | 6878 | cesar | MapContext mapa = model.getMapContext(); |
60 | 4430 | fjp | FLayers layers = mapa.getLayers(); |
61 | 4475 | fjp | EditionManager edMan = CADExtension.getEditionManager(); |
62 | 4430 | fjp | 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 | 6604 | caballero | MapControl mapControl = vista.getMapControl(); |
70 | 6002 | fjp | VectorialLayerEdited lyrEd = (VectorialLayerEdited) edMan.getActiveLayerEdited(); |
71 | lyrEd.clearSelection(); |
||
72 | 5807 | caballero | isStop=stopEditing(lv, mapControl); |
73 | 7830 | jmvivo | if (isStop){
|
74 | 9394 | caballero | lv.removeLayerListener(edMan); |
75 | if (lv instanceof FLyrAnnotation){ |
||
76 | FLyrAnnotation lva=(FLyrAnnotation)lv; |
||
77 | lva.setMapping(lva.getMapping()); |
||
78 | } |
||
79 | 7830 | jmvivo | } |
80 | 4430 | fjp | } |
81 | } |
||
82 | 5807 | caballero | if (isStop) {
|
83 | vista.getMapControl().setTool("zoomIn");
|
||
84 | vista.hideConsole(); |
||
85 | 6002 | fjp | vista.repaintMap(); |
86 | 7830 | jmvivo | CADExtension.clearView(); |
87 | 9394 | caballero | |
88 | 5807 | caballero | } |
89 | 4430 | fjp | } |
90 | PluginServices.getMainFrame().enableControls(); |
||
91 | } |
||
92 | 4421 | fjp | |
93 | 4430 | fjp | /**
|
94 | 5005 | jorpiell | * @see com.iver.andami.plugins.IExtension#isEnabled()
|
95 | 4430 | fjp | */
|
96 | 5267 | fjp | public boolean isEnabled() { |
97 | 4430 | fjp | FLayer[] lyrs = EditionUtilities.getActiveAndEditedLayers();
|
98 | 5267 | fjp | if (lyrs == null) |
99 | return false; |
||
100 | 4430 | fjp | FLyrVect lyrVect = (FLyrVect) lyrs[0];
|
101 | 5267 | fjp | if (lyrVect.getSource() instanceof VectorialEditableAdapter) { |
102 | 5807 | caballero | return true; |
103 | 4950 | caballero | } |
104 | return false; |
||
105 | 5267 | fjp | } |
106 | 4430 | fjp | /**
|
107 | * DOCUMENT ME!
|
||
108 | */
|
||
109 | 5807 | caballero | public boolean stopEditing(FLyrVect layer, MapControl mapControl) { |
110 | 5267 | fjp | VectorialEditableAdapter vea = (VectorialEditableAdapter) layer |
111 | .getSource(); |
||
112 | 5807 | caballero | int resp = JOptionPane.NO_OPTION; |
113 | 4950 | caballero | |
114 | 4421 | fjp | try {
|
115 | 6856 | fjp | if (layer.isWritable()) {
|
116 | 5807 | caballero | 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 | 5267 | fjp | } |
126 | 5807 | caballero | |
127 | vea.getCommandRecord().removeCommandListener(mapControl); |
||
128 | layer.setEditing(false);
|
||
129 | return true; |
||
130 | 6604 | caballero | } |
131 | // Si no existe writer para la capa que tenemos en edici?n
|
||
132 | 5807 | caballero | resp = JOptionPane
|
133 | .showConfirmDialog( |
||
134 | (Component) PluginServices.getMainFrame(),
|
||
135 | PluginServices |
||
136 | .getText( |
||
137 | this,
|
||
138 | 6856 | fjp | "no_existe_writer_para_este_formato_de_capa_o_no_tiene_permisos_de_escritura_los_datos_no_se_guardaran_desea_continuar")
|
139 | 5807 | caballero | + " : " + layer.getName(),
|
140 | 6856 | fjp | PluginServices.getText(this, "cancelar_edicion"), |
141 | 5807 | caballero | JOptionPane.YES_NO_OPTION);
|
142 | if (resp == JOptionPane.YES_OPTION) { // CANCEL EDITING |
||
143 | cancelEdition(layer); |
||
144 | vea.getCommandRecord().removeCommandListener(mapControl); |
||
145 | 6472 | caballero | if (!(layer.getSource().getDriver() instanceof IndexedShpDriver)){ |
146 | VectorialLayerEdited vle=(VectorialLayerEdited)CADExtension.getEditionManager().getLayerEdited(layer); |
||
147 | layer.setLegend((VectorialLegend)vle.getLegend()); |
||
148 | } |
||
149 | 5807 | caballero | layer.setEditing(false);
|
150 | return true; |
||
151 | 5571 | jmvivo | } |
152 | 6604 | caballero | |
153 | 4421 | fjp | } catch (EditionException e) {
|
154 | 5267 | fjp | NotificationManager.addError(e); |
155 | } catch (IOException e) { |
||
156 | NotificationManager.addError(e); |
||
157 | 5558 | fjp | } catch (DriverException e) {
|
158 | 5807 | caballero | NotificationManager.addError(e); |
159 | 6312 | caballero | } catch (FieldNotFoundException e) {
|
160 | e.printStackTrace(); |
||
161 | 4421 | fjp | } |
162 | 5807 | caballero | return false; |
163 | 4950 | caballero | |
164 | 5267 | fjp | } |
165 | |||
166 | 5807 | caballero | |
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 | 6877 | cesar | com.iver.andami.ui.mdiManager.IWindow[] views = PluginServices
|
174 | 6880 | cesar | .getMDIManager().getAllWindows(); |
175 | 5807 | caballero | 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 | 6483 | fjp | vea.cleanSelectableDatasource(); |
185 | 6604 | caballero | layer.setRecordset(vea.getRecordset()); // Queremos que el recordset del layer
|
186 | 6483 | fjp | // refleje los cambios en los campos.
|
187 | 5807 | caballero | ILayerDefinition lyrDef = EditionUtilities.createLayerDefinition(layer); |
188 | 6483 | fjp | String aux="FIELDS:"; |
189 | 6604 | caballero | FieldDescription[] flds = lyrDef.getFieldsDesc();
|
190 | 6483 | fjp | for (int i=0; i < flds.length; i++) |
191 | { |
||
192 | 6604 | caballero | aux = aux + ", " + flds[i].getFieldAlias();
|
193 | 6483 | fjp | } |
194 | 6604 | caballero | System.err.println("Escribiendo la capa " + lyrDef.getName() + |
195 | 6483 | fjp | " con los campos " + aux);
|
196 | 5807 | caballero | writer.initialize(lyrDef); |
197 | vea.stopEdition(writer, EditionEvent.GRAPHIC); |
||
198 | |||
199 | } |
||
200 | |||
201 | private void cancelEdition(FLyrVect layer) throws IOException { |
||
202 | 6877 | cesar | com.iver.andami.ui.mdiManager.IWindow[] views = PluginServices
|
203 | 6880 | cesar | .getMDIManager().getAllWindows(); |
204 | 5807 | caballero | 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 | 4430 | fjp | /**
|
215 | 5005 | jorpiell | * @see com.iver.andami.plugins.IExtension#isVisible()
|
216 | 4430 | fjp | */
|
217 | public boolean isVisible() { |
||
218 | if (EditionUtilities.getEditionStatus() == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE)
|
||
219 | return true; |
||
220 | 6604 | caballero | return false; |
221 | 4421 | fjp | |
222 | 4430 | fjp | } |
223 | 4421 | fjp | } |