Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / org.gvsig.app.document.table.app / org.gvsig.app.document.table.app.mainplugin / src / main / java / org / gvsig / app / extension / TableEditStopExtension.java @ 38564

History | View | Annotate | Download (9.48 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22
package org.gvsig.app.extension;
23

    
24
import java.util.ArrayList;
25
import java.util.List;
26

    
27
import javax.swing.JOptionPane;
28

    
29
import org.gvsig.andami.IconThemeHelper;
30
import org.gvsig.andami.PluginServices;
31
import org.gvsig.andami.messages.NotificationManager;
32
import org.gvsig.andami.plugins.IExtension;
33
import org.gvsig.andami.plugins.status.IExtensionStatus;
34
import org.gvsig.andami.plugins.status.IUnsavedData;
35
import org.gvsig.andami.plugins.status.UnsavedData;
36
import org.gvsig.andami.ui.mdiManager.IWindow;
37
import org.gvsig.app.project.Project;
38
import org.gvsig.app.project.ProjectManager;
39
import org.gvsig.app.project.documents.Document;
40
import org.gvsig.app.project.documents.table.TableDocument;
41
import org.gvsig.app.project.documents.table.TableManager;
42
import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel;
43
import org.gvsig.fmap.dal.exception.DataException;
44
import org.gvsig.fmap.dal.exception.ReadException;
45
import org.gvsig.fmap.dal.exception.WriteException;
46
import org.gvsig.fmap.dal.feature.FeatureStore;
47
import org.gvsig.utils.swing.threads.IMonitorableTask;
48

    
49
public class TableEditStopExtension extends AbstractTableEditExtension {
50

    
51
        public void initialize() {
52
                super.initialize();
53
                IconThemeHelper.registerIcon("action", "table-stop-editing", this);
54
        }
55
        
56
    public void execute(String actionCommand) {
57
        if ("table-stop-editing".equals(actionCommand)) {
58
            stopEditing(table);
59
        }
60
    }
61

    
62
    public void stopEditing(FeatureTableDocumentPanel table) {
63
        // FIXME
64
        int resp =
65
            JOptionPane.showConfirmDialog(null,
66
                PluginServices.getText(this, "realmente_desea_guardar") + " : "
67
                    + table.getModel().getName(), "Guardar",
68
                JOptionPane.YES_NO_OPTION);
69
        try {
70
            if (resp == JOptionPane.NO_OPTION) { // CANCEL EDITING
71
                table.getModel().getStore().cancelEditing();
72
            } else { // GUARDAMOS LA TABLA
73
                table.getModel().getStore().finishEditing();
74
            }
75
        } catch (DataException e) {
76
            // TODO Auto-generated catch block
77
            e.printStackTrace();
78
        }
79
    }
80

    
81
    /**
82
     * @see org.gvsig.andami.plugins.IExtension#isEnabled()
83
     */
84
    public boolean isEnabled() {
85
        return true;
86
    }
87

    
88
    /**
89
     * @see org.gvsig.andami.plugins.IExtension#isVisible()
90
     */
91
    public boolean isVisible() {
92
        IWindow v = PluginServices.getMDIManager().getActiveWindow();
93

    
94
        if (v == null) {
95
            return false;
96
        } else
97
            if (v instanceof FeatureTableDocumentPanel
98
                && ((FeatureTableDocumentPanel) v).getModel().getStore()
99
                    .isEditing()
100
                && ((FeatureTableDocumentPanel) v).getModel()
101
                    .getAssociatedLayer() == null) {
102
                table = (FeatureTableDocumentPanel) v;
103
                return true;
104
            } else {
105
                return false;
106
            }
107
    }
108

    
109
    /**
110
     * <p>
111
     * This class provides the status of extensions. If this extension has some
112
     * unsaved editing table (and save them), and methods to check if the
113
     * extension has some associated background tasks.
114
     * 
115
     * @author Vicente Caballero Navarro
116
     * 
117
     */
118
    private class StopEditingStatus implements IExtensionStatus {
119

    
120
        /**
121
         * This method is used to check if this extension has some unsaved
122
         * editing tables.
123
         * 
124
         * @return true if the extension has some unsaved editing tables, false
125
         *         otherwise.
126
         */
127
        public boolean hasUnsavedData() {
128
            Project project = ProjectManager.getInstance().getCurrentProject();
129
            List<Document> tables = project.getDocuments(TableManager.TYPENAME);
130
            for (int i = 0; i < tables.size(); i++) {
131
                FeatureStore store = ((TableDocument) tables.get(i)).getStore();
132
                if (store == null) {
133
                    continue;
134
                }
135
                if (store.isEditing()) {
136
                    return true;
137
                }
138
            }
139
            return false;
140
        }
141

    
142
        /**
143
         * This method is used to check if the extension has some associated
144
         * background process which is currently running.
145
         * 
146
         * @return true if the extension has some associated background process,
147
         *         false otherwise.
148
         */
149
        public boolean hasRunningProcesses() {
150
            return false;
151
        }
152

    
153
        /**
154
         * <p>
155
         * Gets an array of the traceable background tasks associated with this
156
         * extension. These tasks may be tracked, canceled, etc.
157
         * </p>
158
         * 
159
         * @return An array of the associated background tasks, or null in case
160
         *         there is
161
         *         no associated background tasks.
162
         */
163
        public IMonitorableTask[] getRunningProcesses() {
164
            return null;
165
        }
166

    
167
        /**
168
         * <p>
169
         * Gets an array of the UnsavedData objects, which contain information
170
         * about the unsaved editing tables and allows to save it.
171
         * </p>
172
         * 
173
         * @return An array of the associated unsaved editing layers, or null in
174
         *         case the extension
175
         *         has not unsaved editing tables.
176
         */
177
        public IUnsavedData[] getUnsavedData() {
178
            Project project = ProjectManager.getInstance().getCurrentProject();
179
            List<Document> tables = project.getDocuments(TableManager.TYPENAME);
180
            List<UnsavedTable> unsavedTables = new ArrayList<UnsavedTable>();
181
            for (int i = 0; i < tables.size(); i++) {
182
                TableDocument table = (TableDocument) tables.get(i);
183
                FeatureStore store = table.getStore();
184
                if (store == null) {
185
                    continue;
186
                }
187
                if (store.isEditing()) {
188
                    UnsavedTable ul =
189
                        new UnsavedTable(TableEditStopExtension.this);
190
                    ul.setTable(table);
191
                    unsavedTables.add(ul);
192
                }
193
            }
194
            return unsavedTables
195
                .toArray(new IUnsavedData[unsavedTables.size()]);
196
        }
197
    }
198

    
199
    private class UnsavedTable extends UnsavedData {
200

    
201
        private TableDocument table;
202

    
203
        public UnsavedTable(IExtension extension) {
204
            super(extension);
205
        }
206

    
207
        public String getDescription() {
208
            return PluginServices.getText(this, "editing_table_unsaved");
209
        }
210

    
211
        public String getResourceName() {
212
            return table.getName();
213
        }
214

    
215
        public boolean saveData() {
216
            return executeSaveTable(table);
217
        }
218

    
219
        public void setTable(TableDocument table) {
220
            this.table = table;
221

    
222
        }
223
    }
224

    
225
    // TODO Este c?digo est? duplicado, tambi?n est? en la clase Table en el
226
    // m?todo "public void stopEditing()"
227
    private boolean executeSaveTable(TableDocument table2) {
228
        FeatureStore fs = table2.getStore();
229
        try {
230
            fs.finishEditing();
231
        } catch (WriteException e) {
232
            NotificationManager.addError(
233
                PluginServices.getText(this, "error_saving_table"), e);
234
            return false;
235
        } catch (ReadException e) {
236
            NotificationManager.addError(
237
                PluginServices.getText(this, "error_saving_table"), e);
238
            return false;
239
        } catch (DataException e) {
240
            NotificationManager.addError(
241
                PluginServices.getText(this, "error_saving_table"), e);
242
            return false;
243
        }
244
        // if (ies instanceof IWriteable) {
245
        // IWriteable w = (IWriteable) ies;
246
        // IWriter writer = w.getWriter();
247
        // if (writer == null) {
248
        // return false;
249
        // }
250
        // try {
251
        // ITableDefinition tableDef = ies.getTableDefinition();
252
        // writer.initialize(tableDef);
253
        // ies.stopEdition(writer, EditionEvent.ALPHANUMERIC);
254
        // ies.getSelection().clear();
255
        // } catch (InitializeWriterException e) {
256
        // NotificationManager.addError(PluginServices.getText(this,"error_saving_table"),e);
257
        // return false;
258
        // } catch (StopWriterVisitorException e) {
259
        // NotificationManager.addError(PluginServices.getText(this,"error_saving_table"),e);
260
        // return false;
261
        // } catch (ReadDriverException e) {
262
        // NotificationManager.addError(PluginServices.getText(this,"error_saving_table"),e);
263
        // return false;
264
        // }
265
        // }
266
        return true;
267
    }
268

    
269
    public IExtensionStatus getStatus() {
270
        return new StopEditingStatus();
271
    }
272
}