Statistics
| Revision:

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

History | View | Annotate | Download (7.46 KB)

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

    
3
import java.awt.Component;
4
import java.util.Date;
5

    
6
import javax.swing.JOptionPane;
7
import javax.swing.table.AbstractTableModel;
8

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

    
16
import com.iver.andami.PluginServices;
17
import com.iver.cit.gvsig.project.documents.table.ProjectTable;
18

    
19
/**
20
 * DOCUMENT ME!
21
 *
22
 * @author Fernando Gonz?lez Cort?s
23
 */
24
public class DataSourceDataModel extends AbstractTableModel {
25
        private static Logger logger = Logger.getLogger(DataSourceDataModel.class.getName());
26
        //private SelectableDataSource dataSource;
27
    ProjectTable pt;
28
    boolean hasAssociatedLayer=false;
29
    private Feature lastRowEdited;
30
        private int lastNumRow=-1;
31
        private String order;
32
        private FeatureCollection featureCollection = null;
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
        try {
44
                        featureCollection=(FeatureCollection)pt.getModel().getDataCollection(pt.getModel().getDefaultFeatureType(),null,order);
45
                } catch (ReadException e) {
46
                        // TODO Auto-generated catch block
47
                        e.printStackTrace();
48
                }
49
        //try {
50
        //dataSource = pt.getModelo().getRecordset();
51
        //} catch (DriverLoadException e) {
52
        // TODO Auto-generated catch block
53
        //        e.printStackTrace();
54
        //}
55
    }
56

    
57
    /**
58
     * Returns the name of the field.
59
     *
60
     * @param col index of field
61
     *
62
     * @return Name of field
63
     */
64
    public String getColumnName(int col) {
65
//            if (col==0)
66
//                    return " ";
67
//            col--;
68
            if (col>=pt.getMapping().length) {
69
                    return null;
70
            }
71
            int i=pt.getMapping()[col];
72
            return pt.getAliases()[i];
73
    }
74

    
75
    /**
76
     * Returns the number of fields.
77
     *
78
     * @return number of fields
79
     */
80
    public int getColumnCount() {
81
        return pt.getMapping().length;
82
    }
83

    
84
    /**
85
     * Returns number of rows.
86
     *
87
     * @return number of rows.
88
     */
89
    public int getRowCount() {
90
        try {
91
                FeatureStore fs = pt.getModel();
92
            return ((FeatureCollection)fs.getDataCollection()).size();
93
        } catch (ReadException e) {
94
            return 0;
95
        }
96
    }
97

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

    
144
                        //CHEMA: Problema con las fechas:
145
                        // Esto deberia solucionarse en el ValueFactory.createValueByType
146
                        if (obj instanceof Date) {
147
                                Date date = (Date) obj;
148

    
149
                                String str = "" + (date.getMonth() +1) + "/" + date.getDate()+ "/" +(date.getYear() +1900);
150
                                obj =  str;
151

    
152
                        }
153

    
154

    
155

    
156
//                        if (hasAssociatedLayer)
157
//                                pt.getModelo().getRecordset().stop();
158
//                        return obj;
159
////TODO                } catch (ReadException e1) {
160
////                        logger.error("Error reading data row= "+row+" column= "+col,e1);
161
////                        obj= new String("");
162
////                }
163
//                if (hasAssociatedLayer)
164
//                        try {
165
//                                ies.getRecordset().stop();
166
//                        } catch (ReadDriverException e1) {
167
//                                logger.error("Error reading data row= "+row+" column= "+col,e1);
168
//                                obj= ValueFactory.createValue("");
169
//                        }
170
                return obj;
171
    }
172

    
173
    /**
174
     * DOCUMENT ME!
175
     *
176
     * @param rowIndex DOCUMENT ME!
177
     * @param columnIndex DOCUMENT ME!
178
     *
179
     * @return DOCUMENT ME!
180
     */
181
    public boolean isCellEditable(int rowIndex, int columnIndex) {
182
//            if (columnIndex==0)
183
//            return false;
184
//            try {
185
//                        if (pt.getModelo().getFieldType(columnIndex)==Types.STRUCT)
186
//                                return true;
187
//                } catch (ReadDriverException e) {
188
//                        e.printStackTrace();
189
//                }
190
            return pt.getModel().isEditing();
191
    }
192

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

    
233
//            Value[] values = row.getAttributes();
234
//            values[columnIndex] = v;
235

    
236
//            IRow newRow = null;
237
//
238
//            if (row.getLinkedRow() instanceof IFeature) {
239
//                IGeometry geometry = ((DefaultFeature) row.getLinkedRow()).getGeometry();
240
//                newRow = new DefaultFeature(geometry, values,row.getID());
241
//            } else {
242
//                newRow = new DefaultRow(values,row.getID());
243
//            }
244

    
245
//            des.modifyRow(numRow, newRow,"Editar valor", EditionEvent.ALPHANUMERIC);
246
        } catch (ReadException e1) {
247
            throw new RuntimeException(e1);
248
        } catch (NumberFormatException e) {
249
                /*        NotificationManager.addError(PluginServices.-getText(this,"numero_incorrecto")+
250
                                        "\n"+PluginServices.-getText(this,"fila")+" = "+rowIndex+
251
                                        "\n"+PluginServices.-getText(this,"columna")+ " = "+columnIndex,e);
252
                */
253
                } catch (DataException e) {
254
                        // TODO Auto-generated catch block
255
                        e.printStackTrace();
256
                }
257
    }
258

    
259
    public void setOrder(String s){
260
            order=s;
261
    }
262

    
263

    
264
}