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.Color;
023: import java.awt.event.FocusEvent;
024: import java.awt.event.FocusListener;
025: import java.util.ArrayList;
026: import java.util.LinkedList;
027: import java.util.List;
028:
029: import javax.swing.JLabel;
030: import javax.swing.JPanel;
031: import javax.swing.JTextField;
032: import javax.swing.event.ChangeEvent;
033: import javax.swing.event.ChangeListener;
034:
035: /**
036: * A Helper component that wraps a JTextField with a label into a JPanel (this).
037: * This component also has an efficient event handling mechanism for handling
038: * the text changing in the Text Field. The registered change listeners are only
039: * called when the text has changed.
040: *
041: * @author S.Coleman
042: * @version $Revision: 493784 $
043: */
044: public class JLabeledTextField extends JPanel implements JLabeledField,
045: FocusListener {
046: private JLabel mLabel;
047:
048: private JTextField mTextField;
049:
050: // Maybe move to vector if MT problems occur
051: private ArrayList mChangeListeners = new ArrayList(3);
052:
053: // A temporary cache for the focus listener
054: private String oldValue = "";
055:
056: /**
057: * Default constructor, The label and the Text field are left empty.
058: */
059: public JLabeledTextField() {
060: this ("", 20);
061: }
062:
063: /**
064: * Constructs a new component with the label displaying the passed text.
065: *
066: * @param pLabel
067: * The text to in the label.
068: */
069: public JLabeledTextField(String pLabel) {
070: this (pLabel, 20);
071: }
072:
073: public JLabeledTextField(String pLabel, int size) {
074: super ();
075: mTextField = createTextField(size);
076: mLabel = new JLabel(pLabel);
077: mLabel.setLabelFor(mTextField);
078: init();
079: }
080:
081: public JLabeledTextField(String pLabel, Color bk) {
082: super ();
083: mTextField = createTextField(20);
084: mLabel = new JLabel(pLabel);
085: mLabel.setBackground(bk);
086: mLabel.setLabelFor(mTextField);
087: this .setBackground(bk);
088: init();
089: }
090:
091: public List getComponentList() {
092: List comps = new LinkedList();
093: comps.add(mLabel);
094: comps.add(mTextField);
095: return comps;
096: }
097:
098: public void setEnabled(boolean enable) {
099: super .setEnabled(enable);
100: mTextField.setEnabled(enable);
101: }
102:
103: protected JTextField createTextField(int size) {
104: return new JTextField(size);
105: }
106:
107: /**
108: * Initialises all of the components on this panel.
109: */
110: private void init() {
111: setLayout(new BorderLayout(5, 0));
112: // Register the handler for focus listening. This handler will
113: // only notify the registered when the text changes from when
114: // the focus is gained to when it is lost.
115: mTextField.addFocusListener(this );
116:
117: // Add the sub components
118: add(mLabel, BorderLayout.WEST);
119: add(mTextField, BorderLayout.CENTER);
120: }
121:
122: /**
123: * Callback method when the focus to the Text Field component is lost.
124: *
125: * @param pFocusEvent
126: * The focus event that occured.
127: */
128: public void focusLost(FocusEvent pFocusEvent) {
129: // Compare if the value has changed, since we received focus.
130: if (!oldValue.equals(mTextField.getText())) {
131: notifyChangeListeners();
132: }
133: }
134:
135: /**
136: * Catch what the value was when focus was gained.
137: */
138: public void focusGained(FocusEvent pFocusEvent) {
139: oldValue = mTextField.getText();
140: }
141:
142: /**
143: * Set the text displayed in the label.
144: *
145: * @param pLabel
146: * The new label text.
147: */
148: public void setLabel(String pLabel) {
149: mLabel.setText(pLabel);
150: }
151:
152: /**
153: * Set the text displayed in the Text Field.
154: *
155: * @param pText
156: * The new text to display in the text field.
157: */
158: public void setText(String pText) {
159: mTextField.setText(pText);
160: }
161:
162: /**
163: * Returns the text in the Text Field.
164: *
165: * @return The text in the Text Field.
166: */
167: public String getText() {
168: return mTextField.getText();
169: }
170:
171: /**
172: * Returns the text of the label.
173: *
174: * @return The text of the label.
175: */
176: public String getLabel() {
177: return mLabel.getText();
178: }
179:
180: /**
181: * Adds a change listener, that will be notified when the text in the text
182: * field is changed. The ChangeEvent that will be passed to registered
183: * listeners will contain this object as the source, allowing the new text
184: * to be extracted using the {@link #getText() getText} method.
185: *
186: * @param pChangeListener
187: * The listener to add
188: */
189: public void addChangeListener(ChangeListener pChangeListener) {
190: mChangeListeners.add(pChangeListener);
191: }
192:
193: /**
194: * Removes a change listener.
195: *
196: * @param pChangeListener
197: * The change listener to remove.
198: */
199: public void removeChangeListener(ChangeListener pChangeListener) {
200: mChangeListeners.remove(pChangeListener);
201: }
202:
203: /**
204: * Notify all registered change listeners that the text in the text field
205: * has changed.
206: */
207: protected void notifyChangeListeners() {
208: ChangeEvent ce = new ChangeEvent(this );
209: for (int index = 0; index < mChangeListeners.size(); index++) {
210: ((ChangeListener) mChangeListeners.get(index))
211: .stateChanged(ce);
212: }
213: }
214: }
|