Revision 36459

View differences:

trunk/extensions/extExpressionField/src/com/iver/cit/gvsig/ExpressionFieldExtension.java
79 79
 */
80 80
public class ExpressionFieldExtension extends Extension{
81 81
	public static final String JYTHON="jython";
82
	private static BSFManager interpreter=new BSFManager();
82
    private static BSFManager interpreter;
83 83
	private Table table=null;
84
	private static ArrayList<IOperator> operators=new ArrayList<IOperator>();
84
    private static ArrayList<IOperator> operators;
85 85
	public void initialize() {
86 86
		registerOperations();
87 87
		registerIcons();
88 88
	}
89 89

  
90
    // TODO: fpuga: Maybe, interpreter and operators should be instantiated
91
    // in its own class, not in EvalExpression, EvalExpressionDialog or
92
    // ExpressionFieldExtension. I think that if that class was singleton we
93
    // will get the same behavior that we have now, and the code will more
94
    // encapsulate without need of get ExpressionFieldExtension to use this
90 95
    public BSFManager getInterpreter() {
91
	if (operators.isEmpty()) {
92
	    loadOperators();
96
	if (interpreter == null) {
97
	    interpreter = new BSFManager();
93 98
	}
94 99
	return interpreter;
95 100
    }
96 101

  
102
    public ArrayList<IOperator> getOperators() {
103
	if (operators == null) {
104
	    operators = new ArrayList<IOperator>();
105
	    // getInterpreter() is only to avoid a possible NullPointerException
106
	    getInterpreter();
107
	    ExtensionPoint extensionPoint = (ExtensionPoint) ExtensionPointsSingleton
108
		    .getInstance().get("ColumnOperators");
109
	    Iterator<String> iterator = extensionPoint.keySet().iterator();
110
	    while (iterator.hasNext()) {
111
		try {
112
		    IOperator operator = (IOperator) extensionPoint
113
			    .create(iterator.next());
114
		    operator.eval(interpreter);
115
		    operators.add(operator);
116
		} catch (BSFException e) {
117
		    e.printStackTrace();
118
		} catch (InstantiationException e) {
119
		    e.printStackTrace();
120
		} catch (IllegalAccessException e) {
121
		    e.printStackTrace();
122
		}
123
	    }
124
	}
125
	return operators;
126
    }
127

  
97 128
	public void execute(String actionCommand) {
98 129
		com.iver.andami.ui.mdiManager.IWindow window = PluginServices.getMDIManager().getActiveWindow();
99 130
		table=(Table)window;
100 131

  
101
	if (operators.isEmpty()) {
102
	    loadOperators();
103
	}
104
	EvalExpression ee = new EvalExpression(interpreter, operators);
132

  
133
	EvalExpression ee = new EvalExpression(getInterpreter(), getOperators());
105 134
	ee.setTable(table);
106 135
	EvalExpressionDialog eed = new EvalExpressionDialog(ee);
107 136
	PluginServices.getMDIManager().addWindow(eed);
......
205 234
					this.getClass().getClassLoader().getResource("images/FieldExpression.png")
206 235
				);
207 236
	 }
208

  
209
    public void loadOperators() {
210
	ExtensionPoint extensionPoint = (ExtensionPoint) ExtensionPointsSingleton
211
		.getInstance().get("ColumnOperators");
212
	Iterator<String> iterator = extensionPoint.keySet().iterator();
213
	while (iterator.hasNext()) {
214
	    try {
215
		IOperator operator = (IOperator) extensionPoint.create(iterator
216
			.next());
217
		operator.eval(interpreter);
218
		operators.add(operator);
219
	    } catch (BSFException e) {
220
		e.printStackTrace();
221
	    } catch (InstantiationException e) {
222
		e.printStackTrace();
223
	    } catch (IllegalAccessException e) {
224
		e.printStackTrace();
225
	    }
226
	}
227
    }
228
	 
229

  
230 237
}
trunk/extensions/extExpressionField/src/com/iver/cit/gvsig/project/documents/table/gui/EvalExpression.java
21 21
import com.iver.andami.messages.NotificationManager;
22 22
import com.iver.cit.gvsig.EditionUtilities;
23 23
import com.iver.cit.gvsig.ExpressionFieldExtension;
24
import com.iver.cit.gvsig.ProjectExtension;
25 24
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
26 25
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileWriteException;
27 26
import com.iver.cit.gvsig.exceptions.validate.ValidateRowException;
......
31 30
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition;
32 31
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition;
33 32
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
34
import com.iver.cit.gvsig.fmap.edition.EditableAdapter;
35 33
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
36 34
import com.iver.cit.gvsig.fmap.edition.IEditableSource;
37 35
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
......
41 39
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
42 40
import com.iver.cit.gvsig.fmap.layers.FBitSet;
43 41
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
44
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
45 42
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 43
import com.iver.cit.gvsig.project.documents.table.IOperator;
49 44
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 45

  
53 46
/**
54 47
 * This class implements the logic of a expression and fill a field of the table
55 48
 * 
49
 * To use this class to evaluate and execute an expression something like this
50
 * can be done:
51
 * 
52
 * ExpressionFieldExtension efe = (ExpressionFieldExtension)
53
 * PluginServices.getExtension(ExpressionFieldExtension.class);
54
 * 
55
 * EvalExpression ee = new EvalExpression(efe.getInterpreter(), efe.getOperators());
56
 * ToggleEditing te = new ToggleEditing();
57
 *  // put the layer in edition mode
58
 * te.startEditing(layer); 
59
 * ee.setLayer(layer, 7);
60
 * ee.evalExpression("toUpperCase([NOME_MAPA])");
61
 *  // close edition mode and save the layer
62
 * te.stopEditing(layer, false);
63
 * 
56 64
 * @author Vicente Caballero Navarro
57 65
 */
