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 @ 44259
History | View | Annotate | Download (13.4 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.messages.NotificationManager; |
44 |
import org.gvsig.andami.plugins.IExtension; |
45 |
import org.gvsig.andami.plugins.status.IExtensionStatus; |
46 |
import org.gvsig.andami.plugins.status.IUnsavedData; |
47 |
import org.gvsig.andami.plugins.status.UnsavedData; |
48 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
49 |
import org.gvsig.app.ApplicationLocator; |
50 |
import org.gvsig.app.ApplicationManager; |
51 |
import org.gvsig.app.project.Project; |
52 |
import org.gvsig.app.project.ProjectManager; |
53 |
import org.gvsig.app.project.documents.Document; |
54 |
import org.gvsig.app.project.documents.table.TableDocument; |
55 |
import org.gvsig.app.project.documents.table.TableManager; |
56 |
import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel; |
57 |
import org.gvsig.fmap.dal.EditingNotification; |
58 |
import org.gvsig.fmap.dal.EditingNotificationManager; |
59 |
import org.gvsig.fmap.dal.exception.DataException; |
60 |
import org.gvsig.fmap.dal.exception.ReadException; |
61 |
import org.gvsig.fmap.dal.exception.WriteException; |
62 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
63 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
64 |
import org.gvsig.gui.beans.Messages; |
65 |
import org.gvsig.tools.ToolsLocator; |
66 |
import org.gvsig.tools.i18n.I18nManager; |
67 |
import org.gvsig.tools.util.ArrayUtils; |
68 |
import org.gvsig.utils.swing.threads.IMonitorableTask; |
69 |
|
70 |
public class TableEditStopExtension extends AbstractTableEditExtension { |
71 |
|
72 |
private static Logger logger |
73 |
= LoggerFactory.getLogger(TableEditStopExtension.class); |
74 |
|
75 |
@Override
|
76 |
public void initialize() { |
77 |
super.initialize();
|
78 |
IconThemeHelper.registerIcon("action", "table-stop-editing", this); |
79 |
} |
80 |
|
81 |
public void execute(String actionCommand) { |
82 |
this.execute(actionCommand,null); |
83 |
} |
84 |
|
85 |
@Override
|
86 |
public void execute(String actionCommand, Object[] args) { |
87 |
if ("table-stop-editing".equals(actionCommand)) { |
88 |
TableDocument doc = (TableDocument)ArrayUtils.get(args,0);
|
89 |
if( doc == null ) { |
90 |
doc = (TableDocument) table.getDocument(); |
91 |
} |
92 |
EditingNotificationManager editingNotification = DALSwingLocator.getEditingNotificationManager(); |
93 |
EditingNotification notification = editingNotification.notifyObservers( |
94 |
this,
|
95 |
EditingNotification.BEFORE_ENTER_EDITING_STORE, |
96 |
doc, |
97 |
doc.getStore()); |
98 |
if (notification.isCanceled()) {
|
99 |
return;
|
100 |
} |
101 |
stopEditing(table); |
102 |
ApplicationLocator.getManager().refreshMenusAndToolBars(); |
103 |
editingNotification.notifyObservers( |
104 |
this,
|
105 |
EditingNotification.AFTER_ENTER_EDITING_STORE, |
106 |
doc, |
107 |
doc.getStore()); |
108 |
} |
109 |
} |
110 |
|
111 |
private void stopEditing(FeatureTableDocumentPanel table) { |
112 |
|
113 |
ApplicationManager application = ApplicationLocator.getManager(); |
114 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
115 |
|
116 |
Object[] options = { |
117 |
i18n.getTranslation("_Guardar"),
|
118 |
" " + i18n.getTranslation("_Descartar") + " ", |
119 |
i18n.getTranslation("_Continuar")
|
120 |
}; |
121 |
|
122 |
JPanel explanation_panel = getExplanationPanel(table.getModel().getName());
|
123 |
|
124 |
int resp = JOptionPane |
125 |
.showOptionDialog( |
126 |
(Component) PluginServices.getMainFrame(),
|
127 |
explanation_panel, |
128 |
PluginServices.getText(this, "stop_edition"), |
129 |
JOptionPane.YES_NO_CANCEL_OPTION,
|
130 |
JOptionPane.QUESTION_MESSAGE, null, options, |
131 |
options[2]);
|
132 |
|
133 |
switch( resp ) {
|
134 |
case JOptionPane.NO_OPTION: |
135 |
try {
|
136 |
// CANCEL EDITING
|
137 |
table.getModel().getStore().cancelEditing(); |
138 |
} catch (DataException e) {
|
139 |
logger.warn("Problems canceling table editing: "
|
140 |
+ e.getMessage(), e); |
141 |
application.messageDialog( |
142 |
i18n.getTranslation("_Problems_cancel_table_editing")+"\n\n"+ |
143 |
i18n.getTranslation("_See_error_log_for_more_information"),
|
144 |
null,
|
145 |
i18n.getTranslation("_Warning"),
|
146 |
JOptionPane.WARNING_MESSAGE,
|
147 |
"Table_cant_cancelEditing"
|
148 |
); |
149 |
} |
150 |
break;
|
151 |
case JOptionPane.YES_OPTION: |
152 |
try {
|
153 |
// Save table
|
154 |
table.getModel().getStore().finishEditing(); |
155 |
} catch (DataException e) {
|
156 |
logger.warn("Problems finish table editing: "
|
157 |
+ e.getMessage(), e); |
158 |
application.messageDialog( |
159 |
i18n.getTranslation("_Problems_finish_table_editing")+"\n\n"+ |
160 |
i18n.getTranslation("_See_error_log_for_more_information"),
|
161 |
null,
|
162 |
i18n.getTranslation("_Warning"),
|
163 |
JOptionPane.WARNING_MESSAGE,
|
164 |
"Table_cant_finishEditing"
|
165 |
); |
166 |
} |
167 |
break;
|
168 |
default:
|
169 |
// This happens when user clicks on [x]
|
170 |
// to abruptly close previous JOptionPane dialog
|
171 |
// We do nothing (equivalent to 'Continue editing')
|
172 |
break;
|
173 |
} |
174 |
} |
175 |
|
176 |
public boolean isEnabled() { |
177 |
return true; |
178 |
} |
179 |
|
180 |
@Override
|
181 |
public boolean isVisible() { |
182 |
IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
183 |
|
184 |
if (v == null) { |
185 |
return false; |
186 |
} else if (v instanceof FeatureTableDocumentPanel |
187 |
&& ((FeatureTableDocumentPanel) v).getModel().getStore() |
188 |
.isEditing()) { |
189 |
table = (FeatureTableDocumentPanel) v; |
190 |
return true; |
191 |
} else {
|
192 |
return false; |
193 |
} |
194 |
} |
195 |
|
196 |
/**
|
197 |
* <p>
|
198 |
* This class provides the status of extensions. If this extension has some
|
199 |
* unsaved editing table (and save them), and methods to check if the
|
200 |
* extension has some associated background tasks.
|
201 |
*
|
202 |
*/
|
203 |
private class StopEditingStatus implements IExtensionStatus { |
204 |
|
205 |
/**
|
206 |
* This method is used to check if this extension has some unsaved
|
207 |
* editing tables.
|
208 |
*
|
209 |
* @return true if the extension has some unsaved editing tables, false
|
210 |
* otherwise.
|
211 |
*/
|
212 |
public boolean hasUnsavedData() { |
213 |
Project project = ProjectManager.getInstance().getCurrentProject(); |
214 |
List<Document> tables = project.getDocuments(TableManager.TYPENAME); |
215 |
for (int i = 0; i < tables.size(); i++) { |
216 |
FeatureStore store = ((TableDocument) tables.get(i)).getStore(); |
217 |
if (store == null) { |
218 |
continue;
|
219 |
} |
220 |
if (store.isEditing()) {
|
221 |
return true; |
222 |
} |
223 |
} |
224 |
return false; |
225 |
} |
226 |
|
227 |
/**
|
228 |
* This method is used to check if the extension has some associated
|
229 |
* background process which is currently running.
|
230 |
*
|
231 |
* @return true if the extension has some associated background process,
|
232 |
* false otherwise.
|
233 |
*/
|
234 |
public boolean hasRunningProcesses() { |
235 |
return false; |
236 |
} |
237 |
|
238 |
/**
|
239 |
* <p>
|
240 |
* Gets an array of the traceable background tasks associated with this
|
241 |
* extension. These tasks may be tracked, canceled, etc.
|
242 |
* </p>
|
243 |
*
|
244 |
* @return An array of the associated background tasks, or null in case
|
245 |
* there is no associated background tasks.
|
246 |
*/
|
247 |
public IMonitorableTask[] getRunningProcesses() { |
248 |
return null; |
249 |
} |
250 |
|
251 |
/**
|
252 |
* <p>
|
253 |
* Gets an array of the UnsavedData objects, which contain information
|
254 |
* about the unsaved editing tables and allows to save it.
|
255 |
* </p>
|
256 |
*
|
257 |
* @return An array of the associated unsaved editing layers, or null in
|
258 |
* case the extension has not unsaved editing tables.
|
259 |
*/
|
260 |
public IUnsavedData[] getUnsavedData() { |
261 |
Project project = ProjectManager.getInstance().getCurrentProject(); |
262 |
List<Document> tables = project.getDocuments(TableManager.TYPENAME); |
263 |
List<UnsavedTable> unsavedTables = new ArrayList<UnsavedTable>(); |
264 |
for (int i = 0; i < tables.size(); i++) { |
265 |
TableDocument table = (TableDocument) tables.get(i); |
266 |
FeatureStore store = table.getStore(); |
267 |
if (store == null) { |
268 |
continue;
|
269 |
} |
270 |
if (store.isEditing()) {
|
271 |
UnsavedTable ul |
272 |
= new UnsavedTable(TableEditStopExtension.this);
|
273 |
ul.setTable(table); |
274 |
unsavedTables.add(ul); |
275 |
} |
276 |
} |
277 |
return unsavedTables
|
278 |
.toArray(new IUnsavedData[unsavedTables.size()]);
|
279 |
} |
280 |
} |
281 |
|
282 |
private class UnsavedTable extends UnsavedData { |
283 |
|
284 |
private TableDocument table;
|
285 |
|
286 |
public UnsavedTable(IExtension extension) {
|
287 |
super(extension);
|
288 |
} |
289 |
|
290 |
public String getDescription() { |
291 |
return PluginServices.getText(this, "editing_table_unsaved"); |
292 |
} |
293 |
|
294 |
public String getResourceName() { |
295 |
return table.getName();
|
296 |
} |
297 |
|
298 |
public boolean saveData() { |
299 |
return executeSaveTable(table);
|
300 |
} |
301 |
|
302 |
public void setTable(TableDocument table) { |
303 |
this.table = table;
|
304 |
} |
305 |
|
306 |
@Override
|
307 |
public String getIcon() { |
308 |
return "document-table-icon-small"; |
309 |
} |
310 |
} |
311 |
|
312 |
// TODO Este codigo esta duplicado, tambien esta en la clase Table en el
|
313 |
// metodo "public void stopEditing()"
|
314 |
private boolean executeSaveTable(TableDocument table2) { |
315 |
FeatureStore fs = table2.getStore(); |
316 |
if (fs.isEditing()) {
|
317 |
try {
|
318 |
fs.finishEditing(); |
319 |
} catch (WriteException e) {
|
320 |
NotificationManager.addError(PluginServices.getText(this, "error_saving_table"), e); |
321 |
return false; |
322 |
} catch (ReadException e) {
|
323 |
NotificationManager.addError(PluginServices.getText(this, "error_saving_table"), e); |
324 |
return false; |
325 |
} catch (DataException e) {
|
326 |
NotificationManager.addError(PluginServices.getText(this, "error_saving_table"), e); |
327 |
return false; |
328 |
} |
329 |
} |
330 |
return true; |
331 |
} |
332 |
|
333 |
@Override
|
334 |
public IExtensionStatus getStatus() {
|
335 |
return new StopEditingStatus(); |
336 |
} |
337 |
|
338 |
private JPanel getExplanationPanel(String name) { |
339 |
|
340 |
BorderLayout bl = new BorderLayout(10, 10); |
341 |
JPanel resp = new JPanel(bl); |
342 |
|
343 |
String msg = Messages.getText("realmente_desea_guardar"); |
344 |
JLabel topLabel = new JLabel(msg); |
345 |
|
346 |
JPanel mainPanel = new JPanel(new GridBagLayout()); |
347 |
GridBagConstraints cc = new GridBagConstraints(); |
348 |
|
349 |
cc.gridx = 0;
|
350 |
cc.gridy = 0;
|
351 |
cc.anchor = GridBagConstraints.WEST;
|
352 |
|
353 |
cc.insets = new Insets(3, 6, 3, 6); |
354 |
|
355 |
Font boldf = mainPanel.getFont().deriveFont(Font.BOLD); |
356 |
|
357 |
JLabel lbl = new JLabel(Messages.getText("_Guardar")); |
358 |
lbl.setFont(boldf); |
359 |
mainPanel.add(lbl, cc); |
360 |
cc.gridx = 1;
|
361 |
mainPanel.add(new JLabel(Messages.getText("_Save_changes_performed")), cc); |
362 |
|
363 |
cc.gridx = 0;
|
364 |
cc.gridy = 1;
|
365 |
lbl = new JLabel(Messages.getText("_Descartar")); |
366 |
lbl.setFont(boldf); |
367 |
mainPanel.add(lbl, cc); |
368 |
cc.gridx = 1;
|
369 |
mainPanel.add(new JLabel(Messages.getText("_Discard_and_lose_changes")), cc); |
370 |
|
371 |
cc.gridx = 0;
|
372 |
cc.gridy = 2;
|
373 |
lbl = new JLabel(Messages.getText("_Continuar")); |
374 |
lbl.setFont(boldf); |
375 |
mainPanel.add(lbl, cc); |
376 |
cc.gridx = 1;
|
377 |
mainPanel.add(new JLabel(Messages.getText("_Do_not_save_yet_Stay_in_editing_mode")), cc); |
378 |
|
379 |
resp.add(mainPanel, BorderLayout.CENTER);
|
380 |
resp.add(topLabel, BorderLayout.NORTH);
|
381 |
return resp;
|
382 |
} |
383 |
} |