1
|
|
package abbot.editor.widgets;
|
2
|
|
|
3
|
|
import java.awt.*;
|
4
|
|
import java.awt.event.*;
|
5
|
|
import javax.swing.*;
|
6
|
|
import javax.swing.event.*;
|
7
|
|
import java.util.*;
|
8
|
|
|
9
|
|
import abbot.Log;
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
public class TextArea extends JTextArea {
|
17
|
|
|
18
|
|
public static final String ACTION_FOCUS_LOST = "focus-lost";
|
19
|
|
public static final String ACTION_TEXT_CHANGED = "text-changed";
|
20
|
|
public static final String ACTION_TEXT_INSERTED = "text-changed";
|
21
|
|
public static final String ACTION_TEXT_REMOVED = "text-changed";
|
22
|
|
|
23
|
|
private boolean continuousFire = true;
|
24
|
|
private boolean fieldChanging = false;
|
25
|
|
private ArrayList listeners = new ArrayList();
|
26
|
1
|
public TextArea(String value) {
|
27
|
1
|
super(value);
|
28
|
1
|
addFocusListener(new java.awt.event.FocusAdapter() {
|
29
|
1
|
public void focusLost(java.awt.event.FocusEvent ev) {
|
30
|
1
|
if (!ev.isTemporary() && !isLocalMenuActive()) {
|
31
|
0
|
Log.debug("Firing on focus loss");
|
32
|
0
|
fireActionPerformed(ACTION_FOCUS_LOST);
|
33
|
|
}
|
34
|
|
}
|
35
|
|
});
|
36
|
1
|
getDocument().addDocumentListener(new DocumentListener() {
|
37
|
0
|
public void changedUpdate(DocumentEvent ev) {
|
38
|
0
|
if (!fieldChanging && continuousFire)
|
39
|
0
|
fireActionPerformed(ACTION_TEXT_CHANGED);
|
40
|
|
}
|
41
|
5
|
public void insertUpdate(DocumentEvent ev) {
|
42
|
5
|
if (!fieldChanging && continuousFire)
|
43
|
5
|
fireActionPerformed(ACTION_TEXT_INSERTED);
|
44
|
|
}
|
45
|
1
|
public void removeUpdate(DocumentEvent ev) {
|
46
|
1
|
if (!fieldChanging && continuousFire)
|
47
|
1
|
fireActionPerformed(ACTION_TEXT_REMOVED);
|
48
|
|
}
|
49
|
|
});
|
50
|
|
}
|
51
|
|
|
52
|
|
|
53
|
|
|
54
|
1
|
public void setText(String text) {
|
55
|
1
|
fieldChanging = true;
|
56
|
1
|
super.setText(text);
|
57
|
1
|
fieldChanging = false;
|
58
|
|
}
|
59
|
|
|
60
|
|
|
61
|
0
|
private boolean isLocalMenuActive() {
|
62
|
0
|
boolean active = false;
|
63
|
0
|
Window window = SwingUtilities.getWindowAncestor(TextArea.this);
|
64
|
0
|
while (window != null && !active) {
|
65
|
0
|
window = SwingUtilities.getWindowAncestor(window);
|
66
|
0
|
if (window instanceof JFrame) {
|
67
|
0
|
Component comp = window.getFocusOwner();
|
68
|
0
|
Log.debug("Focus is in " + abbot.tester.Robot.toString(comp));
|
69
|
0
|
active = comp != null && (comp instanceof JMenuItem);
|
70
|
|
}
|
71
|
|
}
|
72
|
0
|
return active;
|
73
|
|
}
|
74
|
|
|
75
|
6
|
protected void fireActionPerformed(String actionCommand) {
|
76
|
6
|
fireActionPerformed(actionCommand, false);
|
77
|
|
}
|
78
|
|
|
79
|
0
|
protected void fireActionPerformed() {
|
80
|
0
|
fireActionPerformed(getText(), true);
|
81
|
|
}
|
82
|
|
|
83
|
|
|
84
|
6
|
protected void fireActionPerformed(String cmd, boolean select) {
|
85
|
6
|
if (select)
|
86
|
0
|
selectAll();
|
87
|
6
|
ActionEvent e =
|
88
|
|
new ActionEvent(this, ActionEvent.ACTION_PERFORMED, cmd);
|
89
|
6
|
Iterator iter = listeners.iterator();
|
90
|
6
|
while (iter.hasNext()) {
|
91
|
6
|
((ActionListener)iter.next()).actionPerformed(e);
|
92
|
|
}
|
93
|
|
}
|
94
|
|
|
95
|
1
|
public void addActionListener(ActionListener l) {
|
96
|
1
|
listeners.add(l);
|
97
|
|
}
|
98
|
|
|
99
|
0
|
public void removeActionListener(ActionListener l) {
|
100
|
0
|
listeners.remove(l);
|
101
|
|
}
|
102
|
|
}
|
103
|
|
|