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