svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app.document.table.app / org.gvsig.app.document.table.app.mainplugin / src / main / java / org / gvsig / app / extension / TableEditStopExtension.java @ 46815
History | View | Annotate | Download (14 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or modify it under
|
7 |
* the terms of the GNU General Public License as published by the Free Software
|
8 |
* Foundation; either version 3 of the License, or (at your option) any later
|
9 |
* version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
13 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
14 |
* details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License along with
|
17 |
* this program; if not, write to the Free Software Foundation, Inc., 51
|
18 |
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us at info AT
|
21 |
* gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.app.extension; |
24 |
|
25 |
import java.awt.BorderLayout; |
26 |
import java.awt.Component; |
27 |
import java.awt.Font; |
28 |
import java.awt.GridBagConstraints; |
29 |
import java.awt.GridBagLayout; |
30 |
import java.awt.Insets; |
31 |
import java.util.ArrayList; |
32 |
import java.util.List; |
33 |
|
34 |
import javax.swing.JLabel; |
35 |
import javax.swing.JOptionPane; |
36 |
import javax.swing.JPanel; |
37 |
|
38 |
import org.slf4j.Logger; |
39 |
import org.slf4j.LoggerFactory; |
40 |
|
41 |
import org.gvsig.andami.IconThemeHelper; |
42 |
import org.gvsig.andami.PluginServices; |
43 |
import org.gvsig.andami.PluginsLocator; |
44 |
import org.gvsig.andami.actioninfo.ActionInfo; |
45 |
import org.gvsig.andami.messages.NotificationManager; |
46 |
import org.gvsig.andami.plugins.IExtension; |
47 |
import org.gvsig.andami.plugins.status.IExtensionStatus; |
48 |
import org.gvsig.andami.plugins.status.IUnsavedData; |
49 |
import org.gvsig.andami.plugins.status.UnsavedData; |
50 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
51 |
import org.gvsig.app.ApplicationLocator; |
52 |
import org.gvsig.app.ApplicationManager; |
53 |
import org.gvsig.app.project.Project; |
54 |
import org.gvsig.app.project.ProjectManager; |
55 |
import org.gvsig.app.project.documents.Document; |
56 |
import org.gvsig.app.project.documents.table.TableDocument; |
57 |
import org.gvsig.app.project.documents.table.TableManager; |
58 |
import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel; |
59 |
import org.gvsig.fmap.dal.EditingNotification; |
60 |
import org.gvsig.fmap.dal.EditingNotificationManager; |
61 |
import org.gvsig.fmap.dal.exception.DataException; |
62 |
import org.gvsig.fmap.dal.exception.ReadException; |
63 |
import org.gvsig.fmap.dal.exception.WriteException; |
64 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
65 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
66 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
67 |
import org.gvsig.gui.beans.Messages; |
68 |
import org.gvsig.tools.ToolsLocator; |
69 |
import org.gvsig.tools.i18n.I18nManager; |
70 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
71 |
import org.gvsig.tools.util.ArrayUtils; |
72 |
import org.gvsig.utils.swing.threads.IMonitorableTask; |
73 |
|
74 |
public class TableEditStopExtension extends AbstractTableEditExtension { |
75 |
|
76 |
private static Logger LOGGER |
77 |
= LoggerFactory.getLogger(TableEditStopExtension.class); |
78 |
|
79 |
@Override
|
80 |
public void initialize() { |
81 |
super.initialize();
|
82 |
IconThemeHelper.registerIcon("action", "table-stop-editing", this); |
83 |
} |
84 |
|
85 |
public void execute(String actionCommand) { |
86 |
this.execute(actionCommand,null); |
87 |
} |
88 |
|
89 |
@Override
|
90 |
public void execute(String actionCommand, Object[] args) { |
91 |
if ("table-stop-editing".equals(actionCommand)) { |
92 |
TableDocument doc = (TableDocument)ArrayUtils.get(args,0);
|
93 |
if( doc == null ) { |
94 |
doc = (TableDocument) table.getDocument(); |
95 |
} |
96 |
EditingNotificationManager editingNotification = DALSwingLocator.getEditingNotificationManager(); |
97 |
EditingNotification notification = editingNotification.notifyObservers( |
98 |
this,
|
99 |
EditingNotification.BEFORE_EXIT_EDITING_STORE, |
100 |
doc, |
101 |
doc.getStore()); |
102 |
if (notification.isCanceled()) {
|
103 |
return;
|
104 |
} |
105 |
stopEditing(table); |
106 |
ApplicationLocator.getManager().refreshMenusAndToolBars(); |
107 |
editingNotification.notifyObservers( |
108 |
this,
|
109 |
EditingNotification.AFTER_EXIT_EDITING_STORE, |
110 |
doc, |
111 |
doc.getStore()); |
112 |
} |
113 |
} |
114 |
|
115 |
private void stopEditing(FeatureTableDocumentPanel table) { |
116 |
|
117 |
final FeatureStore featureStore = table.getFeatureStore();
|
118 |
boolean allowWrite = featureStore.allowWrite();
|
119 |
|
120 |
ApplicationManager application = ApplicationLocator.getManager(); |
121 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
122 |
String message;
|
123 |
Object[] options; |
124 |
if(allowWrite) {
|
125 |
options = new Object[]{ |
126 |
i18n.getTranslation("_Guardar"),
|
127 |
" " + i18n.getTranslation("_Descartar") + " ", |
128 |
i18n.getTranslation("_Continuar")
|
129 |
}; |
130 |
message = i18n.getTranslation("stop_editing_message");
|
131 |
} else {
|
132 |
options = new Object[]{ |
133 |
i18n.getTranslation("_Export"),
|
134 |
" " + i18n.getTranslation("_Descartar") + " ", |
135 |
i18n.getTranslation("_Continuar")
|
136 |
}; |
137 |
message = i18n.getTranslation("export_and_stop_editing_message");
|
138 |
} |
139 |
|
140 |
int resp = JOptionPane |
141 |
.showOptionDialog( |
142 |
(Component) PluginServices.getMainFrame(),
|
143 |
message, |
144 |
i18n.getTranslation("stop_edition"),
|
145 |
JOptionPane.YES_NO_CANCEL_OPTION,
|
146 |
JOptionPane.QUESTION_MESSAGE, null, options, |
147 |
options[2]);
|
148 |
|
149 |
switch( resp ) {
|
150 |
case JOptionPane.NO_OPTION: |
151 |
try {
|
152 |
// CANCEL EDITING
|
153 |
featureStore.cancelEditing(); |
154 |
} catch (DataException e) {
|
155 |
LOGGER.warn("Problems canceling table editing: "
|
156 |
+ e.getMessage(), e); |
157 |
application.messageDialog( |
158 |
i18n.getTranslation("_Problems_cancel_table_editing")+"\n\n"+ |
159 |
i18n.getTranslation("_see_error_log_for_more_information"),
|
160 |
null,
|
161 |
i18n.getTranslation("_Warning"),
|
162 |
JOptionPane.WARNING_MESSAGE,
|
163 |
"Table_cant_cancelEditing"
|
164 |
); |
165 |
} |
166 |
break;
|
167 |
case JOptionPane.YES_OPTION: |
168 |
try {
|
169 |
if(allowWrite){
|
170 |
// Save table
|
171 |
featureStore.finishEditing(); |
172 |
} else {
|
173 |
exportTable(table.getDocument(), featureStore); |
174 |
} |
175 |
} catch (DataException e) {
|
176 |
LOGGER.warn("Problems finish table editing: "
|
177 |
+ e.getMessage(), e); |
178 |
application.messageDialog( |
179 |
i18n.getTranslation("_Problems_finish_table_editing")+"\n\n"+ |
180 |
i18n.getTranslation("_see_error_log_for_more_information"),
|
181 |
null,
|
182 |
i18n.getTranslation("_Warning"),
|
183 |
JOptionPane.WARNING_MESSAGE,
|
184 |
"Table_cant_finishEditing"
|
185 |
); |
186 |
} |
187 |
break;
|
188 |
default:
|
189 |
// This happens when user clicks on [x]
|
190 |
// to abruptly close previous JOptionPane dialog
|
191 |
// We do nothing (equivalent to 'Continue editing')
|
192 |
break;
|
193 |
} |
194 |
} |
195 |
|
196 |
private void exportTable(Document document, FeatureStore store) { |
197 |
ApplicationManager appManager = ApplicationLocator.getManager(); |
198 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
199 |
ActionInfo action = PluginsLocator.getActionInfoManager().getAction("table-exportto");
|
200 |
action.execute(new Object[]{document, WindowManager.MODE.toInteger(WindowManager.MODE.DIALOG)}); |
201 |
String msg = "_Do_you_want_to_finish_editing_If_not_exported_the_data_the_changes_will_be_lost"; |
202 |
if (appManager.confirmDialog(
|
203 |
i18n.getTranslation(msg), |
204 |
i18n.getTranslation("end_editing"),
|
205 |
JOptionPane.YES_NO_OPTION,
|
206 |
JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) { |
207 |
try {
|
208 |
store.cancelEditing(); |
209 |
} catch (Exception e) { |
210 |
LOGGER.warn("Can't abort editing of table '" + document.getName() + "'.", e); |
211 |
msg = i18n.getTranslation("_Problems_finish_table_editing")+"\n\n"+ |
212 |
i18n.getTranslation("_see_error_log_for_more_information");
|
213 |
appManager.messageDialog(msg, "_Warning", JOptionPane.WARNING_MESSAGE); |
214 |
} |
215 |
} |
216 |
} |
217 |
|
218 |
|
219 |
public boolean isEnabled() { |
220 |
return true; |
221 |
} |
222 |
|
223 |
@Override
|
224 |
public boolean isVisible() { |
225 |
IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
226 |
|
227 |
if (v == null) { |
228 |
return false; |
229 |
} else if (v instanceof FeatureTableDocumentPanel |
230 |
&& ((FeatureTableDocumentPanel) v).getModel().getStore() |
231 |
.isEditing()) { |
232 |
table = (FeatureTableDocumentPanel) v; |
233 |
return true; |
234 |
} else {
|
235 |
return false; |
236 |
} |
237 |
} |
238 |
|
239 |
/**
|
240 |
* <p>
|
241 |
* This class provides the status of extensions. If this extension has some
|
242 |
* unsaved editing table (and save them), and methods to check if the
|
243 |
* extension has some associated background tasks.
|
244 |
*
|
245 |
*/
|
246 |
private class StopEditingStatus implements IExtensionStatus { |
247 |
|
248 |
/**
|
249 |
* This method is used to check if this extension has some unsaved
|
250 |
* editing tables.
|
251 |
*
|
252 |
* @return true if the extension has some unsaved editing tables, false
|
253 |
* otherwise.
|
254 |
*/
|
255 |
public boolean hasUnsavedData() { |
256 |
Project project = ProjectManager.getInstance().getCurrentProject(); |
257 |
List<Document> tables = project.getDocuments(TableManager.TYPENAME); |
258 |
for (int i = 0; i < tables.size(); i++) { |
259 |
FeatureStore store = ((TableDocument) tables.get(i)).getStore(); |
260 |
if (store == null) { |
261 |
continue;
|
262 |
} |
263 |
if (store.isEditing()) {
|
264 |
return true; |
265 |
} |
266 |
} |
267 |
return false; |
268 |
} |
269 |
|
270 |
/**
|
271 |
* This method is used to check if the extension has some associated
|
272 |
* background process which is currently running.
|
273 |
*
|
274 |
* @return true if the extension has some associated background process,
|
275 |
* false otherwise.
|
276 |
*/
|
277 |
public boolean hasRunningProcesses() { |
278 |
return false; |
279 |
} |
280 |
|
281 |
/**
|
282 |
* <p>
|
283 |
* Gets an array of the traceable background tasks associated with this
|
284 |
* extension. These tasks may be tracked, canceled, etc.
|
285 |
* </p>
|
286 |
*
|
287 |
* @return An array of the associated background tasks, or null in case
|
288 |
* there is no associated background tasks.
|
289 |
*/
|
290 |
public IMonitorableTask[] getRunningProcesses() { |
291 |
return null; |
292 |
} |
293 |
|
294 |
/**
|
295 |
* <p>
|
296 |
* Gets an array of the UnsavedData objects, which contain information
|
297 |
* about the unsaved editing tables and allows to save it.
|
298 |
* </p>
|
299 |
*
|
300 |
* @return An array of the associated unsaved editing layers, or null in
|
301 |
* case the extension has not unsaved editing tables.
|
302 |
*/
|
303 |
public IUnsavedData[] getUnsavedData() { |
304 |
Project project = ProjectManager.getInstance().getCurrentProject(); |
305 |
List<Document> tables = project.getDocuments(TableManager.TYPENAME); |
306 |
List<UnsavedTable> unsavedTables = new ArrayList<UnsavedTable>(); |
307 |
for (int i = 0; i < tables.size(); i++) { |
308 |
TableDocument table = (TableDocument) tables.get(i); |
309 |
FeatureStore store = table.getStore(); |
310 |
if (store == null) { |
311 |
continue;
|
312 |
} |
313 |
if (store.isEditing()) {
|
314 |
UnsavedTable ul |
315 |
= new UnsavedTable(TableEditStopExtension.this);
|
316 |
ul.setTable(table); |
317 |
unsavedTables.add(ul); |
318 |
} |
319 |
} |
320 |
return unsavedTables
|
321 |
.toArray(new IUnsavedData[unsavedTables.size()]);
|
322 |
} |
323 |
} |
324 |
|
325 |
private class UnsavedTable extends UnsavedData { |
326 |
|
327 |
private TableDocument table;
|
328 |
|
329 |
public UnsavedTable(IExtension extension) {
|
330 |
super(extension);
|
331 |
} |
332 |
|
333 |
public String getDescription() { |
334 |
return PluginServices.getText(this, "editing_table_unsaved"); |
335 |
} |
336 |
|
337 |
public String getResourceName() { |
338 |
return table.getName();
|
339 |
} |
340 |
|
341 |
public boolean saveData() { |
342 |
return executeSaveTable(table);
|
343 |
} |
344 |
|
345 |
public void setTable(TableDocument table) { |
346 |
this.table = table;
|
347 |
} |
348 |
|
349 |
@Override
|
350 |
public String getIcon() { |
351 |
return "document-table-icon-small"; |
352 |
} |
353 |
} |
354 |
|
355 |
// TODO Este codigo esta duplicado, tambien esta en la clase Table en el
|
356 |
// metodo "public void stopEditing()"
|
357 |
private boolean executeSaveTable(TableDocument table2) { |
358 |
FeatureStore fs = table2.getStore(); |
359 |
if (fs.isEditing()) {
|
360 |
try {
|
361 |
fs.finishEditing(); |
362 |
} catch (WriteException e) {
|
363 |
NotificationManager.addError(PluginServices.getText(this, "error_saving_table"), e); |
364 |
return false; |
365 |
} catch (ReadException e) {
|
366 |
NotificationManager.addError(PluginServices.getText(this, "error_saving_table"), e); |
367 |
return false; |
368 |
} catch (DataException e) {
|
369 |
NotificationManager.addError(PluginServices.getText(this, "error_saving_table"), e); |
370 |
return false; |
371 |
} |
372 |
} |
373 |
return true; |
374 |
} |
375 |
|
376 |
@Override
|
377 |
public IExtensionStatus getStatus() {
|
378 |
return new StopEditingStatus(); |
379 |
} |
380 |
} |