001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jorphan.gui;
020:
021: import java.awt.BorderLayout;
022: import java.awt.event.FocusEvent;
023: import java.awt.event.FocusListener;
024: import java.util.ArrayList;
025: import java.util.LinkedList;
026: import java.util.List;
027:
028: import javax.swing.JLabel;
029: import javax.swing.JPanel;
030: import javax.swing.JScrollPane;
031: import javax.swing.JTextArea;
032: import javax.swing.event.ChangeEvent;
033: import javax.swing.event.ChangeListener;
034: import javax.swing.text.Document;
035:
036: /**
037: * A Helper component that wraps a JTextField with a label into a JPanel (this).
038: * This component also has an efficient event handling mechanism for handling
039: * the text changing in the Text Field. The registered change listeners are only
040: * called when the text has changed.
041: *
042: */
043: public class JLabeledTextArea extends JPanel implements JLabeledField,
044: FocusListener {
045: private JLabel mLabel;
046:
047: private JTextArea mTextArea;
048:
049: // Maybe move to vector if MT problems occur
050: private ArrayList mChangeListeners = new ArrayList(3);
051:
052: // A temporary cache for the focus listener
053: private String oldValue = "";
054:
055: /**
056: * Default constructor, The label and the Text field are left empty.
057: */
058: public JLabeledTextArea() {
059: this ("", null);
060: }
061:
062: /**
063: * Constructs a new component with the label displaying the passed text.
064: *
065: * @param label
066: * The text to display in the label.
067: */
068: public JLabeledTextArea(String label) {
069: this (label, null);
070: }
071:
072: /**
073: * Constructs a new component with the label displaying the passed text.
074: *
075: * @param pLabel
076: * The text to display in the label.
077: * @param docModel the document for the text area
078: */
079: public JLabeledTextArea(String pLabel, Document docModel) {
080: super ();
081: mLabel = new JLabel(pLabel);
082: init();
083: if (docModel != null) {
084: mTextArea.setDocument(docModel);
085: }
086: }
087:
088: public List getComponentList() {
089: List comps = new LinkedList();
090: comps.add(mLabel);
091: comps.add(mTextArea);
092: return comps;
093: }
094:
095: public void setDocumentModel(Document docModel) {
096: mTextArea.setDocument(docModel);
097: }
098:
099: /**
100: * Initialises all of the components on this panel.
101: */
102: private void init() {
103: setLayout(new BorderLayout());
104:
105: mTextArea = new JTextArea();
106: mTextArea.setRows(4);
107: mTextArea.setLineWrap(true);
108: mTextArea.setWrapStyleWord(true);
109: // Register the handler for focus listening. This handler will
110: // only notify the registered when the text changes from when
111: // the focus is gained to when it is lost.
112: mTextArea.addFocusListener(this );
113:
114: // Add the sub components
115: this .add(mLabel, BorderLayout.NORTH);
116: this .add(new JScrollPane(mTextArea), BorderLayout.CENTER);
117: }
118:
119: /**
120: * Callback method when the focus to the Text Field component is lost.
121: *
122: * @param pFocusEvent
123: * The focus event that occured.
124: */
125: public void focusLost(FocusEvent pFocusEvent) {
126: // Compare if the value has changed, since we received focus.
127: if (!oldValue.equals(mTextArea.getText())) {
128: notifyChangeListeners();
129: }
130: }
131:
132: /**
133: * Catch what the value was when focus was gained.
134: */
135: public void focusGained(FocusEvent pFocusEvent) {
136: oldValue = mTextArea.getText();
137: }
138:
139: /**
140: * Set the text displayed in the label.
141: *
142: * @param pLabel
143: * The new label text.
144: */
145: public void setLabel(String pLabel) {
146: mLabel.setText(pLabel);
147: }
148:
149: /**
150: * Set the text displayed in the Text Field.
151: *
152: * @param pText
153: * The new text to display in the text field.
154: */
155: public void setText(String pText) {
156: mTextArea.setText(pText);
157: }
158:
159: /**
160: * Returns the text in the Text Field.
161: *
162: * @return The text in the Text Field.
163: */
164: public String getText() {
165: return mTextArea.getText();
166: }
167:
168: /**
169: * Returns the text of the label.
170: *
171: * @return The text of the label.
172: */
173: public String getLabel() {
174: return mLabel.getText();
175: }
176:
177: public void setEnabled(boolean enable) {
178: super .setEnabled(enable);
179: mTextArea.setEnabled(enable);
180: }
181:
182: /**
183: * Adds a change listener, that will be notified when the text in the text
184: * field is changed. The ChangeEvent that will be passed to registered
185: * listeners will contain this object as the source, allowing the new text
186: * to be extracted using the {@link #getText() getText} method.
187: *
188: * @param pChangeListener
189: * The listener to add
190: */
191: public void addChangeListener(ChangeListener pChangeListener) {
192: mChangeListeners.add(pChangeListener);
193: }
194:
195: /**
196: * Removes a change listener.
197: *
198: * @param pChangeListener
199: * The change listener to remove.
200: */
201: public void removeChangeListener(ChangeListener pChangeListener) {
202: mChangeListeners.remove(pChangeListener);
203: }
204:
205: /**
206: * Notify all registered change listeners that the text in the text field
207: * has changed.
208: */
209: private void notifyChangeListeners() {
210: ChangeEvent ce = new ChangeEvent(this );
211: for (int index = 0; index < mChangeListeners.size(); index++) {
212: ((ChangeListener) mChangeListeners.get(index))
213: .stateChanged(ce);
214: }
215: }
216: }
|