Statistics
| Revision:

svn-gvsig-desktop / 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 / project / documents / table / TableOperations.java @ 39081

History | View | Annotate | Download (11.5 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.project.documents.table;
23

    
24
import java.awt.Component;
25
import java.text.ParseException;
26
import java.util.ArrayList;
27
import java.util.Iterator;
28
import java.util.List;
29

    
30
import javax.swing.JOptionPane;
31
import javax.swing.event.TableModelListener;
32

    
33
import org.slf4j.Logger;
34
import org.slf4j.LoggerFactory;
35

    
36
import org.gvsig.andami.PluginServices;
37
import org.gvsig.andami.messages.NotificationManager;
38
import org.gvsig.app.ApplicationLocator;
39
import org.gvsig.app.project.documents.table.gui.CreateNewAttributePanel;
40
import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel;
41
import org.gvsig.fmap.dal.DataTypes;
42
import org.gvsig.fmap.dal.exception.DataException;
43
import org.gvsig.fmap.dal.feature.EditableFeature;
44
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
45
import org.gvsig.fmap.dal.feature.EditableFeatureType;
46
import org.gvsig.fmap.dal.feature.Feature;
47
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
48
import org.gvsig.fmap.dal.feature.FeatureSelection;
49
import org.gvsig.fmap.dal.feature.FeatureSet;
50
import org.gvsig.fmap.dal.feature.FeatureStore;
51
import org.gvsig.fmap.dal.feature.FeatureType;
52
import org.gvsig.fmap.mapcontrol.dal.feature.swing.FeatureTable;
53
import org.gvsig.fmap.mapcontrol.dal.feature.swing.table.FeatureTableModel;
54
import org.gvsig.i18n.Messages;
55
import org.gvsig.tools.dispose.DisposableIterator;
56

    
57
/**
58
 * Feature Table Operations.
59
 * 
60
 * @author Vicente Caballero Navarro
61
 * 
62
 */
63
public class TableOperations {
64

    
65
    private static Logger logger = LoggerFactory.getLogger(TableOperations.class);
66
    
67
    public static final int MAX_FIELD_LENGTH = 254;
68
    private static TableOperations fto = null;
69
    private ArrayList<Feature> selectedFeatures = new ArrayList<Feature>();
70
    private boolean cutting = false;
71
    
72
    private FeatureTableDocumentPanel tablePanel = null;
73
    private FeatureStore featureStore;
74

    
75
    public static TableOperations getInstance() {
76
        if (fto == null) {
77
            fto = new TableOperations();
78
        }
79
        return fto;
80
    }
81

    
82
    public void setTablePanel(FeatureTableDocumentPanel tp) {
83
        tablePanel = tp;
84
        featureStore = tp.getModel().getStore();
85
    }
86

    
87
    public void copyFeatures() throws DataException {
88
        cutting = false;
89
        copy();
90
    }
91

    
92
    public boolean hasSelection() {
93
        return !selectedFeatures.isEmpty();
94
    }
95

    
96
    public void pasteFeatures() throws DataException {
97
        if (cutting) {
98
            delete();
99
            cutting = false;
100
        }
101
        Iterator<Feature> features = selectedFeatures.iterator();
102
        while (features.hasNext()) {
103
            Feature feature = features.next();
104
            featureStore.insert(feature.getEditable());
105
        }
106
    }
107

    
108
    public void cutFeatures() throws DataException {
109
        cutting = true;
110
        copy();
111
    }
112

    
113
    private void copy() throws DataException {
114
        DisposableIterator features = null;
115
        try {
116
            features =
117
                ((FeatureSelection) featureStore.getSelection()).fastIterator();
118
            selectedFeatures.clear();
119
            while (features.hasNext()) {
120
                Feature feature = (Feature) features.next();
121
                selectedFeatures.add(feature);
122
            }
123
        } finally {
124
            if (features != null) {
125
                features.dispose();
126
            }
127
        }
128
    }
129

    
130
    private void delete() throws DataException {
131
        Iterator<Feature> features = selectedFeatures.iterator();
132
        while (features.hasNext()) {
133
            Feature feature = features.next();
134
            featureStore.delete(feature);
135
        }
136
    }
137

    
138
    public void deleteFeatures() throws DataException {
139
        
140
        FeatureTableModel _ftm = this.tablePanel.getTablePanel().getTableModel();
141
        List<TableModelListener> tmll = removeTableModelListeners(_ftm);
142
        
143

    
144
        DisposableIterator features = null;
145
        try {
146
            /*
147
            features =
148
                ((FeatureSelection) featureStore.getSelection()).fastIterator();
149
                */
150
            
151
            FeatureSet all_fset = featureStore.getFeatureSet();
152
            FeatureSelection sele = (FeatureSelection) featureStore.getSelection();
153
            features = all_fset.fastIterator();
154
            Feature item = null;
155
            
156
            while (features.hasNext()) {
157
                item = (Feature) features.next();
158
                if (sele.isSelected(item)) {
159
                    all_fset.delete(item);
160
                }
161
            }
162

    
163
            /*
164
            while (features.hasNext()) {
165
                features.remove();
166
            }
167
            */
168
            
169
        } finally {
170
            if (features != null) {
171
                features.dispose();
172
            }
173
            
174
            addTableModelListeners(_ftm, tmll);
175
        }
176
    }
177

    
178
    /**
179
     * @param _ftm
180
     * @param tmll
181
     */
182
    private void addTableModelListeners(
183
        FeatureTableModel _model,
184
        List<TableModelListener> _list) {
185
        
186
        Iterator<TableModelListener> iter = _list.iterator();
187
        while (iter.hasNext()) {
188
            _model.addTableModelListener(iter.next());
189
        }
190
        _model.fireTableDataChanged();
191
    }
192

    
193
    /**
194
     * @param ftm
195
     * @param class1
196
     * @return
197
     */
198
    private List<TableModelListener> removeTableModelListeners(FeatureTableModel ftm) {
199
        
200
        TableModelListener[] ll = ftm.getListeners(TableModelListener.class);
201
        List<TableModelListener> resp = new ArrayList<TableModelListener>();
202
        
203
        int n = ll.length;
204
        for (int i=0; i<n; i++) {
205
            resp.add(ll[i]);
206
            ftm.removeTableModelListener(ll[i]);
207
        }
208

    
209
        return resp;
210
    }
211

    
212
    public void insertNewFeature() throws DataException {
213
        // if (getModel().getAssociatedTable()!=null){
214
        // JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),"No se puede a?adir una fila a una tabla asociada a una capa.");
215
        // return;
216
        // }
217
        EditableFeature feature = featureStore.createNewFeature();
218
        featureStore.insert(feature);
219
    }
220

    
221
    public void deleteAttributes(FeatureTable table) throws DataException {
222
        EditableFeatureType eft =
223
            featureStore.getDefaultFeatureType().getEditable();
224
        FeatureAttributeDescriptor[] selecteds =
225
            table.getSelectedColumnsAttributeDescriptor();
226
        for (int i = 0; i < selecteds.length; i++) {
227
            eft.remove(selecteds[i].getName());
228
        }
229
        featureStore.update(eft);
230
    }
231

    
232
    public void insertAttributes(FeatureTable table) throws DataException {
233
        EditableFeatureType eft =
234
            featureStore.getDefaultFeatureType().getEditable();
235

    
236
        try {
237
            CreateNewAttributePanel panelNewField =
238
                new CreateNewAttributePanel();
239

    
240
            EditableFeatureAttributeDescriptor ead =
241
                panelNewField.loadFieldDescription(eft);
242
            if (ead == null) {
243
                return;
244
            }
245
            if (ead.getType() == DataTypes.STRING
246
                && ead.getSize() > MAX_FIELD_LENGTH) {
247
                NotificationManager.showMessageInfo(
248
                    PluginServices.getText(this, "max_length_is") + ":"
249
                        + MAX_FIELD_LENGTH, null);
250
                ead.setSize(MAX_FIELD_LENGTH);
251
            }
252
            PluginServices.getMDIManager().closeWindow(panelNewField);
253
        } catch (ParseException e2) {
254
            NotificationManager.addError(e2);
255
        }
256
        featureStore.update(eft);
257

    
258
    }
259

    
260
    public void renameAttributes(FeatureTable table) throws DataException {
261
        
262
        FeatureType _ft = featureStore.getDefaultFeatureType();
263

    
264
        FeatureAttributeDescriptor[] selecteds =
265
            table.getSelectedColumnsAttributeDescriptor();
266

    
267
        for (int i = selecteds.length - 1; i >= 0; i--) {
268
            String newName =
269
                JOptionPane.showInputDialog((Component) PluginServices
270
                    .getMDIManager().getActiveWindow(),
271
                    PluginServices.getText(
272
                    this, "_Please_insert_new_field_name_Cannot_be_undone"),
273
                    selecteds[i]
274
                    .getName());
275
            if (newName == null) {
276
                return;
277
            }
278
            if (_ft.getIndex(newName) != -1) {
279
                NotificationManager.showMessageInfo(
280
                    PluginServices.getText(this, "field_already_exists"), null);
281
                return;
282
            }
283
            
284
            renameAttribute(featureStore, selecteds[i].getName(), newName);
285
        }
286
        
287
        featureStore.finishEditing();
288
        // featureStore.edit(FeatureStore.MODE_FULLEDIT);
289
    }
290

    
291
    /**
292
     * This method renames a field in three steps:
293
     * 
294
     * (1) add new field using type and size of old field.
295
     * (2) copy value from old field to new field.
296
     * (3) remove old field.
297
     * 
298
     * @param fs
299
     * @param name
300
     * @param newName
301
     */
302
    private void renameAttribute(FeatureStore fs, String name, String newName) {
303

    
304
        try {
305
            
306
            // ========== add new field
307
            EditableFeatureType eft = fs.getDefaultFeatureType().getEditable();
308
            FeatureAttributeDescriptor fad = eft.getAttributeDescriptor(name);
309
            eft.add(newName, fad.getType(), fad.getSize());
310
            fs.update(eft);
311
            
312
            // ========== copy value old field -> new field
313
            FeatureSet fset = fs.getFeatureSet();
314
            DisposableIterator diter = fset.fastIterator();
315
            Feature feat = null;
316
            Object val = null;
317
            EditableFeature efeat = null;
318
            while (diter.hasNext()) {
319
                feat = (Feature) diter.next();
320
                val = feat.get(name);
321
                efeat = feat.getEditable();
322
                efeat.set(newName, val);
323
                fset.update(efeat);
324
            }
325
            diter.dispose();
326

    
327
            // ========== delete old field
328
            eft = fs.getDefaultFeatureType().getEditable();
329
            eft.remove(name);
330
            fs.update(eft);
331
            
332
        } catch (Exception ex) {
333
            logger.info("Unable to rename attribute (" + name + " --> " + newName + ")", ex);
334
            ApplicationLocator.getManager().message(
335
                Messages.getText("_Unable_to_rename_attribute"),
336
                JOptionPane.ERROR_MESSAGE);
337
        }
338

    
339
    }
340

    
341
}