KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nqadmin > swingSet > formatting > helpers > SelectorComboBoxModel


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

33
34 package com.nqadmin.swingSet.formatting.helpers;
35
36 import java.sql.*;
37 import com.nqadmin.swingSet.datasources.*;
38
39 /**
40  *
41  * @author dags
42  */

43 public class SelectorComboBoxModel extends javax.swing.DefaultComboBoxModel JavaDoc {
44     
45     /**
46      * Holds value of property dataColumn.
47      */

48     private String JavaDoc dataColumn;
49     
50     /**
51      * Utility field used by bound properties.
52      */

53     private java.beans.PropertyChangeSupport JavaDoc propertyChangeSupport;
54     
55     /**
56      * Holds value of property listColumn.
57      */

58     private String JavaDoc listColumn;
59     
60     /**
61      * Holds value of property table.
62      */

63     private String JavaDoc table;
64     
65     /**
66      * Holds value of property selectText.
67      */

68     private String JavaDoc selectText;
69     
70     private String JavaDoc orderBy;
71     
72     /**
73      * Holds value of property ssConnection.
74      */

75     private SSConnection ssConnection;
76     private SSJdbcRowSetImpl ssRowset;
77     
78     /** Creates a new instance of AccountSelectorModel */
79     
80     public SelectorComboBoxModel() {
81         this(null, null, null, null);
82     }
83     
84     public SelectorComboBoxModel(String JavaDoc table, String JavaDoc bcolumn, String JavaDoc lcolumn) {
85         this(table, bcolumn, lcolumn, null);
86     }
87     
88     public SelectorComboBoxModel(String JavaDoc table, String JavaDoc bcolumn, String JavaDoc lcolumn, String JavaDoc orderBy) {
89         this(null, table, bcolumn, lcolumn, orderBy);
90     }
91     
92     public SelectorComboBoxModel(SSConnection ssConnection, String JavaDoc table, String JavaDoc bcolumn, String JavaDoc lcolumn, String JavaDoc orderBy) {
93         super();
94         propertyChangeSupport = new java.beans.PropertyChangeSupport JavaDoc(this);
95         setSsConnection(ssConnection);
96         setTable(table);
97         setDataColumn(bcolumn);
98         setListColumn(lcolumn);
99         setOrderBy(orderBy);
100                 
101         //populateModel();
102
}
103     
104     public void refresh() {
105         System.out.println("---------------------- refresh() ---------------------");
106         this.populateModel();
107     }
108     
109     public Object JavaDoc getSelectedBoundData(int index) {
110         return ((SelectorElement)this.getElementAt(index)).getDataValue();
111     }
112     
113     private void populateModel() {
114         
115         String JavaDoc sql = null;
116         
117         System.out.println("populateModel();");
118         
119         // if (table == null) table = "publications";
120

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 JavaDoc("0") , "Option 0"));
127             this.addElement(new SelectorElement(new String JavaDoc("1") , "Option 1"));
128             this.addElement(new SelectorElement(new String JavaDoc("2") , "Option 2"));
129             this.addElement(new SelectorElement(new String JavaDoc("0") , "Option 3"));
130             this.addElement(new SelectorElement(new String JavaDoc("1") , "Option 4"));
131             this.addElement(new SelectorElement(new String JavaDoc("2") , "Option 5"));
132             this.addElement(new SelectorElement(new String JavaDoc("0") , "Option 6"));
133             this.addElement(new SelectorElement(new String JavaDoc("1") , "Option 7"));
134             this.addElement(new SelectorElement(new String JavaDoc("2") , "Option 8"));
135             this.addElement(new SelectorElement(new String JavaDoc("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         //data.add(new SelectorElement(new String("-1"), selectText));
168

169         try {
170             ssRowset.beforeFirst();
171             while (ssRowset.next()) {
172                 String JavaDoc s1 = ssRowset.getString(1);
173                 String JavaDoc 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 JavaDoc np) {
180             System.out.println(np);
181         }
182         
183         ssRowset = null;
184         ssConnection = null;
185     }
186     
187     public void setSelectedItem(String JavaDoc bdata) {
188         SelectorElement cual;
189         String JavaDoc 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                 // super.setSelectedItem(cual);
202
return;
203             }
204         }
205         System.out.println("parece que no encontro a '" + bdata +"'");
206     }
207     
208     /**
209      * Adds a PropertyChangeListener to the listener list.
210      * @param l The listener to add.
211      */

212     public void addPropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
213         propertyChangeSupport.addPropertyChangeListener(l);
214     }
215     
216     /**
217      * Removes a PropertyChangeListener from the listener list.
218      * @param l The listener to remove.
219      */

