svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / table / gui / tablemodel / DataSourceDataModel.java @ 25350
History | View | Annotate | Download (7.37 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 | 14578 | jmvivo | import java.util.Date; |
7 | 10459 | caballero | |
8 | import javax.swing.JOptionPane; |
||
9 | import javax.swing.table.AbstractTableModel; |
||
10 | |||
11 | 16683 | vcaballero | import org.apache.log4j.Logger; |
12 | |||
13 | 10626 | caballero | import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
14 | 14578 | jmvivo | import com.hardcode.gdbms.engine.values.DateValue; |
15 | 10459 | caballero | import com.hardcode.gdbms.engine.values.Value; |
16 | import com.hardcode.gdbms.engine.values.ValueFactory; |
||
17 | import com.iver.andami.PluginServices; |
||
18 | 10626 | caballero | import com.iver.cit.gvsig.exceptions.validate.ValidateRowException; |
19 | 10459 | caballero | import com.iver.cit.gvsig.fmap.core.DefaultFeature; |
20 | import com.iver.cit.gvsig.fmap.core.DefaultRow; |
||
21 | import com.iver.cit.gvsig.fmap.core.IFeature; |
||
22 | import com.iver.cit.gvsig.fmap.core.IGeometry; |
||
23 | import com.iver.cit.gvsig.fmap.core.IRow; |
||
24 | import com.iver.cit.gvsig.fmap.edition.EditionEvent; |
||
25 | import com.iver.cit.gvsig.fmap.edition.IEditableSource; |
||
26 | import com.iver.cit.gvsig.fmap.edition.IRowEdited; |
||
27 | 12771 | caballero | import com.iver.cit.gvsig.fmap.layers.FLayer; |
28 | 10459 | caballero | import com.iver.cit.gvsig.project.documents.table.ProjectTable; |
29 | |||
30 | /**
|
||
31 | * DOCUMENT ME!
|
||
32 | *
|
||
33 | * @author Fernando Gonz?lez Cort?s
|
||
34 | */
|
||
35 | public class DataSourceDataModel extends AbstractTableModel { |
||
36 | 16683 | vcaballero | private static Logger logger = Logger.getLogger(DataSourceDataModel.class.getName()); |
37 | //private SelectableDataSource dataSource;
|
||
38 | 10459 | caballero | ProjectTable pt; |
39 | 12771 | caballero | boolean hasAssociatedLayer=false; |
40 | 16683 | vcaballero | private IRowEdited lastRowEdited;
|
41 | private int lastNumRow=-1; |
||
42 | 10459 | caballero | /**
|
43 | * Crea un nuevo DataSourceDataModel.
|
||
44 | *
|
||
45 | * @param pt DOCUMENT ME!
|
||
46 | */
|
||
47 | public DataSourceDataModel(ProjectTable pt) {
|
||
48 | this.pt = pt;
|
||
49 | 12771 | caballero | if (pt.getAssociatedTable()!=null) { |
50 | hasAssociatedLayer=true;
|
||
51 | } |
||
52 | 10459 | caballero | //try {
|
53 | //dataSource = pt.getModelo().getRecordset();
|
||
54 | //} catch (DriverLoadException e) {
|
||
55 | // TODO Auto-generated catch block
|
||
56 | // e.printStackTrace();
|
||
57 | //}
|
||
58 | } |
||
59 | |||
60 | /**
|
||
61 | * Returns the name of the field.
|
||
62 | *
|
||
63 | * @param col index of field
|
||
64 | *
|
||
65 | * @return Name of field
|
||
66 | */
|
||
67 | public String getColumnName(int col) { |
||
68 | // if (col==0)
|
||
69 | // return " ";
|
||
70 | // col--;
|
||
71 | if (col>=pt.getMapping().length) {
|
||
72 | return null; |
||
73 | } |
||
74 | int i=pt.getMapping()[col];
|
||
75 | return pt.getAliases()[i];
|
||
76 | } |
||
77 | |||
78 | /**
|
||
79 | * Returns the number of fields.
|
||
80 | *
|
||
81 | * @return number of fields
|
||
82 | */
|
||
83 | public int getColumnCount() { |
||
84 | return pt.getMapping().length;
|
||
85 | } |
||
86 | |||
87 | /**
|
||
88 | * Returns number of rows.
|
||
89 | *
|
||
90 | * @return number of rows.
|
||
91 | */
|
||
92 | public int getRowCount() { |
||
93 | try {
|
||
94 | IEditableSource des = pt.getModelo(); |
||
95 | return des.getRowCount();
|
||
96 | 10626 | caballero | } catch (ReadDriverException e) {
|
97 | 10459 | caballero | return 0; |
98 | } |
||
99 | } |
||
100 | |||
101 | /**
|
||
102 | * DOCUMENT ME!
|
||
103 | *
|
||
104 | * @param row DOCUMENT ME!
|
||
105 | * @param col DOCUMENT ME!
|
||
106 | *
|
||
107 | * @return DOCUMENT ME!
|
||
108 | */
|
||
109 | public Object getValueAt(int row, int col) { |
||
110 | 17096 | vcaballero | IEditableSource ies=pt.getModelo(); |
111 | 12771 | caballero | if (hasAssociatedLayer) {
|
112 | Object stoppingEditing =((FLayer)pt.getAssociatedTable()).getProperty("stoppingEditing"); |
||
113 | 10459 | caballero | |
114 | 12771 | caballero | if (stoppingEditing != null && ((Boolean)(stoppingEditing)).booleanValue()) { |
115 | return ValueFactory.createValue(""); |
||
116 | } |
||
117 | } |
||
118 | 10459 | caballero | // if (col==0){
|
119 | // return new Integer(row);
|
||
120 | // }
|
||
121 | // col--;
|
||
122 | int numRow;
|
||
123 | 17096 | vcaballero | long[] orderIndexes=pt.getOrderIndexes(); |
124 | if ( orderIndexes != null && row < orderIndexes.length) { |
||
125 | numRow=(int)orderIndexes[row];
|
||
126 | 10459 | caballero | }else {
|
127 | numRow=row; |
||
128 | } |
||
129 | 16683 | vcaballero | Object obj =null; |
130 | 10459 | caballero | try {
|
131 | 12771 | caballero | if (hasAssociatedLayer)
|
132 | 17096 | vcaballero | ies.getRecordset().start(); |
133 | 16683 | vcaballero | IRowEdited rowEdited=null;
|
134 | if (lastNumRow==numRow){
|
||
135 | rowEdited=lastRowEdited; |
||
136 | }else{
|
||
137 | 17096 | vcaballero | rowEdited=ies.getRow(numRow); |
138 | 16683 | vcaballero | lastRowEdited=rowEdited; |
139 | lastNumRow=numRow; |
||
140 | } |
||
141 | obj= rowEdited.getAttribute(pt.getMapping()[col]); |
||
142 | 14578 | jmvivo | |
143 | //CHEMA: Problema con las fechas:
|
||
144 | // Esto deberia solucionarse en el ValueFactory.createValueByType
|
||
145 | if (obj instanceof DateValue) { |
||
146 | Date date = ((DateValue) obj).getValue();
|
||
147 | |||
148 | String str = "" + (date.getMonth() +1) + "/" + date.getDate()+ "/" +(date.getYear() +1900); |
||
149 | obj = ValueFactory.createValue(str); |
||
150 | |||
151 | } |
||
152 | |||
153 | |||
154 | |||
155 | 16683 | vcaballero | // if (hasAssociatedLayer)
|
156 | // pt.getModelo().getRecordset().stop();
|
||
157 | // return obj;
|
||
158 | 10626 | caballero | } catch (ReadDriverException e1) {
|
159 | 16683 | vcaballero | logger.error("Error reading data row= "+row+" column= "+col,e1); |
160 | obj= ValueFactory.createValue("");
|
||
161 | 14572 | vcaballero | } |
162 | 16683 | vcaballero | if (hasAssociatedLayer)
|
163 | try {
|
||
164 | 17096 | vcaballero | ies.getRecordset().stop(); |
165 | 16683 | vcaballero | } catch (ReadDriverException e1) {
|
166 | logger.error("Error reading data row= "+row+" column= "+col,e1); |
||
167 | obj= ValueFactory.createValue("");
|
||
168 | } |
||
169 | return obj;
|
||
170 | 10459 | caballero | } |
171 | |||
172 | /**
|
||
173 | * DOCUMENT ME!
|
||
174 | *
|
||
175 | * @param rowIndex DOCUMENT ME!
|
||
176 | * @param columnIndex DOCUMENT ME!
|
||
177 | *
|
||
178 | * @return DOCUMENT ME!
|
||
179 | */
|
||
180 | public boolean isCellEditable(int rowIndex, int columnIndex) { |
||
181 | // if (columnIndex==0)
|
||
182 | // return false;
|
||
183 | try {
|
||
184 | if (pt.getModelo().getRecordset().getFieldType(columnIndex)==Types.STRUCT) |
||
185 | return true; |
||
186 | 10626 | caballero | } catch (ReadDriverException e) {
|
187 | 10459 | caballero | e.printStackTrace(); |
188 | } |
||
189 | return pt.getModelo().isEditing();
|
||
190 | } |
||
191 | |||
192 | /**
|
||
193 | * DOCUMENT ME!
|
||
194 | *
|
||
195 | * @param aValue DOCUMENT ME!
|
||
196 | * @param rowIndex DOCUMENT ME!
|
||
197 | * @param columnIndex DOCUMENT ME!
|
||
198 | *
|
||
199 | * @throws RuntimeException DOCUMENT ME!
|
||
200 | */
|
||
201 | public void setValueAt(Object aValue, int rowIndex, int columnIndex) { |
||
202 | // if (columnIndex==0)
|
||
203 | // throw new UnsupportedOperationException("Row Number is a read-only column");
|
||
204 | // columnIndex--;
|
||
205 | Value v; |
||
206 | int numRow;
|
||
207 | if (pt.getOrderIndexes() != null) { |
||
208 | numRow=(int)pt.getOrderIndexes()[rowIndex];
|
||
209 | }else {
|
||
210 | numRow=rowIndex; |
||
211 | } |
||
212 | if (getValueAt(rowIndex,columnIndex)==null || getValueAt(rowIndex,columnIndex).toString().equals(aValue)) |
||
213 | return;
|
||
214 | try {
|
||
215 | IEditableSource des = pt.getModelo(); |
||
216 | 14578 | jmvivo | try{
|
217 | 10459 | caballero | v = ValueFactory.createValueByType(aValue.toString(), |
218 | des.getRecordset().getFieldType(columnIndex)); |
||
219 | 14578 | jmvivo | } catch (ParseException e) { |
220 | if (aValue.equals("")){ |
||
221 | v = ValueFactory.createNullValue(); |
||
222 | } else {
|
||
223 | JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),"formato_del_campo_incorrecto"); |
||
224 | return;
|
||
225 | } |
||
226 | } |
||
227 | 10459 | caballero | IRowEdited row = des.getRow(numRow); //.getAttribute(columnIndex);
|
228 | Value[] values = row.getAttributes();
|
||
229 | values[columnIndex] = v; |
||
230 | |||
231 | IRow newRow = null;
|
||
232 | |||
233 | if (row.getLinkedRow() instanceof IFeature) { |
||
234 | IGeometry geometry = ((DefaultFeature) row.getLinkedRow()).getGeometry(); |
||
235 | newRow = new DefaultFeature(geometry, values,row.getID());
|
||
236 | } else {
|
||
237 | newRow = new DefaultRow(values,row.getID());
|
||
238 | } |
||
239 | |||
240 | des.modifyRow(numRow, newRow,"Editar valor", EditionEvent.ALPHANUMERIC);
|
||
241 | 10626 | caballero | } catch (ReadDriverException e1) {
|
242 | 10459 | caballero | throw new RuntimeException(e1); |
243 | } catch (NumberFormatException e) { |
||
244 | /* NotificationManager.addError(PluginServices.-getText(this,"numero_incorrecto")+
|
||
245 | "\n"+PluginServices.-getText(this,"fila")+" = "+rowIndex+
|
||
246 | "\n"+PluginServices.-getText(this,"columna")+ " = "+columnIndex,e);
|
||
247 | */
|
||
248 | 10626 | caballero | } catch (ValidateRowException e) {
|
249 | throw new RuntimeException(e); |
||
250 | 14572 | vcaballero | } |
251 | 10459 | caballero | } |
252 | } |