Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / TableEditStopExtension.java @ 28546

History | View | Annotate | Download (7.13 KB)

1 2936 caballero
package com.iver.cit.gvsig;
2
3 13519 caballero
import java.util.ArrayList;
4
5 5318 caballero
import javax.swing.JOptionPane;
6
7 24923 vcaballero
import org.gvsig.fmap.dal.exception.DataException;
8 24759 jmvivo
import org.gvsig.fmap.dal.exception.ReadException;
9
import org.gvsig.fmap.dal.exception.WriteException;
10
import org.gvsig.fmap.dal.feature.FeatureStore;
11 25068 vcaballero
import org.gvsig.project.document.table.FeatureTableDocument;
12
import org.gvsig.project.document.table.FeatureTableDocumentFactory;
13
import org.gvsig.project.document.table.gui.FeatureTableDocumentPanel;
14 20994 jmvivo
15 2936 caballero
import com.iver.andami.PluginServices;
16 13519 caballero
import com.iver.andami.messages.NotificationManager;
17
import com.iver.andami.plugins.IExtension;
18
import com.iver.andami.plugins.status.IExtensionStatus;
19
import com.iver.andami.plugins.status.IUnsavedData;
20
import com.iver.andami.plugins.status.UnsavedData;
21 6877 cesar
import com.iver.andami.ui.mdiManager.IWindow;
22 13519 caballero
import com.iver.utiles.swing.threads.IMonitorableTask;
23 2936 caballero
24
25
/**
26
 * DOCUMENT ME!
27
 *
28
 * @author Vicente Caballero Navarro
29
 */
