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 @ 47703
History | View | Annotate | Download (11.4 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.ComponentEvent; |
7 |
import java.awt.event.ComponentListener; |
8 |
import java.util.Objects; |
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.apache.commons.lang3.StringUtils; |
16 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
17 |
import org.gvsig.app.ApplicationLocator; |
18 |
import org.gvsig.app.ApplicationManager; |
19 |
import org.gvsig.app.project.documents.Document; |
20 |
import org.gvsig.app.project.documents.view.ViewDocument; |
21 |
import org.gvsig.app.project.documents.view.ViewManager; |
22 |
import org.gvsig.app.project.documents.view.gui.IView; |
23 |
import org.gvsig.expressionevaluator.Expression; |
24 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
25 |
import org.gvsig.expressionevaluator.MutableSymbolTable; |
26 |
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingLocator; |
27 |
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingManager; |
28 |
import org.gvsig.expressionevaluator.swing.ExpressionPickerController; |
29 |
import org.gvsig.fmap.dal.DataStoreNotification; |
30 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
31 |
import org.gvsig.fmap.dal.feature.FeatureStoreNotification; |
32 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
33 |
import org.gvsig.fmap.mapcontext.ViewPort; |
34 |
import org.gvsig.fmap.mapcontext.events.ColorEvent; |
35 |
import org.gvsig.fmap.mapcontext.events.ExtentEvent; |
36 |
import org.gvsig.fmap.mapcontext.events.ProjectionEvent; |
37 |
import org.gvsig.fmap.mapcontext.events.listeners.ViewPortListener; |
38 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
39 |
import org.gvsig.tools.observer.Observable; |
40 |
import org.gvsig.tools.observer.Observer; |
41 |
import org.gvsig.tools.swing.api.ActionListenerSupport; |
42 |
import org.gvsig.tools.swing.api.SimpleImage; |
43 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
44 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
45 |
import org.slf4j.Logger; |
46 |
import org.slf4j.LoggerFactory; |
47 |
|
48 |
/**
|
49 |
*
|
50 |
* @author jjdelcerro
|
51 |
*/
|
52 |
public class EvaluateExpressionPanel extends EvaluateExpressionPanelView { |
53 |
|
54 |
private static final Logger LOGGER = LoggerFactory.getLogger(EvaluateExpressionPanel.class); |
55 |
|
56 |
private ExpressionPickerController pickerExpression;
|
57 |
private final ActionListenerSupport actionListener; |
58 |
|
59 |
public EvaluateExpressionPanel(ActionListenerSupport actionListener, FeatureStore store) {
|
60 |
this.actionListener = actionListener;
|
61 |
initComponents(store); |
62 |
} |
63 |
|
64 |
private void initComponents(FeatureStore store) { |
65 |
ApplicationManager application = ApplicationLocator.getApplicationManager(); |
66 |
ExpressionEvaluatorSwingManager expressionSwingManager = ExpressionEvaluatorSwingLocator.getManager(); |
67 |
|
68 |
this.pickerExpression = expressionSwingManager.createExpressionPickerController(
|
69 |
this.txtExpression,
|
70 |
this.btnExpression,
|
71 |
this.btnExpressionHistory,
|
72 |
this.btnExpressionBookmarks
|
73 |
); |
74 |
if (store != null) { |
75 |
this.pickerExpression.getConfig().addElement(DALSwingLocator.getDataSwingManager().createFeatureStoreElement(store));
|
76 |
} |
77 |
|
78 |
MutableSymbolTable symbolTable = ExpressionUtils.createSymbolTable(); |
79 |
IWindow window = application.getActiveWindow(); |
80 |
symbolTable.setVar("window", window);
|
81 |
this.pickerExpression.getConfig().setPreviewSymbolTable(symbolTable);
|
82 |
|
83 |
this.btnExecute.addActionListener((ActionEvent e) -> { |
84 |
doExecuteExpression(); |
85 |
}); |
86 |
this.txtTitle.setText("Expresion"); |
87 |
} |
88 |
|
89 |
private void doExecuteExpression() { |
90 |
ApplicationManager application = ApplicationLocator.getApplicationManager(); |
91 |
WindowManager windowManager = ToolsSwingLocator.getWindowManager(); |
92 |
|
93 |
Expression exp = this.pickerExpression.get(); |
94 |
if (exp == null) { |
95 |
return; //??? |
96 |
} |
97 |
MutableSymbolTable symbolTable = ExpressionUtils.createSymbolTable(); |
98 |
IWindow window = application.getActiveWindow(); |
99 |
symbolTable.setVar("window", window);
|
100 |
this.pickerExpression.getConfig().setPreviewSymbolTable(symbolTable);
|
101 |
Object value;
|
102 |
try {
|
103 |
value = exp.execute(symbolTable); |
104 |
} catch (Throwable th) { |
105 |
LOGGER.warn("", th);
|
106 |
return; //??? |
107 |
} |
108 |
if (value == null) { |
109 |
return;
|
110 |
} |
111 |
MyPanel panel = new MyPanel(this.actionListener, symbolTable, exp, (String) this.cboContentType.getSelectedItem()); |
112 |
if (this.chkLaunchOnChangeBBox.isSelected()) { |
113 |
Document doc = application.getActiveDocument();
|
114 |
panel.setWindow(doc.getMainWindow()); |
115 |
} |
116 |
if (this.chkLaunchOnChangeSelection.isSelected()) { |
117 |
ViewDocument viewdoc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME); |
118 |
if (viewdoc != null) { |
119 |
FLyrVect layer = viewdoc.getMapContext().getLayers().getFirstActiveVectorLayer(); |
120 |
panel.setLayer(layer); |
121 |
} |
122 |
} |
123 |
panel.setValue(value); |
124 |
windowManager.showWindow(panel, this.txtTitle.getText(), this.getWindowMode()); |
125 |
} |
126 |
|
127 |
private WindowManager.MODE getWindowMode() {
|
128 |
String s = (String) this.cboWindowType.getSelectedItem(); |
129 |
switch (s.toLowerCase()) {
|
130 |
case "modal": |
131 |
case "dialog": |
132 |
return WindowManager.MODE.DIALOG;
|
133 |
case "tool": |
134 |
case "palette": |
135 |
case "semimodal": |
136 |
return WindowManager.MODE.TOOL;
|
137 |
case "window": |
138 |
default:
|
139 |
return WindowManager.MODE.WINDOW;
|
140 |
} |
141 |
} |
142 |
|
143 |
private static class MyPanel extends JPanel { |
144 |
|
145 |
private final Expression expression; |
146 |
private final String contentType; |
147 |
private IWindow window;
|
148 |
private final ViewPortListener viewPortListener; |
149 |
private final MutableSymbolTable symbolTable; |
150 |
private final ActionListenerSupport actionListener; |
151 |
private boolean isProcessing; |
152 |
private Thread lastUpdate = null; |
153 |
private Observer selectionListener; |
154 |
|
155 |
public MyPanel(ActionListenerSupport actionListener, MutableSymbolTable symbolTable, Expression expression, String contentType) { |
156 |
this.actionListener = actionListener;
|
157 |
this.setOpaque(false); |
158 |
this.symbolTable = symbolTable;
|
159 |
this.expression = expression;
|
160 |
this.contentType = contentType;
|
161 |
this.isProcessing = false; |
162 |
this.selectionListener = null; |
163 |
this.actionListener.addActionListener((ActionEvent e) -> { |
164 |
doUpdate(); |
165 |
}); |
166 |
this.viewPortListener = new ViewPortListener() { |
167 |
@Override
|
168 |
public void extentChanged(ExtentEvent e) { |
169 |
doUpdate(); |
170 |
} |
171 |
|
172 |
@Override
|
173 |
public void backColorChanged(ColorEvent e) { |
174 |
} |
175 |
|
176 |
@Override
|
177 |
public void projectionChanged(ProjectionEvent e) { |
178 |
} |
179 |
}; |
180 |
this.selectionListener = (Observable observable, Object notification) -> { |
181 |
FeatureStoreNotification n = (FeatureStoreNotification) notification; |
182 |
if (window == null ) { |
183 |
try {
|
184 |
FeatureStore store = (FeatureStore) n.getSource(); |
185 |
store.deleteObserver(selectionListener); |
186 |
} catch (Throwable t) { |
187 |
LOGGER.warn("Can't remove observer from store", t);
|
188 |
} |
189 |
} |
190 |
if (StringUtils.equalsIgnoreCase(n.getType(), DataStoreNotification.SELECTION_CHANGE)) {
|
191 |
doUpdate(); |
192 |
} |
193 |
}; |
194 |
this.addComponentListener(new ComponentListener() { |
195 |
@Override
|
196 |
public void componentResized(ComponentEvent e) { |
197 |
} |
198 |
|
199 |
@Override
|
200 |
public void componentMoved(ComponentEvent e) { |
201 |
} |
202 |
|
203 |
@Override
|
204 |
public void componentShown(ComponentEvent e) { |
205 |
} |
206 |
|
207 |
@Override
|
208 |
public void componentHidden(ComponentEvent e) { |
209 |
if (window instanceof IView) { |
210 |
ViewPort viewPort = ((IView) window).getMapControl().getMapContext().getViewPort(); |
211 |
viewPort.removeViewPortListener(viewPortListener); |
212 |
lastUpdate = null;
|
213 |
window = null;
|
214 |
} |
215 |
} |
216 |
}); |
217 |
} |
218 |
|
219 |
private synchronized void doUpdate() { |
220 |
if (window == null) { |
221 |
return;
|
222 |
} |
223 |
Thread th = new Thread(() -> { |
224 |
isProcessing = true;
|
225 |
Object value = null; |
226 |
|
227 |
try {
|
228 |
value = this.expression.execute(this.symbolTable); |
229 |
this.setValue(value);
|
230 |
} catch (Throwable tt) { |
231 |
LOGGER.warn("", tt);
|
232 |
} |
233 |
if (lastUpdate != null) { |
234 |
lastUpdate.start(); |
235 |
lastUpdate = null;
|
236 |
} else {
|
237 |
isProcessing = false;
|
238 |
} |
239 |
}, "EvaluateExpressionPanel_update");
|
240 |
if (isProcessing == true) { |
241 |
lastUpdate = th; |
242 |
return;
|
243 |
} |
244 |
th.start(); |
245 |
} |
246 |
|
247 |
public void setValue(Object value) { |
248 |
if (!SwingUtilities.isEventDispatchThread()) { |
249 |
Object v = value;
|
250 |
SwingUtilities.invokeLater(() -> {
|
251 |
setValue(v); |
252 |
}); |
253 |
return;
|
254 |
} |
255 |
if (value == null) { |
256 |
this.removeAll();
|
257 |
this.revalidate();
|
258 |
return;
|
259 |
} |
260 |
if (value instanceof SimpleImage) { |
261 |
value = ((SimpleImage) value).getBufferedImage(); |
262 |
} |
263 |
if (value instanceof Image) { |
264 |
JLabel label = new JLabel(); |
265 |
label.setIcon(new ImageIcon((Image) value)); |
266 |
value = label; |
267 |
} |
268 |
|
269 |
JComponent component;
|
270 |
if (value instanceof JComponent) { |
271 |
component = (JComponent) value;
|
272 |
} else {
|
273 |
JEditorPane browser = new JEditorPane(); |
274 |
browser.setEditable(false);
|
275 |
browser.setOpaque(false);
|
276 |
browser.setContentType(contentType); |
277 |
browser.setText(Objects.toString(value)); |
278 |
component = browser; |
279 |
} |
280 |
this.removeAll();
|
281 |
this.setLayout(new BorderLayout()); |
282 |
this.add(component);
|
283 |
this.revalidate();
|
284 |
|
285 |
} |
286 |
|
287 |
public void setWindow(IWindow window) { |
288 |
this.window = window;
|
289 |
if (window instanceof IView) { |
290 |
ViewPort viewPort = ((IView) this.window).getMapControl().getMapContext().getViewPort();
|
291 |
viewPort.addViewPortListener(this.viewPortListener);
|
292 |
} |
293 |
} |
294 |
|
295 |
public void setLayer(FLyrVect layer) { |
296 |
FeatureStore store = layer.getFeatureStore(); |
297 |
if (store != null) { |
298 |
store.deleteObserver(this.selectionListener);
|
299 |
store.addObserver(this.selectionListener);
|
300 |
} |
301 |
} |
302 |
} |
303 |
} |