Revision 13517 branches/v10/applications/appgvSIG/src/com/iver/cit/gvsig/TableEditStopExtension.java
TableEditStopExtension.java | ||
---|---|---|
1 | 1 |
package com.iver.cit.gvsig; |
2 | 2 |
|
3 | 3 |
import java.io.IOException; |
4 |
import java.util.ArrayList; |
|
4 | 5 |
|
5 | 6 |
import javax.swing.JOptionPane; |
6 | 7 |
|
8 |
import com.hardcode.driverManager.DriverLoadException; |
|
9 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
|
7 | 10 |
import com.iver.andami.PluginServices; |
11 |
import com.iver.andami.messages.NotificationManager; |
|
8 | 12 |
import com.iver.andami.plugins.Extension; |
13 |
import com.iver.andami.plugins.IExtension; |
|
14 |
import com.iver.andami.plugins.status.IExtensionStatus; |
|
15 |
import com.iver.andami.plugins.status.IUnsavedData; |
|
16 |
import com.iver.andami.plugins.status.UnsavedData; |
|
9 | 17 |
import com.iver.andami.ui.mdiManager.IWindow; |
18 |
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition; |
|
19 |
import com.iver.cit.gvsig.fmap.edition.EditionEvent; |
|
20 |
import com.iver.cit.gvsig.fmap.edition.EditionException; |
|
21 |
import com.iver.cit.gvsig.fmap.edition.IEditableSource; |
|
22 |
import com.iver.cit.gvsig.fmap.edition.IWriteable; |
|
23 |
import com.iver.cit.gvsig.fmap.edition.IWriter; |
|
24 |
import com.iver.cit.gvsig.project.documents.table.ProjectTable; |
|
25 |
import com.iver.cit.gvsig.project.documents.table.ProjectTableFactory; |
|
10 | 26 |
import com.iver.cit.gvsig.project.documents.table.gui.Table; |
27 |
import com.iver.utiles.swing.threads.IMonitorableTask; |
|
11 | 28 |
|
12 | 29 |
|
13 | 30 |
/** |
... | ... | |
77 | 94 |
return false; |
78 | 95 |
} |
79 | 96 |
} |
97 |
/** |
|
98 |
* <p>This class provides the status of extensions. |
|
99 |
* If this extension has some unsaved editing table (and save them), and methods |
|
100 |
* to check if the extension has some associated background tasks. |
|
101 |
* |
|
102 |
* @author Vicente Caballero Navarro |
|
103 |
* |
|
104 |
*/ |
|
105 |
private class StopEditingStatus implements IExtensionStatus { |
|
106 |
/** |
|
107 |
* This method is used to check if this extension has some unsaved editing tables. |
|
108 |
* |
|
109 |
* @return true if the extension has some unsaved editing tables, false otherwise. |
|
110 |
*/ |
|
111 |
public boolean hasUnsavedData() { |
|
112 |
ProjectExtension pe=(ProjectExtension)PluginServices.getExtension(ProjectExtension.class); |
|
113 |
ProjectTable[] tables=(ProjectTable[])pe.getProject().getDocumentsByType(ProjectTableFactory.registerName).toArray(new ProjectTable[0]); |
|
114 |
for (int i=0;i<tables.length;i++) { |
|
115 |
if (tables[i].getModelo().isEditing()) |
|
116 |
return true; |
|
117 |
} |
|
118 |
return false; |
|
119 |
} |
|
120 |
/** |
|
121 |
* This method is used to check if the extension has some associated |
|
122 |
* background process which is currently running. |
|
123 |
* |
|
124 |
* @return true if the extension has some associated background process, |
|
125 |
* false otherwise. |
|
126 |
*/ |
|
127 |
public boolean hasRunningProcesses() { |
|
128 |
return false; |
|
129 |
} |
|
130 |
/** |
|
131 |
* <p>Gets an array of the traceable background tasks associated with this |
|
132 |
* extension. These tasks may be tracked, canceled, etc.</p> |
|
133 |
* |
|
134 |
* @return An array of the associated background tasks, or null in case there is |
|
135 |
* no associated background tasks. |
|
136 |
*/ |
|
137 |
public IMonitorableTask[] getRunningProcesses() { |
|
138 |
return null; |
|
139 |
} |
|
140 |
/** |
|
141 |
* <p>Gets an array of the UnsavedData objects, which contain information about |
|
142 |
* the unsaved editing tables and allows to save it.</p> |
|
143 |
* |
|
144 |
* @return An array of the associated unsaved editing layers, or null in case the extension |
|
145 |
* has not unsaved editing tables. |
|
146 |
*/ |
|
147 |
public IUnsavedData[] getUnsavedData() { |
|
148 |
ProjectExtension pe=(ProjectExtension)PluginServices.getExtension(ProjectExtension.class); |
|
149 |
ProjectTable[] tables =(ProjectTable[])pe.getProject().getDocumentsByType(ProjectTableFactory.registerName).toArray(new ProjectTable[0]); |
|
150 |
ArrayList unsavedTables = new ArrayList(); |
|
151 |
for (int i=0;i<tables.length;i++) { |
|
152 |
if (tables[i].getModelo().isEditing()) { |
|
153 |
UnsavedTable ul=new UnsavedTable(TableEditStopExtension.this); |
|
154 |
ul.setTable(tables[i]); |
|
155 |
unsavedTables.add(ul); |
|
156 |
} |
|
157 |
} |
|
158 |
return (IUnsavedData[])unsavedTables.toArray(new IUnsavedData[0]); |
|
159 |
} |
|
160 |
} |
|
161 |
|
|
162 |
private class UnsavedTable extends UnsavedData{ |
|
163 |
|
|
164 |
private ProjectTable table; |
|
165 |
|
|
166 |
public UnsavedTable(IExtension extension) { |
|
167 |
super(extension); |
|
168 |
} |
|
169 |
|
|
170 |
public String getDescription() { |
|
171 |
return PluginServices.getText(this,"editing_table_unsaved"); |
|
172 |
} |
|
173 |
|
|
174 |
public String getResourceName() { |
|
175 |
return table.getName(); |
|
176 |
} |
|
177 |
|
|
178 |
|
|
179 |
|
|
180 |
public boolean saveData() { |
|
181 |
return executeSaveTable(table); |
|
182 |
} |
|
183 |
|
|
184 |
|
|
185 |
|
|
186 |
public void setTable(ProjectTable table) { |
|
187 |
this.table=table; |
|
188 |
|
|
189 |
} |
|
190 |
} |
|
191 |
|
|
192 |
|
|
193 |
//TODO Este c?digo est? duplicado, tambi?n est? en la clase Table en el m?todo "public void stopEditing()" |
|
194 |
private boolean executeSaveTable(ProjectTable table2) { |
|
195 |
IEditableSource ies = table2.getModelo(); |
|
196 |
if (ies instanceof IWriteable) { |
|
197 |
IWriteable w = (IWriteable) ies; |
|
198 |
IWriter writer = w.getWriter(); |
|
199 |
if (writer == null) { |
|
200 |
return false; |
|
201 |
} |
|
202 |
try { |
|
203 |
ITableDefinition tableDef = ies.getTableDefinition(); |
|
204 |
writer.initialize(tableDef); |
|
205 |
ies.stopEdition(writer, EditionEvent.ALPHANUMERIC); |
|
206 |
ies.getSelection().clear(); |
|
207 |
} catch (DriverLoadException e) { |
|
208 |
NotificationManager.addError(PluginServices.getText(this,"error_saving_table"),e); |
|
209 |
return false; |
|
210 |
} catch (EditionException e) { |
|
211 |
NotificationManager.addError(PluginServices.getText(this,"error_saving_table"),e); |
|
212 |
return false; |
|
213 |
} catch (DriverException e) { |
|
214 |
NotificationManager.addError(PluginServices.getText(this,"error_saving_table"),e); |
|
215 |
return false; |
|
216 |
} |
|
217 |
} |
|
218 |
return true; |
|
219 |
} |
|
220 |
public IExtensionStatus getStatus() { |
|
221 |
return new StopEditingStatus(); |
|
222 |
} |
|
80 | 223 |
} |
Also available in: Unified diff