Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / table / gui / tablemodel / DataSourceDataModel.java @ 10661

History | View | Annotate | Download (6.12 KB)

1 10459 caballero
package com.iver.cit.gvsig.project.documents.table.gui.tablemodel;
2
3
import java.awt.Component;
4
import java.sql.Types;
5
import java.text.ParseException;
6
7
import javax.swing.JOptionPane;
8
import javax.swing.table.AbstractTableModel;
9
10
import com.hardcode.driverManager.DriverLoadException;
11 10626 caballero
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
12 10459 caballero
import com.hardcode.gdbms.engine.values.Value;
13
import com.hardcode.gdbms.engine.values.ValueFactory;
14
import com.iver.andami.PluginServices;
15 10626 caballero
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
16
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileWriteException;
17
import com.iver.cit.gvsig.exceptions.validate.ValidateRowException;
18 10459 caballero
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
19
import com.iver.cit.gvsig.fmap.core.DefaultRow;
20
import com.iver.cit.gvsig.fmap.core.IFeature;
21
import com.iver.cit.gvsig.fmap.core.IGeometry;
22
import com.iver.cit.gvsig.fmap.core.IRow;
23
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
24
import com.iver.cit.gvsig.fmap.edition.IEditableSource;
25
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
26
import com.iver.cit.gvsig.project.documents.table.ProjectTable;
27
28
/**
29
 * DOCUMENT ME!
30
 *
31
 * @author Fernando Gonz?lez Cort?s
32
 */
