1 32 33 package com.nqadmin.swingSet; 34 35 import java.io.*; 36 import java.sql.*; 37 import com.nqadmin.swingSet.datasources.SSRowSet; 38 import java.awt.*; 39 import java.awt.event.*; 40 import javax.swing.*; 41 import javax.swing.border.*; 42 import javax.swing.text.*; 43 import javax.swing.event.*; 44 import java.beans.PropertyChangeListener ; 45 import java.beans.PropertyChangeEvent ; 46 47 57 public class SSLabel extends JLabel { 58 59 62 protected JTextField textField = new JTextField(); 63 64 67 protected SSRowSet sSRowSet; 68 69 72 protected String columnName = ""; 73 74 77 private final MyLabelTextListener labelTextListener = new MyLabelTextListener(); 78 79 82 private final MyTextFieldDocumentListener textFieldDocumentListener = new MyTextFieldDocumentListener(); 83 84 88 public SSLabel() { 89 super("<label text here>"); 90 init(); 91 } 92 93 98 public SSLabel(Icon _image) { 99 super(_image); 100 init(); 101 } 102 103 109 public SSLabel(Icon _image, int _horizontalAlignment) { 110 super(_image, _horizontalAlignment); 111 init(); 112 } 113 114 121 public SSLabel(SSRowSet _sSRowSet, String _columnName) { 122 sSRowSet = _sSRowSet; 123 columnName = _columnName; 124 init(); 125 bind(); 126 } 127 128 134 public void setColumnName(String _columnName) { 135 String oldValue = columnName; 136 columnName = _columnName; 137 firePropertyChange("columnName", oldValue, columnName); 138 bind(); 139 } 140 141 146 public String getColumnName() { 147 return columnName; 148 } 149 150 155 public void setSSRowSet(SSRowSet _sSRowSet) { 156 SSRowSet oldValue = sSRowSet; 157 sSRowSet = _sSRowSet; 158 firePropertyChange("sSRowSet", oldValue, sSRowSet); 159 bind(); 160 } 161 162 167 public SSRowSet getSSRowSet() { 168 return sSRowSet; 169 } 170 171 177 public void bind(SSRowSet _sSRowSet, String _columnName) { 178 SSRowSet oldValue = sSRowSet; 179 sSRowSet = _sSRowSet; 180 firePropertyChange("sSRowSet", oldValue, sSRowSet); 181 182 String oldValue2 = columnName; 183 columnName = _columnName; 184 firePropertyChange("columnName", oldValue2, columnName); 185 186 bind(); 187 } 188 189 192 protected void init() { 193 194 setPreferredSize(new Dimension(200,20)); 196 } 197 198 201 protected void bind() { 202 203 if (columnName==null || columnName.trim().equals("") || sSRowSet==null) { 205 return; 206 } 207 208 removeListeners(); 210 211 textField.setDocument(new SSTextDocument(sSRowSet, columnName)); 213 214 updateDisplay(); 216 217 addListeners();; 219 220 } 221 222 226 protected void updateDisplay() { 227 228 setText(textField.getText()); 230 231 } 233 236 private void addListeners() { 237 textField.getDocument().addDocumentListener(textFieldDocumentListener); 238 addPropertyChangeListener("text", labelTextListener); 239 } 240 241 244 private void removeListeners() { 245 textField.getDocument().removeDocumentListener(textFieldDocumentListener); 246 removePropertyChangeListener("text", labelTextListener); 247 } 248 249 253 private class MyTextFieldDocumentListener implements DocumentListener, Serializable { 254 public void changedUpdate(DocumentEvent de) { 255 removePropertyChangeListener("text", labelTextListener); 256 257 updateDisplay(); 258 259 addPropertyChangeListener("text", labelTextListener); 260 } 261 262 public void insertUpdate(DocumentEvent de) { 265 removePropertyChangeListener("text", labelTextListener); 266 267 updateDisplay(); 268 269 addPropertyChangeListener("text", labelTextListener); 270 } 271 272 public void removeUpdate(DocumentEvent de) { 275 removePropertyChangeListener("text", labelTextListener); 276 277 updateDisplay(); 278 279 addPropertyChangeListener("text", labelTextListener); 280 } 281 } 283 287 private class MyLabelTextListener implements PropertyChangeListener , Serializable { 288 public void propertyChange(PropertyChangeEvent pce) { 289 textField.getDocument().removeDocumentListener(textFieldDocumentListener); 290 291 textField.setText(getText()); 292 293 textField.getDocument().addDocumentListener(textFieldDocumentListener); 294 } 295 296 } 298 } 300 301 302 353 | Popular Tags |