Revision 46068

View differences:

trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.app/org.gvsig.app.mainplugin/src/main/java/org/gvsig/app/extension/evalexpression/EvaluateExpressionPanel.java
11 11
import javax.swing.JEditorPane;
12 12
import javax.swing.JLabel;
13 13
import javax.swing.JPanel;
14
import javax.swing.SwingUtilities;
14 15
import org.gvsig.andami.ui.mdiManager.IWindow;
15 16
import org.gvsig.app.ApplicationLocator;
16 17
import org.gvsig.app.ApplicationManager;
......
126 127
        private final ViewPortListener viewPortListener;
127 128
        private final MutableSymbolTable symbolTable;
128 129
        private final ActionListenerSupport actionListener;
130
		private boolean isProcessing;
131
		private Thread lastUpdate = null;
129 132
        
130 133
        public MyPanel(ActionListenerSupport actionListener, MutableSymbolTable symbolTable, Expression expression, String contentType) {
131 134
            this.actionListener = actionListener;
......
133 136
            this.symbolTable = symbolTable;
134 137
            this.expression = expression;
135 138
            this.contentType = contentType;
139
			this.isProcessing = false;
136 140
            this.actionListener.addActionListener(new ActionListener() {
137 141
                @Override
138 142
                public void actionPerformed(ActionEvent e) {
......
176 180
            });
177 181
        }
178 182
        
179
        private void doUpdate() {
180
            Object value;
181
            try {
182
                value = this.expression.execute(this.symbolTable);
183
            } catch(Throwable th) {
184
                LOGGER.warn("",th);
185
                return; //???
186
            }
187
            this.setValue(value);
183
        private synchronized void doUpdate() {
184
			Thread th = new Thread(() -> {
185
				isProcessing = true;
186
				Object value = null;
187
				
188
				try {
189
					value = this.expression.execute(this.symbolTable);
190
				} catch (Throwable tt) {
191
					LOGGER.warn("", tt);
192
					return; //???
193
				}
194
				this.setValue(value);
195
				if(lastUpdate!=null) {
196
					lastUpdate.start();
197
					lastUpdate = null;
198
				} else {
199
					isProcessing = false;
200
				}
201
			});
202
			if (isProcessing==true) {
203
				lastUpdate = th;
204
				return;
205
			}
206
			th.start();
207

  
188 208
        }
189 209
        
190 210
        public void setValue(Object value) {
211
			if (!SwingUtilities.isEventDispatchThread()) {
212
				Object v = value;
213
				SwingUtilities.invokeLater(() -> {
214
					setValue(v);
215
				});
216
				return;
217
			}
191 218
            if( value == null ) {
192 219
                this.removeAll();
193 220
                this.revalidate();
......
217 244
            this.setLayout(new BorderLayout());
218 245
            this.add(component);
219 246
            this.revalidate();
247
			
220 248
        }
221 249
        
222 250
        public void setWindow(IWindow window) {
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/main/java/org/gvsig/expressionevaluator/impl/symboltable/ProgrammingSymbolTable.java
30 30
import org.gvsig.expressionevaluator.impl.function.programming.RangeFunction;
31 31
import org.gvsig.expressionevaluator.impl.function.programming.ReturnFunction;
32 32
import org.gvsig.expressionevaluator.impl.function.programming.ShowWindowFunction;
33
import org.gvsig.expressionevaluator.impl.function.programming.SwingBlockFunction;
33 34
import org.gvsig.expressionevaluator.impl.function.programming.TryFunction;
34 35
import org.gvsig.expressionevaluator.impl.function.programming.TupleFunction;
35 36
import org.gvsig.expressionevaluator.impl.function.programming.URLFunction;
......
89 90
        this.addFunction(new CreateChartPanelFunction());
90 91
        this.addFunction(new ShowWindowFunction());
91 92
        this.addFunction(new CreateHtmlPanelFunction());
93
        this.addFunction(new SwingBlockFunction());
92 94
    }
93 95

  
94 96
    private void addOperator(Function operator) {
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/main/java/org/gvsig/expressionevaluator/impl/function/programming/SwingBlockFunction.java
1
package org.gvsig.expressionevaluator.impl.function.programming;
2

  
3
import java.util.logging.Level;
4
import java.util.logging.Logger;
5
import javax.swing.SwingUtilities;
6
import org.apache.commons.lang3.Range;
7
import org.apache.commons.lang3.mutable.MutableObject;
8
import org.gvsig.expressionevaluator.Code;
9
import org.gvsig.expressionevaluator.Codes;
10
import org.gvsig.expressionevaluator.Function;
11
import org.gvsig.expressionevaluator.Interpreter;
12
import org.gvsig.expressionevaluator.impl.DefaultInterpreter;
13
import org.gvsig.expressionevaluator.spi.AbstractFunction;
14

  
15
public class SwingBlockFunction extends AbstractFunction {
16

  
17
	public static final String NAME = "SWING";
18

  
19
	public SwingBlockFunction() {
20
		super(Function.GROUP_PROGRAMMING,
21
			NAME,
22
			Range.between(1, Integer.MAX_VALUE),
23
			"Evaluate each of the arguments sequentially.",
24
			"SWING({{sentence}}, sentence2, ...)",
25
			null,
26
			"Object",
27
			false
28
		);
29
	}
30

  
31
	@Override
32
	public boolean isHidden() {
33
		return true;
34
	}
35

  
36
	@Override
37
	public boolean useArgumentsInsteadObjects() {
38
		return true;
39
	}
40

  
41
	@Override
42
	public boolean allowConstantFolding() {
43
		return false;
44
	}
45

  
46
	@Override
47
	public Object call(Interpreter interpreter, Object[] args) throws Exception {
48
		throw new UnsupportedOperationException("Not supported yet.");
49
	}
50

  
51
	@Override
52
	public Object call(Interpreter interpreter, final Codes args) throws Exception {
53
		if (SwingUtilities.isEventDispatchThread()) {
54
			Object value = null;
55
			for (Code arg : args) {
56
				// LLamamos a runCode para que no atrape los returns.
57
				value = ((DefaultInterpreter) interpreter).runCode(arg);
58
			}
59
			return value;
60
		} else {
61
			MutableObject value = new MutableObject();
62
			SwingUtilities.invokeAndWait(new Runnable() {
63
				@Override
64
				public void run() {
65
					try {
66
						for (Code arg : args) {
67
							// LLamamos a runCode para que no atrape los returns.
68
							value.setValue(((DefaultInterpreter) interpreter).runCode(arg));
69
						}
70
					} catch (Exception ex) {
71
					}
72
				}
73
			});
74
			return value.getValue();
75
		}
76
	}
77

  
78
}

Also available in: Unified diff