Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / table / gui / tablemodel / DataSourceDataModel.java @ 23303

History | View | Annotate | Download (7.78 KB)

1
package com.iver.cit.gvsig.project.documents.table.gui.tablemodel;
2

    
3
import java.util.ConcurrentModificationException;
4
import java.util.Date;
5

    
6
import javax.swing.table.AbstractTableModel;
7

    
8
import org.apache.log4j.Logger;
9
import org.gvsig.fmap.data.DataException;
10
import org.gvsig.fmap.data.ReadException;
11
import org.gvsig.fmap.data.feature.Feature;
12
import org.gvsig.fmap.data.feature.FeatureCollection;
13
import org.gvsig.fmap.data.feature.FeatureStore;
14

    
15
import com.iver.cit.gvsig.project.documents.table.ProjectTable;
16

    
17
/**
18
 * DOCUMENT ME!
19
 *
20
 * @author Fernando Gonz?lez Cort?s
21
 */
22
public class DataSourceDataModel extends AbstractTableModel {
23
        private static Logger logger = Logger.getLogger(DataSourceDataModel.class.getName());
24
        //private SelectableDataSource dataSource;
25
    ProjectTable pt;
26
    boolean hasAssociatedLayer=false;
27
    private Feature lastRowEdited;
28
        private int lastNumRow=-1;
29
        private String order;
30
        private FeatureCollection featureCollection = null;
31
        private long rowcount = -1;
32

    
33
    /**
34
     * Crea un nuevo DataSourceDataModel.
35
     *
36
     * @param pt DOCUMENT ME!
37
     */
38
    public DataSourceDataModel(ProjectTable pt) {
39
        this.pt = pt;
40
        if (pt.getAssociatedTable()!=null) {
41
                hasAssociatedLayer=true;
42
        }
43
        this.refreshCollection();
44
        //try {
45
        //dataSource = pt.getModelo().getRecordset();
46
        //} catch (DriverLoadException e) {
47
        // TODO Auto-generated catch block
48
        //        e.printStackTrace();
49
        //}
50
    }
51

    
52
    private void refreshCollection() {
53
                try {
54
                        featureCollection = (FeatureCollection) pt.getModel()
55
                                        .getDataCollection(pt.getModel().getDefaultFeatureType(),
56
                                                        null, order);
57
                } catch (ReadException e) {
58
                        // TODO Auto-generated catch block
59
                        e.printStackTrace();
60
                }
61

    
62
        }
63

    
64

    
65

    
66
    /**
67
     * Returns the name of the field.
68
     *
69
     * @param col index of field
70
     *
71
     * @return Name of field
72
     */
73
    public String getColumnName(int col) {
74
//            if (col==0)
75
//                    return " ";
76
//            col--;
77
            if (col>=pt.getMapping().length) {
78
                    return null;
79
            }
80
            int i=pt.getMapping()[col];
81
            return pt.getAliases()[i];
82
    }
83

    
84
    /**
85
     * Returns the number of fields.
86
     *
87
     * @return number of fields
88
     */
89
    public int getColumnCount() {
90
        return pt.getMapping().length;
91
    }
92

    
93
    /**
94
     * Returns number of rows.
95
     *
96
     * @return number of rows.
97
     */
98
    public int getRowCount() {
99
            int count = 0;
100
            try {
101
                        count = this.featureCollection.size();
102
                } catch (ConcurrentModificationException e) {
103
                        this.refreshCollection();
104
                        count = this.featureCollection.size();
105
                }
106
                return count;
107
    }
108

    
109
    /**
110
     * DOCUMENT ME!
111
     *
112
     * @param row DOCUMENT ME!
113
     * @param col DOCUMENT ME!
114
     *
115
     * @return DOCUMENT ME!
116
     */
117
    public Object getValueAt(int row, int col) {
118
//            FeatureStore fs=pt.getModelo();
119
//            if (hasAssociatedLayer) {
120
//                    Object stoppingEditing =((FLayer)pt.getAssociatedTable()).getProperty("stoppingEditing");
121
//
122
//                    if (stoppingEditing != null && ((Boolean)(stoppingEditing)).booleanValue()) {
123
//                            return new String("");
124
//                    }
125
//            }
126
//       if (col==0){
127
//               return new Integer(row);
128
//       }
129
//       col--;
130
       int numRow;
131
//       long[] orderIndexes=pt.getOrderIndexes();
132
//       if ( orderIndexes != null && row < orderIndexes.length) {
133
//               numRow=(int)orderIndexes[row];
134
//       }else {
135
               numRow=row;
136
//       }
137
       Object obj =null;
138
////TODO            try {
139
//                    if (hasAssociatedLayer)
140
//                            fs.getRecordset().start();
141
                    Feature rowEdited=null;
142
                        if (lastNumRow==numRow){
143
                                rowEdited=lastRowEdited;
144
                        }else{
145
                                try {
146
                                        rowEdited=featureCollection.getFeature(numRow);
147
                                } catch (ReadException e) {
148
                                        e.printStackTrace();
149
                                }
150
                                lastRowEdited=rowEdited;
151
                                lastNumRow=numRow;
152
                        }
153
                    obj= rowEdited.get(pt.getMapping()[col]);
154

    
155
                        //CHEMA: Problema con las fechas:
156
                        // Esto deberia solucionarse en el ValueFactory.createValueByType
157
                        if (obj instanceof Date) {
158
                                Date date = (Date) obj;
159

    
160
                                String str = "" + (date.getMonth() +1) + "/" + date.getDate()+ "/" +(date.getYear() +1900);
161
                                obj =  str;
162

    
163
                        }
164

    
165

    
166

    
167
//                        if (hasAssociatedLayer)
168
//                                pt.getModelo().getRecordset().stop();
169
//                        return obj;
170
////TODO                } catch (ReadException e1) {
171
////                        logger.error("Error reading data row= "+row+" column= "+col,e1);
172
////                        obj= new String("");
173
////                }
174
//                if (hasAssociatedLayer)
175
//                        try {
176
//                                ies.getRecordset().stop();
177
//                        } catch (ReadDriverException e1) {
178
//                                logger.error("Error reading data row= "+row+" column= "+col,e1);
179
//                                obj= ValueFactory.createValue("");
180
//                        }
181
                return obj;
182
    }
183

    
184
    /**
185
     * DOCUMENT ME!
186
     *
187
     * @param rowIndex DOCUMENT ME!
188
     * @param columnIndex DOCUMENT ME!
189
     *
190
     * @return DOCUMENT ME!
191
     */
192
    public boolean isCellEditable(int rowIndex, int columnIndex) {
193
//            if (columnIndex==0)
194
//            return false;
195
//            try {
196
//                        if (pt.getModelo().getFieldType(columnIndex)==Types.STRUCT)
197
//                                return true;
198
//                } catch (ReadDriverException e) {
199
//                        e.printStackTrace();
200
//                }
201
            return pt.getModel().isEditing();
202
    }
203

    
204
    /**
205
     * DOCUMENT ME!
206
     *
207
     * @param aValue DOCUMENT ME!
208
     * @param rowIndex DOCUMENT ME!
209
     * @param columnIndex DOCUMENT ME!
210
     *
211
     * @throws RuntimeException DOCUMENT ME!
212
     */
213
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
214
//             if (columnIndex==0)
215
//             throw new UnsupportedOperationException("Row Number is a read-only column");
216
//             columnIndex--;
217
//            Object v;
218
             int numFeature;
219
         if (pt.getOrderIndexes() != null) {
220
                 numFeature=(int)pt.getOrderIndexes()[rowIndex];
221
         }else {
222
                 numFeature=rowIndex;
223
         }
224
            if (getValueAt(rowIndex,columnIndex)==null || getValueAt(rowIndex,columnIndex).toString().equals(aValue)) {
225
                        return;
226
                }
227
        try {
228
                FeatureStore fs = pt.getModel();
229
//                try{
230
//                v = ValueFactory.createValueByType(aValue.toString(),
231
//                    des.getRecordset().getFieldType(columnIndex));
232
//            } catch (ParseException e) {
233
//                    if (aValue.equals("")){
234
//                            v = ValueFactory.createNullValue();
235
//                    } else {
236
//                            JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),"formato_del_campo_incorrecto");
237
//                            return;
238
//                    }
239
//                }
240
                Feature feature = featureCollection.getFeature(numFeature); //.getAttribute(columnIndex);
241
            feature.editing();
242
            feature.set(columnIndex,aValue);
243
            fs.update(feature);
244

    
245
//            Value[] values = row.getAttributes();
246
//            values[columnIndex] = v;
247

    
248
//            IRow newRow = null;
249
//
250
//            if (row.getLinkedRow() instanceof IFeature) {
251
//                IGeometry geometry = ((DefaultFeature) row.getLinkedRow()).getGeometry();
252
//                newRow = new DefaultFeature(geometry, values,row.getID());
253
//            } else {
254
//                newRow = new DefaultRow(values,row.getID());
255
//            }
256

    
257
//            des.modifyRow(numRow, newRow,"Editar valor", EditionEvent.ALPHANUMERIC);
258
        } catch (ReadException e1) {
259
            throw new RuntimeException(e1);
260
        } catch (NumberFormatException e) {
261
                /*        NotificationManager.addError(PluginServices.-getText(this,"numero_incorrecto")+
262
                                        "\n"+PluginServices.-getText(this,"fila")+" = "+rowIndex+
263
                                        "\n"+PluginServices.-getText(this,"columna")+ " = "+columnIndex,e);
264
                */
265
                } catch (DataException e) {
266
                        // TODO Auto-generated catch block
267
                        e.printStackTrace();
268
                }
269
    }
270

    
271
    public void setOrder(String s){
272
            order=s;
273
             try {
274
                         featureCollection=(FeatureCollection)pt.getModel().getDataCollection(pt.getModel().getDefaultFeatureType(),null,order);
275
                 } catch (ReadException e) {
276
                         // TODO Auto-generated catch block
277
                         e.printStackTrace();
278
                 }
279
    }
280

    
281

    
282
}