Revision 23787

View differences:

branches/v2_0_0_prep/applications/appgvSIG/src/org/gvsig/fmap/data/feature/swing/table/EditableFeatureCollectionTableModel.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Gobernment (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

  
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2008 {DiSiD Technologies}  {{Task}}
26
 */
27
package org.gvsig.fmap.data.feature.swing.table;
28

  
29
import org.gvsig.fmap.data.DataException;
30
import org.gvsig.fmap.data.ReadException;
31
import org.gvsig.fmap.data.feature.*;
32
import org.gvsig.fmap.data.feature.paging.FeatureCollectionPagingHelper;
33

  
34
/**
35
 * Extends the FeatureCollectionTableModel to add edition capabilities.
36
 * 
37
 * TODO: the current implementation does not work, update to the last API
38
 * changes for Feature edition.
39
 * 
40
 * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
41
 */
42
public class EditableFeatureCollectionTableModel extends
43
        FeatureCollectionTableModel {
44

  
45
    /**
46
     * Serial Version UID
47
     */
48
    private static final long serialVersionUID = -8486424379657039107L;
49
    
50
    private FeatureQuery featureQuery;
51

  
52
    /**
53
     * @see FeatureCollectionTableModel#FeatureCollectionTableModel(FeatureStore,
54
     *      FeatureQuery, int)
55
     */
56
    public EditableFeatureCollectionTableModel(FeatureStore featureStore,
57
            FeatureQuery featureQuery, int pageSize) throws ReadException {
58
        super(featureStore, featureQuery, pageSize);
59
    }
60

  
61
    /**
62
     * @see FeatureCollectionTableModel#FeatureCollectionTableModel(FeatureStore,
63
     *      FeatureQuery)
64
     */
65
    public EditableFeatureCollectionTableModel(FeatureStore featureStore,
66
            FeatureQuery featureQuery) throws ReadException {
67
        super(featureStore, featureQuery);
68
    }
69

  
70
    /**
71
     * @see FeatureCollectionTableModel#FeatureCollectionTableModel(FeatureCollectionPagingHelper)
72
     */
73
    public EditableFeatureCollectionTableModel(
74
            FeatureCollectionPagingHelper helper) {
75
        super(helper);
76
    }
77

  
78
    @Override
79
    public boolean isCellEditable(int rowIndex, int columnIndex) {
80
        return getFeatureStore().isEditing();
81
    }
82

  
83
    @Override
84
    public void setValueAt(Object value, int rowIndex, int columnIndex) {
85
        // Get the feature at rowIndex
86
        Feature feature = getHelper().getFeatureAt(rowIndex);
87
        // Only set the value if the feature exists
88
        if (feature != null) {
89
            // We only need to update if the value to set is not equal to the
90
            // current value
91
            Object currentValue = feature.get(columnIndex);
92
            if (value != currentValue
93
                    || (value != null && !value.equals(currentValue))) {
94
                try {
95
                    // getFeatureStore().startEditing();
96
                    //                    
97
                    // EditableFeature editable = feature.getEditableFeature();
98
                    // editable.set(columnIndex, value);
99
                    //                    
100
                    // getFeatureStore().update(editable);
101
                    // getFeatureStore().finishEditing();
102

  
103
                    getFeatureStore().startEditing();
104
                    feature.editing();
105
                    feature.set(columnIndex, value);
106
                    getFeatureStore().update(feature);
107
                    // getHelper().reload();
108
                    // TODO: REVISAR
109
                    getHelper().setFeatureCollection(
110
                            (FeatureCollection) getFeatureStore()
111
                                    .getDataCollection(featureQuery));
112
                } catch (DataException ex) {
113
                    throw new SetFeatureValueException(rowIndex, columnIndex,
114
                            value, ex);
115
                }
116
            }
117
        }
118
    }
119

  
120
}
branches/v2_0_0_prep/applications/appgvSIG/src/org/gvsig/fmap/data/feature/swing/table/FeatureCollectionTableModel.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Gobernment (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

  
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2008 {DiSiD Technologies}  {Create a JTable TableModel for a FeatureCollection}
26
 */
27
package org.gvsig.fmap.data.feature.swing.table;
28

  
29
import javax.swing.table.AbstractTableModel;
30

  
31
import org.gvsig.fmap.data.ReadException;
32
import org.gvsig.fmap.data.feature.*;
33
import org.gvsig.fmap.data.feature.paging.FeatureCollectionPagingHelper;
34
import org.gvsig.fmap.data.feature.paging.FeatureCollectionPagingHelperImpl;
35

  
36
/**
37
 * TableModel to access data of a FeatureCollection.
38
 * 
39
 * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
40
 */
41
public class FeatureCollectionTableModel extends AbstractTableModel {
42

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

  
45
    private FeatureCollectionPagingHelper helper;
46

  
47
    /**
48
     * Constructs a TableModel from a FeatureCollection, with the default page
49
     * size.
50
     * 
51
     * @param featureCollection
52
     *            to extract data from
53
     * @throws ReadException
54
     *             if there is an error reading data from the FeatureStore
55
     */
56
    public FeatureCollectionTableModel(FeatureStore featureStore,
57
            FeatureQuery featureQuery) throws ReadException {
58
        this(featureStore, featureQuery,
59
                FeatureCollectionPagingHelper.DEFAULT_PAGE_SIZE);
60
    }
61

  
62
    /**
63
     * Constructs a TableModel from a FeatureCollection, and a given page size.
64
     * 
65
     * @param featureCollection
66
     *            to extract data from
67
     * @param pageSize
68
     *            the number of elements per page data
69
     * @throws ReadException
70
     *             if there is an error reading data from the FeatureStore
71
     */
72
    public FeatureCollectionTableModel(FeatureStore featureStore,
73
            FeatureQuery featureQuery,
74
            int pageSize) throws ReadException {
75
        this(new FeatureCollectionPagingHelperImpl(featureStore,
76
                featureQuery,
77
                pageSize));
78
    }
79

  
80
    /**
81
     * Constructs a TableModel from a FeatureCollection and a Paging helper.
82
     * 
83
     * @param featureCollection
84
     *            to extract data from
85
     * @param helper
86
     *            the paging helper
87
     * @throws ReadException
88
     *             if there is an error reading data from the FeatureStore
89
     */
90
    public FeatureCollectionTableModel(FeatureCollectionPagingHelper helper) {
91
        this.helper = helper;
92
    }
93

  
94
    public int getColumnCount() {
95
        // Return the number of fields of the Features
96
        FeatureType featureType = getFeatureType();
97
        return featureType.getFields().length;
98
    }
99

  
100
    public int getRowCount() {
101
        // Return the total size of the collection
102
        return getHelper().getTotalSize();
103
    }
104

  
105
    public Object getValueAt(int rowIndex, int columnIndex) {
106
        // Get the Feature at row "rowIndex", and return the value of the
107
        // attribute at "columnIndex"
108
        return getHelper().getFeatureAt(rowIndex).get(columnIndex);
109
    }
110

  
111
    @SuppressWarnings("unchecked")
112
    public Class getColumnClass(int columnIndex) {
113
        // Return the class of the FeatureAttributeDescriptor for the value
114
        FeatureAttributeDescriptor attributeDesc = getAttributeDescriptor(columnIndex);
115
        Class clazz = attributeDesc.getObjectClass();
116
        return (clazz == null ? super.getColumnClass(columnIndex) : clazz);
117
    }
118
    
119
    public String getColumnName(int column) {
120
        // Return the Feature attribute name
121
        FeatureAttributeDescriptor attributeDesc = getAttributeDescriptor(column);
122
        return attributeDesc.getName();
123
    }
124

  
125
    /**
126
     * Returns a reference to the Paging Helper used to load the data from the
127
     * DataStore.
128
     * 
129
     * @return the paging helper
130
     */
131
    public FeatureCollectionPagingHelper getHelper() {
132
        return helper;
133
    }
134
    
135
    /**
136
     * Returns the FeatureCollection used to get the data.
137
     * 
138
     * @return the FeatureCollection
139
     */
140
    protected FeatureCollection getFeatureCollection() {
141
        return getHelper().getFeatureCollection();
142
    }
143

  
144
    /**
145
     * Returns the FeatureStore of the Collection.
146
     * 
147
     * @return the FeatureStore
148
     */
149
    protected FeatureStore getFeatureStore() {
150
        return getHelper().getFeatureStore();
151
    }
152

  
153
    /**
154
     * Returns the type of the features.
155
     */
156
    private FeatureType getFeatureType() {
157
        return getFeatureCollection().getFeatureType();
158
    }
159

  
160
    /**
161
     * Returns the descriptor of a Feature attribute.
162
     */
163
    private FeatureAttributeDescriptor getAttributeDescriptor(int columnIndex) {
164
        return getFeatureType().getByOrder(columnIndex);
165
    }
166
}
branches/v2_0_0_prep/applications/appgvSIG/src/org/gvsig/fmap/data/feature/swing/table/FeatureTableModel.java
28 28

  
29 29
import javax.swing.table.AbstractTableModel;
30 30

  
31
import org.gvsig.fmap.data.DataException;
32
import org.gvsig.fmap.data.ReadException;
31
import org.gvsig.fmap.data.exceptions.DataException;
32
import org.gvsig.fmap.data.exceptions.ReadException;
33 33
import org.gvsig.fmap.data.feature.*;
34 34
import org.gvsig.fmap.data.feature.paging.FeaturePagingHelper;
35 35
import org.gvsig.fmap.data.feature.paging.FeaturePagingHelperImpl;
......
55 55
     *             if there is an error reading data from the FeatureStore
56 56
     */
57 57
    public FeatureTableModel(FeatureStore featureStore,
58
            FeatureQuery featureQuery) throws ReadException {
58
            FeatureQuery featureQuery) throws DataException {
59 59
        this(featureStore, featureQuery, FeaturePagingHelper.DEFAULT_PAGE_SIZE);
60 60
    }
61 61

  
......
70 70
     *             if there is an error reading data from the FeatureStore
71 71
     */
72 72
    public FeatureTableModel(FeatureStore featureStore,
73
            FeatureQuery featureQuery, int pageSize) throws ReadException {
73
            FeatureQuery featureQuery, int pageSize) throws DataException {
74 74
        this(new FeaturePagingHelperImpl(featureStore, featureQuery, pageSize));
75 75
    }
76 76

  
......
92 92
    public int getColumnCount() {
93 93
        // Return the number of fields of the Features
94 94
        FeatureType featureType = getFeatureType();
95
        return featureType.getFields().length;
95
        return featureType.size();
96 96
    }
97 97

  
98 98
    public int getRowCount() {
......
126 126
        return getFeatureStore().isEditing();
127 127
    }
128 128

  
129
    /**
130
     * TODO: the current implementation does not work, update to the last API
131
     * changes for Feature edition.
132
     */
133 129
    @Override
134 130
    public void setValueAt(Object value, int rowIndex, int columnIndex) {
135 131
        // Get the feature at rowIndex
......
142 138
            if (value != currentValue
143 139
                    || (value != null && !value.equals(currentValue))) {
144 140
                try {
145
                    // getFeatureStore().startEditing();
146
                    //                    
147
                    // EditableFeature editable = feature.getEditableFeature();
148
                    // editable.set(columnIndex, value);
149
                    //                    
150
                    // getFeatureStore().update(editable);
151
                    // getFeatureStore().finishEditing();
141
                    FeatureStore store = getFeatureStore();
142
                    store.edit();
143
                    store.update(setFeatureValue(feature, columnIndex, value));
144
                    store.finishEditing();
152 145

  
153
                    getFeatureStore().startEditing();
154
                    feature.editing();
155
                    setFeatureValue(feature, columnIndex, value);
156
                    getFeatureStore().update(feature);
157
                    // getHelper().reload();
158
                    // TODO: REVISAR
159
                    // getHelper().setFeatureCollection(
160
                    // (FeatureCollection) getFeatureStore()
161
                    // .getDataCollection(getFeatureQuery()));
162

  
146
                    getHelper().reloadCurrentPage();
163 147
                    fireTableCellUpdated(rowIndex, columnIndex);
164 148
                } catch (DataException ex) {
165 149
                    throw new SetFeatureValueException(rowIndex, columnIndex,
......
180 164
    }
181 165

  
182 166
    /**
167
     * Sets the FeatureType to show in the table. Used for FeatureStores with
168
     * many simultaneous FeatureTypes supported. Will cause a reload of the
169
     * current data.
170
     * 
171
     * @param featureType
172
     *            the FeatureType of the Features
173
     * @throws DataException
174
     *             if there is an error loading the data
175
     */
176
    public void setFeatureType(FeatureType featureType) throws DataException {
177
        getFeatureQuery().setFeatureType(featureType);
178
        getHelper().reload();
179
        fireTableStructureChanged();
180
    }
181

  
182
    /**
183 183
     * Initialize the TableModel
184 184
     */
185 185
    protected void initialize() {
......
222 222
     * @throws IsNotFeatureSettingException
223 223
     *             if there is an error setting the value
224 224
     */
225
    protected void setFeatureValue(Feature feature, int columnIndex,
226
            Object value) throws IsNotFeatureSettingException {
227
        feature.set(columnIndex, value);
225
    protected EditableFeature setFeatureValue(Feature feature, int columnIndex,
226
            Object value) {
227
        EditableFeature editableFeature = feature.getEditable();
228
        editableFeature.set(columnIndex, value);
229
        return editableFeature;
228 230
    }
229 231

  
230 232
    /**
......
253 255
    protected FeatureQuery getFeatureQuery() {
254 256
        return getHelper().getFeatureQuery();
255 257
    }
256
    
258

  
257 259
    /**
258 260
     * Returns the type of the features.
259 261
     */
260 262
    private FeatureType getFeatureType() {
261
        return getFeatureCollection().getFeatureType();
263
        return getFeatureQuery().getFeatureType();
262 264
    }
263 265

  
264 266
    /**
265 267
     * Returns the descriptor of a Feature attribute.
266 268
     */
267 269
    private FeatureAttributeDescriptor getAttributeDescriptor(int columnIndex) {
268
        return getFeatureType().getByOrder(columnIndex);
270
        return getFeatureType().getAttributeDescriptor(columnIndex);
269 271
    }
270 272
}
branches/v2_0_0_prep/applications/appgvSIG/src/org/gvsig/fmap/data/feature/swing/table/ConfigurableFeatureTableModel.java
28 28

  
29 29
import java.util.*;
30 30

  
31
import org.gvsig.fmap.data.ReadException;
31
import org.gvsig.fmap.data.exceptions.DataException;
32 32
import org.gvsig.fmap.data.feature.*;
33 33
import org.gvsig.fmap.data.feature.paging.FeaturePagingHelper;
34 34

  
......
45 45
    private List<String> columnNames;
46 46

  
47 47
    private List<String> visibleColumnNames;
48
    
48

  
49 49
    private Map<String, String> name2Alias;
50 50

  
51 51
    /**
52 52
     * @see FeatureTableModel#FeatureTableModel(FeatureStore, FeatureQuery)
53 53
     */
54 54
    public ConfigurableFeatureTableModel(FeatureStore featureStore,
55
            FeatureQuery featureQuery) throws ReadException {
55
            FeatureQuery featureQuery) throws DataException {
56 56
        super(featureStore, featureQuery);
57 57
    }
58 58

  
......
60 60
     * @see FeatureTableModel#FeatureTableModel(FeatureStore, FeatureQuery, int)
61 61
     */
62 62
    public ConfigurableFeatureTableModel(FeatureStore featureStore,
63
            FeatureQuery featureQuery, int pageSize) throws ReadException {
63
            FeatureQuery featureQuery, int pageSize) throws DataException {
64 64
        super(featureStore, featureQuery, pageSize);
65 65
    }
66 66

  
......
102 102
            visibleColumnNames.remove(name);
103 103
            fireTableStructureChanged();
104 104
        }
105
        
105

  
106 106
    }
107 107

  
108 108
    /**
......
132 132
        visibleColumnNames.addAll(columnNames);
133 133
        fireTableStructureChanged();
134 134
    }
135
    
135

  
136 136
    /**
137 137
     * Sets the alias for a column.
138 138
     * 
......
145 145
        name2Alias.put(name, alias);
146 146
        fireTableStructureChanged();
147 147
    }
148
    
148

  
149 149
    public void orderByColumn(String name, boolean ascending)
150
            throws ReadException {
151
        String order = ascending ? name.concat(" ASC") : name.concat(" DESC"); 
152
        getHelper().getFeatureQuery().setOrder(order);
150
            throws DataException {
151
        FeatureQueryOrder order = getHelper().getFeatureQuery().getOrder();
152
        order.add(name, ascending);
153 153
        getHelper().reload();
154 154
        fireTableDataChanged();
155 155
    }
......
168 168
    private void initializeAliases() {
169 169
        int columns = super.getColumnCount();
170 170
        name2Alias = new HashMap<String, String>(columns);
171
        
171

  
172 172
        for (int i = 0; i < columns; i++) {
173 173
            String columnName = super.getColumnName(i);
174 174
            name2Alias.put(columnName, columnName);
......
189 189
            visibleColumnNames.add(columnName);
190 190
        }
191 191
    }
192
    
192

  
193 193
    /**
194 194
     * Returns the alias for the name of a column.
195 195
     * 
......
231 231
    }
232 232

  
233 233
    @Override
234
    protected void setFeatureValue(Feature feature, int columnIndex,
235
            Object value) throws IsNotFeatureSettingException {
234
    protected EditableFeature setFeatureValue(Feature feature, int columnIndex,
235
            Object value) {
236 236
        int realColumnIndex = getRealColumnIndex(columnIndex);
237
        super.setFeatureValue(feature, realColumnIndex, value);
237
        return super.setFeatureValue(feature, realColumnIndex, value);
238 238
    }
239 239
}

Also available in: Unified diff