Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / TableEditStopExtension.java @ 22932

History | View | Annotate | Download (7.14 KB)

1
package com.iver.cit.gvsig;
2

    
3
import java.util.ArrayList;
4

    
5
import javax.swing.JOptionPane;
6

    
7
import org.gvsig.fmap.data.ReadException;
8
import org.gvsig.fmap.data.WriteException;
9
import org.gvsig.fmap.data.feature.FeatureStore;
10

    
11
import com.iver.andami.PluginServices;
12
import com.iver.andami.messages.NotificationManager;
13
import com.iver.andami.plugins.Extension;
14
import com.iver.andami.plugins.IExtension;
15
import com.iver.andami.plugins.status.IExtensionStatus;
16
import com.iver.andami.plugins.status.IUnsavedData;
17
import com.iver.andami.plugins.status.UnsavedData;
18
import com.iver.andami.ui.mdiManager.IWindow;
19
import com.iver.cit.gvsig.project.documents.table.ProjectTable;
20
import com.iver.cit.gvsig.project.documents.table.ProjectTableFactory;
21
import com.iver.cit.gvsig.project.documents.table.exceptions.CancelEditingTableException;
22
import com.iver.cit.gvsig.project.documents.table.gui.Table;
23
import com.iver.utiles.swing.threads.IMonitorableTask;
24

    
25

    
26
/**
27
 * DOCUMENT ME!
28
 *
29
 * @author Vicente Caballero Navarro
30
 */