58 66
public class EvalExpression {
......
79 87

  
80 88
    public EvalExpression(BSFManager interpreter, ArrayList<IOperator> operators) {
81 89
		limit=prefs.getInt("limit_rows_in_memory",-1);
82
		//fpuga: I think interpreter should be instantiated here and not passed
83
		this.interpreter = interpreter;  
90
	this.interpreter = interpreter;
84 91
	this.operators = operators;
85 92
	}
86 93
	
94
    /**
95
     * 
96
     * @param layer
97
     *            Must be in edition or a ClassCastException will be thrown
98
     * @param selectedIndex
99
     *            The index of the field in the FieldDescription which will be
100
     *            filled by the expression
101
     */
87 102
	public void setLayer(FLyrVect layer, int selectedIndex) {
88 103
	this.layer = layer;
89 104
	ies = (VectorialEditableAdapter) layer.getSource();
......
161 176
	 }
162 177

  
163 178

  
164
	 /**
165
	     * Returns the value created from object.
166
	     *
167
	     * @param obj value.
168
	     *
169
	     * @return Value.
170
	     */
171 179
	    private Value getValue(Object obj) {
172 180
	        int typeField = fieldDescriptor.getFieldType();
173 181
	        Value value = null;//ValueFactory.createNullValue();
......
228 236
	public FieldDescription[] getFieldDescriptors() {
229 237
		return fieldDescriptors;
230 238
	}
231
//	public void setFieldValue(Object obj,int i) {
232
//    	try {
233
//			((DBFDriver)table.getModel().getModelo().getOriginalDriver()).setFieldValue(i,selectedIndex,obj);
234
//		} catch (DriverLoadException e) {
235
//			e.printStackTrace();
236
//		} catch (IOException e) {
237
//			e.printStackTrace();
238
//		}
239
//    }
239

  
240 240
	public void saveEdits(int numRows) throws ReadDriverException, InitializeWriterException, StopWriterVisitorException {
241 241
		if (limit==-1 || numRows == 0 || (numRows % limit)!=0) {
242 242
			return;
......
364 364
    public BSFManager getInterpreter() {
365 365
	return this.interpreter;
366 366
    }
367
}
367
}
trunk/extensions/extExpressionField/src/com/iver/cit/gvsig/project/documents/table/gui/EvalExpressionDialog.java
69 69
    private JList listCommand = null;
70 70
    private JPanel pMessage;
71 71
    private EvalExpression evalExpression;
72
    int lastType = -1;
73
    private JButton bClear = null;
74
    private JTabbedPane tabPrincipal = null;
75
    private JPanel pPrincipal = null;
76
    private JPanel pAdvanced = null;
77
    private JPanel pAdvancedNorth = null;
78
    private JTextField jTextField = null;
79
    private JButton bFile = null;
80
    private JPanel pAdvancedCenter = null;
81
    private JLabel lblLeng = null;
82
    private JButton bEval = null;
83
    private JScrollPane jScrollPane3 = null;
84
    private JTextArea txtMessage2 = null;
72 85
	
73 86

  
74 87
    public EvalExpressionDialog(EvalExpression ee) {
......
232 245
		return pMessage;
233 246
	}
234 247
    
235
    /**
236
     * Evaluate the fields.
237
     *
238
     * @param interpreter
239
     *
240
     * @throws EvalError
241
     */
242
    int lastType=-1;
243
	private JButton bClear = null;
244
	private JTabbedPane tabPrincipal = null;
245
	private JPanel pPrincipal = null;
246
	private JPanel pAdvanced = null;
247
	private JPanel pAdvancedNorth = null;
248
	private JTextField jTextField = null;
249
	private JButton bFile = null;
250
	private JPanel pAdvancedCenter = null;
251
	private JLabel lblLeng = null;
252
	private JButton bEval = null;
253
	private JScrollPane jScrollPane3 = null;
254
	private JTextArea txtMessage2 = null;
248

  
255 249
	
256 250
	private void refreshOperators(int type) {
257 251
        if (lastType!=-1 && lastType==type)
......
392 386
                    		getTxtMessage2().setText(operator.getTooltip());
393 387
                    		if (e.getClickCount() == 2) {
394 388
                        		String text = getTxtExp().getText();
395
                        		int caretPos = getTxtExp().getCaretPosition();
396 389
                        		int selStart = getTxtExp().getSelectionStart();
397 390
                        		int selEnd = getTxtExp().getSelectionEnd();
398 391
                        		
392
			    // int caretPos = getTxtExp().getCaretPosition();
399 393
//                        		if (caretPos == text.length()){
400 394
//                        			getTxtExp().setText(operator.addText(text));
401 395
//                        		} else {

Also available in: Unified diff