Statistics
| Revision:

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 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 43462 jjdelcerro
import org.gvsig.app.ApplicationManager;
51 40435 jjdelcerro
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 42775 jjdelcerro
import org.gvsig.fmap.dal.EditingNotification;
58
import org.gvsig.fmap.dal.EditingNotificationManager;
59 40435 jjdelcerro
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 42775 jjdelcerro
import org.gvsig.fmap.dal.swing.DALSwingLocator;
64 40435 jjdelcerro
import org.gvsig.gui.beans.Messages;
65 43462 jjdelcerro
import org.gvsig.tools.ToolsLocator;
66
import org.gvsig.tools.i18n.I18nManager;
67 42967 jjdelcerro
import org.gvsig.tools.util.ArrayUtils;
68 40435 jjdelcerro
import org.gvsig.utils.swing.threads.IMonitorableTask;
69
70
public class TableEditStopExtension extends AbstractTableEditExtension {
71 42146 fdiaz
72 42966 jjdelcerro
    private static Logger logger
73
            = LoggerFactory.getLogger(TableEditStopExtension.class);
74 40435 jjdelcerro
75 42966 jjdelcerro
    @Override
76
    public void initialize() {
77
        super.initialize();
78
        IconThemeHelper.registerIcon("action", "table-stop-editing", this);
79
    }
80 42146 fdiaz
81 40435 jjdelcerro
    public void execute(String actionCommand) {
82 42967 jjdelcerro
        this.execute(actionCommand,null);
83
    }
84
85
    @Override
86
    public void execute(String actionCommand, Object[] args) {
87 40435 jjdelcerro
        if ("table-stop-editing".equals(actionCommand)) {
88 42967 jjdelcerro
            TableDocument doc = (TableDocument)ArrayUtils.get(args,0);
89
            if( doc == null ) {
90
                doc = (TableDocument) table.getDocument();
91
            }
92 42775 jjdelcerro
            EditingNotificationManager editingNotification = DALSwingLocator.getEditingNotificationManager();
93 41323 jjdelcerro
            EditingNotification notification = editingNotification.notifyObservers(
94 42146 fdiaz
                    this,
95
                    EditingNotification.BEFORE_ENTER_EDITING_STORE,
96 41323 jjdelcerro
                    doc,
97
                    doc.getStore());
98 42966 jjdelcerro
            if (notification.isCanceled()) {
99 41323 jjdelcerro
                return;
100
            }
101 40435 jjdelcerro
            stopEditing(table);
102
            ApplicationLocator.getManager().refreshMenusAndToolBars();
103 41323 jjdelcerro
            editingNotification.notifyObservers(
104 42146 fdiaz
                    this,
105
                    EditingNotification.AFTER_ENTER_EDITING_STORE,
106 41323 jjdelcerro
                    doc,
107
                    doc.getStore());
108 40435 jjdelcerro
        }
109
    }
110
111
    private void stopEditing(FeatureTableDocumentPanel table) {
112
113 43462 jjdelcerro
        ApplicationManager application = ApplicationLocator.getManager();
114
        I18nManager i18n = ToolsLocator.getI18nManager();
115
116 40435 jjdelcerro
        Object[] options = {
117 43462 jjdelcerro
            i18n.getTranslation("_Guardar"),
118
            "       " + i18n.getTranslation("_Descartar") + "       ",
119
            i18n.getTranslation("_Continuar")
120
        };
121 42146 fdiaz
122 40435 jjdelcerro
        JPanel explanation_panel = getExplanationPanel(table.getModel().getName());
123 42146 fdiaz
124 40435 jjdelcerro
        int resp = JOptionPane
125 42966 jjdelcerro
                .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 40435 jjdelcerro
133 43462 jjdelcerro
        switch( resp ) {
134
        case JOptionPane.NO_OPTION:
135
            try {
136 40435 jjdelcerro
                // CANCEL EDITING
137
                table.getModel().getStore().cancelEditing();
138 43462 jjdelcerro
            } 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 40435 jjdelcerro
            }
150 43462 jjdelcerro
            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 42966 jjdelcerro
                    + e.getMessage(), e);
158 43462 jjdelcerro
                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 40435 jjdelcerro
        }
