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 @ 36443

History | View | Annotate | Download (7.6 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

    
29
import javax.swing.JOptionPane;
30

    
31
import org.gvsig.andami.PluginServices;
32
import org.gvsig.andami.messages.NotificationManager;
33
import org.gvsig.app.project.documents.table.gui.CreateNewAttributePanel;
34
import org.gvsig.fmap.dal.DataTypes;
35
import org.gvsig.fmap.dal.exception.DataException;
36
import org.gvsig.fmap.dal.feature.EditableFeature;
37
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
38
import org.gvsig.fmap.dal.feature.EditableFeatureType;
39
import org.gvsig.fmap.dal.feature.Feature;
40
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
41
import org.gvsig.fmap.dal.feature.FeatureSelection;
42
import org.gvsig.fmap.dal.feature.FeatureStore;
43
import org.gvsig.fmap.mapcontrol.dal.feature.swing.FeatureTable;
44
import org.gvsig.tools.dispose.DisposableIterator;
45

    
46
/**
47
 * Feature Table Operations.
48
 * 
49
 * @author Vicente Caballero Navarro
50
 * 
51
 */
52
public class TableOperations {
53

    
54
    public static final int MAX_FIELD_LENGTH = 254;
55
    private static TableOperations fto = null;
56
    private FeatureStore featureStore;
57
    private ArrayList<Feature> selectedFeatures = new ArrayList<Feature>();
58
    private boolean cutting = false;
59

    
60
    public static TableOperations getInstance() {
61
        if (fto == null) {
62
            fto = new TableOperations();
63
        }
64
        return fto;
65
    }
66

    
67
    public void setStore(FeatureStore store) {
68
        featureStore = store;
69
    }
70

    
71
    public void copyFeatures() throws DataException {
72
        cutting = false;
73
        copy();
74
    }
75

    
76
    public boolean hasSelection() {
77
        return !selectedFeatures.isEmpty();
78
    }
79

    
80
    public void pasteFeatures() throws DataException {
81
        if (cutting) {
82
            delete();
83
            cutting = false;
84
        }
85
        Iterator<Feature> features = selectedFeatures.iterator();
86
        while (features.hasNext()) {
87
            Feature feature = features.next();
88
            featureStore.insert(feature.getEditable());
89
        }
90
    }
91

    
92
    public void cutFeatures() throws DataException {
93
        cutting = true;
94
        copy();
95
    }
96

    
97
    private void copy() throws DataException {
98
        DisposableIterator features = null;
99
        try {
100
            features =
101
                ((FeatureSelection) featureStore.getSelection()).fastIterator();
102
            selectedFeatures.clear();
103
            while (features.hasNext()) {
104
                Feature feature = (Feature) features.next();
105
                selectedFeatures.add(feature);
106
            }
107
        } finally {
108
            if (features != null) {
109
                features.dispose();
110
            }
111
        }
112
    }
113

    
114
    private void delete() throws DataException {
115
        Iterator<Feature> features = selectedFeatures.iterator();
116
        while (features.hasNext()) {
117
            Feature feature = features.next();
118
            featureStore.delete(feature);
119
        }
120
    }
121

    
122
    public void deleteFeatures() throws DataException {
123
        DisposableIterator features = null;
124
        try {
125
            features =
126
                ((FeatureSelection) featureStore.getSelection()).fastIterator();
127
            while (features.hasNext()) {
128
                Feature feature = (Feature) features.next();
129
                featureStore.delete(feature);
130
            }
131
        } finally {
132
            if (features != null) {
133
                features.dispose();
134
            }
135
        }
136
    }
137

    
138
    public void insertNewFeature() throws DataException {
139
        // if (getModel().getAssociatedTable()!=null){
140
        // JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),"No se puede a?adir una fila a una tabla asociada a una capa.");
141
        // return;
142
        // }
143
        EditableFeature feature = featureStore.createNewFeature();
144
        featureStore.insert(feature);
145
    }
146

    
147
    public void deleteAttributes(FeatureTable table) throws DataException {
148
        EditableFeatureType eft =
149
            featureStore.getDefaultFeatureType().getEditable();
150
        FeatureAttributeDescriptor[] selecteds =
151
            table.getSelectedColumnsAttributeDescriptor();
152
        for (int i = 0; i < selecteds.length; i++) {
153
            eft.remove(selecteds[i].getName());
154
        }
155
        featureStore.update(eft);
156
    }
157

    
158
    public void insertAttributes(FeatureTable table) throws DataException {
159
        EditableFeatureType eft =
160
            featureStore.getDefaultFeatureType().getEditable();
161

    
162
        try {
163
            CreateNewAttributePanel panelNewField =
164
                new CreateNewAttributePanel();
165

    
166
            EditableFeatureAttributeDescriptor ead =
167
                panelNewField.loadFieldDescription(eft);
168
            if (ead == null) {
169
                return;
170
            }
171
            if (ead.getType() == DataTypes.STRING
172
                && ead.getSize() > MAX_FIELD_LENGTH) {
173
                NotificationManager.showMessageInfo(
174
                    PluginServices.getText(this, "max_length_is") + ":"
175
                        + MAX_FIELD_LENGTH, null);
176
                ead.setSize(MAX_FIELD_LENGTH);
177
            }
178
            PluginServices.getMDIManager().closeWindow(panelNewField);
179
        } catch (ParseException e2) {
180
            NotificationManager.addError(e2);
181
        }
182
        featureStore.update(eft);
183

    
184
    }
185

    
186
    public void renameAttributes(FeatureTable table) throws DataException {
187
        EditableFeatureType eft =
188
            featureStore.getDefaultFeatureType().getEditable();
189
        FeatureAttributeDescriptor[] selecteds =
190
            table.getSelectedColumnsAttributeDescriptor();
191

    
192
        for (int i = selecteds.length - 1; i >= 0; i--) {
193
            String newName =
194
                JOptionPane.showInputDialog((Component) PluginServices
195
                    .getMDIManager().getActiveWindow(), PluginServices.getText(
196
                    this, "please_insert_new_field_name"), selecteds[i]
197
                    .getName());
198
            if (newName == null) {
199
                return;
200
            }
201
            if (eft.getIndex(newName) != -1) {
202
                NotificationManager.showMessageInfo(
203
                    PluginServices.getText(this, "field_already_exists"), null);
204
                return;
205
            }
206
            FeatureAttributeDescriptor ad =
207
                (FeatureAttributeDescriptor) eft.get(selecteds[i].getName());
208
            eft.remove(ad.getName());
209
            EditableFeatureAttributeDescriptor ead =
210
                eft.add(newName, ad.getType(), ad.getSize());
211
            ead.setPrecision(ad.getPrecision());
212
        }
213

    
214
        featureStore.update(eft);
215
    }
216

    
217
}