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 @ 42967

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