Statistics
| Revision:

gvsig-derived-geometries / org.gvsig.derivedgeometries / trunk / org.gvsig.derivedgeometries / org.gvsig.derivedgeometries.swing / org.gvsig.derivedgeometries.swing.impl / src / main / java / org / gvsig / derivedgeometries / swing / impl / views / SelectedFeaturesTableModel.java @ 68

History | View | Annotate | Download (5.54 KB)

1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2014 gvSIG Association
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.derivedgeometries.swing.impl.views;
24

    
25
import java.util.ArrayList;
26
import java.util.Collections;
27
import java.util.List;
28

    
29
import javax.swing.table.AbstractTableModel;
30

    
31
import org.gvsig.derivedgeometries.swing.api.exceptions.FormatRowException;
32
import org.gvsig.derivedgeometries.swing.api.exceptions.LoadSelectedFeatureDataException;
33
import org.gvsig.fmap.dal.exception.DataException;
34
import org.gvsig.fmap.dal.feature.Feature;
35
import org.gvsig.fmap.dal.feature.FeatureReference;
36
import org.gvsig.fmap.dal.feature.FeatureStore;
37
import org.gvsig.fmap.dal.feature.FeatureType;
38
import org.gvsig.tools.ToolsLocator;
39
import org.gvsig.tools.i18n.I18nManager;
40

    
41
public class SelectedFeaturesTableModel extends AbstractTableModel {
42

    
43
    private static final long serialVersionUID = -3905635376996886077L;
44

    
45
    private List<String[]> data = null;
46

    
47
    private String[] columnNames = null;
48

    
49
    private int rowCount = 0;
50

    
51
    private FeatureStore featureStore;
52

    
53
    // Autoincrement variable
54
    private int orderCount = 0;
55

    
56
    public SelectedFeaturesTableModel() {
57
        this.featureStore = null;
58
    }
59

    
60
    public SelectedFeaturesTableModel(FeatureStore theFeatureStore) {
61
        this.featureStore = theFeatureStore;
62
    }
63

    
64
    public void loadData(List<FeatureReference> features)
65
        throws LoadSelectedFeatureDataException {
66
        clear();
67
        List<String[]> tmpData = new ArrayList<String[]>();
68
        Feature feature = null;
69
        for (FeatureReference featureReference : features) {
70

    
71
            try {
72
                feature = featureReference.getFeature();
73
                tmpData.add(formatRow(feature));
74
            } catch (DataException e) {
75
                String message =
76
                    String.format("Error getting feature through %1 ",
77
                        featureReference);
78
                throw new LoadSelectedFeatureDataException(message, e);
79
            } catch (FormatRowException e) {
80
                String message =
81
                    String.format("Error formatting data of %1", feature);
82
                throw new LoadSelectedFeatureDataException(message, e);
83
            }
84

    
85
        }
86

    
87
        data = Collections.unmodifiableList(tmpData);
88
        rowCount = data.size();
89

    
90
        try {
91
            columnNames =
92
                getColumnNamesFromType(featureStore.getDefaultFeatureType());
93
        } catch (DataException e) {
94
            String message = String.format("Error getting feature type of %1", featureStore);
95
            throw new LoadSelectedFeatureDataException(message, e);
96
        }
97

    
98
        fireTableStructureChanged();
99
    }
100

    
101
    private String[] getColumnNamesFromType(FeatureType featureType) {
102
        int count = featureType.size();
103
        List<String> names = new ArrayList<String>();
104
        I18nManager i18nManager = ToolsLocator.getI18nManager();
105
        names.add(i18nManager.getTranslation("_Order"));
106
        for (int i = 0; i < count; i++) {
107
            names.add(featureType.getAttributeDescriptor(i).getName());
108
        }
109
        return names.toArray(new String[] {});
110
    }
111

    
112
    private String[] formatRow(Feature feature) throws FormatRowException {
113
        FeatureType featureType;
114
        try {
115
            featureType = featureStore.getDefaultFeatureType();
116
        } catch (DataException e) {
117
            String message =
118
                String.format("Error getting feature type of %1",
119
                    featureStore.getName());
120
            throw new FormatRowException(message, e);
121
        }
122
        int featureSize = featureType.size();
123
        List<String> featureData = null;
124
        if (featureSize > 0) {
125
            featureData = new ArrayList<String>();
126

    
127
            // Add Order data and autoincrement
128
            featureData.add(String.valueOf(orderCount));
129
            orderCount++;
130

    
131
            for (int i = 0; i < featureSize; i++) {
132
                featureData.add(String.valueOf(feature.get(i)));
133
            }
134
        }
135
        return featureData.toArray(new String[] {});
136
    }
137

    
138
    public int getRowCount() {
139
        return rowCount;
140
    }
141

    
142
    public int getColumnCount() {
143
        if (columnNames == null) {
144
            return 0;
145
        }
146
        return columnNames.length;
147
    }
148

    
149
    public Object getValueAt(int rowIndex, int columnIndex) {
150
        if (data == null) {
151
            return "";
152
        }
153
        return data.get(rowIndex)[columnIndex];
154
    }
155

    
156
    @Override
157
    public String getColumnName(int column) {
158
        if (columnNames == null) {
159
            return null;
160
        }
161
        return columnNames[column];
162
    }
163

    
164
    public void clear() {
165
        data = null;
166
        columnNames = null;
167
        rowCount = 0;
168
        orderCount = 0;
169
        fireTableDataChanged();
170
    }
171
}