1 18 19 package org.apache.jorphan.gui; 20 21 import java.awt.BorderLayout ; 22 import java.awt.event.FocusEvent ; 23 import java.awt.event.FocusListener ; 24 import java.util.ArrayList ; 25 import java.util.LinkedList ; 26 import java.util.List ; 27 28 import javax.swing.JLabel ; 29 import javax.swing.JPanel ; 30 import javax.swing.JScrollPane ; 31 import javax.swing.JTextArea ; 32 import javax.swing.event.ChangeEvent ; 33 import javax.swing.event.ChangeListener ; 34 import javax.swing.text.Document ; 35 36 45 public class JLabeledTextArea 46 extends JPanel 47 implements JLabeledField, FocusListener 48 { 49 private JLabel mLabel; 50 private JTextArea mTextArea; 51 52 private ArrayList mChangeListeners = new ArrayList (3); 54 55 private String oldValue = ""; 57 58 61 public JLabeledTextArea() 62 { 63 this("", null); 64 } 65 66 72 public JLabeledTextArea(String pLabel, Document docModel) 73 { 74 super(); 75 mLabel = new JLabel (pLabel); 76 if (docModel != null) 77 { 78 setDocumentModel(docModel); 79 } 80 init(); 81 } 82 83 public List getComponentList() 84 { 85 List comps = new LinkedList (); 86 comps.add(mLabel); 87 comps.add(mTextArea); 88 return comps; 89 } 90 91 public void setDocumentModel(Document docModel) 92 { 93 mTextArea.setDocument(docModel); 94 } 95 96 99 private void init() 100 { 101 setLayout(new BorderLayout ()); 102 103 mTextArea = new JTextArea (); 104 mTextArea.setRows(4); 105 mTextArea.setLineWrap(true); 106 mTextArea.setWrapStyleWord(true); 107 mTextArea.addFocusListener(this); 111 112 this.add(mLabel, BorderLayout.NORTH); 114 this.add(new JScrollPane (mTextArea), BorderLayout.CENTER); 115 } 116 117 123 public void focusLost(FocusEvent pFocusEvent) 124 { 125 if (!oldValue.equals(mTextArea.getText())) 127 { 128 notifyChangeListeners(); 129 } 130 } 131 132 135 public void focusGained(FocusEvent pFocusEvent) 136 { 137 oldValue = mTextArea.getText(); 138 } 139 140 145 public void setLabel(String pLabel) 146 { 147 mLabel.setText(pLabel); 148 } 149 150 155 public void setText(String pText) 156 { 157 mTextArea.setText(pText); 158 } 159 160 165 public String getText() 166 { 167 return mTextArea.getText(); 168 } 169 170 175 public String getLabel() 176 { 177 return mLabel.getText(); 178 } 179 180 188 public void addChangeListener(ChangeListener pChangeListener) 189 { 190 mChangeListeners.add(pChangeListener); 191 } 192 193 198 public void removeChangeListener(ChangeListener pChangeListener) 199 { 200 mChangeListeners.remove(pChangeListener); 201 } 202 203 207 private void notifyChangeListeners() 208 { 209 ChangeEvent ce = new ChangeEvent (this); 210 for (int index = 0; index < mChangeListeners.size(); index++) 211 { 212 ((ChangeListener ) mChangeListeners.get(index)).stateChanged(ce); 213 } 214 } 215 } | Popular Tags |