Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / TableEditStopExtension.java @ 14845

History | View | Annotate | Download (7.13 KB)

1
package com.iver.cit.gvsig;
2

    
3
import java.util.ArrayList;
4

    
5
import javax.swing.JOptionPane;
6

    
7
import com.hardcode.gdbms.driver.exceptions.InitializeWriterException;
8
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
9
import com.iver.andami.PluginServices;
10
import com.iver.andami.messages.NotificationManager;
11
import com.iver.andami.plugins.Extension;
12
import com.iver.andami.plugins.IExtension;
13
import com.iver.andami.plugins.status.IExtensionStatus;
14
import com.iver.andami.plugins.status.IUnsavedData;
15
import com.iver.andami.plugins.status.UnsavedData;
16
import com.iver.andami.ui.mdiManager.IWindow;
17
import com.iver.cit.gvsig.exceptions.table.CancelEditingTableException;
18
import com.iver.cit.gvsig.exceptions.visitors.StopWriterVisitorException;
19
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition;
20
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
21
import com.iver.cit.gvsig.fmap.edition.IEditableSource;
22
import com.iver.cit.gvsig.fmap.edition.IWriteable;
23
import com.iver.cit.gvsig.fmap.edition.IWriter;
24
import com.iver.cit.gvsig.project.documents.table.ProjectTable;
25
import com.iver.cit.gvsig.project.documents.table.ProjectTableFactory;
26
import com.iver.cit.gvsig.project.documents.table.gui.Table;
27
import com.iver.utiles.swing.threads.IMonitorableTask;
28

    
29

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

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

    
54
    }
55

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

    
75

    
76
    /**
77
     * @see com.iver.andami.plugins.IExtension#isEnabled()
78
     */
79
    public boolean isEnabled() {
80
       return true;
81
    }
82

    
83
    /**
84
     * @see com.iver.andami.plugins.IExtension#isVisible()
85
     */
86
    public boolean isVisible() {
87
        IWindow v = PluginServices.getMDIManager().getActiveWindow();
88

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

    
168
        private class UnsavedTable extends UnsavedData{
169

    
170
                private ProjectTable table;
171

    
172
                public UnsavedTable(IExtension extension) {
173
                        super(extension);
174
                }
175

    
176
                public String getDescription() {
177
                        return PluginServices.getText(this,"editing_table_unsaved");
178
                }
179

    
180
                public String getResourceName() {
181
                        return table.getName();
182
                }
183

    
184

    
185

    
186
                public boolean saveData() {
187
                        return executeSaveTable(table);
188
                }
189

    
190

    
191

    
192
                public void setTable(ProjectTable table) {
193
                        this.table=table;
194

    
195
                }
196
        }
197

    
198

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