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 @ 42966
History | View | Annotate | Download (12 KB)
1 | 40558 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40558 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | *
|
||
6 | 42966 | jjdelcerro | * 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 | 40558 | jjdelcerro | *
|
11 | 42966 | jjdelcerro | * 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 | 40558 | jjdelcerro | *
|
16 | 42966 | jjdelcerro | * 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 | 40558 | jjdelcerro | *
|
20 | 42966 | jjdelcerro | * For any additional information, do not hesitate to contact us at info AT
|
21 | * gvsig.com, or visit our website www.gvsig.com.
|
||
22 | 40435 | jjdelcerro | */
|
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.project.Project; |
||
51 | import org.gvsig.app.project.ProjectManager; |
||
52 | import org.gvsig.app.project.documents.Document; |
||
53 | import org.gvsig.app.project.documents.table.TableDocument; |
||
54 | import org.gvsig.app.project.documents.table.TableManager; |
||
55 | import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel; |
||
56 | 42775 | jjdelcerro | import org.gvsig.fmap.dal.EditingNotification; |
57 | import org.gvsig.fmap.dal.EditingNotificationManager; |
||
58 | 40435 | jjdelcerro | import org.gvsig.fmap.dal.exception.DataException; |
59 | import org.gvsig.fmap.dal.exception.ReadException; |
||
60 | import org.gvsig.fmap.dal.exception.WriteException; |
||
61 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
62 | 42775 | jjdelcerro | import org.gvsig.fmap.dal.swing.DALSwingLocator; |
63 | 40435 | jjdelcerro | import org.gvsig.gui.beans.Messages; |
64 | import org.gvsig.utils.swing.threads.IMonitorableTask; |
||
65 | |||
66 | public class TableEditStopExtension extends AbstractTableEditExtension { |
||
67 | 42146 | fdiaz | |
68 | 42966 | jjdelcerro | private static Logger logger |
69 | = LoggerFactory.getLogger(TableEditStopExtension.class); |
||
70 | 40435 | jjdelcerro | |
71 | 42966 | jjdelcerro | @Override
|
72 | public void initialize() { |
||
73 | super.initialize();
|
||
74 | IconThemeHelper.registerIcon("action", "table-stop-editing", this); |
||
75 | } |
||
76 | 42146 | fdiaz | |
77 | 40435 | jjdelcerro | public void execute(String actionCommand) { |
78 | if ("table-stop-editing".equals(actionCommand)) { |
||
79 | 41323 | jjdelcerro | TableDocument doc = (TableDocument) table.getDocument(); |
80 | 42775 | jjdelcerro | EditingNotificationManager editingNotification = DALSwingLocator.getEditingNotificationManager(); |
81 | 41323 | jjdelcerro | EditingNotification notification = editingNotification.notifyObservers( |
82 | 42146 | fdiaz | this,
|
83 | EditingNotification.BEFORE_ENTER_EDITING_STORE, |
||
84 | 41323 | jjdelcerro | doc, |
85 | doc.getStore()); |
||
86 | 42966 | jjdelcerro | if (notification.isCanceled()) {
|
87 | 41323 | jjdelcerro | return;
|
88 | } |
||
89 | 40435 | jjdelcerro | stopEditing(table); |
90 | ApplicationLocator.getManager().refreshMenusAndToolBars(); |
||
91 | 41323 | jjdelcerro | editingNotification.notifyObservers( |
92 | 42146 | fdiaz | this,
|
93 | EditingNotification.AFTER_ENTER_EDITING_STORE, |
||
94 | 41323 | jjdelcerro | doc, |
95 | doc.getStore()); |
||
96 | 40435 | jjdelcerro | } |
97 | } |
||
98 | |||
99 | private void stopEditing(FeatureTableDocumentPanel table) { |
||
100 | |||
101 | Object[] options = { |
||
102 | PluginServices.getText(this, "_Guardar"), |
||
103 | " " + PluginServices.getText(this, "_Descartar") + " ", |
||
104 | 42966 | jjdelcerro | PluginServices.getText(this, "_Continuar")}; |
105 | 42146 | fdiaz | |
106 | 40435 | jjdelcerro | JPanel explanation_panel = getExplanationPanel(table.getModel().getName());
|
107 | 42146 | fdiaz | |
108 | 40435 | jjdelcerro | int resp = JOptionPane |
109 | 42966 | jjdelcerro | .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 | 40435 | jjdelcerro | |
117 | try {
|
||
118 | if (resp == JOptionPane.NO_OPTION) { |
||
119 | // CANCEL EDITING
|
||
120 | table.getModel().getStore().cancelEditing(); |
||
121 | } else {
|
||
122 | 42146 | fdiaz | |
123 | 40435 | jjdelcerro | 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 | 42966 | jjdelcerro | + e.getMessage(), e); |
135 | 40435 | jjdelcerro | } |
136 | } |
||
137 | |||
138 | public boolean isEnabled() { |
||
139 | return true; |
||
140 | } |
||
141 | |||
142 | 42966 | jjdelcerro | @Override
|
143 | 40435 | jjdelcerro | public boolean isVisible() { |
144 | IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
||
145 | |||
146 | if (v == null) { |
||
147 | return false; |
||
148 | 42966 | jjdelcerro | } else if (v instanceof FeatureTableDocumentPanel |
149 | 40435 | jjdelcerro | && ((FeatureTableDocumentPanel) v).getModel().getStore() |
150 | 42966 | jjdelcerro | .isEditing()) { |
151 | table = (FeatureTableDocumentPanel) v; |
||
152 | return true; |
||
153 | } else {
|
||
154 | return false; |
||
155 | } |
||
156 | 40435 | jjdelcerro | } |
157 | |||
158 | /**
|
||
159 | * <p>
|
||
160 | * This class provides the status of extensions. If this extension has some
|
||
161 | * unsaved editing table (and save them), and methods to check if the
|
||
162 | * extension has some associated background tasks.
|
||
163 | 42146 | fdiaz | *
|
164 | 40435 | jjdelcerro | */
|
165 | private class StopEditingStatus implements IExtensionStatus { |
||
166 | |||
167 | /**
|
||
168 | * This method is used to check if this extension has some unsaved
|
||
169 | * editing tables.
|
||
170 | 42146 | fdiaz | *
|
171 | 40435 | jjdelcerro | * @return true if the extension has some unsaved editing tables, false
|
172 | 42966 | jjdelcerro | * otherwise.
|
173 | 40435 | jjdelcerro | */
|
174 | public boolean hasUnsavedData() { |
||
175 | Project project = ProjectManager.getInstance().getCurrentProject(); |
||
176 | List<Document> tables = project.getDocuments(TableManager.TYPENAME); |
||
177 | for (int i = 0; i < tables.size(); i++) { |
||
178 | FeatureStore store = ((TableDocument) tables.get(i)).getStore(); |
||
179 | if (store == null) { |
||
180 | continue;
|
||
181 | } |
||
182 | if (store.isEditing()) {
|
||
183 | return true; |
||
184 | } |
||
185 | } |
||
186 | return false; |
||
187 | } |
||
188 | |||
189 | /**
|
||
190 | * This method is used to check if the extension has some associated
|
||
191 | * background process which is currently running.
|
||
192 | 42146 | fdiaz | *
|
193 | 40435 | jjdelcerro | * @return true if the extension has some associated background process,
|
194 | 42966 | jjdelcerro | * false otherwise.
|
195 | 40435 | jjdelcerro | */
|
196 | public boolean hasRunningProcesses() { |
||
197 | return false; |
||
198 | } |
||
199 | |||
200 | /**
|
||
201 | * <p>
|
||
202 | * Gets an array of the traceable background tasks associated with this
|
||
203 | * extension. These tasks may be tracked, canceled, etc.
|
||
204 | * </p>
|
||
205 | 42146 | fdiaz | *
|
206 | 40435 | jjdelcerro | * @return An array of the associated background tasks, or null in case
|
207 | 42966 | jjdelcerro | * there is no associated background tasks.
|
208 | 40435 | jjdelcerro | */
|
209 | public IMonitorableTask[] getRunningProcesses() { |
||
210 | return null; |
||
211 | } |
||
212 | |||
213 | /**
|
||
214 | * <p>
|
||
215 | * Gets an array of the UnsavedData objects, which contain information
|
||
216 | * about the unsaved editing tables and allows to save it.
|
||
217 | * </p>
|
||
218 | 42146 | fdiaz | *
|
219 | 40435 | jjdelcerro | * @return An array of the associated unsaved editing layers, or null in
|
220 | 42966 | jjdelcerro | * case the extension has not unsaved editing tables.
|
221 | 40435 | jjdelcerro | */
|
222 | public IUnsavedData[] getUnsavedData() { |
||
223 | Project project = ProjectManager.getInstance().getCurrentProject(); |
||
224 | List<Document> tables = project.getDocuments(TableManager.TYPENAME); |
||
225 | List<UnsavedTable> unsavedTables = new ArrayList<UnsavedTable>(); |
||
226 | for (int i = 0; i < tables.size(); i++) { |
||
227 | TableDocument table = (TableDocument) tables.get(i); |
||
228 | FeatureStore store = table.getStore(); |
||
229 | if (store == null) { |
||
230 | continue;
|
||
231 | } |
||
232 | if (store.isEditing()) {
|
||
233 | 42966 | jjdelcerro | UnsavedTable ul |
234 | = new UnsavedTable(TableEditStopExtension.this);
|
||
235 | 40435 | jjdelcerro | ul.setTable(table); |
236 | unsavedTables.add(ul); |
||
237 | } |
||
238 | } |
||
239 | return unsavedTables
|
||
240 | 42966 | jjdelcerro | .toArray(new IUnsavedData[unsavedTables.size()]);
|
241 | 40435 | jjdelcerro | } |
242 | } |
||
243 | |||
244 | private class UnsavedTable extends UnsavedData { |
||
245 | |||
246 | private TableDocument table;
|
||
247 | |||
248 | public UnsavedTable(IExtension extension) {
|
||
249 | super(extension);
|
||
250 | } |
||
251 | |||
252 | public String getDescription() { |
||
253 | return PluginServices.getText(this, "editing_table_unsaved"); |
||
254 | } |
||
255 | |||
256 | public String getResourceName() { |
||
257 | return table.getName();
|
||
258 | } |
||
259 | |||
260 | public boolean saveData() { |
||
261 | return executeSaveTable(table);
|
||
262 | } |
||
263 | |||
264 | public void setTable(TableDocument table) { |
||
265 | this.table = table;
|
||
266 | } |
||
267 | 42146 | fdiaz | |
268 | 42966 | jjdelcerro | @Override
|
269 | 40435 | jjdelcerro | public String getIcon() { |
270 | return "document-table-icon-small"; |
||
271 | } |
||
272 | } |
||
273 | |||
274 | 42966 | jjdelcerro | // TODO Este codigo esta duplicado, tambien esta en la clase Table en el
|
275 | // metodo "public void stopEditing()"
|
||
276 | 40435 | jjdelcerro | private boolean executeSaveTable(TableDocument table2) { |
277 | FeatureStore fs = table2.getStore(); |
||
278 | 42200 | fdiaz | if (fs.isEditing()) {
|
279 | try {
|
||
280 | fs.finishEditing(); |
||
281 | } catch (WriteException e) {
|
||
282 | NotificationManager.addError(PluginServices.getText(this, "error_saving_table"), e); |
||
283 | return false; |
||
284 | } catch (ReadException e) {
|
||
285 | NotificationManager.addError(PluginServices.getText(this, "error_saving_table"), e); |
||
286 | return false; |
||
287 | } catch (DataException e) {
|
||
288 | NotificationManager.addError(PluginServices.getText(this, "error_saving_table"), e); |
||
289 | return false; |
||
290 | } |
||
291 | 40435 | jjdelcerro | } |
292 | return true; |
||
293 | } |
||
294 | |||
295 | 42966 | jjdelcerro | @Override
|
296 | 40435 | jjdelcerro | public IExtensionStatus getStatus() {
|
297 | return new StopEditingStatus(); |
||
298 | } |
||
299 | 42146 | fdiaz | |
300 | 40435 | jjdelcerro | private JPanel getExplanationPanel(String name) { |
301 | 42146 | fdiaz | |
302 | 40435 | jjdelcerro | BorderLayout bl = new BorderLayout(10, 10); |
303 | JPanel resp = new JPanel(bl); |
||
304 | 42146 | fdiaz | |
305 | 40435 | jjdelcerro | String msg = Messages.getText("realmente_desea_guardar"); |
306 | JLabel topLabel = new JLabel(msg); |
||
307 | 42146 | fdiaz | |
308 | 40435 | jjdelcerro | JPanel mainPanel = new JPanel(new GridBagLayout()); |
309 | GridBagConstraints cc = new GridBagConstraints(); |
||
310 | 42146 | fdiaz | |
311 | 40435 | jjdelcerro | cc.gridx = 0;
|
312 | cc.gridy = 0;
|
||
313 | cc.anchor = GridBagConstraints.WEST;
|
||
314 | 42146 | fdiaz | |
315 | 40435 | jjdelcerro | cc.insets = new Insets(3, 6, 3, 6); |
316 | 42146 | fdiaz | |
317 | 40435 | jjdelcerro | Font boldf = mainPanel.getFont().deriveFont(Font.BOLD); |
318 | 42146 | fdiaz | |
319 | 40435 | jjdelcerro | JLabel lbl = new JLabel(Messages.getText("_Guardar")); |
320 | lbl.setFont(boldf); |
||
321 | mainPanel.add(lbl, cc); |
||
322 | cc.gridx = 1;
|
||
323 | mainPanel.add(new JLabel(Messages.getText("_Save_changes_performed")), cc); |
||
324 | 42146 | fdiaz | |
325 | 40435 | jjdelcerro | cc.gridx = 0;
|
326 | cc.gridy = 1;
|
||
327 | lbl = new JLabel(Messages.getText("_Descartar")); |
||
328 | lbl.setFont(boldf); |
||
329 | mainPanel.add(lbl, cc); |
||
330 | cc.gridx = 1;
|
||
331 | mainPanel.add(new JLabel(Messages.getText("_Discard_and_lose_changes")), cc); |
||
332 | |||
333 | cc.gridx = 0;
|
||
334 | cc.gridy = 2;
|
||
335 | lbl = new JLabel(Messages.getText("_Continuar")); |
||
336 | lbl.setFont(boldf); |
||
337 | mainPanel.add(lbl, cc); |
||
338 | cc.gridx = 1;
|
||
339 | mainPanel.add(new JLabel(Messages.getText("_Do_not_save_yet_Stay_in_editing_mode")), cc); |
||
340 | |||
341 | resp.add(mainPanel, BorderLayout.CENTER);
|
||
342 | resp.add(topLabel, BorderLayout.NORTH);
|
||
343 | return resp;
|
||
344 | } |
||
345 | } |