33
public class DataSourceDataModel extends AbstractTableModel {
34
    //private SelectableDataSource dataSource;
35
    ProjectTable pt;
36
37
    /**
38
     * Crea un nuevo DataSourceDataModel.
39
     *
40
     * @param pt DOCUMENT ME!
41
     */
42
    public DataSourceDataModel(ProjectTable pt) {
43
        this.pt = pt;
44
45
        //try {
46
        //dataSource = pt.getModelo().getRecordset();
47
        //} catch (DriverLoadException e) {
48
        // TODO Auto-generated catch block
49
        //        e.printStackTrace();
50
        //}
51
    }
52
53
    /**
54
     * Returns the name of the field.
55
     *
56
     * @param col index of field
57
     *
58
     * @return Name of field
59
     */
60
    public String getColumnName(int col) {
61
//            if (col==0)
62
//                    return " ";
63
//            col--;
64
            if (col>=pt.getMapping().length) {
65
                    return null;
66
            }
67
            int i=pt.getMapping()[col];
68
            return pt.getAliases()[i];
69
    }
70
71
    /**
72
     * Returns the number of fields.
73
     *
74
     * @return number of fields
75
     */
76
    public int getColumnCount() {
77
        return pt.getMapping().length;
78
    }
79
80
    /**
81
     * Returns number of rows.
82
     *
83
     * @return number of rows.
84
     */
85
    public int getRowCount() {
86
        try {
87
                IEditableSource des = pt.getModelo();
88
            return des.getRowCount();
89 10626 caballero
        } catch (ReadDriverException e) {
90 10459 caballero
            return 0;
91
        }
92
    }
93
94
    /**
95
     * DOCUMENT ME!
96
     *
97
     * @param row DOCUMENT ME!
98
     * @param col DOCUMENT ME!
99
     *
100
     * @return DOCUMENT ME!
101
     */
102
    public Object getValueAt(int row, int col) {
103
104
//       if (col==0){
105
//               return new Integer(row);
106
//       }
107
//       col--;
108
       int numRow;
109
       if (pt.getOrderIndexes() != null && row < pt.getOrderIndexes().length) {
110
               numRow=(int)pt.getOrderIndexes()[row];
111
       }else {
112
               numRow=row;
113
       }
114
            try {
115
                    IEditableSource des = pt.getModelo();
116
                    Object obj =null;
117
                        IRowEdited rowEdited=des.getRow(numRow);
118
                        obj= rowEdited.getAttribute(pt.getMapping()[col]);
119
                                return obj;
120 10626 caballero
                } catch (ReadDriverException e1) {
121 10459 caballero
                        e1.printStackTrace();
122
                        return ValueFactory.createValue("").toString();
123 10626 caballero
                } catch (ExpansionFileReadException e1) {
124 10459 caballero
                        e1.printStackTrace();
125
                        return ValueFactory.createValue("").toString();
126
                }
127
    }
128
129
    /**
130
     * DOCUMENT ME!
131
     *
132
     * @param rowIndex DOCUMENT ME!
133
     * @param columnIndex DOCUMENT ME!
134
     *
135
     * @return DOCUMENT ME!
136
     */
137
    public boolean isCellEditable(int rowIndex, int columnIndex) {
138
//            if (columnIndex==0)
139
//            return false;
140
            try {
141
                        if (pt.getModelo().getRecordset().getFieldType(columnIndex)==Types.STRUCT)
142
                                return true;
143 10626 caballero
                } catch (ReadDriverException e) {
144 10459 caballero
                        e.printStackTrace();
145
                }
146
            return pt.getModelo().isEditing();
147
    }
148
149
    /**
150
     * DOCUMENT ME!
151
     *
152
     * @param aValue DOCUMENT ME!
153
     * @param rowIndex DOCUMENT ME!
154
     * @param columnIndex DOCUMENT ME!
155
     *
156
     * @throws RuntimeException DOCUMENT ME!
157
     */
158
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
159
//             if (columnIndex==0)
160
//             throw new UnsupportedOperationException("Row Number is a read-only column");
161
//             columnIndex--;
162
            Value v;
163
             int numRow;
164
         if (pt.getOrderIndexes() != null) {
165
                 numRow=(int)pt.getOrderIndexes()[rowIndex];
166
         }else {
167
                 numRow=rowIndex;
168
         }
169
            if (getValueAt(rowIndex,columnIndex)==null || getValueAt(rowIndex,columnIndex).toString().equals(aValue))
170
                return;
171
        try {
172
                IEditableSource des = pt.getModelo();
173
                v = ValueFactory.createValueByType(aValue.toString(),
174
                    des.getRecordset().getFieldType(columnIndex));
175
            IRowEdited row = des.getRow(numRow); //.getAttribute(columnIndex);
176
            Value[] values = row.getAttributes();
177
            values[columnIndex] = v;
178
179
            IRow newRow = null;
180
181
            if (row.getLinkedRow() instanceof IFeature) {
182
                IGeometry geometry = ((DefaultFeature) row.getLinkedRow()).getGeometry();
183
                newRow = new DefaultFeature(geometry, values,row.getID());
184
            } else {
185
                newRow = new DefaultRow(values,row.getID());
186
            }
187
188
            des.modifyRow(numRow, newRow,"Editar valor", EditionEvent.ALPHANUMERIC);
189 10626 caballero
        } catch (ReadDriverException e1) {
190 10459 caballero
            throw new RuntimeException(e1);
191
        } catch (ParseException e) {
192
                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),"formato_del_campo_incorrecto");
193
                    return;
194
        } catch (NumberFormatException e) {
195
                /*        NotificationManager.addError(PluginServices.-getText(this,"numero_incorrecto")+
196
                                        "\n"+PluginServices.-getText(this,"fila")+" = "+rowIndex+
197
                                        "\n"+PluginServices.-getText(this,"columna")+ " = "+columnIndex,e);
198
                */
199 10626 caballero
                } catch (ValidateRowException e) {
200
                         throw new RuntimeException(e);
201
                } catch (ExpansionFileWriteException e) {
202
                         throw new RuntimeException(e);
203
                } catch (ExpansionFileReadException e) {
204
                         throw new RuntimeException(e);
205 10459 caballero
                }
206
    }
207
}