Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / project / document / table / FeatureTableOperations.java @ 26053

History | View | Annotate | Download (5.35 KB)

1
package org.gvsig.project.document.table;
2

    
3
import java.awt.Component;
4
import java.text.ParseException;
5
import java.util.ArrayList;
6
import java.util.Iterator;
7

    
8
import javax.swing.JOptionPane;
9

    
10
import org.gvsig.fmap.dal.DataTypes;
11
import org.gvsig.fmap.dal.exception.DataException;
12
import org.gvsig.fmap.dal.feature.EditableFeature;
13
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
14
import org.gvsig.fmap.dal.feature.EditableFeatureType;
15
import org.gvsig.fmap.dal.feature.Feature;
16
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
17
import org.gvsig.fmap.dal.feature.FeatureSelection;
18
import org.gvsig.fmap.dal.feature.FeatureStore;
19
import org.gvsig.fmap.data.feature.swing.FeatureTable;
20
import org.gvsig.project.document.table.gui.CreateNewAttributePanel;
21

    
22
import com.iver.andami.PluginServices;
23
import com.iver.andami.messages.NotificationManager;
24
/**
25
 * Feature Table Operations.
26
 *
27
 * @author Vicente Caballero Navarro
28
 *
29
 */
30
public class FeatureTableOperations {
31
        public static final int MAX_FIELD_LENGTH = 254;
32
        private static FeatureTableOperations fto=null;
33
        private FeatureStore featureStore;
34
        private ArrayList<Feature> selectedFeatures=new ArrayList<Feature>();
35
        private boolean cutting=false;
36

    
37
        public static FeatureTableOperations getInstance(){
38
                if (fto==null){
39
                        fto=new FeatureTableOperations();
40
                }
41
                return fto;
42
        }
43

    
44
        public void setStore(FeatureStore store) {
45
                featureStore=store;
46
        }
47

    
48
        public void copyFeatures() throws DataException {
49
                cutting=false;
50
                copy();
51
        }
52

    
53
        public boolean hasSelection() {
54
                return !selectedFeatures.isEmpty();
55
        }
56

    
57
        public void pasteFeatures() throws DataException {
58
                if (cutting){
59
                        delete();
60
                        cutting=false;
61
                }
62
                Iterator<Feature> features=selectedFeatures.iterator();
63
                while (features.hasNext()) {
64
                        Feature feature = (Feature) features.next();
65
                        featureStore.insert(feature.getEditable());
66
                }
67
        }
68

    
69
        public void cutFeatures() throws DataException {
70
                cutting=true;
71
                copy();
72
        }
73
        private void copy() throws DataException{
74
                Iterator<Feature> features=((FeatureSelection)featureStore.getSelection()).iterator();
75
                selectedFeatures.clear();
76
                while (features.hasNext()) {
77
                        Feature feature = (Feature) features.next();
78
                        selectedFeatures.add(feature);
79
                }
80
        }
81
        private void delete() throws DataException{
82
                Iterator<Feature> features=selectedFeatures.iterator();
83
                while (features.hasNext()) {
84
                        Feature feature = (Feature) features.next();
85
                        featureStore.delete(feature);
86
                }
87
        }
88
        public void deleteFeatures() throws DataException{
89
                Iterator<Feature> features=((FeatureSelection)featureStore.getSelection()).iterator();
90
                while (features.hasNext()) {
91
                        Feature feature = (Feature) features.next();
92
                        featureStore.delete(feature);
93
                }
94
        }
95
        public void insertNewFeature() throws DataException {
96
//                if (getModel().getAssociatedTable()!=null){
97
//                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),"No se puede a?adir una fila a una tabla asociada a una capa.");
98
//                        return;
99
//                }
100
                EditableFeature feature = featureStore.createNewFeature();
101
                featureStore.insert(feature);
102
        }
103
        public void deleteAttributes(FeatureTable table) throws DataException {
104
                EditableFeatureType eft = featureStore.getDefaultFeatureType().getEditable();
105
                FeatureAttributeDescriptor[] selecteds =table.getSelectedColumnsAttributeDescriptor();
106
                for (int i = 0; i < selecteds.length; i++) {
107
                        eft.remove(selecteds[i].getName());
108
                }
109
                featureStore.update(eft);
110
        }
111

    
112
        public void insertAttributes(FeatureTable table) throws DataException {
113
                EditableFeatureType eft = featureStore.getDefaultFeatureType().getEditable();
114

    
115
                try {
116
                        CreateNewAttributePanel panelNewField = new CreateNewAttributePanel();
117

    
118
                        EditableFeatureAttributeDescriptor ead = panelNewField
119
                        .loadFieldDescription(eft);
120
                        if (ead == null)
121
                                return;
122
                        if (ead.getDataType() == DataTypes.STRING
123
                                        && ead.getSize() > MAX_FIELD_LENGTH) {
124
                                NotificationManager.showMessageInfo( PluginServices.getText(this,
125
                                "max_length_is")
126
                                + ":" + MAX_FIELD_LENGTH, null);
127
                                ead.setSize(MAX_FIELD_LENGTH);
128
                        }
129
                        PluginServices.getMDIManager().closeWindow(
130
                                        panelNewField);
131
                } catch (ParseException e2) {
132
                        NotificationManager.addError(e2);
133
                }
134
                featureStore.update(eft);
135

    
136
        }
137

    
138
        public void renameAttributes(FeatureTable table) throws DataException {
139
                EditableFeatureType eft = featureStore.getDefaultFeatureType().getEditable();
140
                FeatureAttributeDescriptor[] selecteds =table.getSelectedColumnsAttributeDescriptor();
141

    
142
                for (int i = selecteds.length - 1; i >= 0; i--) {
143
                        String newName = JOptionPane
144
                        .showInputDialog(
145
                                        (Component) PluginServices
146
                                        .getMDIManager()
147
                                        .getActiveWindow(),
148
                                        PluginServices
149
                                        .getText(this,
150
                                                        "please_insert_new_field_name"),
151
                                                        selecteds[i].getName());
152
                        if (newName == null)
153
                                return;
154
                        if (eft.getIndex(newName) != -1) {
155
                                NotificationManager.showMessageInfo(
156
                                                PluginServices.getText(this,
157
                                                "field_already_exists"),
158
                                                null);
159
                                return;
160
                        }
161
                        FeatureAttributeDescriptor ad = (FeatureAttributeDescriptor) eft
162
                        .get(selecteds[i].getName());
163
                        eft.remove(ad.getName());
164
                        EditableFeatureAttributeDescriptor ead = eft
165
                        .add(newName, ad.getDataType(), ad
166
                                        .getSize());
167
                        ead.setPrecision(ad.getPrecision());
168
                }
169

    
170
                featureStore.update(eft);
171
        }
172

    
173

    
174
}