30 26053 vcaballero
public class TableEditStopExtension extends AbstractTableEditExtension {
31 2936 caballero
32
    /**
33 5005 jorpiell
     * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
34 2936 caballero
     */
35
    public void execute(String actionCommand) {
36 26053 vcaballero
            if ("STOPEDITING".equals(actionCommand)) {
37 5318 caballero
            stopEditing(table);
38 2936 caballero
        }
39
    }
40
41
    /**
42 5318 caballero
         * DOCUMENT ME!
43
         */
44 25068 vcaballero
        public void stopEditing(FeatureTableDocumentPanel table) {
45 25089 jmvivo
//                FIXME
46 26053 vcaballero
                int resp = JOptionPane
47
                                .showConfirmDialog(null, PluginServices.getText(this,
48
                                                "realmente_desea_guardar") +" : "+ table.getModel().getName(), "Guardar",
49
                                                JOptionPane.YES_NO_OPTION);
50
                try {
51
                        if (resp == JOptionPane.NO_OPTION) { // CANCEL EDITING
52
                                table.getModel().getStore().cancelEditing();
53
                        } else { // GUARDAMOS LA TABLA
54
                                table.getModel().getStore().finishEditing();
55
                        }
56
                } catch (DataException e) {
57
                        // TODO Auto-generated catch block
58
                        e.printStackTrace();
59
                }
60 5318 caballero
        }
61
62
63
    /**
64 5005 jorpiell
     * @see com.iver.andami.plugins.IExtension#isEnabled()
65 2936 caballero
     */
66
    public boolean isEnabled() {
67 5081 caballero
       return true;
68 2936 caballero
    }
69
70
    /**
71 5005 jorpiell
     * @see com.iver.andami.plugins.IExtension#isVisible()
72 2936 caballero
     */
73
    public boolean isVisible() {
74 6880 cesar
        IWindow v = PluginServices.getMDIManager().getActiveWindow();
75 2936 caballero
76
        if (v == null) {
77
            return false;
78 25068 vcaballero
        } else if (v instanceof FeatureTableDocumentPanel && ((FeatureTableDocumentPanel) v).getModel().getStore().isEditing() && ((FeatureTableDocumentPanel)v).getModel().getAssociatedLayer()==null) {
79 26053 vcaballero
            table=(FeatureTableDocumentPanel)v;
80
                return true;
81 2936 caballero
        } else {
82
            return false;
83
        }
84
    }
85 13519 caballero
    /**
86
         * <p>This class provides the status of extensions.
87
         * If this extension has some unsaved editing table (and save them), and methods
88
         * to check if the extension has some associated background tasks.
89
         *
90
         * @author Vicente Caballero Navarro
91
         *
92
         */
93
        private class StopEditingStatus implements IExtensionStatus {
94
                /**
95
             * This method is used to check if this extension has some unsaved editing tables.
96
             *
97
             * @return true if the extension has some unsaved editing tables, false otherwise.
98
             */
99
                public boolean hasUnsavedData() {
100
                        ProjectExtension pe=(ProjectExtension)PluginServices.getExtension(ProjectExtension.class);
101 25068 vcaballero
                        FeatureTableDocument[] tables=pe.getProject().getDocumentsByType(FeatureTableDocumentFactory.registerName).toArray(new FeatureTableDocument[0]);
102 13519 caballero
                        for (int i=0;i<tables.length;i++) {
103 25068 vcaballero
                                if (tables[i].getStore() == null){
104 14845 jmvivo
                                        continue;
105
                                }
106 25068 vcaballero
                                if (tables[i].getStore().isEditing()) {
107 13519 caballero
                                        return true;
108 24759 jmvivo
                                }
109 13519 caballero
                        }
110
                        return false;
111
                }
112
                /**
113
             * This method is used to check if the extension has some associated
114
             * background process which is currently running.
115
             *
116
             * @return true if the extension has some associated background process,
117
             * false otherwise.
118
             */
119
                public boolean hasRunningProcesses() {
120
                        return false;
121
                }
122
                 /**
123
             * <p>Gets an array of the traceable background tasks associated with this
124
             * extension. These tasks may be tracked, canceled, etc.</p>
125
             *
126
             * @return An array of the associated background tasks, or null in case there is
127
             * no associated background tasks.
128
             */
129
                public IMonitorableTask[] getRunningProcesses() {
130
                        return null;
131
                }
132
                /**
133
             * <p>Gets an array of the UnsavedData objects, which contain information about
134
             * the unsaved editing tables and allows to save it.</p>
135
             *
136
             * @return An array of the associated unsaved editing layers, or null in case the extension
137
             * has not unsaved editing tables.
138
             */
139
                public IUnsavedData[] getUnsavedData() {
140
                        ProjectExtension pe=(ProjectExtension)PluginServices.getExtension(ProjectExtension.class);
141 25068 vcaballero
                        FeatureTableDocument[] tables =pe.getProject().getDocumentsByType(FeatureTableDocumentFactory.registerName).toArray(new FeatureTableDocument[0]);
142 13519 caballero
                        ArrayList unsavedTables = new ArrayList();
143
                        for (int i=0;i<tables.length;i++) {
144 25068 vcaballero
                                if (tables[i].getStore() == null){
145 14845 jmvivo
                                        continue;
146
                                }
147 25068 vcaballero
                                if (tables[i].getStore().isEditing()) {
148 13519 caballero
                                        UnsavedTable ul=new UnsavedTable(TableEditStopExtension.this);
149
                                        ul.setTable(tables[i]);
150
                                        unsavedTables.add(ul);
151
                                }
152
                        }
153
                        return (IUnsavedData[])unsavedTables.toArray(new IUnsavedData[0]);
154
                }
155
        }
156
157
        private class UnsavedTable extends UnsavedData{
158
159 25068 vcaballero
                private FeatureTableDocument table;
160 13519 caballero
161
                public UnsavedTable(IExtension extension) {
162
                        super(extension);
163
                }
164
165
                public String getDescription() {
166
                        return PluginServices.getText(this,"editing_table_unsaved");
167
                }
168
169
                public String getResourceName() {
170
                        return table.getName();
171
                }
172
173
174
175
                public boolean saveData() {
176
                        return executeSaveTable(table);
177
                }
178
179
180
181 25068 vcaballero
                public void setTable(FeatureTableDocument table) {
182 13519 caballero
                        this.table=table;
183
184
                }
185
        }
186
187
188
        //TODO Este c?digo est? duplicado, tambi?n est? en la clase Table en el m?todo "public void stopEditing()"
189 25068 vcaballero
        private boolean executeSaveTable(FeatureTableDocument table2) {
190
                FeatureStore fs = table2.getStore();
191 21354 vcaballero
                try {
192
                        fs.finishEditing();
193
                } catch (WriteException e) {
194
                        NotificationManager.addError(PluginServices.getText(this,"error_saving_table"),e);
195
                        return false;
196
                } catch (ReadException e) {
197
                        NotificationManager.addError(PluginServices.getText(this,"error_saving_table"),e);
198
                        return false;
199 24923 vcaballero
                } catch (DataException e) {
200
                        NotificationManager.addError(PluginServices.getText(this,"error_saving_table"),e);
201
                        return false;
202 13519 caballero
                }
203 21354 vcaballero
//                if (ies instanceof IWriteable) {
204
//                        IWriteable w = (IWriteable) ies;
205
//                        IWriter writer = w.getWriter();
206
//                        if (writer == null) {
207
//                                return false;
208
//                        }
209
//                        try {
210
//                                ITableDefinition tableDef = ies.getTableDefinition();
211
//                                writer.initialize(tableDef);
212
//                                ies.stopEdition(writer, EditionEvent.ALPHANUMERIC);
213
//                                ies.getSelection().clear();
214
//                        } catch (InitializeWriterException e) {
215
//                                NotificationManager.addError(PluginServices.getText(this,"error_saving_table"),e);
216
//                                return false;
217
//                        } catch (StopWriterVisitorException e) {
218
//                                NotificationManager.addError(PluginServices.getText(this,"error_saving_table"),e);
219
//                                return false;
220
//                        } catch (ReadDriverException e) {
221
//                                NotificationManager.addError(PluginServices.getText(this,"error_saving_table"),e);
222
//                                return false;
223
//                        }
224
//                }
225 13519 caballero
                return true;
226
        }
227
        public IExtensionStatus getStatus() {
228
                return new StopEditingStatus();
229
        }
230 2936 caballero
}