Revision 44148
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.swing/org.gvsig.expressionevaluator.swing.impl/src/main/java/org/gvsig/expressionevaluator/swing/impl/AbstractAutomaticExpressionChecker.java | ||
---|---|---|
1 |
package org.gvsig.expressionevaluator.swing.impl; |
|
2 |
|
|
3 |
import java.awt.Color; |
|
4 |
import java.awt.Dimension; |
|
5 |
import java.awt.event.ActionEvent; |
|
6 |
import java.awt.event.ActionListener; |
|
7 |
import java.awt.event.KeyAdapter; |
|
8 |
import java.awt.event.KeyEvent; |
|
9 |
import java.util.Objects; |
|
10 |
import javax.swing.JEditorPane; |
|
11 |
import javax.swing.JMenuItem; |
|
12 |
import javax.swing.JOptionPane; |
|
13 |
import javax.swing.JPopupMenu; |
|
14 |
import javax.swing.JScrollPane; |
|
15 |
import javax.swing.Timer; |
|
16 |
import javax.swing.event.DocumentEvent; |
|
17 |
import javax.swing.event.DocumentListener; |
|
18 |
import javax.swing.text.JTextComponent; |
|
19 |
import org.apache.commons.lang3.StringUtils; |
|
20 |
import org.gvsig.expressionevaluator.Expression; |
|
21 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
|
22 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
|
23 |
import org.gvsig.expressionevaluator.ExpressionRuntimeException; |
|
24 |
import org.gvsig.expressionevaluator.ExpressionSyntaxException; |
|
25 |
import org.gvsig.expressionevaluator.MutableSymbolTable; |
|
26 |
import org.gvsig.expressionevaluator.swing.ExpressionBuilderConfig; |
|
27 |
import org.gvsig.tools.ToolsLocator; |
|
28 |
import org.gvsig.tools.i18n.I18nManager; |
|
29 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
|
30 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
|
31 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
|
32 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
|
33 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
|
34 |
|
|
35 |
/** |
|
36 |
* |
|
37 |
* @author jjdelcerro |
|
38 |
*/ |
|
39 |
@SuppressWarnings("UseSpecificCatch") |
|
40 |
public abstract class AbstractAutomaticExpressionChecker { |
|
41 |
|
|
42 |
private static final Color COLOR_ERROR = new Color(255,225,232); |
|
43 |
|
|
44 |
private JTextComponent text; |
|
45 |
private ExpressionBuilderConfig config; |
|
46 |
|
|
47 |
private Color color_ok; |
|
48 |
private Timer timerExpression; |
|
49 |
|
|
50 |
private JMenuItem menuItemShowPreview; |
|
51 |
private JMenuItem menuItemShowTip; |
|
52 |
private JMenuItem menuItemShowMessage; |
|
53 |
|
|
54 |
private String message; |
|
55 |
protected String tip; |
|
56 |
private String preview; |
|
57 |
private DocumentListener doclistener; |
|
58 |
private KeyAdapter keylistener; |
|
59 |
private ActionListener timelistener; |
|
60 |
|
|
61 |
public AbstractAutomaticExpressionChecker(ExpressionBuilderConfig config, JTextComponent theText) { |
|
62 |
this.text = theText; |
|
63 |
this.config = config; |
|
64 |
this.initComponents(); |
|
65 |
} |
|
66 |
|
|
67 |
private void initComponents() { |
|
68 |
final I18nManager i18n = ToolsLocator.getI18nManager(); |
|
69 |
final ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
|
70 |
|
|
71 |
this.color_ok = this.text.getBackground(); |
|
72 |
|
|
73 |
toolsSwingManager.setDefaultPopupMenu(this.text); |
|
74 |
toolsSwingManager.addClearButton(this.text); |
|
75 |
|
|
76 |
JPopupMenu popupMenu = this.text.getComponentPopupMenu(); |
|
77 |
|
|
78 |
this.menuItemShowTip = new JMenuItem(i18n.getTranslation("_Show_tip")); |
|
79 |
this.menuItemShowTip.addActionListener(new ActionListener() { |
|
80 |
@Override |
|
81 |
public void actionPerformed(ActionEvent e) { |
|
82 |
if( StringUtils.isBlank(tip) ) { |
|
83 |
return; |
|
84 |
} |
|
85 |
JOptionPane.showMessageDialog( |
|
86 |
text.getParent(), |
|
87 |
tip, |
|
88 |
i18n.getTranslation("Tip"), |
|
89 |
JOptionPane.INFORMATION_MESSAGE |
|
90 |
); |
|
91 |
} |
|
92 |
}); |
|
93 |
popupMenu.add(this.menuItemShowTip); |
|
94 |
|
|
95 |
this.menuItemShowPreview = new JMenuItem(i18n.getTranslation("_Show_preview")); |
|
96 |
this.menuItemShowPreview.addActionListener(new ActionListener() { |
|
97 |
@Override |
|
98 |
public void actionPerformed(ActionEvent e) { |
|
99 |
showMessageDialog( |
|
100 |
i18n.getTranslation("_Preview_value"), |
|
101 |
preview |
|
102 |
); |
|
103 |
} |
|
104 |
}); |
|
105 |
popupMenu.add(this.menuItemShowPreview); |
|
106 |
|
|
107 |
this.menuItemShowMessage = new JMenuItem(i18n.getTranslation("_Show_message")); |
|
108 |
this.menuItemShowMessage.addActionListener(new ActionListener() { |
|
109 |
@Override |
|
110 |
public void actionPerformed(ActionEvent e) { |
|
111 |
showMessageDialog( |
|
112 |
i18n.getTranslation("_Message"), |
|
113 |
message |
|
114 |
); |
|
115 |
} |
|
116 |
}); |
|
117 |
popupMenu.add(this.menuItemShowMessage); |
|
118 |
|
|
119 |
this.timelistener = new ActionListener() { |
|
120 |
@Override |
|
121 |
public void actionPerformed(ActionEvent e) { |
|
122 |
doCheckExpression(); |
|
123 |
} |
|
124 |
}; |
|
125 |
this.doclistener = new DocumentListener() { |
|
126 |
@Override |
|
127 |
public void insertUpdate(DocumentEvent e) { |
|
128 |
timerExpression.restart(); |
|
129 |
} |
|
130 |
|
|
131 |
@Override |
|
132 |
public void removeUpdate(DocumentEvent e) { |
|
133 |
timerExpression.restart(); |
|
134 |
} |
|
135 |
|
|
136 |
@Override |
|
137 |
public void changedUpdate(DocumentEvent e) { |
|
138 |
timerExpression.restart(); |
|
139 |
} |
|
140 |
}; |
|
141 |
this.keylistener = new KeyAdapter() { |
|
142 |
@Override |
|
143 |
public void keyPressed(KeyEvent e) { |
|
144 |
if (e.getKeyCode() == KeyEvent.VK_C && e.isControlDown()) { |
|
145 |
doCheckExpression(); |
|
146 |
} |
|
147 |
} |
|
148 |
}; |
|
149 |
this.timerExpression = new Timer(3000, this.timelistener); |
|
150 |
this.timerExpression.setRepeats(false); |
|
151 |
this.text.getDocument().addDocumentListener(this.doclistener); |
|
152 |
this.text.addKeyListener(this.keylistener); |
|
153 |
} |
|
154 |
|
|
155 |
private void doCheckExpression() { |
|
156 |
if( this.text == null || this.config == null ) { |
|
157 |
return; |
|
158 |
} |
|
159 |
Expression expression = this.getExpression(); |
|
160 |
this.setTip(null); |
|
161 |
this.setPreview(null); |
|
162 |
this.setMessage(JOptionPane.INFORMATION_MESSAGE, null); |
|
163 |
if (expression == null) { |
|
164 |
return; |
|
165 |
} |
|
166 |
try { |
|
167 |
expression.getCode(); |
|
168 |
this.setMessage(JOptionPane.INFORMATION_MESSAGE, ""); |
|
169 |
} catch (ExpressionSyntaxException ex) { |
|
170 |
this.setMessage(JOptionPane.ERROR_MESSAGE, ex.getDescription()); |
|
171 |
return; |
|
172 |
} catch (Exception ex) { |
|
173 |
this.setMessage(JOptionPane.ERROR_MESSAGE, "Syntax error in expression"); |
|
174 |
return; |
|
175 |
} |
|
176 |
if (this.config.getPreviewSymbolTable() == null) { |
|
177 |
try { |
|
178 |
ExpressionEvaluatorManager theManager = ExpressionEvaluatorLocator.getManager(); |
|
179 |
MutableSymbolTable symbolTable = theManager.createSymbolTable(); |
|
180 |
Object theValue = expression.execute(symbolTable); |
|
181 |
this.setPreview(theValue); |
|
182 |
} catch (Exception ex) { |
|
183 |
this.setMessage(JOptionPane.INFORMATION_MESSAGE, ""); |
|
184 |
} |
|
185 |
} else { |
|
186 |
try { |
|
187 |
Object theValue = expression.execute(this.config.getPreviewSymbolTable()); |
|
188 |
this.setPreview(theValue); |
|
189 |
} catch (ExpressionRuntimeException ex) { |
|
190 |
this.setTip(ex.getTip()); |
|
191 |
this.setMessage(JOptionPane.ERROR_MESSAGE, "Runtime error. " + ex.getMessage()); |
|
192 |
} catch (Exception ex) { |
|
193 |
this.setMessage(JOptionPane.ERROR_MESSAGE, "Runtime error. " + ex.getMessage()); |
|
194 |
} |
|
195 |
} |
|
196 |
} |
|
197 |
|
|
198 |
protected boolean isTipEnabled() { |
|
199 |
return !StringUtils.isBlank(this.tip); |
|
200 |
} |
|
201 |
|
|
202 |
protected boolean isPreviewEnabled() { |
|
203 |
return !StringUtils.isBlank(this.preview); |
|
204 |
} |
|
205 |
|
|
206 |
protected boolean isMessageEnabled() { |
|
207 |
return !StringUtils.isBlank(this.message); |
|
208 |
} |
|
209 |
|
|
210 |
protected String getTip() { |
|
211 |
return this.tip; |
|
212 |
} |
|
213 |
|
|
214 |
protected String getMessage() { |
|
215 |
return this.message; |
|
216 |
} |
|
217 |
|
|
218 |
protected String getPreview() { |
|
219 |
return this.preview; |
|
220 |
} |
|
221 |
|
|
222 |
protected void showMessageDialog() { |
|
223 |
final I18nManager i18n = ToolsLocator.getI18nManager(); |
|
224 |
if( this.isMessageEnabled() ) { |
|
225 |
this.showMessageDialog( |
|
226 |
i18n.getTranslation("_Message"), |
|
227 |
this.getMessage() |
|
228 |
); |
|
229 |
} else if( this.isPreviewEnabled()) { |
|
230 |
this.showMessageDialog( |
|
231 |
i18n.getTranslation("_Preview_value"), |
|
232 |
this.getPreview() |
|
233 |
); |
|
234 |
} |
|
235 |
} |
|
236 |
|
|
237 |
protected void showMessageDialog(String title, String msg) { |
|
238 |
final I18nManager i18n = ToolsLocator.getI18nManager(); |
|
239 |
final ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
|
240 |
|
|
241 |
JEditorPane textMessage = new JEditorPane(); |
|
242 |
JScrollPane scroll = new JScrollPane(textMessage); |
|
243 |
scroll.setPreferredSize(new Dimension(500, 300)); |
|
244 |
toolsSwingManager.setDefaultPopupMenu(textMessage); |
|
245 |
textMessage.setContentType("text/html"); |
|
246 |
textMessage.setText("<html>"+msg+"</html>"); |
|
247 |
textMessage.setCaretPosition(0); |
|
248 |
WindowManager_v2 winmgr = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
|
249 |
Dialog dialog = winmgr.createDialog( |
|
250 |
scroll, |
|
251 |
title, |
|
252 |
null, |
|
253 |
WindowManager_v2.BUTTONS_OK |
|
254 |
); |
|
255 |
dialog.show(WindowManager.MODE.DIALOG); |
|
256 |
} |
|
257 |
|
|
258 |
protected void setPreview(Object value) { |
|
259 |
this.preview = Objects.toString(value, null); |
|
260 |
this.text.setToolTipText( |
|
261 |
"<html>" + |
|
262 |
StringUtils.abbreviate(Objects.toString(this.preview,""),100) + |
|
263 |
"</html>" |
|
264 |
); |
|
265 |
this.menuItemShowPreview.setEnabled(this.isPreviewEnabled()); |
|
266 |
this.text.setBackground(color_ok); |
|
267 |
} |
|
268 |
|
|
269 |
protected void setMessage(int mode, String text) { |
|
270 |
this.message = text; |
|
271 |
if( StringUtils.isBlank(text) ) { |
|
272 |
this.text.setToolTipText(""); |
|
273 |
this.menuItemShowMessage.setEnabled(false); |
|
274 |
} else { |
|
275 |
this.text.setToolTipText( |
|
276 |
"<html>" + |
|
277 |
StringUtils.abbreviate(Objects.toString(text,""),100) + |
|
278 |
"</html>" |
|
279 |
); |
|
280 |
this.menuItemShowMessage.setEnabled(true); |
|
281 |
} |
|
282 |
switch (mode) { |
|
283 |
case JOptionPane.INFORMATION_MESSAGE: |
|
284 |
this.text.setBackground(color_ok); |
|
285 |
break; |
|
286 |
case JOptionPane.WARNING_MESSAGE: |
|
287 |
case JOptionPane.ERROR_MESSAGE: |
|
288 |
default: |
|
289 |
this.text.setBackground(COLOR_ERROR); |
|
290 |
break; |
|
291 |
} |
|
292 |
} |
|
293 |
|
|
294 |
protected void setTip(String theTip) { |
|
295 |
this.tip = theTip; |
|
296 |
this.menuItemShowTip.setEnabled(this.isTipEnabled()); |
|
297 |
} |
|
298 |
|
|
299 |
protected abstract Expression getExpression(); |
|
300 |
} |
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.swing/org.gvsig.expressionevaluator.swing.impl/src/main/java/org/gvsig/expressionevaluator/swing/impl/DefaultCalculatorController.java | ||
---|---|---|
182 | 182 |
} |
183 | 183 |
|
184 | 184 |
private Object doCalculate(boolean updateText) { |
185 |
if( !this.text.isEditable() ) { |
|
185 |
if( !this.text.isEditable() ||!this.text.isEnabled() ) {
|
|
186 | 186 |
return null; |
187 | 187 |
} |
188 | 188 |
String source = this.text.getText(); |
... | ... | |
231 | 231 |
|
232 | 232 |
@Override |
233 | 233 |
public T get() { |
234 |
Object value = doCalculate(false); |
|
234 |
Object value; |
|
235 |
if( !this.text.isEditable() ||!this.text.isEnabled() ) { |
|
236 |
value = this.text.getText(); |
|
237 |
} else { |
|
238 |
value = doCalculate(false); |
|
239 |
} |
|
235 | 240 |
try { |
236 | 241 |
DataTypesManager dataManager = ToolsLocator.getDataTypesManager(); |
237 | 242 |
value = dataManager.coerce(this.dataType, value); |
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.swing/org.gvsig.expressionevaluator.swing.impl/src/main/java/org/gvsig/expressionevaluator/swing/impl/DefaultJExpressionBuilder.java | ||
---|---|---|
1 | 1 |
package org.gvsig.expressionevaluator.swing.impl; |
2 | 2 |
|
3 | 3 |
import java.awt.BorderLayout; |
4 |
import java.awt.Color; |
|
5 | 4 |
import java.awt.Dimension; |
6 | 5 |
import java.awt.Toolkit; |
7 | 6 |
import java.awt.datatransfer.Clipboard; |
... | ... | |
32 | 31 |
import javax.swing.JTabbedPane; |
33 | 32 |
import javax.swing.JTree; |
34 | 33 |
import javax.swing.SwingUtilities; |
35 |
import javax.swing.Timer; |
|
36 | 34 |
import javax.swing.event.CaretEvent; |
37 | 35 |
import javax.swing.event.CaretListener; |
38 |
import javax.swing.event.DocumentEvent; |
|
39 |
import javax.swing.event.DocumentListener; |
|
40 | 36 |
import javax.swing.event.ListSelectionEvent; |
41 | 37 |
import javax.swing.event.ListSelectionListener; |
42 | 38 |
import javax.swing.event.TreeModelEvent; |
... | ... | |
50 | 46 |
import org.apache.commons.lang3.StringUtils; |
51 | 47 |
import org.gvsig.expressionevaluator.Expression; |
52 | 48 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
53 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
|
54 |
import org.gvsig.expressionevaluator.ExpressionRuntimeException; |
|
55 |
import org.gvsig.expressionevaluator.ExpressionSyntaxException; |
|
56 | 49 |
import org.gvsig.expressionevaluator.Function; |
57 |
import org.gvsig.expressionevaluator.MutableSymbolTable; |
|
58 | 50 |
import org.gvsig.expressionevaluator.SymbolTable; |
59 | 51 |
import org.gvsig.expressionevaluator.swing.Element; |
60 | 52 |
import org.gvsig.expressionevaluator.swing.Element.CalculatedElement; |
... | ... | |
75 | 67 |
import org.gvsig.tools.swing.api.pickercontroller.PickerController; |
76 | 68 |
import org.gvsig.tools.swing.api.script.ScriptSwingManager; |
77 | 69 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
78 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
|
79 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
|
80 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
|
81 | 70 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
82 | 71 |
|
83 | 72 |
/** |
... | ... | |
89 | 78 |
extends DefaultJExpressionBuilderView |
90 | 79 |
implements JExpressionBuilder { |
91 | 80 |
|
92 |
private static final Color COLOR_ERROR = new Color(255,225,232); |
|
93 |
|
|
94 | 81 |
private final ExpressionEvaluatorSwingManager manager; |
95 | 82 |
private GroupElement currentGroup; |
96 | 83 |
private PickerController<List<Script>> scriptPicker; |
... | ... | |
98 | 85 |
|
99 | 86 |
private final ExpressionBuilderConfig config; |
100 | 87 |
private ElementsTreeModel treeModel; |
101 |
private Timer timerExpression; |
|
102 | 88 |
private ImageIcon defaultIconGroup; |
103 | 89 |
private ImageIcon defaultIconElement; |
104 |
private Color color_ok; |
|
105 |
private String messageValue; |
|
106 | 90 |
private InfoPanel infoPanel; |
91 |
private AbstractAutomaticExpressionChecker automaticExpressionChecker; |
|
107 | 92 |
|
108 | 93 |
private static class InfoPanel implements Component { |
109 | 94 |
|
... | ... | |
278 | 263 |
} |
279 | 264 |
} |
280 | 265 |
} |
281 |
|
|
266 |
|
|
282 | 267 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
283 | 268 |
public DefaultJExpressionBuilder(ExpressionEvaluatorSwingManager manager, ExpressionBuilderConfig config) { |
284 | 269 |
this.manager = manager; |
... | ... | |
309 | 294 |
final IconTheme theme = ToolsSwingLocator.getIconThemeManager().getCurrent(); |
310 | 295 |
final I18nManager i18n = ToolsLocator.getI18nManager(); |
311 | 296 |
|
297 |
this.automaticExpressionChecker = new AbstractAutomaticExpressionChecker( |
|
298 |
config, txtExpression |
|
299 |
) { |
|
300 |
@Override |
|
301 |
protected Expression getExpression() { |
|
302 |
return DefaultJExpressionBuilder.this.getExpression(); |
|
303 |
} |
|
304 |
|
|
305 |
@Override |
|
306 |
protected void setMessage(int mode, String text) { |
|
307 |
super.setMessage(mode, text); |
|
308 |
lblMsg.setText("<html>"+StringUtils.abbreviate(text,100)+"</html>"); |
|
309 |
} |
|
310 |
|
|
311 |
@Override |
|
312 |
protected void setPreview(Object value) { |
|
313 |
super.setPreview(value); |
|
314 |
lblMsg.setText("<html>"+StringUtils.abbreviate(this.getPreview(),100)+"</html>"); |
|
315 |
} |
|
316 |
|
|
317 |
@Override |
|
318 |
protected void setTip(String theTip) { |
|
319 |
super.setTip(theTip); |
|
320 |
btnTip.setToolTipText(this.getTip()); |
|
321 |
btnTip.setVisible(this.isTipEnabled()); |
|
322 |
} |
|
323 |
|
|
324 |
}; |
|
312 | 325 |
this.infoPanel = new InfoPanel(this.pnlDescription); |
313 | 326 |
|
314 | 327 |
this.btnTip.setVisible(false); |
... | ... | |
327 | 340 |
this.defaultIconGroup = theme.get("expressionbuilder-element-group"); |
328 | 341 |
this.defaultIconElement = theme.get("expressionbuilder-element"); |
329 | 342 |
|
330 |
this.timerExpression = new Timer(3000, new ActionListener() { |
|
331 |
@Override |
|
332 |
public void actionPerformed(ActionEvent e) { |
|
333 |
doCheckExpression(); |
|
334 |
} |
|
335 |
}); |
|
336 |
this.timerExpression.setRepeats(false); |
|
337 |
|
|
338 | 343 |
toolsSwingManager.translate(this.tabExpressionBuilder); |
339 |
toolsSwingManager.setDefaultPopupMenu(this.txtExpression); |
|
340 | 344 |
toolsSwingManager.setDefaultPopupMenu(this.txtGroupElement); |
341 | 345 |
toolsSwingManager.setDefaultPopupMenu(this.txtSimpleElementFilter); |
342 |
toolsSwingManager.addClearButton(this.txtExpression); |
|
343 | 346 |
toolsSwingManager.addClearButton(this.txtSimpleElementFilter, new ActionListener() { |
344 | 347 |
@Override |
345 | 348 |
public void actionPerformed(ActionEvent e) { |
... | ... | |
351 | 354 |
msgMenuTextEditor.addActionListener(new ActionListener() { |
352 | 355 |
@Override |
353 | 356 |
public void actionPerformed(ActionEvent e) { |
354 |
JEditorPane text = new JEditorPane(); |
|
355 |
JScrollPane scroll = new JScrollPane(text); |
|
356 |
scroll.setPreferredSize(new Dimension(500, 300)); |
|
357 |
toolsSwingManager.setDefaultPopupMenu(text); |
|
358 |
text.setContentType("text/html"); |
|
359 |
text.setText("<html>"+messageValue+"</html>"); |
|
360 |
text.setCaretPosition(0); |
|
361 |
WindowManager_v2 winmgr = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
|
362 |
Dialog dialog = winmgr.createDialog( |
|
363 |
scroll, |
|
364 |
i18n.getTranslation("_Preview_value"), |
|
365 |
null, |
|
366 |
WindowManager_v2.BUTTONS_OK |
|
367 |
); |
|
368 |
dialog.show(WindowManager.MODE.DIALOG); |
|
357 |
automaticExpressionChecker.showMessageDialog(); |
|
369 | 358 |
} |
370 | 359 |
}); |
371 | 360 |
JMenuItem msgMenuCopy = new JMenuItem(i18n.getTranslation("copy")); |
372 | 361 |
msgMenuTextEditor.addActionListener(new ActionListener() { |
373 | 362 |
@Override |
374 | 363 |
public void actionPerformed(ActionEvent e) { |
375 |
StringSelection selection = new StringSelection(messageValue);
|
|
364 |
StringSelection selection = new StringSelection(automaticExpressionChecker.getMessage());
|
|
376 | 365 |
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); |
377 | 366 |
clipboard.setContents(selection, selection); |
378 | 367 |
} |
... | ... | |
386 | 375 |
|
387 | 376 |
this.pnlDescription.setLayout(new BorderLayout()); |
388 | 377 |
// this.pnlDescription.setBackground(this.txtDescription.getBackground()); |
389 |
this.color_ok = this.txtExpression.getBackground(); |
|
390 | 378 |
this.txtExpression.addCaretListener(new CaretListener() { |
391 | 379 |
@Override |
392 | 380 |
public void caretUpdate(CaretEvent e) { |
393 | 381 |
lblColumn.setText(Integer.toString(e.getDot())); |
394 | 382 |
} |
395 | 383 |
}); |
396 |
this.txtExpression.getDocument().addDocumentListener(new DocumentListener() { |
|
397 |
@Override |
|
398 |
public void insertUpdate(DocumentEvent e) { |
|
399 |
timerExpression.restart(); |
|
400 |
} |
|
401 |
|
|
402 |
@Override |
|
403 |
public void removeUpdate(DocumentEvent e) { |
|
404 |
timerExpression.restart(); |
|
405 |
} |
|
406 |
|
|
407 |
@Override |
|
408 |
public void changedUpdate(DocumentEvent e) { |
|
409 |
timerExpression.restart(); |
|
410 |
} |
|
411 |
}); |
|
412 |
this.txtExpression.addKeyListener(new KeyAdapter() { |
|
413 |
@Override |
|
414 |
public void keyPressed(KeyEvent e) { |
|
415 |
if( e.getKeyCode()==KeyEvent.VK_C && e.isControlDown() ) { |
|
416 |
doCheckExpression(); |
|
417 |
} |
|
418 |
} |
|
419 |
}); |
|
420 | 384 |
this.treeElements.setCellRenderer(new DefaultTreeCellRenderer() { |
421 | 385 |
@Override |
422 | 386 |
public java.awt.Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { |
... | ... | |
639 | 603 |
this.setPreferredSize(new Dimension(650, 350)); |
640 | 604 |
} |
641 | 605 |
|
642 |
private void doCheckExpression() { |
|
643 |
Expression expression = this.getExpression(); |
|
644 |
this.btnTip.setVisible(false); |
|
645 |
if( expression == null ) { |
|
646 |
this.setMessage(""); |
|
647 |
this.txtExpression.setBackground(this.color_ok); |
|
648 |
return; |
|
649 |
} |
|
650 |
try { |
|
651 |
expression.getCode(); |
|
652 |
this.setMessage(""); |
|
653 |
this.txtExpression.setBackground(this.color_ok); |
|
654 |
} catch(ExpressionSyntaxException ex) { |
|
655 |
this.setMessage(ex.getDescription()); |
|
656 |
this.txtExpression.setBackground(COLOR_ERROR); |
|
657 |
return; |
|
658 |
} catch(Exception ex) { |
|
659 |
this.setMessage("Syntax error in expression"); |
|
660 |
this.txtExpression.setBackground(COLOR_ERROR); |
|
661 |
return; |
|
662 |
} |
|
663 |
if( this.config.getPreviewSymbolTable()==null ) { |
|
664 |
try { |
|
665 |
ExpressionEvaluatorManager theManager = ExpressionEvaluatorLocator.getManager(); |
|
666 |
MutableSymbolTable symbolTable = theManager.createSymbolTable(); |
|
667 |
Object value = expression.execute(symbolTable); |
|
668 |
this.setMessage(Utils.toString(value)); |
|
669 |
this.txtExpression.setBackground(this.color_ok); |
|
670 |
} catch(Exception ex) { |
|
671 |
this.setMessage(""); |
|
672 |
} |
|
673 |
} else { |
|
674 |
try { |
|
675 |
Object value = expression.execute(this.config.getPreviewSymbolTable()); |
|
676 |
this.setMessage(Utils.toString(value)); |
|
677 |
this.txtExpression.setBackground(this.color_ok); |
|
678 |
} catch(ExpressionRuntimeException ex) { |
|
679 |
if( !StringUtils.isBlank(ex.getTip()) ) { |
|
680 |
this.btnTip.setToolTipText(ex.getTip()); |
|
681 |
this.btnTip.setVisible(true); |
|
682 |
} |
|
683 |
this.setMessage("Runtime error. "+ex.getMessage()); |
|
684 |
this.txtExpression.setBackground(COLOR_ERROR); |
|
685 |
} catch(Exception ex) { |
|
686 |
this.setMessage("Runtime error. "+ex.getMessage()); |
|
687 |
this.txtExpression.setBackground(COLOR_ERROR); |
|
688 |
} |
|
689 |
} |
|
690 |
} |
|
691 |
|
|
692 |
private void setMessage(String msg) { |
|
693 |
this.messageValue = msg; |
|
694 |
this.lblMsg.setText("<html>"+StringUtils.abbreviate(msg,100)+"</html>"); |
|
695 |
} |
|
696 |
|
|
697 | 606 |
private String removeCursorMark(String s) { |
698 | 607 |
s = s.replace("{{", ""); |
699 | 608 |
s = s.replace("}}", ""); |
... | ... | |
841 | 750 |
|
842 | 751 |
@Override |
843 | 752 |
public void setExpression(Expression expression) { |
844 |
this.txtExpression.setText(expression.getPhrase()); |
|
845 |
if( expression.getUserScript()!=null ) { |
|
846 |
this.scriptEditor.set(expression.getUserScript()); |
|
753 |
if( expression==null ) { |
|
754 |
this.txtExpression.setText(""); |
|
755 |
this.scriptEditor.set(null); |
|
756 |
this.scriptPicker.set(null); |
|
757 |
} else { |
|
758 |
this.txtExpression.setText(expression.getPhrase()); |
|
759 |
if( expression.getUserScript()!=null ) { |
|
760 |
this.scriptEditor.set(expression.getUserScript()); |
|
761 |
} |
|
762 |
if( expression.getScripts()!=null ) { |
|
763 |
this.scriptPicker.set(expression.getScripts().toList()); |
|
764 |
} |
|
847 | 765 |
} |
848 |
if( expression.getScripts()!=null ) { |
|
849 |
this.scriptPicker.set(expression.getScripts().toList()); |
|
850 |
} |
|
851 | 766 |
} |
852 | 767 |
|
853 | 768 |
@Override |
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.swing/org.gvsig.expressionevaluator.swing.impl/src/main/java/org/gvsig/expressionevaluator/swing/impl/DefaultExpressionPickerController.java | ||
---|---|---|
58 | 58 |
private final ExpressionEvaluatorSwingManager manager; |
59 | 59 |
private final ActionListener buttonAction; |
60 | 60 |
private final JMenuItem menuItem; |
61 |
private final AbstractAutomaticExpressionChecker automaticExpressionChecker; |
|
61 | 62 |
|
62 | 63 |
public DefaultExpressionPickerController(ExpressionEvaluatorSwingManager manager, JTextComponent text, JButton button) { |
63 | 64 |
ToolsSwingManager swingManager = ToolsSwingLocator.getToolsSwingManager(); |
... | ... | |
67 | 68 |
this.config = new DefaultExpressionBuilderConfig(manager); |
68 | 69 |
this.text = text; |
69 | 70 |
this.button = button; |
70 |
this.value = ExpressionEvaluatorLocator.getManager().createExpression();
|
|
71 |
this.value = null;
|
|
71 | 72 |
this.button.setIcon(this.getIcon("picker-expression")); |
72 | 73 |
this.buttonAction = new ActionListener() { |
73 | 74 |
@Override |
... | ... | |
83 | 84 |
this.menuItem = new JMenuItem(i18n.getTranslation("_Show_expression_builder")); |
84 | 85 |
this.menuItem.addActionListener(buttonAction); |
85 | 86 |
popupMenu.add(this.menuItem); |
87 |
|
|
88 |
this.automaticExpressionChecker = new AbstractAutomaticExpressionChecker(config, text) { |
|
89 |
@Override |
|
90 |
protected Expression getExpression() { |
|
91 |
return get(); |
|
92 |
} |
|
93 |
}; |
|
86 | 94 |
} |
87 | 95 |
|
88 |
@Override |
|
89 |
public void unbind() { |
|
90 |
this.button.removeActionListener(this.buttonAction); |
|
91 |
JPopupMenu popupMenu = this.text.getComponentPopupMenu(); |
|
92 |
popupMenu.remove(this.menuItem); |
|
93 |
} |
|
94 |
|
|
95 | 96 |
private void doSelectExpression() { |
96 |
this.value.setPhrase(this.text.getText());
|
|
97 |
Expression theValue = this.get();
|
|
97 | 98 |
|
98 | 99 |
DefaultJExpressionBuilder panel = new DefaultJExpressionBuilder(this.manager, this.config); |
99 |
panel.setExpression(this.value);
|
|
100 |
panel.setExpression(theValue);
|
|
100 | 101 |
|
101 | 102 |
WindowManager_v2 windowManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
102 | 103 |
Dialog dialog = windowManager.createDialog( |
... | ... | |
113 | 114 |
|
114 | 115 |
@Override |
115 | 116 |
public Expression get() { |
117 |
if( value == null ) { |
|
118 |
return null; |
|
119 |
} |
|
116 | 120 |
this.value.setPhrase(this.text.getText()); |
117 | 121 |
return this.value; |
118 | 122 |
} |
... | ... | |
121 | 125 |
public void set(Expression value) { |
122 | 126 |
if( value==null ) { |
123 | 127 |
this.text.setText(""); |
124 |
this.value.setPhrase("");
|
|
128 |
this.value = null;
|
|
125 | 129 |
} else { |
126 | 130 |
this.value = value; |
127 | 131 |
this.text.setText(this.value.getPhrase()); |
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.swing/org.gvsig.expressionevaluator.swing.impl/src/main/java/org/gvsig/expressionevaluator/swing/impl/elements/DefaultFunctionElement.java | ||
---|---|---|
5 | 5 |
*/ |
6 | 6 |
package org.gvsig.expressionevaluator.swing.impl.elements; |
7 | 7 |
|
8 |
import java.util.List; |
|
8 | 9 |
import org.apache.commons.lang3.StringUtils; |
9 | 10 |
import org.gvsig.expressionevaluator.Function; |
10 | 11 |
import org.gvsig.expressionevaluator.swing.Element.FunctionElement; |
... | ... | |
66 | 67 |
html.append("<b>").append(i18n.getTranslation("_Template")).append(":</b> ").append(this.getName()).append("()<br>\n"); |
67 | 68 |
} else { |
68 | 69 |
html.append("<b>").append(i18n.getTranslation("_Function")).append("</b> ").append(this.getName()).append("<br>\n<br>\n"); |
70 |
List<String> aliases = theFunction.aliases(); |
|
71 |
if( aliases!=null && !aliases.isEmpty() ) { |
|
72 |
html.append("<b>").append(i18n.getTranslation("_Aliases")).append(":</b> ").append("<ul>\n"); |
|
73 |
for (String alias : aliases) { |
|
74 |
html.append("<li>").append(alias).append("</li>\n"); |
|
75 |
} |
|
76 |
html.append("</ul>\n"); |
|
77 |
} |
|
69 | 78 |
html.append("<b>").append(i18n.getTranslation("_Return")).append("</b> "); |
70 | 79 |
html.append(StringUtils.isEmpty(theFunction.returnType()) ? "Objec" : theFunction.returnType()); |
71 | 80 |
html.append("<br>\n"); |
Also available in: Unified diff