01: package abbot.editor.editors;
02:
03: import java.awt.*;
04: import java.awt.event.*;
05: import javax.swing.JTextArea;
06: import javax.swing.JButton;
07: import abbot.i18n.Strings;
08: import abbot.script.Expression;
09:
10: /** A Expression only has its description available for editing. */
11:
12: public class ExpressionEditor extends StepEditor {
13:
14: private Expression step;
15: private JTextArea expr;
16: private JButton run;
17: private Color DEFAULT_FG;
18: private JTextArea status;
19:
20: public ExpressionEditor(Expression step) {
21: super (step);
22: this .step = step;
23: remove(getComponentCount() - 1);
24: expr = addTextArea(null, step.getExpression());
25: expr.setName("expression.text");
26: expr.setColumns(80);
27: expr.setToolTipText(Strings.get("expression.text.tip"));
28: expr.setLineWrap(false);
29: run = addButton(Strings.get("expression.eval"));
30: run.setToolTipText(Strings.get("expression.eval.tip"));
31: DEFAULT_FG = expr.getForeground();
32: expr.addKeyListener(new KeyAdapter() {
33: public void keyReleased(KeyEvent e) {
34: if (e.getKeyCode() == KeyEvent.VK_ENTER
35: && (e.getModifiers() & InputEvent.SHIFT_MASK) != 0) {
36: run.doClick();
37: }
38: }
39: });
40: // TODO: properly format for readability
41: status = new JTextArea();
42: status.setBorder(null);
43: status.setForeground(Color.red);
44: status.setBackground(getBackground());
45: status.setLineWrap(true);
46: status.setEditable(false);
47: add(status);
48: }
49:
50: public Dimension getPreferredSize() {
51: Insets insets = getInsets();
52: Dimension size = super .getPreferredSize();
53: size.width = expr.getPreferredSize().width;
54: if (insets != null) {
55: size.width += insets.left + insets.right;
56: }
57: return size;
58: }
59:
60: public void actionPerformed(ActionEvent ev) {
61: if (ev.getSource() == expr) {
62: expr.setForeground(DEFAULT_FG);
63: expr.setToolTipText(Strings.get("expression.text.tip"));
64: step.setExpression(expr.getText());
65: status.setText("");
66: fireStepChanged();
67: } else if (ev.getSource() == run) {
68: expr.setForeground(DEFAULT_FG);
69: expr.setToolTipText(Strings.get("expression.text.tip"));
70: status.setText("");
71: new Thread("expression runner") {
72: public void run() {
73: try {
74: step.run();
75: } catch (Throwable e) {
76: if (e instanceof bsh.EvalError)
77: expr.setForeground(Color.red);
78: status.setText(e.getMessage());
79: }
80: fireStepChanged();
81: }
82: }.start();
83: } else {
84: super.actionPerformed(ev);
85: }
86: }
87: }
|