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 @ 42775
History | View | Annotate | Download (13.1 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
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.app.extension; |
25 |
|
26 |
import java.awt.BorderLayout; |
27 |
import java.awt.Component; |
28 |
import java.awt.Font; |
29 |
import java.awt.GridBagConstraints; |
30 |
import java.awt.GridBagLayout; |
31 |
import java.awt.Insets; |
32 |
import java.util.ArrayList; |
33 |
import java.util.List; |
34 |
|
35 |
import javax.swing.JLabel; |
36 |
import javax.swing.JOptionPane; |
37 |
import javax.swing.JPanel; |
38 |
|
39 |
import org.slf4j.Logger; |
40 |
import org.slf4j.LoggerFactory; |
41 |
|
42 |
import org.gvsig.andami.IconThemeHelper; |
43 |
import org.gvsig.andami.PluginServices; |
44 |
import org.gvsig.andami.messages.NotificationManager; |
45 |
import org.gvsig.andami.plugins.IExtension; |
46 |
import org.gvsig.andami.plugins.status.IExtensionStatus; |
47 |
import org.gvsig.andami.plugins.status.IUnsavedData; |
48 |
import org.gvsig.andami.plugins.status.UnsavedData; |
49 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
50 |
import org.gvsig.app.ApplicationLocator; |
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.utils.swing.threads.IMonitorableTask; |
66 |
|
67 |
public class TableEditStopExtension extends AbstractTableEditExtension { |
68 |
|
69 |
private static Logger logger = |
70 |
LoggerFactory.getLogger(TableEditStopExtension.class); |
71 |
|
72 |
public void initialize() { |
73 |
super.initialize();
|
74 |
IconThemeHelper.registerIcon("action", "table-stop-editing", this); |
75 |
} |
76 |
|
77 |
public void execute(String actionCommand) { |
78 |
if ("table-stop-editing".equals(actionCommand)) { |
79 |
TableDocument doc = (TableDocument) table.getDocument(); |
80 |
EditingNotificationManager editingNotification = DALSwingLocator.getEditingNotificationManager(); |
81 |
EditingNotification notification = editingNotification.notifyObservers( |
82 |
this,
|
83 |
EditingNotification.BEFORE_ENTER_EDITING_STORE, |
84 |
doc, |
85 |
doc.getStore()); |
86 |
if( notification.isCanceled() ) {
|
87 |
return;
|
88 |
} |
89 |
stopEditing(table); |
90 |
ApplicationLocator.getManager().refreshMenusAndToolBars(); |
91 |
editingNotification.notifyObservers( |
92 |
this,
|
93 |
EditingNotification.AFTER_ENTER_EDITING_STORE, |
94 |
doc, |
95 |
doc.getStore()); |
96 |
} |
97 |
} |
98 |
|
99 |
private void stopEditing(FeatureTableDocumentPanel table) { |
100 |
|
101 |
Object[] options = { |
102 |
PluginServices.getText(this, "_Guardar"), |
103 |
" " + PluginServices.getText(this, "_Descartar") + " ", |
104 |
PluginServices.getText(this, "_Continuar") }; |
105 |
|
106 |
JPanel explanation_panel = getExplanationPanel(table.getModel().getName());
|
107 |
|
108 |
int resp = JOptionPane |
109 |
.showOptionDialog( |
110 |
(Component) PluginServices.getMainFrame(),
|
111 |
explanation_panel, |
112 |
PluginServices.getText(this, "stop_edition"), |
113 |
JOptionPane.YES_NO_CANCEL_OPTION,
|
114 |
JOptionPane.QUESTION_MESSAGE, null, options, |
115 |
options[2]);
|
116 |
|
117 |
try {
|
118 |
if (resp == JOptionPane.NO_OPTION) { |
119 |
// CANCEL EDITING
|
120 |
table.getModel().getStore().cancelEditing(); |
121 |
} else {
|
122 |
|
123 |
if (resp == JOptionPane.YES_OPTION) { |
124 |
// Save table
|
125 |
table.getModel().getStore().finishEditing(); |
126 |
} else {
|
127 |
// This happens when user clicks on [x]
|
128 |
// to abruptly close previous JOptionPane dialog
|
129 |
// We do nothing (equivalent to 'Continue editing')
|
130 |
} |
131 |
} |
132 |
} catch (DataException e) {
|
133 |
logger.error("While finishing or canceling table editing: "
|
134 |
+ e.getMessage(), e); |
135 |
} |
136 |
} |
137 |
|
138 |
/**
|
139 |
* @see org.gvsig.andami.plugins.IExtension#isEnabled()
|
140 |
*/
|
141 |
public boolean isEnabled() { |
142 |
return true; |
143 |
} |
144 |
|
145 |
/**
|
146 |
* @see org.gvsig.andami.plugins.IExtension#isVisible()
|
147 |
*/
|
148 |
public boolean isVisible() { |
149 |
IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
150 |
|
151 |
if (v == null) { |
152 |
return false; |
153 |
} else
|
154 |
if (v instanceof FeatureTableDocumentPanel |
155 |
&& ((FeatureTableDocumentPanel) v).getModel().getStore() |
156 |
.isEditing()) { |
157 |
table = (FeatureTableDocumentPanel) v; |
158 |
return true; |
159 |
} else {
|
160 |
return false; |
161 |
} |
162 |
} |
163 |
|
164 |
/**
|
165 |
* <p>
|
166 |
* This class provides the status of extensions. If this extension has some
|
167 |
* unsaved editing table (and save them), and methods to check if the
|
168 |
* extension has some associated background tasks.
|
169 |
*
|
170 |
* @author Vicente Caballero Navarro
|
171 |
*
|
172 |
*/
|
173 |
private class StopEditingStatus implements IExtensionStatus { |
174 |
|
175 |
/**
|
176 |
* This method is used to check if this extension has some unsaved
|
177 |
* editing tables.
|
178 |
*
|
179 |
* @return true if the extension has some unsaved editing tables, false
|
180 |
* otherwise.
|
181 |
*/
|
182 |
public boolean hasUnsavedData() { |
183 |
Project project = ProjectManager.getInstance().getCurrentProject(); |
184 |
List<Document> tables = project.getDocuments(TableManager.TYPENAME); |
185 |
for (int i = 0; i < tables.size(); i++) { |
186 |
FeatureStore store = ((TableDocument) tables.get(i)).getStore(); |
187 |
if (store == null) { |
188 |
continue;
|
189 |
} |
190 |
if (store.isEditing()) {
|
191 |
return true; |
192 |
} |
193 |
} |
194 |
return false; |
195 |
} |
196 |
|
197 |
/**
|
198 |
* This method is used to check if the extension has some associated
|
199 |
* background process which is currently running.
|
200 |
*
|
201 |
* @return true if the extension has some associated background process,
|
202 |
* false otherwise.
|
203 |
*/
|
204 |
public boolean hasRunningProcesses() { |
205 |
return false; |
206 |
} |
207 |
|
208 |
/**
|
209 |
* <p>
|
210 |
* Gets an array of the traceable background tasks associated with this
|
211 |
* extension. These tasks may be tracked, canceled, etc.
|
212 |
* </p>
|
213 |
*
|
214 |
* @return An array of the associated background tasks, or null in case
|
215 |
* there is
|
216 |
* no associated background tasks.
|
217 |
*/
|
218 |
public IMonitorableTask[] getRunningProcesses() { |
219 |
return null; |
220 |
} |
221 |
|
222 |
/**
|
223 |
* <p>
|
224 |
* Gets an array of the UnsavedData objects, which contain information
|
225 |
* about the unsaved editing tables and allows to save it.
|
226 |
* </p>
|
227 |
*
|
228 |
* @return An array of the associated unsaved editing layers, or null in
|
229 |
* case the extension
|
230 |
* has not unsaved editing tables.
|
231 |
*/
|
232 |
public IUnsavedData[] getUnsavedData() { |
233 |
Project project = ProjectManager.getInstance().getCurrentProject(); |
234 |
List<Document> tables = project.getDocuments(TableManager.TYPENAME); |
235 |
List<UnsavedTable> unsavedTables = new ArrayList<UnsavedTable>(); |
236 |
for (int i = 0; i < tables.size(); i++) { |
237 |
TableDocument table = (TableDocument) tables.get(i); |
238 |
FeatureStore store = table.getStore(); |
239 |
if (store == null) { |
240 |
continue;
|
241 |
} |
242 |
if (store.isEditing()) {
|
243 |
UnsavedTable ul = |
244 |
new UnsavedTable(TableEditStopExtension.this);
|
245 |
ul.setTable(table); |
246 |
unsavedTables.add(ul); |
247 |
} |
248 |
} |
249 |
return unsavedTables
|
250 |
.toArray(new IUnsavedData[unsavedTables.size()]);
|
251 |
} |
252 |
} |
253 |
|
254 |
private class UnsavedTable extends UnsavedData { |
255 |
|
256 |
private TableDocument table;
|
257 |
|
258 |
public UnsavedTable(IExtension extension) {
|
259 |
super(extension);
|
260 |
} |
261 |
|
262 |
public String getDescription() { |
263 |
return PluginServices.getText(this, "editing_table_unsaved"); |
264 |
} |
265 |
|
266 |
public String getResourceName() { |
267 |
return table.getName();
|
268 |
} |
269 |
|
270 |
public boolean saveData() { |
271 |
return executeSaveTable(table);
|
272 |
} |
273 |
|
274 |
public void setTable(TableDocument table) { |
275 |
this.table = table;
|
276 |
} |
277 |
|
278 |
public String getIcon() { |
279 |
return "document-table-icon-small"; |
280 |
} |
281 |
} |
282 |
|
283 |
// TODO Este c?digo est? duplicado, tambi?n est? en la clase Table en el
|
284 |
// m?todo "public void stopEditing()"
|
285 |
private boolean executeSaveTable(TableDocument table2) { |
286 |
FeatureStore fs = table2.getStore(); |
287 |
if (fs.isEditing()) {
|
288 |
try {
|
289 |
fs.finishEditing(); |
290 |
} catch (WriteException e) {
|
291 |
NotificationManager.addError(PluginServices.getText(this, "error_saving_table"), e); |
292 |
return false; |
293 |
} catch (ReadException e) {
|
294 |
NotificationManager.addError(PluginServices.getText(this, "error_saving_table"), e); |
295 |
return false; |
296 |
} catch (DataException e) {
|
297 |
NotificationManager.addError(PluginServices.getText(this, "error_saving_table"), e); |
298 |
return false; |
299 |
} |
300 |
} |
301 |
// if (ies instanceof IWriteable) {
|
302 |
// IWriteable w = (IWriteable) ies;
|
303 |
// IWriter writer = w.getWriter();
|
304 |
// if (writer == null) {
|
305 |
// return false;
|
306 |
// }
|
307 |
// try {
|
308 |
// ITableDefinition tableDef = ies.getTableDefinition();
|
309 |
// writer.initialize(tableDef);
|
310 |
// ies.stopEdition(writer, EditionEvent.ALPHANUMERIC);
|
311 |
// ies.getSelection().clear();
|
312 |
// } catch (InitializeWriterException e) {
|
313 |
// NotificationManager.addError(PluginServices.getText(this,"error_saving_table"),e);
|
314 |
// return false;
|
315 |
// } catch (StopWriterVisitorException e) {
|
316 |
// NotificationManager.addError(PluginServices.getText(this,"error_saving_table"),e);
|
317 |
// return false;
|
318 |
// } catch (ReadDriverException e) {
|
319 |
// NotificationManager.addError(PluginServices.getText(this,"error_saving_table"),e);
|
320 |
// return false;
|
321 |
// }
|
322 |
// }
|
323 |
return true; |
324 |
} |
325 |
|
326 |
public IExtensionStatus getStatus() {
|
327 |
return new StopEditingStatus(); |
328 |
} |
329 |
|
330 |
|
331 |
private JPanel getExplanationPanel(String name) { |
332 |
|
333 |
BorderLayout bl = new BorderLayout(10, 10); |
334 |
JPanel resp = new JPanel(bl); |
335 |
|
336 |
String msg = Messages.getText("realmente_desea_guardar"); |
337 |
JLabel topLabel = new JLabel(msg); |
338 |
|
339 |
JPanel mainPanel = new JPanel(new GridBagLayout()); |
340 |
GridBagConstraints cc = new GridBagConstraints(); |
341 |
|
342 |
cc.gridx = 0;
|
343 |
cc.gridy = 0;
|
344 |
cc.anchor = GridBagConstraints.WEST;
|
345 |
|
346 |
cc.insets = new Insets(3, 6, 3, 6); |
347 |
|
348 |
Font boldf = mainPanel.getFont().deriveFont(Font.BOLD); |
349 |
|
350 |
JLabel lbl = new JLabel(Messages.getText("_Guardar")); |
351 |
lbl.setFont(boldf); |
352 |
mainPanel.add(lbl, cc); |
353 |
cc.gridx = 1;
|
354 |
mainPanel.add(new JLabel(Messages.getText("_Save_changes_performed")), cc); |
355 |
|
356 |
cc.gridx = 0;
|
357 |
cc.gridy = 1;
|
358 |
lbl = new JLabel(Messages.getText("_Descartar")); |
359 |
lbl.setFont(boldf); |
360 |
mainPanel.add(lbl, cc); |
361 |
cc.gridx = 1;
|
362 |
mainPanel.add(new JLabel(Messages.getText("_Discard_and_lose_changes")), cc); |
363 |
|
364 |
cc.gridx = 0;
|
365 |
cc.gridy = 2;
|
366 |
lbl = new JLabel(Messages.getText("_Continuar")); |
367 |
lbl.setFont(boldf); |
368 |
mainPanel.add(lbl, cc); |
369 |
cc.gridx = 1;
|
370 |
mainPanel.add(new JLabel(Messages.getText("_Do_not_save_yet_Stay_in_editing_mode")), cc); |
371 |
|
372 |
resp.add(mainPanel, BorderLayout.CENTER);
|
373 |
resp.add(topLabel, BorderLayout.NORTH);
|
374 |
return resp;
|
375 |
} |
376 |
} |