KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nqadmin > swingSet > utils > SSSyncManager


1 /* $Id: SSSyncManager.java,v 1.6 2005/02/22 16:09:46 prasanth Exp $
2  *
3  * Tab Spacing = 4
4  *
5  * Copyright (c) 2005, The Pangburn Company and Prasanth R. Pasala.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * Redistributions of source code must retain the above copyright notice, this
12  * list of conditions and the following disclaimer. Redistributions in binary
13  * form must reproduce the above copyright notice, this list of conditions and
14  * the following disclaimer in the documentation and/or other materials
15  * provided with the distribution. The names of its contributors may not be
16  * used to endorse or promote products derived from this software without
17  * specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  *
31  */

32
33  package com.nqadmin.swingSet.utils;
34
35  import com.nqadmin.swingSet.datasources.*;
36  import java.sql.SQLException JavaDoc;
37  import com.nqadmin.swingSet.SSDBComboBox;
38  import com.nqadmin.swingSet.SSDataNavigator;
39  import javax.sql.RowSetListener JavaDoc;
40  import java.awt.event.ActionListener JavaDoc;
41  import java.awt.event.ActionEvent JavaDoc;
42  import javax.sql.RowSetEvent JavaDoc;
43
44 /**
45  * SSSyncManager.java
46  *<p>
47  * SwingSet - Open Toolkit For Making Swing Controls Database-Aware
48  *<p><pre>
49  * Used to synchronize a data navigator and a navigation combo box.
50  *</pre><p>
51  * @author $Author: prasanth $
52  * @version $Revision: 1.6 $
53  */

