1 33 34 package com.nqadmin.swingSet.formatting.helpers; 35 36 import java.sql.*; 37 import java.util.regex.Pattern ; 38 import java.util.regex.Matcher ; 39 import com.nqadmin.swingSet.datasources.*; 40 41 45 public class SelectorListModel extends javax.swing.AbstractListModel { 46 47 private java.util.ArrayList data = new java.util.ArrayList (); 48 49 50 53 private String dataColumn; 54 55 58 private java.beans.PropertyChangeSupport propertyChangeSupport; 59 60 63 private String listColumn; 64 65 68 private String table; 69 70 73 private String selectText; 74 75 private String orderBy; 76 77 80 private SSConnection ssConnection; 81 private SSJdbcRowSetImpl ssRowset; 82 83 84 85 public SelectorListModel() { 86 this(null, null, null, null); 87 } 88 89 public SelectorListModel(String table, String bcolumn, String lcolumn) { 90 this(table, bcolumn, lcolumn, null); 91 } 92 93 public SelectorListModel(String table, String bcolumn, String lcolumn, String orderBy) { 94 this(null, table, bcolumn, lcolumn, orderBy); 95 } 96 97 public SelectorListModel(SSConnection ssConnection, String table, String bcolumn, String lcolumn, String orderBy) { 98 super(); 99 propertyChangeSupport = new java.beans.PropertyChangeSupport (this); 100 setSsConnection(ssConnection); 101 setTable(table); 102 setDataColumn(bcolumn); 103 setListColumn(lcolumn); 104 setOrderBy(orderBy); 105 106 } 108 109 public void refresh() { 110 System.out.println("---------------------- refresh() ---------------------"); 111 data = new java.util.ArrayList (); 112 this.populateModel(); 113 } 114 115 public Object getSelectedBoundData(int index) { 116 Object itm = data.get(index); 117 118 if (itm != null) { 119 return ((SelectorElement)(itm)).getDataValue(); 120 } else { 121 return "<null>"; 122 } 123 } 124 125 private void populateModel() { 126 127 String sql = null; 128 129 131 133 if (dataColumn == null || listColumn == null || table == null) { 134 System.out.println("dataColumn = " + dataColumn); 135 System.out.println("listColumn = " + listColumn); 136 System.out.println("table = " + table); 137 System.out.println("Sample Model"); 138 data.add(new SelectorElement(new String ("0") , "Option 0")); 139 data.add(new SelectorElement(new String ("1") , "Option 1")); 140 data.add(new SelectorElement(new String ("2") , "Option 2")); 141 data.add(new SelectorElement(new String ("0") , "Option 3")); 142 data.add(new SelectorElement(new String ("1") , "Option 4")); 143 data.add(new SelectorElement(new String ("2") , "Option 5")); 144 data.add(new SelectorElement(new String ("0") , "Option 6")); 145 data.add(new SelectorElement(new String ("1") , "Option 7")); 146 data.add(new SelectorElement(new String ("2") , "Option 8")); 147 data.add(new SelectorElement(new String ("0") , "Option 9")); 148 data.add(new SelectorElement(new String ("1") , "Option 0")); 149 data.add(new SelectorElement(new String ("2") , "Option 1")); 150 data.add(new SelectorElement(new String ("0") , "Option 2")); 151 data.add(new SelectorElement(new String ("1") , "Option 3")); 152 data.add(new SelectorElement(new String ("2") , "Option 4")); 153 return; 154 } 155 156 System.out.println(dataColumn + " " + listColumn + " " + table); 157 System.out.println("-----------------------------------------------------------"); 158 159 if (orderBy != null) { 160 sql = "select " + dataColumn + ", " + listColumn + " from " + table + " ORDER BY " + orderBy; 161 } 162 else 163 sql = "select " + dataColumn + ", " + listColumn + " from " + table; 164 165 167 ssRowset = new SSJdbcRowSetImpl(); 168 ssRowset.setSSConnection(ssConnection); 169 170 171 173 ssRowset.setCommand(sql); 174 175 try { 176 ssRowset.execute(); 177 ssRowset.last(); 178 System.out.println("Hay " + ssRowset.getRow() + " registros"); 179 } catch (SQLException se) { 180 System.out.println("sql = " + sql); 181 System.out.println("ssRowset.execute() " + se); 182 } 183 184 186 try { 187 ssRowset.beforeFirst(); 188 while (ssRowset.next()) { 189 String s1 = ssRowset.getString(1); 190 String s2 = ssRowset.getString(2); 191 data.add(new SelectorElement(s1,s2)); 193 } 194 } catch (SQLException se) { 195 System.out.println("ssRowset.next()" + se); 196 } catch (java.lang.NullPointerException np) { 197 System.out.println(np); 198 } 199 200 ssRowset = null; 201 ssConnection = null; 202 } 203 204 public void setSelectedItem(String bdata) { 205 SelectorElement cual; 206 String tofind; 207 208 tofind = bdata.toUpperCase().trim(); 209 210 System.out.println("setSelectedItem = " + tofind); 211 212 for (int i=0; i < data.size(); i++) { 213 cual = (SelectorElement)(data.get(i)); 214 215 System.out.println("BoundData = '" + cual.getDataValue() + "'"); 216 217 if ((cual.getDataValue()).equals(bdata)) { 218 return; 220 } 221 } 222 System.out.println("parece que no encontro a '" + bdata +"'"); 223 } 224 225 229 public void addPropertyChangeListener(java.beans.PropertyChangeListener l) { 230 propertyChangeSupport.addPropertyChangeListener(l); 231 } 232 233 237 public void removePropertyChangeListener(java.beans.PropertyChangeListener l) { 238 propertyChangeSupport.removePropertyChangeListener(l); 239 } 240 241 245 public String getDataColumn() { 246 return this.dataColumn; 247 } 248 249 253 public void setDataColumn(String dataColumn) { 254 String oldDataColumn = this.dataColumn; 255 this.dataColumn = dataColumn; 256 try { 257 propertyChangeSupport.firePropertyChange("dataColumn", oldDataColumn, dataColumn); 258 } catch(java.lang.NullPointerException npe) { 259 260 } 261 } 263 264 268 public String getListColumn() { 269 270 return this.listColumn; 271 } 272 273 277 public void setListColumn(String listColumn) { 278 String oldListColumn = this.listColumn; 279 this.listColumn = listColumn; 280 try { 281 propertyChangeSupport.firePropertyChange("listColumn", oldListColumn, listColumn); 282 } catch(java.lang.NullPointerException npe) { 283 284 } 285 287 } 288 289 293 public String getTable() { 294 return this.table; 295 } 296 297 301 public void setTable(String table) { 302 303 String oldTable = this.table; 304 this.table = table; 305 306 try { 307 propertyChangeSupport.firePropertyChange("table", oldTable, table); 308 } catch(java.lang.NullPointerException npe) { 309 310 } 311 } 313 314 318 public void setOrderBy(String orderBy) { 319 320 String oldorderBy = this.orderBy; 321 this.orderBy = orderBy; 322 323 try { 324 propertyChangeSupport.firePropertyChange("orderBy", oldorderBy, orderBy); 325 } catch(java.lang.NullPointerException npe) { 326 327 } 328 } 330 331 public String getOrderBy() { 332 return orderBy; 333 } 334 335 339 public String getSelectText() { 340 341 return this.selectText; 342 } 343 344 348 public void setSelectText(String selectText) { 349 350 String oldSelectText = this.selectText; 351 this.selectText = selectText; 352 try { 353 propertyChangeSupport.firePropertyChange("selectText", oldSelectText, selectText); 354 } catch (java.lang.NullPointerException npe) { 355 356 } 357 } 359 360 public void execute() { 361 refresh(); 362 } 363 364 368 public com.nqadmin.swingSet.datasources.SSConnection getSsConnection() { 369 370 return this.ssConnection; 371 } 372 373 377 public void setSsConnection(com.nqadmin.swingSet.datasources.SSConnection ssConnection) 378 379 { 380 try { 381 com.nqadmin.swingSet.datasources.SSConnection oldSsConnection = this.ssConnection; 382 this.ssConnection = ssConnection; 383 propertyChangeSupport.firePropertyChange("ssConnection", oldSsConnection, ssConnection); 384 } catch(java.lang.NullPointerException nop) { 385 386 } 387 } 388 389 public Object getElementAt(int index) { 390 return data.get(index); 391 } 392 393 public int getSize() { 394 return data.size(); 395 } 396 } 397 | Popular Tags |