220     public void removePropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
221         propertyChangeSupport.removePropertyChangeListener(l);
222     }
223     
224     /**
225      * Getter for property dataColumn.
226      * @return Value of property dataColumn.
227      */

228     public String JavaDoc getDataColumn() {
229         return this.dataColumn;
230     }
231     
232     /**
233      * Setter for property dataColumn.
234      * @param dataColumn New value of property dataColumn.
235      */

236     public void setDataColumn(String JavaDoc dataColumn) {
237         String JavaDoc oldDataColumn = this.dataColumn;
238         this.dataColumn = dataColumn;
239         try {
240             propertyChangeSupport.firePropertyChange("dataColumn", oldDataColumn, dataColumn);
241         } catch(java.lang.NullPointerException JavaDoc npe) {
242             
243         }
244         //this.refresh();
245
}
246     
247     /**
248      * Getter for property listColumn.
249      * @return Value of property listColumn.
250      */

251     public String JavaDoc getListColumn() {
252         
253         return this.listColumn;
254     }
255     
256     /**
257      * Setter for property listColumn.
258      * @param listColumn New value of property listColumn.
259      */

260     public void setListColumn(String JavaDoc listColumn) {
261         String JavaDoc oldListColumn = this.listColumn;
262         this.listColumn = listColumn;
263         try {
264             propertyChangeSupport.firePropertyChange("listColumn", oldListColumn, listColumn);
265         } catch(java.lang.NullPointerException JavaDoc npe) {
266             
267         }
268         //this.refresh();
269

270     }
271     
272     /**
273      * Getter for property table.
274      * @return Value of property table.
275      */

276     public String JavaDoc getTable() {
277         return this.table;
278     }
279     
280     /**
281      * Setter for property table.
282      * @param table New value of property table.
283      */

284     public void setTable(String JavaDoc table) {
285         
286         String JavaDoc oldTable = this.table;
287         this.table = table;
288         
289         try {
290             propertyChangeSupport.firePropertyChange("table", oldTable, table);
291         } catch(java.lang.NullPointerException JavaDoc npe) {
292             
293         }
294         //this.refresh();
295
}
296     
297     /**
298      * Setter for property orderBy.
299      * @param orderBy New value of orderBy property
300      */

301     public void setOrderBy(String JavaDoc orderBy) {
302         
303         String JavaDoc oldorderBy = this.orderBy;
304         this.orderBy = orderBy;
305         
306         try {
307             propertyChangeSupport.firePropertyChange("orderBy", oldorderBy, orderBy);
308         } catch(java.lang.NullPointerException JavaDoc npe) {
309             
310         }
311         //this.refresh();
312
}
313
314     public String JavaDoc getOrderBy() {
315         return orderBy;
316     }
317
318     /**
319      * Getter for property selectText.
320      * @return Value of property selectText.
321      */

322     public String JavaDoc getSelectText() {
323         
324         return this.selectText;
325     }
326     
327     /**
328      * Setter for property selectText.
329      * @param selectText New value of property selectText.
330      */

331     public void setSelectText(String JavaDoc selectText) {
332         
333         String JavaDoc oldSelectText = this.selectText;
334         this.selectText = selectText;
335         try {
336             propertyChangeSupport.firePropertyChange("selectText", oldSelectText, selectText);
337         } catch (java.lang.NullPointerException JavaDoc npe) {
338             
339         }
340         //this.refresh();
341
}
342     
343     public void execute() {
344         refresh();
345     }
346     
347     /**
348      * Getter for property ssConnection.
349      * @return Value of property ssConnection.
350      */

351     public com.nqadmin.swingSet.datasources.SSConnection getSsConnection() {
352         
353         return this.ssConnection;
354     }
355     
356     /**
357      * Setter for property ssConnection.
358      * @param ssConnection New value of property ssConnection.
359      */

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 JavaDoc nop) {
368             
369         }
370     }
371 }
372
Popular Tags