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 45 55 public class SSSlider extends JSlider { 56 57 60 protected JTextField textField = new JTextField(); 61 62 65 protected SSRowSet sSRowSet; 66 67 70 protected String columnName = ""; 71 72 75 protected int columnType; 76 77 80 private final MySliderListener sliderListener = new MySliderListener(); 81 82 85 private final MyTextFieldDocumentListener textFieldDocumentListener = new MyTextFieldDocumentListener(); 86 87 91 public SSSlider() { 92 init(); 93 } 94 95 100 public SSSlider(int _orientation) { 101 super(_orientation); 102 init(); 103 } 104 105 111 public SSSlider(int _min, int _max) { 112 super(_min, _max); 113 init(); 114 } 115 116 123 public SSSlider(SSRowSet _sSRowSet, String _columnName) throws java.sql.SQLException { 124 sSRowSet = _sSRowSet; 125 columnName = _columnName; 126 init(); 127 bind(); 128 } 129 130 136 public void setColumnName(String _columnName) throws java.sql.SQLException { 137 String oldValue = columnName; 138 columnName = _columnName; 139 firePropertyChange("columnName", oldValue, columnName); 140 bind(); 141 } 142 143 148 public String getColumnName() { 149 return columnName; 150 } 151 152 157 public void setSSRowSet(SSRowSet _sSRowSet) throws java.sql.SQLException { 158 SSRowSet oldValue = sSRowSet; 159 sSRowSet = _sSRowSet; 160 firePropertyChange("sSRowSet", oldValue, sSRowSet); 161 bind(); 162 } 163 164 169 public SSRowSet getSSRowSet() { 170 return sSRowSet; 171 } 172 173 179 public void bind(SSRowSet _sSRowSet, String _columnName) throws java.sql.SQLException { 180 SSRowSet oldValue = sSRowSet; 181 sSRowSet = _sSRowSet; 182 firePropertyChange("sSRowSet", oldValue, sSRowSet); 183 184 String oldValue2 = columnName; 185 columnName = _columnName; 186 firePropertyChange("columnName", oldValue2, columnName); 187 188 bind(); 189 } 190 191 194 protected void init() { 195 196 setPreferredSize(new Dimension(200,20)); 198 } 199 200 203 protected void bind() throws java.sql.SQLException { 204 205 if (columnName==null || columnName.trim().equals("") || sSRowSet==null) { 207 return; 208 } 209 210 removeListeners(); 212 213 columnType = sSRowSet.getColumnType(columnName); 215 216 textField.setDocument(new SSTextDocument(sSRowSet, columnName)); 218 219 updateDisplay(); 221 222 addListeners(); 224 225 } 226 227 231 protected void updateDisplay() { 232 233 switch(columnType) { 235 case java.sql.Types.INTEGER: 236 case java.sql.Types.SMALLINT: 237 case java.sql.Types.TINYINT: 238 case java.sql.Types.BIGINT: 239 case java.sql.Types.FLOAT: 240 case java.sql.Types.DOUBLE: 241 case java.sql.Types.NUMERIC: 242 setValue(Integer.parseInt(textField.getText())); 244 break; 245 246 default: 247 break; 248 } 249 250 } 252 255 private void addListeners() { 256 textField.getDocument().addDocumentListener(textFieldDocumentListener); 257 addChangeListener(sliderListener); 258 } 259 260 263 private void removeListeners() { 264 textField.getDocument().removeDocumentListener(textFieldDocumentListener); 265 removeChangeListener(sliderListener); 266 } 267 268 272 private class MyTextFieldDocumentListener implements DocumentListener, Serializable { 273 274 public void changedUpdate(DocumentEvent de) { 275 removeChangeListener(sliderListener); 276 277 updateDisplay(); 278 279 addChangeListener(sliderListener); 280 } 281 282 public void insertUpdate(DocumentEvent de) { 285 removeChangeListener(sliderListener); 286 287 updateDisplay(); 288 289 addChangeListener(sliderListener); 290 } 291 292 public void removeUpdate(DocumentEvent de) { 295 removeChangeListener(sliderListener); 296 297 updateDisplay(); 298 299 addChangeListener(sliderListener); 300 } 301 302 } 304 308 private class MySliderListener implements ChangeListener, Serializable { 309 310 public void stateChanged(ChangeEvent ce) { 311 textField.getDocument().removeDocumentListener(textFieldDocumentListener); 312 313 textField.setText(String.valueOf(getValue())); 314 315 textField.getDocument().addDocumentListener(textFieldDocumentListener); 316 } 317 318 } 320 } 322 323 324 372 | Popular Tags |