Revision 36458 trunk/extensions/extExpressionField/src/com/iver/cit/gvsig/project/documents/table/gui/EvalExpression.java
EvalExpression.java | ||
---|---|---|
1 | 1 |
package com.iver.cit.gvsig.project.documents.table.gui; |
2 | 2 |
|
3 |
import java.awt.Component; |
|
4 |
import java.io.UnsupportedEncodingException; |
|
3 | 5 |
import java.sql.Types; |
6 |
import java.util.ArrayList; |
|
4 | 7 |
import java.util.BitSet; |
5 | 8 |
import java.util.Date; |
6 | 9 |
import java.util.prefs.Preferences; |
7 | 10 |
|
11 |
import javax.swing.JOptionPane; |
|
12 |
|
|
8 | 13 |
import org.apache.bsf.BSFException; |
14 |
import org.apache.bsf.BSFManager; |
|
9 | 15 |
|
10 | 16 |
import com.hardcode.gdbms.driver.exceptions.InitializeWriterException; |
11 | 17 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
... | ... | |
14 | 20 |
import com.iver.andami.PluginServices; |
15 | 21 |
import com.iver.andami.messages.NotificationManager; |
16 | 22 |
import com.iver.cit.gvsig.EditionUtilities; |
23 |
import com.iver.cit.gvsig.ExpressionFieldExtension; |
|
24 |
import com.iver.cit.gvsig.ProjectExtension; |
|
17 | 25 |
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException; |
18 | 26 |
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileWriteException; |
19 | 27 |
import com.iver.cit.gvsig.exceptions.validate.ValidateRowException; |
... | ... | |
23 | 31 |
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition; |
24 | 32 |
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition; |
25 | 33 |
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited; |
34 |
import com.iver.cit.gvsig.fmap.edition.EditableAdapter; |
|
26 | 35 |
import com.iver.cit.gvsig.fmap.edition.EditionEvent; |
27 | 36 |
import com.iver.cit.gvsig.fmap.edition.IEditableSource; |
28 | 37 |
import com.iver.cit.gvsig.fmap.edition.IRowEdited; |
... | ... | |
30 | 39 |
import com.iver.cit.gvsig.fmap.edition.IWriteable; |
31 | 40 |
import com.iver.cit.gvsig.fmap.edition.IWriter; |
32 | 41 |
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter; |
42 |
import com.iver.cit.gvsig.fmap.layers.FBitSet; |
|
33 | 43 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
44 |
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial; |
|
45 |
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
|
46 |
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData; |
|
47 |
import com.iver.cit.gvsig.project.ProjectFactory; |
|
48 |
import com.iver.cit.gvsig.project.documents.table.IOperator; |
|
49 |
import com.iver.cit.gvsig.project.documents.table.Index; |
|
50 |
import com.iver.cit.gvsig.project.documents.table.ProjectTable; |
|
51 |
import com.iver.cit.gvsig.project.documents.table.ProjectTableFactory; |
|
52 |
|
|
34 | 53 |
/** |
35 | 54 |
* This class implements the logic of a expression and fill a field of the table |
36 | 55 |
* |
... | ... | |
38 | 57 |
*/ |
39 | 58 |
public class EvalExpression { |
40 | 59 |
private FieldDescription[] fieldDescriptors; |
41 |
private int selectedIndex; |
|
42 | 60 |
private FieldDescription fieldDescriptor; |
43 |
private FLyrVect lv;
|
|
61 |
private FLyrVect layer;
|
|
44 | 62 |
private IEditableSource ies =null; |
45 |
private Table table=null; |
|
46 | 63 |
private static Preferences prefs = Preferences.userRoot().node( "fieldExpressionOptions" ); |
47 | 64 |
private int limit; |
65 |
private SelectableDataSource sds; |
|
66 |
private BSFManager interpreter; |
|
67 |
private Index indexRow; |
|
68 |
private int selectedIndex; |
|
69 |
private Table table; |
|
70 |
private ArrayList<IOperator> operators = new ArrayList<IOperator>(); |
|
71 |
|
|
72 |
/** |
|
73 |
* @deprecated |
|
74 |
*/ |
|
48 | 75 |
public EvalExpression() { |
76 |
limit=prefs.getInt("limit_rows_in_memory",-1); |
|
77 |
} |
|
78 |
|
|
79 |
|
|
80 |
public EvalExpression(BSFManager interpreter, ArrayList<IOperator> operators) { |
|
49 | 81 |
limit=prefs.getInt("limit_rows_in_memory",-1); |
82 |
//fpuga: I think interpreter should be instantiated here and not passed |
|
83 |
this.interpreter = interpreter; |
|
84 |
this.operators = operators; |
|
50 | 85 |
} |
51 |
public void setTable(Table table) { |
|
86 |
|
|
87 |
public void setLayer(FLyrVect layer, int selectedIndex) { |
|
88 |
this.layer = layer; |
|
89 |
ies = (VectorialEditableAdapter) layer.getSource(); |
|
90 |
this.selectedIndex = selectedIndex; |
|
91 |
init(); |
|
92 |
|
|
93 |
} |
|
94 |
|
|
95 |
public void setTable(Table table) { |
|
96 |
// TODO: table is only needed to make table.refresh on the dialog. |
|
97 |
// Probably a fireevent can be done to avoid this |
|
98 |
this.table = table; |
|
99 |
layer = (FLyrVect) table.getModel().getAssociatedTable(); |
|
100 |
if (layer == null) |
|
101 |
ies=table.getModel().getModelo(); |
|
102 |
else |
|
103 |
ies = (VectorialEditableAdapter) layer.getSource(); |
|
52 | 104 |
BitSet columnSelected = table.getSelectedFieldIndices(); |
53 |
fieldDescriptors = table.getModel().getModelo().getFieldsDescription(); |
|
54 |
selectedIndex = columnSelected.nextSetBit(0); |
|
55 |
fieldDescriptor = fieldDescriptors[selectedIndex]; |
|
56 |
this.table=table; |
|
57 |
lv=(FLyrVect)table.getModel().getAssociatedTable(); |
|
58 |
if (lv ==null) |
|
59 |
ies=table.getModel().getModelo(); |
|
60 |
else |
|
61 |
ies = (VectorialEditableAdapter) lv.getSource(); |
|
105 |
selectedIndex = columnSelected.nextSetBit(0); |
|
106 |
init(); |
|
107 |
} |
|
108 |
|
|
109 |
private void init() { |
|
110 |
try { |
|
111 |
sds = ies.getRecordset(); |
|
112 |
fieldDescriptors = sds.getFieldsDescription(); |
|
113 |
fieldDescriptor = fieldDescriptors[selectedIndex]; |
|
114 |
interpreter.declareBean("sds", sds,SelectableDataSource.class); |
|
115 |
indexRow=new Index(); |
|
116 |
interpreter.declareBean("indexRow", indexRow,Index.class); |
|
117 |
} catch (BSFException e) { |
|
118 |
e.printStackTrace(); |
|
119 |
} catch (ReadDriverException e) { |
|
120 |
e.printStackTrace(); |
|
121 |
} |
|
62 | 122 |
} |
63 | 123 |
public void setValue(Object obj,int i) { |
64 | 124 |
//VectorialEditableAdapter vea = (VectorialEditableAdapter) lv.getSource(); |
... | ... | |
182 | 242 |
return; |
183 | 243 |
} |
184 | 244 |
ies.endComplexRow(PluginServices.getText(this, "expression")); |
185 |
if ((lv != null) && |
|
186 |
lv.getSource() instanceof VectorialEditableAdapter) { |
|
187 |
VectorialEditableAdapter vea = (VectorialEditableAdapter) lv.getSource(); |
|
245 |
if ((layer != null) |
|
246 |
&& layer.getSource() instanceof VectorialEditableAdapter) { |
|
247 |
VectorialEditableAdapter vea = (VectorialEditableAdapter) layer |
|
248 |
.getSource(); |
|
188 | 249 |
ISpatialWriter spatialWriter = (ISpatialWriter) vea.getDriver(); |
189 | 250 |
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); |
|
251 |
// We want that the recordset of the layer shows the changes of the fields |
|
252 |
layer.setRecordset(vea.getRecordset()); |
|
253 |
ILayerDefinition lyrDef = EditionUtilities |
|
254 |
.createLayerDefinition(layer); |
|
193 | 255 |
spatialWriter.initialize(lyrDef); |
194 | 256 |
vea.saveEdits(spatialWriter,EditionEvent.ALPHANUMERIC); |
195 | 257 |
vea.getCommandRecord().clearAll(); |
... | ... | |
209 | 271 |
ies.getCommandRecord().clearAll(); |
210 | 272 |
} |
211 | 273 |
ies.startComplexRow(); |
212 |
table.refresh(); |
|
213 | 274 |
} |
275 |
|
|
276 |
|
|
277 |
|
|
278 |
/** |
|
279 |
* Evaluate the expression. |
|
280 |
* @throws ReadDriverException |
|
281 |
* @throws BSFException |
|
282 |
*/ |
|
283 |
public boolean evalExpression(String expression) throws ReadDriverException, BSFException{ |
|
284 |
long rowCount = sds.getRowCount(); |
|
285 |
byte[] expressionBytes; |
|
286 |
String encoding = System.getProperty("file.encoding"); |
|
287 |
try { |
|
288 |
expressionBytes = expression.getBytes(encoding); |
|
289 |
expression = new String(expressionBytes, "ISO-8859-1"); |
|
290 |
} catch (UnsupportedEncodingException e) { |
|
291 |
e.printStackTrace(); |
|
292 |
} |
|
293 |
expression=expression.replaceAll("\\[","field(\"").replaceAll("\\]","\")"); |
|
294 |
|
|
295 |
interpreter.declareBean("ee",this,EvalExpression.class); |
|
296 |
interpreter.exec(ExpressionFieldExtension.JYTHON,null,-1,-1,"def expression():\n" + |
|
297 |
" return " +expression+ ""); |
|
298 |
if (rowCount > 0) { |
|
299 |
try { |
|
300 |
interpreter.exec(ExpressionFieldExtension.JYTHON,null,-1,-1,"def isCorrect():\n" + |
|
301 |
" ee.isCorrectValue(expression())\n"); |
|
302 |
interpreter.exec(ExpressionFieldExtension.JYTHON,null,-1,-1,"isCorrect()"); |
|
303 |
} catch (BSFException ee) { |
|
304 |
String message=ee.getMessage(); |
|
305 |
if (message.length()>200){ |
|
306 |
message=message.substring(0,200); |
|
307 |
} |
|
308 |
int option=JOptionPane.showConfirmDialog((Component) PluginServices.getMainFrame(), |
|
309 |
PluginServices.getText(this, |
|
310 |
"error_expression")+"\n"+message+"\n"+PluginServices.getText(this,"continue?")); |
|
311 |
if (option!=JOptionPane.OK_OPTION) { |
|
312 |
return false; |
|
313 |
} |
|
314 |
} |
|
315 |
} |
|
316 |
ies.startComplexRow(); |
|
317 |
|
|
318 |
ArrayList exceptions=new ArrayList(); |
|
319 |
interpreter.declareBean("exceptions",exceptions,ArrayList.class); |
|
320 |
FBitSet selection=sds.getSelection(); |
|
321 |
if (selection.cardinality() > 0) { |
|
322 |
interpreter.declareBean("selection", selection, FBitSet.class); |
|
323 |
interpreter.exec(ExpressionFieldExtension.JYTHON,null,-1,-1,"def p():\n" + |
|
324 |
" i=selection.nextSetBit(0)\n" + |
|
325 |
" while i >=0:\n" + |
|
326 |
" indexRow.set(i)\n" + |
|
327 |
" obj=expression()\n" + |
|
328 |
" ee.setValue(obj,i)\n" + |
|
329 |
" ee.saveEdits(i)\n" + |
|
330 |
" i=selection.nextSetBit(i+1)\n"); |
|
331 |
} else { |
|
332 |
interpreter.exec(ExpressionFieldExtension.JYTHON,null,-1,-1,"def p():\n" + |
|
333 |
" for i in xrange("+rowCount +"):\n" + |
|
334 |
" indexRow.set(i)\n" + |
|
335 |
// " print i , expression() , repr (expression())\n" + |
|
336 |
" ee.setValue(expression(),i)\n" + |
|
337 |
" ee.saveEdits(i)\n"); |
|
338 |
} |
|
339 |
try { |
|
340 |
interpreter.eval(ExpressionFieldExtension.JYTHON,null,-1,-1,"p()"); |
|
341 |
} catch (BSFException ee) { |
|
342 |
|
|
343 |
JOptionPane.showMessageDialog((Component) PluginServices.getMainFrame(), |
|
344 |
PluginServices.getText(this, "evaluate_expression_with_errors")+" "+(rowCount-indexRow.get())+"\n"+ee.getMessage()); |
|
345 |
} |
|
346 |
|
|
347 |
ies.endComplexRow(PluginServices.getText(this, "expression")); |
|
348 |
|
|
349 |
return true; |
|
350 |
} |
|
351 |
|
|
352 |
public Table getTable() { |
|
353 |
return this.table; |
|
354 |
} |
|
355 |
|
|
356 |
public ArrayList<IOperator> getOperators() { |
|
357 |
return this.operators; |
|
358 |
} |
|
359 |
|
|
360 |
public FLyrVect getLayer() { |
|
361 |
return this.layer; |
|
362 |
} |
|
363 |
|
|
364 |
public BSFManager getInterpreter() { |
|
365 |
return this.interpreter; |
|
366 |
} |
|
214 | 367 |
} |
Also available in: Unified diff