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

History | View | Annotate | Download (12 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.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
import org.gvsig.fmap.dal.EditingNotification;
57
import org.gvsig.fmap.dal.EditingNotificationManager;
58
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
import org.gvsig.fmap.dal.swing.DALSwingLocator;
63
import org.gvsig.gui.beans.Messages;
64
import org.gvsig.utils.swing.threads.IMonitorableTask;
65

    
66
public class TableEditStopExtension extends AbstractTableEditExtension {
67

    
68
    private static Logger logger
69
            = LoggerFactory.getLogger(TableEditStopExtension.class);
70

    
71
    @Override
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
    public boolean isEnabled() {
139
        return true;
140
    }
141

    
142
    @Override
143
    public boolean isVisible() {
144
        IWindow v = PluginServices.getMDIManager().getActiveWindow();
145

    
146
        if (v == null) {
147
            return false;
148
        } else if (v instanceof FeatureTableDocumentPanel
149
                && ((FeatureTableDocumentPanel) v).getModel().getStore()
150
                .isEditing()) {
151
            table = (FeatureTableDocumentPanel) v;
152
            return true;
153
        } else {
154
            return false;
155
        }
156
    }
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
     *
164
     */
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
         *
171
         * @return true if the extension has some unsaved editing tables, false
172
         * otherwise.
173
         */
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
         *
193
         * @return true if the extension has some associated background process,
194
         * false otherwise.
195
         */
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
         *
206
         * @return An array of the associated background tasks, or null in case
207
         * there is no associated background tasks.
208
         */
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
         *
219
         * @return An array of the associated unsaved editing layers, or null in
220
         * case the extension has not unsaved editing tables.
221
         */
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
                    UnsavedTable ul
234
                            = new UnsavedTable(TableEditStopExtension.this);
235
                    ul.setTable(table);
236
                    unsavedTables.add(ul);
237
                }
238
            }
239
            return unsavedTables
240
                    .toArray(new IUnsavedData[unsavedTables.size()]);
241
        }
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

    
268
        @Override
269
        public String getIcon() {
270
            return "document-table-icon-small";
271
        }
272
    }
273

    
274
    // TODO Este codigo esta duplicado, tambien esta en la clase Table en el
275
    // metodo "public void stopEditing()"
276
    private boolean executeSaveTable(TableDocument table2) {
277
        FeatureStore fs = table2.getStore();
278
        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
        }
292
        return true;
293
    }
294

    
295
    @Override
296
    public IExtensionStatus getStatus() {
297
        return new StopEditingStatus();
298
    }
299

    
300
    private JPanel getExplanationPanel(String name) {
301

    
302
        BorderLayout bl = new BorderLayout(10, 10);
303
        JPanel resp = new JPanel(bl);
304

    
305
        String msg = Messages.getText("realmente_desea_guardar");
306
        JLabel topLabel = new JLabel(msg);
307

    
308
        JPanel mainPanel = new JPanel(new GridBagLayout());
309
        GridBagConstraints cc = new GridBagConstraints();
310

    
311
        cc.gridx = 0;
312
        cc.gridy = 0;
313
        cc.anchor = GridBagConstraints.WEST;
314

    
315
        cc.insets = new Insets(3, 6, 3, 6);
316

    
317
        Font boldf = mainPanel.getFont().deriveFont(Font.BOLD);
318

    
319
        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

    
325
        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
}