31
public class TableEditStopExtension extends Extension {
32
    /**
33
     * @see com.iver.andami.plugins.IExtension#initialize()
34
     */
35
    public void initialize() {
36
    }
37

    
38
    /**
39
     * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
40
     */
41
    public void execute(String actionCommand) {
42
            IWindow v = PluginServices.getMDIManager().getActiveWindow();
43
            Table table = (Table) v;
44
        if ("STOPEDITING".equals(actionCommand)) {
45
            stopEditing(table);
46
        }
47
                /* IEditableSource edTable = (IEditableSource) table.getModel().getAssociatedTable();
48
                edTable.getCommandRecord().removeCommandListener(table); */
49

    
50
    }
51

    
52
    /**
53
         * DOCUMENT ME!
54
         */
55
        public void stopEditing(Table table) {
56
                int resp = JOptionPane
57
                                .showConfirmDialog(null, PluginServices.getText(this,
58
                                                "realmente_desea_guardar") +" : "+ table.getModel().getName(), "Guardar",
59
                                                JOptionPane.YES_NO_OPTION);
60
                        if (resp == JOptionPane.NO_OPTION) { // CANCEL EDITING
61
                                try {
62
                                        table.cancelEditing();
63
                                } catch (CancelEditingTableException e) {
64
                                        e.printStackTrace();
65
                                }
66
                        } else { // GUARDAMOS LA TABLA
67
                                table.stopEditing();
68
                        }
69
        }
70

    
71

    
72
    /**
73
     * @see com.iver.andami.plugins.IExtension#isEnabled()
74
     */
75
    public boolean isEnabled() {
76
       return true;
77
    }
78

    
79
    /**
80
     * @see com.iver.andami.plugins.IExtension#isVisible()
81
     */
82
    public boolean isVisible() {
83
        IWindow v = PluginServices.getMDIManager().getActiveWindow();
84

    
85
        if (v == null) {
86
            return false;
87
        } else if (v instanceof Table && ((Table) v).isEditing() && ((Table)v).getModel().getAssociatedTable()==null) {
88
            return true;
89
        } else {
90
            return false;
91
        }
92
    }
93
    /**
94
         * <p>This class provides the status of extensions.
95
         * If this extension has some unsaved editing table (and save them), and methods
96
         * to check if the extension has some associated background tasks.
97
         *
98
         * @author Vicente Caballero Navarro
99
         *
100
         */
101
        private class StopEditingStatus implements IExtensionStatus {
102
                /**
103
             * This method is used to check if this extension has some unsaved editing tables.
104
             *
105
             * @return true if the extension has some unsaved editing tables, false otherwise.
106
             */
107
                public boolean hasUnsavedData() {
108
                        ProjectExtension pe=(ProjectExtension)PluginServices.getExtension(ProjectExtension.class);
109
                        ProjectTable[] tables=(ProjectTable[])pe.getProject().getDocumentsByType(ProjectTableFactory.registerName).toArray(new ProjectTable[0]);
110
                        for (int i=0;i<tables.length;i++) {
111
                                if (tables[i].getModel() == null){
112
                                        continue;
113
                                }
114
                                if (tables[i].getModel().isEditing())
115
                                        return true;
116
                        }
117
                        return false;
118
                }
119
                /**
120
             * This method is used to check if the extension has some associated
121
             * background process which is currently running.
122
             *
123
             * @return true if the extension has some associated background process,
124
             * false otherwise.
125
             */
126
                public boolean hasRunningProcesses() {
127
                        return false;
128
                }
129
                 /**
130
             * <p>Gets an array of the traceable background tasks associated with this
131
             * extension. These tasks may be tracked, canceled, etc.</p>
132
             *
133
             * @return An array of the associated background tasks, or null in case there is
134
             * no associated background tasks.
135
             */
136
                public IMonitorableTask[] getRunningProcesses() {
137
                        return null;
138
                }
139
                /**
140
             * <p>Gets an array of the UnsavedData objects, which contain information about
141
             * the unsaved editing tables and allows to save it.</p>
142
             *
143
             * @return An array of the associated unsaved editing layers, or null in case the extension
144
             * has not unsaved editing tables.
145
             */
146
                public IUnsavedData[] getUnsavedData() {
147
                        ProjectExtension pe=(ProjectExtension)PluginServices.getExtension(ProjectExtension.class);
148
                        ProjectTable[] tables =(ProjectTable[])pe.getProject().getDocumentsByType(ProjectTableFactory.registerName).toArray(new ProjectTable[0]);
149
                        ArrayList unsavedTables = new ArrayList();
150
                        for (int i=0;i<tables.length;i++) {
151
                                if (tables[i].getModel() == null){
152
                                        continue;
153
                                }
154
                                if (tables[i].getModel().isEditing()) {
155
                                        UnsavedTable ul=new UnsavedTable(TableEditStopExtension.this);
156
                                        ul.setTable(tables[i]);
157
                                        unsavedTables.add(ul);
158
                                }
159
                        }
160
                        return (IUnsavedData[])unsavedTables.toArray(new IUnsavedData[0]);
161
                }
162
        }
163

    
164
        private class UnsavedTable extends UnsavedData{
165

    
166
                private ProjectTable table;
167

    
168
                public UnsavedTable(IExtension extension) {
169
                        super(extension);
170
                }
171

    
172
                public String getDescription() {
173
                        return PluginServices.getText(this,"editing_table_unsaved");
174
                }
175

    
176
                public String getResourceName() {
177
                        return table.getName();
178
                }
179

    
180

    
181

    
182
                public boolean saveData() {
183
                        return executeSaveTable(table);
184
                }
185

    
186

    
187

    
188
                public void setTable(ProjectTable table) {
189
                        this.table=table;
190

    
191
                }
192
        }
193

    
194

    
195
        //TODO Este c?digo est? duplicado, tambi?n est? en la clase Table en el m?todo "public void stopEditing()"
196
        private boolean executeSaveTable(ProjectTable table2) {
197
                FeatureStore fs = table2.getModel();
198
                try {
199
                        fs.finishEditing();
200
                } catch (WriteException e) {
201
                        NotificationManager.addError(PluginServices.getText(this,"error_saving_table"),e);
202
                        return false;
203
                } catch (ReadException e) {
204
                        NotificationManager.addError(PluginServices.getText(this,"error_saving_table"),e);
205
                        return false;
206
                }
207
//                if (ies instanceof IWriteable) {
208
//                        IWriteable w = (IWriteable) ies;
209
//                        IWriter writer = w.getWriter();
210
//                        if (writer == null) {
211
//                                return false;
212
//                        }
213
//                        try {
214
//                                ITableDefinition tableDef = ies.getTableDefinition();
215
//                                writer.initialize(tableDef);
216
//                                ies.stopEdition(writer, EditionEvent.ALPHANUMERIC);
217
//                                ies.getSelection().clear();
218
//                        } catch (InitializeWriterException e) {
219
//                                NotificationManager.addError(PluginServices.getText(this,"error_saving_table"),e);
220
//                                return false;
221
//                        } catch (StopWriterVisitorException e) {
222
//                                NotificationManager.addError(PluginServices.getText(this,"error_saving_table"),e);
223
//                                return false;
224
//                        } catch (ReadDriverException e) {
225
//                                NotificationManager.addError(PluginServices.getText(this,"error_saving_table"),e);
226
//                                return false;
227
//                        }
228
//                }
229
                return true;
230
        }
231
        public IExtensionStatus getStatus() {
232
                return new StopEditingStatus();
233
        }
234
}