svn-gvsig-desktop / 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 @ 46068
History | View | Annotate | Download (8.95 KB)
1 |
package org.gvsig.app.extension.evalexpression; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.Image; |
5 |
import java.awt.event.ActionEvent; |
6 |
import java.awt.event.ActionListener; |
7 |
import java.awt.event.ComponentEvent; |
8 |
import java.awt.event.ComponentListener; |
9 |
import javax.swing.ImageIcon; |
10 |
import javax.swing.JComponent; |
11 |
import javax.swing.JEditorPane; |
12 |
import javax.swing.JLabel; |
13 |
import javax.swing.JPanel; |
14 |
import javax.swing.SwingUtilities; |
15 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
16 |
import org.gvsig.app.ApplicationLocator; |
17 |
import org.gvsig.app.ApplicationManager; |
18 |
import org.gvsig.app.project.documents.Document; |
19 |
import org.gvsig.app.project.documents.view.gui.IView; |
20 |
import org.gvsig.expressionevaluator.Expression; |
21 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
22 |
import org.gvsig.expressionevaluator.MutableSymbolTable; |
23 |
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingLocator; |
24 |
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingManager; |
25 |
import org.gvsig.expressionevaluator.swing.ExpressionPickerController; |
26 |
import org.gvsig.fmap.mapcontext.ViewPort; |
27 |
import org.gvsig.fmap.mapcontext.events.ColorEvent; |
28 |
import org.gvsig.fmap.mapcontext.events.ExtentEvent; |
29 |
import org.gvsig.fmap.mapcontext.events.ProjectionEvent; |
30 |
import org.gvsig.fmap.mapcontext.events.listeners.ViewPortListener; |
31 |
import org.gvsig.tools.swing.api.ActionListenerSupport; |
32 |
import org.gvsig.tools.swing.api.SimpleImage; |
33 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
34 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
35 |
import org.slf4j.Logger; |
36 |
import org.slf4j.LoggerFactory; |
37 |
|
38 |
/**
|
39 |
*
|
40 |
* @author jjdelcerro
|
41 |
*/
|
42 |
public class EvaluateExpressionPanel extends EvaluateExpressionPanelView { |
43 |
|
44 |
private static final Logger LOGGER = LoggerFactory.getLogger(EvaluateExpressionPanel.class); |
45 |
|
46 |
private ExpressionPickerController pickerExpression;
|
47 |
private final ActionListenerSupport actionListener; |
48 |
|
49 |
public EvaluateExpressionPanel(ActionListenerSupport actionListener) {
|
50 |
this.actionListener = actionListener;
|
51 |
initComponents(); |
52 |
} |
53 |
|
54 |
private void initComponents() { |
55 |
ApplicationManager application = ApplicationLocator.getApplicationManager(); |
56 |
ExpressionEvaluatorSwingManager expressionSwingManager = ExpressionEvaluatorSwingLocator.getManager(); |
57 |
|
58 |
this.pickerExpression = expressionSwingManager.createExpressionPickerController(
|
59 |
this.txtExpression,
|
60 |
this.btnExpression,
|
61 |
this.btnExpressionHistory,
|
62 |
this.btnExpressionBookmarks
|
63 |
); |
64 |
MutableSymbolTable symbolTable = ExpressionUtils.createSymbolTable(); |
65 |
IWindow window = application.getActiveWindow(); |
66 |
symbolTable.setVar("window", window);
|
67 |
this.pickerExpression.setPreviewSymbolTable(symbolTable);
|
68 |
|
69 |
this.btnExecute.addActionListener((ActionEvent e) -> { |
70 |
doExecuteExpression(); |
71 |
}); |
72 |
this.txtTitle.setText("Expresion"); |
73 |
} |
74 |
|
75 |
private void doExecuteExpression() { |
76 |
ApplicationManager application = ApplicationLocator.getApplicationManager(); |
77 |
WindowManager windowManager = ToolsSwingLocator.getWindowManager(); |
78 |
|
79 |
Expression exp = this.pickerExpression.get(); |
80 |
if( exp == null ) { |
81 |
return ; //??? |
82 |
} |
83 |
MutableSymbolTable symbolTable = ExpressionUtils.createSymbolTable(); |
84 |
IWindow window = application.getActiveWindow(); |
85 |
symbolTable.setVar("window", window);
|
86 |
this.pickerExpression.setPreviewSymbolTable(symbolTable);
|
87 |
Object value;
|
88 |
try {
|
89 |
value = exp.execute(symbolTable); |
90 |
} catch(Throwable th) { |
91 |
LOGGER.warn("",th);
|
92 |
return; //??? |
93 |
} |
94 |
if( value == null ) { |
95 |
return;
|
96 |
} |
97 |
MyPanel panel = new MyPanel(this.actionListener, symbolTable, exp, (String) this.cboContentType.getSelectedItem()); |
98 |
if( this.chkLaunchOnChangeBBox.isSelected() ) { |
99 |
Document doc = application.getActiveDocument();
|
100 |
panel.setWindow(doc.getMainWindow()); |
101 |
} |
102 |
panel.setValue(value); |
103 |
windowManager.showWindow(panel, this.txtTitle.getText(), this.getWindowMode()); |
104 |
} |
105 |
|
106 |
private WindowManager.MODE getWindowMode() {
|
107 |
String s = (String) this.cboWindowType.getSelectedItem(); |
108 |
switch(s.toLowerCase()) {
|
109 |
case "modal": |
110 |
case "dialog": |
111 |
return WindowManager.MODE.DIALOG;
|
112 |
case "tool": |
113 |
case "palette": |
114 |
case "semimodal": |
115 |
return WindowManager.MODE.TOOL;
|
116 |
case "window": |
117 |
default:
|
118 |
return WindowManager.MODE.WINDOW;
|
119 |
} |
120 |
} |
121 |
|
122 |
private static class MyPanel extends JPanel { |
123 |
|
124 |
private final Expression expression; |
125 |
private final String contentType; |
126 |
private IWindow window;
|
127 |
private final ViewPortListener viewPortListener; |
128 |
private final MutableSymbolTable symbolTable; |
129 |
private final ActionListenerSupport actionListener; |
130 |
private boolean isProcessing; |
131 |
private Thread lastUpdate = null; |
132 |
|
133 |
public MyPanel(ActionListenerSupport actionListener, MutableSymbolTable symbolTable, Expression expression, String contentType) { |
134 |
this.actionListener = actionListener;
|
135 |
this.setOpaque(false); |
136 |
this.symbolTable = symbolTable;
|
137 |
this.expression = expression;
|
138 |
this.contentType = contentType;
|
139 |
this.isProcessing = false; |
140 |
this.actionListener.addActionListener(new ActionListener() { |
141 |
@Override
|
142 |
public void actionPerformed(ActionEvent e) { |
143 |
doUpdate(); |
144 |
} |
145 |
}); |
146 |
this.viewPortListener = new ViewPortListener() { |
147 |
@Override
|
148 |
public void extentChanged(ExtentEvent e) { |
149 |
doUpdate(); |
150 |
} |
151 |
|
152 |
@Override
|
153 |
public void backColorChanged(ColorEvent e) { |
154 |
} |
155 |
|
156 |
@Override
|
157 |
public void projectionChanged(ProjectionEvent e) { |
158 |
} |
159 |
}; |
160 |
this.addComponentListener(new ComponentListener() { |
161 |
@Override
|
162 |
public void componentResized(ComponentEvent e) { |
163 |
} |
164 |
|
165 |
@Override
|
166 |
public void componentMoved(ComponentEvent e) { |
167 |
} |
168 |
|
169 |
@Override
|
170 |
public void componentShown(ComponentEvent e) { |
171 |
} |
172 |
|
173 |
@Override
|
174 |
public void componentHidden(ComponentEvent e) { |
175 |
if( window instanceof IView) { |
176 |
ViewPort viewPort = ((IView)window).getMapControl().getMapContext().getViewPort(); |
177 |
viewPort.removeViewPortListener(viewPortListener); |
178 |
} |
179 |
} |
180 |
}); |
181 |
} |
182 |
|
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 |
|
208 |
} |
209 |
|
210 |
public void setValue(Object value) { |
211 |
if (!SwingUtilities.isEventDispatchThread()) { |
212 |
Object v = value;
|
213 |
SwingUtilities.invokeLater(() -> {
|
214 |
setValue(v); |
215 |
}); |
216 |
return;
|
217 |
} |
218 |
if( value == null ) { |
219 |
this.removeAll();
|
220 |
this.revalidate();
|
221 |
return;
|
222 |
} |
223 |
if( value instanceof SimpleImage ) { |
224 |
value = ((SimpleImage)value).getBufferedImage(); |
225 |
} |
226 |
if( value instanceof Image ) { |
227 |
JLabel label = new JLabel(); |
228 |
label.setIcon(new ImageIcon((Image)value)); |
229 |
value = label; |
230 |
} |
231 |
|
232 |
JComponent component;
|
233 |
if( value instanceof JComponent ) { |
234 |
component = (JComponent) value;
|
235 |
} else {
|
236 |
JEditorPane browser = new JEditorPane(); |
237 |
browser.setEditable(false);
|
238 |
browser.setOpaque(false);
|
239 |
browser.setContentType(contentType); |
240 |
browser.setText(value.toString()); |
241 |
component = browser; |
242 |
} |
243 |
this.removeAll();
|
244 |
this.setLayout(new BorderLayout()); |
245 |
this.add(component);
|
246 |
this.revalidate();
|
247 |
|
248 |
} |
249 |
|
250 |
public void setWindow(IWindow window) { |
251 |
this.window = window;
|
252 |
if( window instanceof IView) { |
253 |
ViewPort viewPort = ((IView)this.window).getMapControl().getMapContext().getViewPort();
|
254 |
viewPort.addViewPortListener(this.viewPortListener);
|
255 |
} |
256 |
} |
257 |
} |
258 |
} |