KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jorphan > gui > JLabeledTextArea


1 // $Header: /home/cvs/jakarta-jmeter/src/jorphan/org/apache/jorphan/gui/JLabeledTextArea.java,v 1.6 2004/02/11 23:48:32 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jorphan.gui;
20
21 import java.awt.BorderLayout JavaDoc;
22 import java.awt.event.FocusEvent JavaDoc;
23 import java.awt.event.FocusListener JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.LinkedList JavaDoc;
26 import java.util.List JavaDoc;
27
28 import javax.swing.JLabel JavaDoc;
29 import javax.swing.JPanel JavaDoc;
30 import javax.swing.JScrollPane JavaDoc;
31 import javax.swing.JTextArea JavaDoc;
32 import javax.swing.event.ChangeEvent JavaDoc;
33 import javax.swing.event.ChangeListener JavaDoc;
34 import javax.swing.text.Document JavaDoc;
35
36 /**
37  * A Helper component that wraps a JTextField with a label into a JPanel
38  * (this). This component also has an efficient event handling mechanism for
39  * handling the text changing in the Text Field. The registered change
40  * listeners are only called when the text has changed.
41  *
42  * @author S.Coleman
43  * @version $Revision: 1.6 $
44  */

45 public class JLabeledTextArea
46     extends JPanel JavaDoc
47     implements JLabeledField, FocusListener JavaDoc
48 {
49     private JLabel JavaDoc mLabel;
50     private JTextArea JavaDoc mTextArea;
51
52     // Maybe move to vector if MT problems occur
53
private ArrayList JavaDoc mChangeListeners = new ArrayList JavaDoc(3);
54
55     // A temporary cache for the focus listener
56
private String JavaDoc oldValue = "";
57     
58     /**
59      * Default constructor, The label and the Text field are left empty.
60      */

61     public JLabeledTextArea()
62     {
63         this("", null);
64     }
65
66     /**
67      * Constructs a new component with the label displaying the
68      * passed text.
69      *
70      * @param pLabel The text to in the label.
71      */

72     public JLabeledTextArea(String JavaDoc pLabel, Document JavaDoc docModel)
73     {
74         super();
75         mLabel = new JLabel JavaDoc(pLabel);
76         if (docModel != null)
77         {
78             setDocumentModel(docModel);
79         }
80         init();
81     }
82
83     public List JavaDoc getComponentList()
84     {
85         List JavaDoc comps = new LinkedList JavaDoc();
86         comps.add(mLabel);
87         comps.add(mTextArea);
88         return comps;
89     }
90
91     public void setDocumentModel(Document JavaDoc docModel)
92     {
93         mTextArea.setDocument(docModel);
94     }
95
96     /**
97      * Initialises all of the components on this panel.
98      */

99     private void init()
100     {
101         setLayout(new BorderLayout JavaDoc());
102
103         mTextArea = new JTextArea JavaDoc();
104         mTextArea.setRows(4);
105         mTextArea.setLineWrap(true);
106         mTextArea.setWrapStyleWord(true);
107         // Register the handler for focus listening. This handler will
108
// only notify the registered when the text changes from when
109
// the focus is gained to when it is lost.
110
mTextArea.addFocusListener(this);
111
112         // Add the sub components
113
this.add(mLabel, BorderLayout.NORTH);
114         this.add(new JScrollPane JavaDoc(mTextArea), BorderLayout.CENTER);
115     }
116
117     /**
118      * Callback method when the focus to the Text Field component
119      * is lost.
120      *
121      * @param pFocusEvent The focus event that occured.
122      */

123     public void focusLost(FocusEvent JavaDoc pFocusEvent)
124     {
125          // Compare if the value has changed, since we received focus.
126
if (!oldValue.equals(mTextArea.getText()))
127          {
128               notifyChangeListeners();
129          }
130     }
131     
132     /**
133      * Catch what the value was when focus was gained.
134      */

135     public void focusGained(FocusEvent JavaDoc pFocusEvent)
136     {
137          oldValue = mTextArea.getText();
138     }
139
140     /**
141      * Set the text displayed in the label.
142      *
143      * @param pLabel The new label text.
144      */

145     public void setLabel(String JavaDoc pLabel)
146     {
147         mLabel.setText(pLabel);
148     }
149
150     /**
151      * Set the text displayed in the Text Field.
152      *
153      * @param pText The new text to display in the text field.
154      */

155     public void setText(String JavaDoc pText)
156     {
157         mTextArea.setText(pText);
158     }
159
160     /**
161      * Returns the text in the Text Field.
162      *
163      * @return The text in the Text Field.
164      */

165     public String JavaDoc getText()
166     {
167         return mTextArea.getText();
168     }
169
170     /**
171      * Returns the text of the label.
172      *
173      * @return The text of the label.
174      */

175     public String JavaDoc getLabel()
176     {
177         return mLabel.getText();
178     }
179
180     /**
181      * Adds a change listener, that will be notified when the text in the
182      * text field is changed. The ChangeEvent that will be passed
183      * to registered listeners will contain this object as the source, allowing
184      * the new text to be extracted using the {@link #getText() getText} method.
185      *
186      * @param pChangeListener The listener to add
187      */

188     public void addChangeListener(ChangeListener JavaDoc pChangeListener)
189     {
190         mChangeListeners.add(pChangeListener);
191     }
192
193     /**
194      * Removes a change listener.
195      *
196      * @param pChangeListener The change listener to remove.
197      */

198     public void removeChangeListener(ChangeListener JavaDoc pChangeListener)
199     {
200         mChangeListeners.remove(pChangeListener);
201     }
202
203     /**
204      * Notify all registered change listeners that the
205      * text in the text field has changed.
206      */

207     private void notifyChangeListeners()
208     {
209         ChangeEvent JavaDoc ce = new ChangeEvent JavaDoc(this);
210         for (int index = 0; index < mChangeListeners.size(); index++)
211         {
212             ((ChangeListener JavaDoc) mChangeListeners.get(index)).stateChanged(ce);
213         }
214     }
215 }
Popular Tags