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