54 public class SSSyncManager {
55
56     /**
57      * SSDBComboBox used for record navigation.
58      */

59     protected SSDBComboBox comboBox;
60
61     /**
62      * SSDataNavigator to be synchronized with navigation combo box.
63      */

64     protected SSDataNavigator dataNavigator;
65
66     /**
67      * SSRowSet navigated with data navigator and combo box.
68      */

69     protected SSRowSet rowset;
70
71     /**
72      * SSRowSet column used as basis for synchronization.
73      */

74     protected String JavaDoc columnName;
75
76     /**
77      * Listener on combo box to detect combo-based navigations.
78      */

79     private final MyComboListener comboListener = new MyComboListener();
80
81     /**
82      * Listener on SSRowSet to detect data navigator-based navigations.
83      */

84     private final MyRowSetListener rowsetListener = new MyRowSetListener();
85
86     /**
87
88      * Creates a SSSyncManager with the specified combo box and data navigator.
89      *
90      * @param _comboBox SSDBComboBox used for record navigation
91      * @param _dataNavigator SSDataNavigator to be synchronized
92      * with navigation combo box
93      */

94     public SSSyncManager(SSDBComboBox _comboBox, SSDataNavigator _dataNavigator) {
95         comboBox = _comboBox;
96         dataNavigator = _dataNavigator;
97         rowset = dataNavigator.getSSRowSet();
98     }
99
100     /**
101      * Sets column to be used as basis for synchronization.
102      *
103      * @param _columnName SSRowSet column used as basis for synchronization.
104      */

105     public void setColumnName(String JavaDoc _columnName) {
106         columnName = _columnName;
107     }
108
109     /**
110      * Sets data navigator to be synchronized.
111      *
112      * @param _dataNavigator data navigator to be synchronized
113      */

114     public void setDataNavigator(SSDataNavigator _dataNavigator) {
115         dataNavigator = _dataNavigator;
116         rowset = dataNavigator.getSSRowSet();
117     }
118
119     /**
120      * Sets combo box to be synchronized.
121      *
122      * @param _comboBox combo box to be synchronized
123      */

124
125     public void setComboBox(SSDBComboBox _comboBox) {
126         comboBox = _comboBox;
127     }
128
129     /**
130      * Start synchronization between navigation components.
131      */

132     public void sync() {
133         addListeners();
134         adjustValue();
135     }
136
137     /**
138      * Stop synchronization between navigation components.
139      */

140     public void async() {
141         removeListeners();
142     }
143
144     /**
145      * Adds listeners to combo box & rowset.
146      */

147     private void addListeners() {
148         comboBox.addActionListener(comboListener);
149         dataNavigator.getSSRowSet().addRowSetListener(rowsetListener);
150     }
151
152     /**
153      * Removes listeners from combo box & rowset.
154      */

155     private void removeListeners() {
156         comboBox.removeActionListener(comboListener);
157         dataNavigator.getSSRowSet().removeRowSetListener(rowsetListener);
158     }
159
160     /**
161      * Listener for rowset.
162      */

163     private class MyRowSetListener implements RowSetListener JavaDoc {
164
165         public void cursorMoved(RowSetEvent JavaDoc rse) {
166             adjustValue();
167         }
168
169         public void rowChanged(RowSetEvent JavaDoc rse) {
170             adjustValue();
171         }
172
173         public void rowSetChanged(RowSetEvent JavaDoc rse) {
174             adjustValue();
175         }
176
177     }
178
179     /**
180
181      * Method to update combo box based on rowset.
182      */

183     protected void adjustValue() {
184         comboBox.removeActionListener(comboListener);
185         try{
186             if(rowset != null & rowset.getRow() > 0){
187             // GET THE CURRENT VALUE FROM THE ROWSET.
188
String JavaDoc currentRowValue = rowset.getString(columnName);
189             // CHECK IF THE COMBO BOX IS DISPLAYING THE SAME ONE.
190
if(comboBox.getSelectedStringValue() == null || !comboBox.getSelectedStringValue().equals(currentRowValue)){
191                 // IF NOT CHANGE THE SELECTION OF THE COMBO BOX.
192
comboBox.setSelectedStringValue(rowset.getString(columnName));
193                 }
194             } else {
195                 comboBox.setSelectedIndex(-1);
196             }
197         } catch(SQLException JavaDoc se) {
198             se.printStackTrace();
199         }
200         comboBox.addActionListener(comboListener);
201     }
202
203
204     /**
205      *Listener for combo box to update data navigator when
206      * combo box-based navigation occurs.
207      */

208     private class MyComboListener implements ActionListener JavaDoc {
209         //protected long id = -1;
210
protected String JavaDoc id = "";
211
212     // WHEN THERE IS A CHANGE IN THIS VALUE MOVE THE ROWSET SO THAT
213
// ITS POSITIONED AT THE RIGHT RECORD.
214
public void actionPerformed(ActionEvent JavaDoc ae) {
215             try {
216
217                 if(rowset == null || rowset.getRow() < 1 || comboBox.getSelectedIndex() == -1){
218                     return;
219                 }
220
221                 //id = comboBox.getSelectedValue();
222
id = comboBox.getSelectedStringValue();
223                 rowset.removeRowSetListener(rowsetListener);
224
225             // UPDATE THE PRESENT ROW BEFORE MOVING TO ANOTHER ROW.
226
dataNavigator.updatePresentRow();
227
228                 //if(id != rowset.getLong(columnName)) {
229
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 != rowset.getLong(columnName)) {
236
while (!id.equals(rowset.getString(columnName))) {
237                         if (!rowset.next()) {
238                             rowset.beforeFirst();
239                             rowset.next();
240                         }
241
242                         count++;
243
244                         //number of items in combo is the number of records in resultset.
245
//so if for some reason item is in combo but deleted in rowset
246
//To avoid infinite loop in such scenario
247
if (count > numRecords + 5) {
248                             //JOptionPane.showInternalMessageDialog(this,"Record deleted. Info the admin about this","Row not found",JOptionPane.OK_OPTION);
249
break;
250                         }
251                     }
252                 }
253                 rowset.addRowSetListener(rowsetListener);
254
255             } catch(SQLException JavaDoc se) {
256                 se.printStackTrace();
257             }
258         }
259     } // private class MyComboListener implements ActionListener {
260

261 } // end public class SSSyncManager {
262

263 /*
264  * $Log: SSSyncManager.java,v $
265  * Revision 1.6 2005/02/22 16:09:46 prasanth
266  * In adjustValue while checking combo selection make sure the underlying value returned
267  * is not null. If the selected item is -1 the getSelectedStringValue will return null.
268  *
269  * Revision 1.5 2005/02/10 16:51:23 prasanth
270  * On rowset events checking if the combo box is displaying the right value
271  * or not. Changing the selection only if it is not displaying the right one.
272  *
273  * Revision 1.4 2005/02/10 03:36:08 yoda2
274  * JavaDoc cleanup and updated to support string columns used for synchronization (to match recent changes in SSDBComboBox).
275  *
276  * Revision 1.3 2005/02/05 15:06:56 yoda2
277  * Got rid of depreciated calls.
278  *
279  * Revision 1.2 2005/02/04 22:49:15 yoda2
280  * API cleanup & updated Copyright info.
281  *
282  * Revision 1.1 2005/01/03 19:53:43 prasanth
283  * Initial Commit.
284  *
285  */
Popular Tags