1 33 34 package com.nqadmin.swingSet.formatting.helpers; 35 36 import java.sql.*; 37 import com.nqadmin.swingSet.datasources.*; 38 39 43 public class SelectorComboBoxModel extends javax.swing.DefaultComboBoxModel { 44 45 48 private String dataColumn; 49 50 53 private java.beans.PropertyChangeSupport propertyChangeSupport; 54 55 58 private String listColumn; 59 60 63 private String table; 64 65 68 private String selectText; 69 70 private String orderBy; 71 72 75 private SSConnection ssConnection; 76 private SSJdbcRowSetImpl ssRowset; 77 78 79 80 public SelectorComboBoxModel() { 81 this(null, null, null, null); 82 } 83 84 public SelectorComboBoxModel(String table, String bcolumn, String lcolumn) { 85 this(table, bcolumn, lcolumn, null); 86 } 87 88 public SelectorComboBoxModel(String table, String bcolumn, String lcolumn, String orderBy) { 89 this(null, table, bcolumn, lcolumn, orderBy); 90 } 91 92 public SelectorComboBoxModel(SSConnection ssConnection, String table, String bcolumn, String lcolumn, String orderBy) { 93 super(); 94 propertyChangeSupport = new java.beans.PropertyChangeSupport (this); 95 setSsConnection(ssConnection); 96 setTable(table); 97 setDataColumn(bcolumn); 98 setListColumn(lcolumn); 99 setOrderBy(orderBy); 100 101 } 103 104 public void refresh() { 105 System.out.println("---------------------- refresh() ---------------------"); 106 this.populateModel(); 107 } 108 109 public Object getSelectedBoundData(int index) { 110 return ((SelectorElement)this.getElementAt(index)).getDataValue(); 111 } 112 113 private void populateModel() { 114 115 String sql = null; 116 117 System.out.println("populateModel();"); 118 119 121 if (dataColumn == null || listColumn == null || table == null) { 122 System.out.println("dataColumn = " + dataColumn); 123 System.out.println("listColumn = " + listColumn); 124 System.out.println("table = " + table); 125 System.out.println("Sample Model"); 126 this.addElement(new SelectorElement(new String ("0") , "Option 0")); 127 this.addElement(new SelectorElement(new String ("1") , "Option 1")); 128 this.addElement(new SelectorElement(new String ("2") , "Option 2")); 129 this.addElement(new SelectorElement(new String ("0") , "Option 3")); 130 this.addElement(new SelectorElement(new String ("1") , "Option 4")); 131 this.addElement(new SelectorElement(new String ("2") , "Option 5")); 132 this.addElement(new SelectorElement(new String ("0") , "Option 6")); 133 this.addElement(new SelectorElement(new String ("1") , "Option 7")); 134 this.addElement(new SelectorElement(new String ("2") , "Option 8")); 135 this.addElement(new SelectorElement(new String ("0") , "Option 9")); 136 return; 137 } 138 139 System.out.println(dataColumn + " " + listColumn + " " + table); 140 System.out.println("-----------------------------------------------------------"); 141 142 if (orderBy != null) { 143 sql = "select " + dataColumn + ", " + listColumn + " from " + table + " ORDER BY " + orderBy; 144 } 145 else 146 sql = "select " + dataColumn + ", " + listColumn + " from " + table; 147 148 System.out.println("sql1 = " + sql); 149 150 ssRowset = new SSJdbcRowSetImpl(); 151 ssRowset.setSSConnection(ssConnection); 152 153 154 System.out.println("sql2 = " + sql); 155 156 ssRowset.setCommand(sql); 157 158 try { 159 ssRowset.execute(); 160 ssRowset.last(); 161 System.out.println("Hay " + ssRowset.getRow() + " registros"); 162 } catch (SQLException se) { 163 System.out.println("sql = " + sql); 164 System.out.println("ssRowset.execute() " + se); 165 } 166 167 169 try { 170 ssRowset.beforeFirst(); 171 while (ssRowset.next()) { 172 String s1 = ssRowset.getString(1); 173 String s2 = ssRowset.getString(2); 174 System.out.println(s2); 175 this.addElement(new SelectorElement(s1,s2)); 176 } 177 } catch (SQLException se) { 178 System.out.println("ssRowset.next()" + se); 179 } catch (java.lang.NullPointerException np) { 180 System.out.println(np); 181 } 182 183 ssRowset = null; 184 ssConnection = null; 185 } 186 187 public void setSelectedItem(String bdata) { 188 SelectorElement cual; 189 String tofind; 190 191 tofind = bdata.toUpperCase().trim(); 192 193 System.out.println("setSelectedItem = " + tofind); 194 195 for (int i=0; i < this.getSize(); i++) { 196 cual = (SelectorElement)(this.getElementAt(i)); 197 198 System.out.println("BoundData = '" + cual.getDataValue() + "'"); 199 200 if ((cual.getDataValue()).equals(bdata)) { 201 return; 203 } 204 } 205 System.out.println("parece que no encontro a '" + bdata +"'"); 206 } 207 208 212 public void addPropertyChangeListener(java.beans.PropertyChangeListener l) { 213 propertyChangeSupport.addPropertyChangeListener(l); 214 } 215 216 220 public void removePropertyChangeListener(java.beans.PropertyChangeListener l) { 221 propertyChangeSupport.removePropertyChangeListener(l); 222 } 223 224 228 public String getDataColumn() { 229 return this.dataColumn; 230 } 231 232 236 public void setDataColumn(String dataColumn) { 237 String oldDataColumn = this.dataColumn; 238 this.dataColumn = dataColumn; 239 try { 240 propertyChangeSupport.firePropertyChange("dataColumn", oldDataColumn, dataColumn); 241 } catch(java.lang.NullPointerException npe) { 242 243 } 244 } 246 247 251 public String getListColumn() { 252 253 return this.listColumn; 254 } 255 256 260 public void setListColumn(String listColumn) { 261 String oldListColumn = this.listColumn; 262 this.listColumn = listColumn; 263 try { 264 propertyChangeSupport.firePropertyChange("listColumn", oldListColumn, listColumn); 265 } catch(java.lang.NullPointerException npe) { 266 267 } 268 270 } 271 272 276 public String getTable() { 277 return this.table; 278 } 279 280 284 public void setTable(String table) { 285 286 String oldTable = this.table; 287 this.table = table; 288 289 try { 290 propertyChangeSupport.firePropertyChange("table", oldTable, table); 291 } catch(java.lang.NullPointerException npe) { 292 293 } 294 } 296 297 301 public void setOrderBy(String orderBy) { 302 303 String oldorderBy = this.orderBy; 304 this.orderBy = orderBy; 305 306 try { 307 propertyChangeSupport.firePropertyChange("orderBy", oldorderBy, orderBy); 308 } catch(java.lang.NullPointerException npe) { 309 310 } 311 } 313 314 public String getOrderBy() { 315 return orderBy; 316 } 317 318 322 public String getSelectText() { 323 324 return this.selectText; 325 } 326 327 331 public void setSelectText(String selectText) { 332 333 String oldSelectText = this.selectText; 334 this.selectText = selectText; 335 try { 336 propertyChangeSupport.firePropertyChange("selectText", oldSelectText, selectText); 337 } catch (java.lang.NullPointerException npe) { 338 339 } 340 } 342 343 public void execute() { 344 refresh(); 345 } 346 347 351 public com.nqadmin.swingSet.datasources.SSConnection getSsConnection() { 352 353 return this.ssConnection; 354 } 355 356 360 public void setSsConnection(com.nqadmin.swingSet.datasources.SSConnection ssConnection) 361 362 { 363 try { 364 com.nqadmin.swingSet.datasources.SSConnection oldSsConnection = this.ssConnection; 365 this.ssConnection = ssConnection; 366 propertyChangeSupport.firePropertyChange("ssConnection", oldSsConnection, ssConnection); 367 } catch(java.lang.NullPointerException nop) { 368 369 } 370 } 371 } 372 | Popular Tags |