View Javadoc

1   /*
2   * The contents of this file are subject to the BT "ZEUS" Open Source 
3   * Licence (L77741), Version 1.0 (the "Licence"); you may not use this file 
4   * except in compliance with the Licence. You may obtain a copy of the Licence
5   * from $ZEUS_INSTALL/licence.html or alternatively from
6   * http://www.labs.bt.com/projects/agents/zeus/licence.htm
7   * 
8   * Except as stated in Clause 7 of the Licence, software distributed under the
9   * Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or 
10  * implied. See the Licence for the specific language governing rights and 
11  * limitations under the Licence.
12  * 
13  * The Original Code is within the package zeus.*.
14  * The Initial Developer of the Original Code is British Telecommunications
15  * public limited company, whose registered office is at 81 Newgate Street, 
16  * London, EC1A 7AJ, England. Portions created by British Telecommunications 
17  * public limited company are Copyright 1996-9. All Rights Reserved.
18  * 
19  * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
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 }