svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extExpressionField / src / com / iver / cit / gvsig / project / documents / table / gui / EvalExpression.java @ 27752
History | View | Annotate | Download (8.18 KB)
1 | 27752 | vcaballero | package com.iver.cit.gvsig.project.documents.table.gui; |
---|---|---|---|
2 | |||
3 | import java.util.Date; |
||
4 | import java.util.prefs.Preferences; |
||
5 | |||
6 | import org.apache.bsf.BSFException; |
||
7 | import org.gvsig.fmap.dal.exception.DataException; |
||
8 | import org.gvsig.fmap.dal.feature.EditableFeature; |
||
9 | import org.gvsig.fmap.dal.feature.Feature; |
||
10 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
11 | import org.gvsig.fmap.dal.feature.FeatureSet; |
||
12 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
13 | import org.gvsig.project.document.table.gui.FeatureTableDocumentPanel; |
||
14 | |||
15 | /**
|
||
16 | * @author Vicente Caballero Navarro
|
||
17 | */
|
||
18 | public class EvalExpression { |
||
19 | private FeatureType featureType;
|
||
20 | private FeatureAttributeDescriptor selectedDescriptor;
|
||
21 | // private FeatureAttributeDescriptor fieldDescriptor;
|
||
22 | // private FLyrVect lv;
|
||
23 | // private FeatureStore ies =null;
|
||
24 | private FeatureTableDocumentPanel table=null; |
||
25 | private static Preferences prefs = Preferences.userRoot().node( "fieldExpressionOptions" ); |
||
26 | private int limit; |
||
27 | public EvalExpression() {
|
||
28 | limit=prefs.getInt("limit_rows_in_memory",-1); |
||
29 | } |
||
30 | public void setTable(FeatureTableDocumentPanel table) { |
||
31 | try {
|
||
32 | selectedDescriptor = table.getTablePanel().getTable().getSelectedColumnsAttributeDescriptor()[0];
|
||
33 | featureType = table.getModel().getStore().getDefaultFeatureType(); |
||
34 | } catch (DataException e) {
|
||
35 | // TODO Auto-generated catch block
|
||
36 | e.printStackTrace(); |
||
37 | } |
||
38 | // selectedIndex = columnSelected.nextSetBit(0);
|
||
39 | // fieldDescriptor = (FeatureAttributeDescriptor)featureType.get(selectedIndex);
|
||
40 | this.table=table;
|
||
41 | // lv=(FLyrVect)table.getModel().getAssociatedLayer();
|
||
42 | // if (lv ==null)
|
||
43 | // ies=table.getModel().getStore();
|
||
44 | // else
|
||
45 | // ies = (VectorialEditableAdapter) lv.getSource();
|
||
46 | } |
||
47 | |||
48 | public void setValue(FeatureSet featureSet,Feature feature,Object obj) { |
||
49 | EditableFeature eFeature=feature.getEditable(); |
||
50 | eFeature.set(selectedDescriptor.getIndex(), obj); |
||
51 | try {
|
||
52 | featureSet.update(eFeature); |
||
53 | } catch (DataException e) {
|
||
54 | // TODO Auto-generated catch block
|
||
55 | e.printStackTrace(); |
||
56 | } |
||
57 | } |
||
58 | |||
59 | // public void setValue(Object obj,int i) {
|
||
60 | // //VectorialEditableAdapter vea = (VectorialEditableAdapter) lv.getSource();
|
||
61 | // Value value = getValue(obj);
|
||
62 | // //System.out.println("num = "+i);
|
||
63 | // IRow feat=null;
|
||
64 | // try {
|
||
65 | // feat = ies.getRow(i).getLinkedRow().cloneRow();
|
||
66 | // } catch (ExpansionFileReadException e) {
|
||
67 | // NotificationManager.addError(e);
|
||
68 | // } catch (ReadDriverException e) {
|
||
69 | // NotificationManager.addError(e);
|
||
70 | // }
|
||
71 | // Value[] values = feat.getAttributes();
|
||
72 | // values[selectedIndex] = value;
|
||
73 | // feat.setAttributes(values);
|
||
74 | //
|
||
75 | // IRowEdited edRow = new DefaultRowEdited(feat,
|
||
76 | // IRowEdited.STATUS_MODIFIED, i);
|
||
77 | // try {
|
||
78 | // ies.modifyRow(edRow.getIndex(), edRow.getLinkedRow(), "",
|
||
79 | // EditionEvent.ALPHANUMERIC);
|
||
80 | // } catch (ExpansionFileWriteException e) {
|
||
81 | // NotificationManager.addError(e);
|
||
82 | // } catch (ExpansionFileReadException e) {
|
||
83 | // NotificationManager.addError(e);
|
||
84 | // } catch (ValidateRowException e) {
|
||
85 | // NotificationManager.addError(e);
|
||
86 | // } catch (ReadDriverException e) {
|
||
87 | // NotificationManager.addError(e);
|
||
88 | // }
|
||
89 | //
|
||
90 | // }
|
||
91 | |||
92 | public void isCorrectValue(Object obj) throws BSFException { |
||
93 | if (obj instanceof Number || obj instanceof Date || obj instanceof Boolean || obj instanceof String || obj == null ) { |
||
94 | |||
95 | }else{
|
||
96 | throw new BSFException("incorrect"); |
||
97 | } |
||
98 | } |
||
99 | |||
100 | |||
101 | /**
|
||
102 | * Returns the value created from object.
|
||
103 | *
|
||
104 | * @param obj value.
|
||
105 | *
|
||
106 | * @return Value.
|
||
107 | */
|
||
108 | // private Value getValue(Object obj) {
|
||
109 | // int typeField = fieldDescriptor.getFieldType();
|
||
110 | // Value value = null;//ValueFactory.createNullValue();
|
||
111 | //
|
||
112 | // if (obj instanceof Number) {
|
||
113 | // if (typeField == Types.DOUBLE || typeField == Types.NUMERIC) {
|
||
114 | // double dv = ((Number) obj).doubleValue();
|
||
115 | // value = ValueFactory.createValue(dv);
|
||
116 | // } else if (typeField == Types.FLOAT) {
|
||
117 | // float df = ((Number) obj).floatValue();
|
||
118 | // value = ValueFactory.createValue(df);
|
||
119 | // } else if (typeField == Types.INTEGER) {
|
||
120 | // int di = ((Number) obj).intValue();
|
||
121 | // value = ValueFactory.createValue(di);
|
||
122 | // } else if (typeField == Types.BIGINT) {
|
||
123 | // long di = ((Number) obj).longValue();
|
||
124 | // value = ValueFactory.createValue(di);
|
||
125 | // } else if (typeField == Types.VARCHAR) {
|
||
126 | // String s = ((Number) obj).toString();
|
||
127 | // value = ValueFactory.createValue(s);
|
||
128 | // } else if (typeField == Types.BOOLEAN) {
|
||
129 | // if (((Number) obj).intValue()==0){
|
||
130 | // value=ValueFactory.createValue(false);
|
||
131 | // }else{
|
||
132 | // value=ValueFactory.createValue(true);
|
||
133 | // }
|
||
134 | // }
|
||
135 | // } else if (obj instanceof Date) {
|
||
136 | // if (typeField == Types.DATE) {
|
||
137 | // Date date = (Date) obj;
|
||
138 | // value = ValueFactory.createValue(date);
|
||
139 | // } else if (typeField == Types.VARCHAR) {
|
||
140 | // String s = ((Date) obj).toString();
|
||
141 | // value = ValueFactory.createValue(s);
|
||
142 | // }
|
||
143 | // } else if (obj instanceof Boolean) {
|
||
144 | // if (typeField == Types.BOOLEAN) {
|
||
145 | // boolean b = ((Boolean) obj).booleanValue();
|
||
146 | // value = ValueFactory.createValue(b);
|
||
147 | // } else if (typeField == Types.VARCHAR) {
|
||
148 | // String s = ((Boolean) obj).toString();
|
||
149 | // value = ValueFactory.createValue(s);
|
||
150 | // }
|
||
151 | // } else if (obj instanceof String) {
|
||
152 | // if (typeField == Types.VARCHAR) {
|
||
153 | // String s = obj.toString();
|
||
154 | // value = ValueFactory.createValue(s);
|
||
155 | // }
|
||
156 | // }else{
|
||
157 | // value=ValueFactory.createNullValue();
|
||
158 | // }
|
||
159 | //
|
||
160 | // return value;
|
||
161 | // }
|
||
162 | public FeatureAttributeDescriptor getFieldDescriptorSelected() {
|
||
163 | return selectedDescriptor;
|
||
164 | } |
||
165 | public FeatureType getFieldDescriptors() {
|
||
166 | return featureType;
|
||
167 | } |
||
168 | // public void setFieldValue(Object obj,int i) {
|
||
169 | // try {
|
||
170 | // ((DBFDriver)table.getModel().getModelo().getOriginalDriver()).setFieldValue(i,selectedIndex,obj);
|
||
171 | // } catch (DriverLoadException e) {
|
||
172 | // e.printStackTrace();
|
||
173 | // } catch (IOException e) {
|
||
174 | // e.printStackTrace();
|
||
175 | // }
|
||
176 | // }
|
||
177 | public void saveEdits() throws DataException { |
||
178 | // if (limit==-1 || numRows == 0 || (numRows % limit)!=0) {
|
||
179 | // return;
|
||
180 | // }
|
||
181 | table.getModel().getStore().finishEditing(); |
||
182 | |||
183 | |||
184 | // ies.endComplexRow(PluginServices.getText(this, "expression"));
|
||
185 | // if ((lv != null) &&
|
||
186 | // lv.getSource() instanceof VectorialEditableAdapter) {
|
||
187 | // VectorialEditableAdapter vea = (VectorialEditableAdapter) lv.getSource();
|
||
188 | // ISpatialWriter spatialWriter = (ISpatialWriter) vea.getDriver();
|
||
189 | // vea.cleanSelectableDatasource();
|
||
190 | // lv.setRecordset(vea.getRecordset()); // Queremos que el recordset del layer
|
||
191 | // // refleje los cambios en los campos.
|
||
192 | // ILayerDefinition lyrDef = EditionUtilities.createLayerDefinition(lv);
|
||
193 | // spatialWriter.initialize(lyrDef);
|
||
194 | // vea.saveEdits(spatialWriter,EditionEvent.ALPHANUMERIC);
|
||
195 | // vea.getCommandRecord().clearAll();
|
||
196 | // } else {
|
||
197 | // if (ies instanceof IWriteable){
|
||
198 | // IWriteable w = (IWriteable) ies;
|
||
199 | // IWriter writer = w.getWriter();
|
||
200 | // if (writer == null){
|
||
201 | // }else{
|
||
202 | // ITableDefinition tableDef = ies.getTableDefinition();
|
||
203 | // writer.initialize(tableDef);
|
||
204 | //
|
||
205 | // ies.saveEdits(writer,EditionEvent.ALPHANUMERIC);
|
||
206 | // ies.getSelection().clear();
|
||
207 | // }
|
||
208 | // }
|
||
209 | // ies.getCommandRecord().clearAll();
|
||
210 | // }
|
||
211 | // ies.startComplexRow();
|
||
212 | // table.refresh();
|
||
213 | } |
||
214 | } |