1 32 33 package com.nqadmin.swingSet.utils; 34 35 import com.nqadmin.swingSet.datasources.*; 36 import java.sql.SQLException ; 37 import com.nqadmin.swingSet.SSDBComboBox; 38 import com.nqadmin.swingSet.SSDataNavigator; 39 import javax.sql.RowSetListener ; 40 import java.awt.event.ActionListener ; 41 import java.awt.event.ActionEvent ; 42 import javax.sql.RowSetEvent ; 43 44 54 public class SSSyncManager { 55 56 59 protected SSDBComboBox comboBox; 60 61 64 protected SSDataNavigator dataNavigator; 65 66 69 protected SSRowSet rowset; 70 71 74 protected String columnName; 75 76 79 private final MyComboListener comboListener = new MyComboListener(); 80 81 84 private final MyRowSetListener rowsetListener = new MyRowSetListener(); 85 86 94 public SSSyncManager(SSDBComboBox _comboBox, SSDataNavigator _dataNavigator) { 95 comboBox = _comboBox; 96 dataNavigator = _dataNavigator; 97 rowset = dataNavigator.getSSRowSet(); 98 } 99 100 105 public void setColumnName(String _columnName) { 106 columnName = _columnName; 107 } 108 109 114 public void setDataNavigator(SSDataNavigator _dataNavigator) { 115 dataNavigator = _dataNavigator; 116 rowset = dataNavigator.getSSRowSet(); 117 } 118 119 124 125 public void setComboBox(SSDBComboBox _comboBox) { 126 comboBox = _comboBox; 127 } 128 129 132 public void sync() { 133 addListeners(); 134 adjustValue(); 135 } 136 137 140 public void async() { 141 removeListeners(); 142 } 143 144 147 private void addListeners() { 148 comboBox.addActionListener(comboListener); 149 dataNavigator.getSSRowSet().addRowSetListener(rowsetListener); 150 } 151 152 155 private void removeListeners() { 156 comboBox.removeActionListener(comboListener); 157 dataNavigator.getSSRowSet().removeRowSetListener(rowsetListener); 158 } 159 160 163 private class MyRowSetListener implements RowSetListener { 164 165 public void cursorMoved(RowSetEvent rse) { 166 adjustValue(); 167 } 168 169 public void rowChanged(RowSetEvent rse) { 170 adjustValue(); 171 } 172 173 public void rowSetChanged(RowSetEvent rse) { 174 adjustValue(); 175 } 176 177 } 178 179 183 protected void adjustValue() { 184 comboBox.removeActionListener(comboListener); 185 try{ 186 if(rowset != null & rowset.getRow() > 0){ 187 String currentRowValue = rowset.getString(columnName); 189 if(comboBox.getSelectedStringValue() == null || !comboBox.getSelectedStringValue().equals(currentRowValue)){ 191 comboBox.setSelectedStringValue(rowset.getString(columnName)); 193 } 194 } else { 195 comboBox.setSelectedIndex(-1); 196 } 197 } catch(SQLException se) { 198 se.printStackTrace(); 199 } 200 comboBox.addActionListener(comboListener); 201 } 202 203 204 208 private class MyComboListener implements ActionListener { 209 protected String id = ""; 211 212 public void actionPerformed(ActionEvent ae) { 215 try { 216 217 if(rowset == null || rowset.getRow() < 1 || comboBox.getSelectedIndex() == -1){ 218 return; 219 } 220 221 id = comboBox.getSelectedStringValue(); 223 rowset.removeRowSetListener(rowsetListener); 224 225 dataNavigator.updatePresentRow(); 227 228 if(!id.equals(rowset.getString(columnName))) { 230 231 int index = comboBox.getSelectedIndex() + 1; 232 rowset.absolute(index); 233 int numRecords = comboBox.getItemCount(); 234 int count = 0; 235 while (!id.equals(rowset.getString(columnName))) { 237 if (!rowset.next()) { 238 rowset.beforeFirst(); 239 rowset.next(); 240 } 241 242 count++; 243 244 if (count > numRecords + 5) { 248 break; 250 } 251 } 252 } 253 rowset.addRowSetListener(rowsetListener); 254 255 } catch(SQLException se) { 256 se.printStackTrace(); 257 } 258 } 259 } 261 } 263 | Popular Tags |