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/DefaultJExpressionBuilder.java
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 |
Also available in: Unified diff