1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package zeus.gui.fields;
25
26 import java.awt.Toolkit;
27 import java.awt.event.*;
28 import javax.swing.*;
29 import javax.swing.event.*;
30 import javax.swing.text.*;
31
32
33 public class LargeTextField extends JTextArea implements DocumentListener {
34
35 public LargeTextField() { super(); }
36 public LargeTextField(int rows, int cols) { super(rows,cols); }
37 public LargeTextField(String t) { super(t); }
38 public LargeTextField(String t, int rows, int cols) { super(t,rows,cols); }
39
40 protected Document createDefaultModel() {
41 Document doc = new LargeTextFieldDocument();
42 doc.addDocumentListener(this);
43 return doc;
44 }
45
46 public void insertUpdate(DocumentEvent e) {
47 fireChanged();
48 }
49 public void removeUpdate(DocumentEvent e) {
50 fireChanged();
51 }
52 public void changedUpdate(DocumentEvent e) {
53 fireChanged();
54 }
55
56 static char[] specialChars = { '\t', '\n', '\r' };
57
58 class LargeTextFieldDocument extends PlainDocument {
59 public void insertString(int offs, String str, AttributeSet a)
60 throws BadLocationException {
61
62 if ( str != null && str.length() == 1 &&
63 member(str.charAt(0),specialChars) ) {
64 fireActionPerformed();
65 return;
66 }
67 super.insertString(offs,str,a);
68 }
69 }
70 static boolean member(char item, char[] array) {
71 for(int i = 0; i < array.length; i++ )
72 if ( array[i] == item ) return true;
73 return false;
74 }
75
76 protected EventListenerList actionListeners = new EventListenerList();
77 protected String actionCommand = "";
78
79 public void addActionListener(ActionListener x) {
80 actionListeners.add(ActionListener.class, x);
81 }
82 public void removeActionListener(ActionListener x) {
83 actionListeners.remove(ActionListener.class, x);
84 }
85
86 public String getActionCommand() { return actionCommand; }
87 public void setActionCommand(String cmd) { actionCommand = cmd; }
88
89 protected void fireActionPerformed() {
90 ActionEvent evt =
91 new ActionEvent(this,ActionEvent.ACTION_PERFORMED,actionCommand);
92 Object[] listeners = actionListeners.getListenerList();
93 for(int i= listeners.length-2; i >= 0; i -=2) {
94 if (listeners[i] == ActionListener.class) {
95 ActionListener l = (ActionListener)listeners[i+1];
96 l.actionPerformed(evt);
97 }
98 }
99 }
100
101 protected EventListenerList changeListeners = new EventListenerList();
102 public void addChangeListener(ChangeListener x) {
103 changeListeners.add(ChangeListener.class, x);
104 }
105 public void removeChangeListener(ChangeListener x) {
106 changeListeners.remove(ChangeListener.class, x);
107 }
108
109 protected void fireChanged() {
110 ChangeEvent c = new ChangeEvent(this);
111 Object[] listeners = changeListeners.getListenerList();
112 for(int i= listeners.length-2; i >= 0; i -=2) {
113 if (listeners[i] == ChangeListener.class) {
114 ChangeListener cl = (ChangeListener)listeners[i+1];
115 cl.stateChanged(c);
116 }
117 }
118 }
119 }