174
    }
175
176
    public boolean isEnabled() {
177
        return true;
178
    }
179
180 42966 jjdelcerro
    @Override
181 40435 jjdelcerro
    public boolean isVisible() {
182
        IWindow v = PluginServices.getMDIManager().getActiveWindow();
183
184
        if (v == null) {
185
            return false;
186 42966 jjdelcerro
        } else if (v instanceof FeatureTableDocumentPanel
187 40435 jjdelcerro
                && ((FeatureTableDocumentPanel) v).getModel().getStore()
188 42966 jjdelcerro
                .isEditing()) {
189
            table = (FeatureTableDocumentPanel) v;
190
            return true;
191
        } else {
192
            return false;
193
        }
194 40435 jjdelcerro
    }
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 42146 fdiaz
     *
202 40435 jjdelcerro
     */
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 42146 fdiaz
         *
209 40435 jjdelcerro
         * @return true if the extension has some unsaved editing tables, false
210 42966 jjdelcerro
         * otherwise.
211 40435 jjdelcerro
         */
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 42146 fdiaz
         *
231 40435 jjdelcerro
         * @return true if the extension has some associated background process,
232 42966 jjdelcerro
         * false otherwise.
233 40435 jjdelcerro
         */
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 42146 fdiaz
         *
244 40435 jjdelcerro
         * @return An array of the associated background tasks, or null in case
245 42966 jjdelcerro
         * there is no associated background tasks.
246 40435 jjdelcerro
         */
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 42146 fdiaz
         *
257 40435 jjdelcerro
         * @return An array of the associated unsaved editing layers, or null in
258 42966 jjdelcerro
         * case the extension has not unsaved editing tables.
259 40435 jjdelcerro
         */
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 42966 jjdelcerro
                    UnsavedTable ul
272
                            = new UnsavedTable(TableEditStopExtension.this);
273 40435 jjdelcerro
                    ul.setTable(table);
274
                    unsavedTables.add(ul);
275
                }
276
            }
277
            return unsavedTables
278 42966 jjdelcerro
                    .toArray(new IUnsavedData[unsavedTables.size()]);
279 40435 jjdelcerro
        }
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 42146 fdiaz
306 42966 jjdelcerro
        @Override
307 40435 jjdelcerro
        public String getIcon() {
308
            return "document-table-icon-small";
309
        }
310
    }
311
312 42966 jjdelcerro
    // TODO Este codigo esta duplicado, tambien esta en la clase Table en el
313
    // metodo "public void stopEditing()"
314 40435 jjdelcerro
    private boolean executeSaveTable(TableDocument table2) {
315
        FeatureStore fs = table2.getStore();
316 42200 fdiaz
        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 40435 jjdelcerro
        }
330
        return true;
331
    }
332
333 42966 jjdelcerro
    @Override
334 40435 jjdelcerro
    public IExtensionStatus getStatus() {
335
        return new StopEditingStatus();
336
    }
337 42146 fdiaz
338 40435 jjdelcerro
    private JPanel getExplanationPanel(String name) {
339 42146 fdiaz
340 40435 jjdelcerro
        BorderLayout bl = new BorderLayout(10, 10);
341
        JPanel resp = new JPanel(bl);
342 42146 fdiaz
343 40435 jjdelcerro
        String msg = Messages.getText("realmente_desea_guardar");
344
        JLabel topLabel = new JLabel(msg);
345 42146 fdiaz
346 40435 jjdelcerro
        JPanel mainPanel = new JPanel(new GridBagLayout());
347
        GridBagConstraints cc = new GridBagConstraints();
348 42146 fdiaz
349 40435 jjdelcerro
        cc.gridx = 0;
350
        cc.gridy = 0;
351
        cc.anchor = GridBagConstraints.WEST;
352 42146 fdiaz
353 40435 jjdelcerro
        cc.insets = new Insets(3, 6, 3, 6);
354 42146 fdiaz
355 40435 jjdelcerro
        Font boldf = mainPanel.getFont().deriveFont(Font.BOLD);
356 42146 fdiaz
357 40435 jjdelcerro
        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 42146 fdiaz
363 40435 jjdelcerro
        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
}