root / trunk / extensions / extCAD / src / com / iver / cit / gvsig / project / documents / table / gui / EvalExpresion.java @ 12739
History | View | Annotate | Download (8.2 KB)
1 | 10254 | caballero | package com.iver.cit.gvsig.project.documents.table.gui; |
---|---|---|---|
2 | |||
3 | import java.sql.Types; |
||
4 | import java.util.BitSet; |
||
5 | import java.util.Date; |
||
6 | import java.util.prefs.Preferences; |
||
7 | |||
8 | import com.hardcode.driverManager.DriverLoadException; |
||
9 | 10626 | caballero | import com.hardcode.gdbms.driver.exceptions.InitializeWriterException; |
10 | import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
||
11 | 10254 | caballero | import com.hardcode.gdbms.engine.values.Value; |
12 | import com.hardcode.gdbms.engine.values.ValueFactory; |
||
13 | import com.iver.andami.PluginServices; |
||
14 | 12739 | caballero | import com.iver.andami.messages.NotificationManager; |
15 | 10254 | caballero | import com.iver.cit.gvsig.EditionUtilities; |
16 | 10626 | caballero | import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException; |
17 | import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileWriteException; |
||
18 | import com.iver.cit.gvsig.exceptions.layers.StopEditionLayerException; |
||
19 | import com.iver.cit.gvsig.exceptions.validate.ValidateRowException; |
||
20 | import com.iver.cit.gvsig.exceptions.visitors.StopWriterVisitorException; |
||
21 | 10283 | caballero | import com.iver.cit.gvsig.fmap.core.DefaultFeature; |
22 | import com.iver.cit.gvsig.fmap.core.IGeometry; |
||
23 | 10254 | caballero | import com.iver.cit.gvsig.fmap.core.IRow; |
24 | import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
||
25 | import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition; |
||
26 | import com.iver.cit.gvsig.fmap.drivers.ITableDefinition; |
||
27 | import com.iver.cit.gvsig.fmap.edition.EditionEvent; |
||
28 | import com.iver.cit.gvsig.fmap.edition.IEditableSource; |
||
29 | import com.iver.cit.gvsig.fmap.edition.IRowEdited; |
||
30 | import com.iver.cit.gvsig.fmap.edition.ISpatialWriter; |
||
31 | import com.iver.cit.gvsig.fmap.edition.IWriteable; |
||
32 | import com.iver.cit.gvsig.fmap.edition.IWriter; |
||
33 | import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter; |
||
34 | import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
||
35 | /**
|
||
36 | * @author Vicente Caballero Navarro
|
||
37 | */
|
||
38 | public class EvalExpresion { |
||
39 | private FieldDescription[] fieldDescriptors; |
||
40 | private int selectedIndex; |
||
41 | private FieldDescription fieldDescriptor;
|
||
42 | private FLyrVect lv;
|
||
43 | private IEditableSource ies =null; |
||
44 | private Table table=null; |
||
45 | private static Preferences prefs = Preferences.userRoot().node( "fieldExpresionOptions" ); |
||
46 | private int limit; |
||
47 | public EvalExpresion() {
|
||
48 | limit=prefs.getInt("limit_rows_in_memory",-1); |
||
49 | } |
||
50 | public void setTable(Table table) { |
||
51 | BitSet columnSelected = table.getSelectedFieldIndices();
|
||
52 | fieldDescriptors = table.getModel().getModelo().getFieldsDescription(); |
||
53 | selectedIndex = columnSelected.nextSetBit(0);
|
||
54 | fieldDescriptor = fieldDescriptors[selectedIndex]; |
||
55 | this.table=table;
|
||
56 | lv=(FLyrVect)table.getModel().getAssociatedTable(); |
||
57 | if (lv ==null) |
||
58 | ies=table.getModel().getModelo(); |
||
59 | else
|
||
60 | ies = (VectorialEditableAdapter) lv.getSource(); |
||
61 | } |
||
62 | public void setValue(Object obj,int i) { |
||
63 | //VectorialEditableAdapter vea = (VectorialEditableAdapter) lv.getSource();
|
||
64 | Value value = getValue(obj); |
||
65 | try {
|
||
66 | 10283 | caballero | IRowEdited rowEdited=ies.getRow(i); |
67 | Value[] values = rowEdited.getAttributes();
|
||
68 | values[selectedIndex] = value; |
||
69 | IRow newRow = null;
|
||
70 | IGeometry geometry = ((DefaultFeature) rowEdited.getLinkedRow()).getGeometry(); |
||
71 | newRow = new DefaultFeature(geometry, values,rowEdited.getID());
|
||
72 | ies.modifyRow(rowEdited.getIndex(), newRow,"", EditionEvent.ALPHANUMERIC);
|
||
73 | 10626 | caballero | |
74 | 10283 | caballero | // IRow feat=null;
|
75 | // try {
|
||
76 | // feat = ies.getRow(i).getLinkedRow().cloneRow();
|
||
77 | // } catch (DriverIOException e1) {
|
||
78 | // e1.printStackTrace();
|
||
79 | // } catch (IOException e1) {
|
||
80 | // e1.printStackTrace();
|
||
81 | // }
|
||
82 | // Value[] values = feat.getAttributes();
|
||
83 | // values[selectedIndex] = value;
|
||
84 | // feat.setAttributes(values);
|
||
85 | //
|
||
86 | // IRowEdited edRow = new DefaultRowEdited(feat,
|
||
87 | // IRowEdited.STATUS_MODIFIED, i);
|
||
88 | //
|
||
89 | // try {
|
||
90 | //
|
||
91 | // ies.modifyRow(edRow.getIndex(), edRow.getLinkedRow(), "",
|
||
92 | // EditionEvent.ALPHANUMERIC);
|
||
93 | // } catch (IOException e) {
|
||
94 | // e.printStackTrace();
|
||
95 | // } catch (DriverIOException e) {
|
||
96 | // e.printStackTrace();
|
||
97 | 10626 | caballero | } catch (ReadDriverException e) {
|
98 | 12739 | caballero | NotificationManager.addError(e.getMessage(),e); |
99 | 10626 | caballero | } catch (ExpansionFileReadException e) {
|
100 | 12739 | caballero | NotificationManager.addError(e.getMessage(),e); |
101 | 10626 | caballero | } catch (ValidateRowException e) {
|
102 | 12739 | caballero | NotificationManager.addError(e.getMessage(),e); |
103 | 10626 | caballero | } catch (ExpansionFileWriteException e) {
|
104 | 12739 | caballero | NotificationManager.addError(e.getMessage(),e); |
105 | 10626 | caballero | } |
106 | 10254 | caballero | |
107 | } |
||
108 | /**
|
||
109 | * Returns the value created from object.
|
||
110 | *
|
||
111 | * @param obj value.
|
||
112 | *
|
||
113 | * @return Value.
|
||
114 | */
|
||
115 | private Value getValue(Object obj) { |
||
116 | int typeField = fieldDescriptor.getFieldType();
|
||
117 | Value value = ValueFactory.createNullValue(); |
||
118 | |||
119 | 10283 | caballero | if (obj instanceof Double || obj instanceof Float || obj instanceof Integer) { |
120 | 10254 | caballero | if (typeField == Types.DOUBLE) { |
121 | 10283 | caballero | double dv = ((Number) obj).doubleValue(); |
122 | 10254 | caballero | value = ValueFactory.createValue(dv); |
123 | } else if (typeField == Types.FLOAT) { |
||
124 | 10283 | caballero | float df = ((Number) obj).floatValue(); |
125 | 10254 | caballero | value = ValueFactory.createValue(df); |
126 | } else if (typeField == Types.INTEGER) { |
||
127 | 10283 | caballero | int di = ((Number) obj).intValue(); |
128 | 10254 | caballero | value = ValueFactory.createValue(di); |
129 | } else if (typeField == Types.VARCHAR) { |
||
130 | 10283 | caballero | String s = ((Number) obj).toString(); |
131 | 10254 | caballero | value = ValueFactory.createValue(s); |
132 | } |
||
133 | } else if (obj instanceof Date) { |
||
134 | if (typeField == Types.DATE) { |
||
135 | Date date = (Date) obj; |
||
136 | value = ValueFactory.createValue(date); |
||
137 | } else if (typeField == Types.VARCHAR) { |
||
138 | String s = ((Date) obj).toString(); |
||
139 | value = ValueFactory.createValue(s); |
||
140 | } |
||
141 | } else if (obj instanceof Boolean) { |
||
142 | if (typeField == Types.BOOLEAN) { |
||
143 | boolean b = ((Boolean) obj).booleanValue(); |
||
144 | value = ValueFactory.createValue(b); |
||
145 | } else if (typeField == Types.VARCHAR) { |
||
146 | String s = ((Boolean) obj).toString(); |
||
147 | value = ValueFactory.createValue(s); |
||
148 | } |
||
149 | } else if (obj instanceof String) { |
||
150 | if (typeField == Types.VARCHAR) { |
||
151 | String s = obj.toString();
|
||
152 | value = ValueFactory.createValue(s); |
||
153 | } |
||
154 | } |
||
155 | |||
156 | return value;
|
||
157 | } |
||
158 | public FieldDescription getFieldDescriptorSelected() {
|
||
159 | return fieldDescriptor;
|
||
160 | } |
||
161 | public FieldDescription[] getFieldDescriptors() { |
||
162 | return fieldDescriptors;
|
||
163 | } |
||
164 | 10283 | caballero | // public void setFieldValue(Object obj,int i) {
|
165 | // try {
|
||
166 | // ((DBFDriver)table.getModel().getModelo().getOriginalDriver()).setFieldValue(i,selectedIndex,obj);
|
||
167 | // } catch (DriverLoadException e) {
|
||
168 | // e.printStackTrace();
|
||
169 | // } catch (IOException e) {
|
||
170 | // e.printStackTrace();
|
||
171 | // }
|
||
172 | // }
|
||
173 | 10626 | caballero | public void saveEdits(int numRows) throws DriverLoadException, ReadDriverException, InitializeWriterException, StopWriterVisitorException { |
174 | 10283 | caballero | if (limit==-1 || numRows == 0 || (numRows % limit)!=0) { |
175 | 10254 | caballero | return;
|
176 | } |
||
177 | ies.endComplexRow(PluginServices.getText(this, "expresion")); |
||
178 | if ((lv != null) && |
||
179 | lv.getSource() instanceof VectorialEditableAdapter) {
|
||
180 | VectorialEditableAdapter vea = (VectorialEditableAdapter) lv.getSource(); |
||
181 | ISpatialWriter spatialWriter = (ISpatialWriter) vea.getDriver(); |
||
182 | vea.cleanSelectableDatasource(); |
||
183 | lv.setRecordset(vea.getRecordset()); // Queremos que el recordset del layer
|
||
184 | // refleje los cambios en los campos.
|
||
185 | ILayerDefinition lyrDef = EditionUtilities.createLayerDefinition(lv); |
||
186 | spatialWriter.initialize(lyrDef); |
||
187 | vea.saveEdits(spatialWriter,EditionEvent.ALPHANUMERIC); |
||
188 | vea.getCommandRecord().clearAll(); |
||
189 | } else {
|
||
190 | 10283 | caballero | if (ies instanceof IWriteable){ |
191 | 10254 | caballero | IWriteable w = (IWriteable) ies; |
192 | IWriter writer = w.getWriter(); |
||
193 | if (writer == null){ |
||
194 | }else{
|
||
195 | ITableDefinition tableDef = ies.getTableDefinition(); |
||
196 | writer.initialize(tableDef); |
||
197 | |||
198 | ies.saveEdits(writer,EditionEvent.ALPHANUMERIC); |
||
199 | ies.getSelection().clear(); |
||
200 | } |
||
201 | } |
||
202 | ies.getCommandRecord().clearAll(); |
||
203 | } |
||
204 | ies.startComplexRow(); |
||
205 | table.refresh(); |
||
206 | } |
||
207 | } |