KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: SelectorListModel.java,v 1.5 2005/02/22 15:14:48 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 java.util.regex.Pattern JavaDoc;
38 import java.util.regex.Matcher JavaDoc;
39 import com.nqadmin.swingSet.datasources.*;
40
41 /**
42  *
43  * @author dags
44  */

45 public class SelectorListModel extends javax.swing.AbstractListModel JavaDoc {
46     
47     private java.util.ArrayList JavaDoc data = new java.util.ArrayList JavaDoc();
48     
49     
50     /**
51      * Holds value of property dataColumn.
52      */

53     private String JavaDoc dataColumn;
54     
55     /**
56      * Utility field used by bound properties.
57      */

58     private java.beans.PropertyChangeSupport JavaDoc propertyChangeSupport;
59     
60     /**
61      * Holds value of property listColumn.
62      */

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

68     private String JavaDoc table;
69     
70     /**
71      * Holds value of property selectText.
72      */

73     private String JavaDoc selectText;
74     
75     private String JavaDoc orderBy;
76     
77     /**
78      * Holds value of property ssConnection.
79      */

80     private SSConnection ssConnection;
81     private SSJdbcRowSetImpl ssRowset;
82     
83     /** Creates a new instance of AccountSelectorModel */
84     
85     public SelectorListModel() {
86         this(null, null, null, null);
87     }
88     
89     public SelectorListModel(String JavaDoc table, String JavaDoc bcolumn, String JavaDoc lcolumn) {
90         this(table, bcolumn, lcolumn, null);
91     }
92     
93     public SelectorListModel(String JavaDoc table, String JavaDoc bcolumn, String JavaDoc lcolumn, String JavaDoc orderBy) {
94         this(null, table, bcolumn, lcolumn, orderBy);
95     }
96     
97     public SelectorListModel(SSConnection ssConnection, String JavaDoc table, String JavaDoc bcolumn, String JavaDoc lcolumn, String JavaDoc orderBy) {
98         super();
99         propertyChangeSupport = new java.beans.PropertyChangeSupport JavaDoc(this);
100         setSsConnection(ssConnection);
101         setTable(table);
102         setDataColumn(bcolumn);
103         setListColumn(lcolumn);
104         setOrderBy(orderBy);
105                 
106         //populateModel();
107
}
108     
109     public void refresh() {
110         System.out.println("---------------------- refresh() ---------------------");
111         data = new java.util.ArrayList JavaDoc();
112         this.populateModel();
113     }
114     
115     public Object JavaDoc getSelectedBoundData(int index) {
116         Object JavaDoc 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 JavaDoc sql = null;
128         
129 // System.out.println("populateModel();");
130

131         // if (table == null) table = "publications";
132

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 JavaDoc("0") , "Option 0"));
139             data.add(new SelectorElement(new String JavaDoc("1") , "Option 1"));
140             data.add(new SelectorElement(new String JavaDoc("2") , "Option 2"));
141             data.add(new SelectorElement(new String JavaDoc("0") , "Option 3"));
142             data.add(new SelectorElement(new String JavaDoc("1") , "Option 4"));
143             data.add(new SelectorElement(new String JavaDoc("2") , "Option 5"));
144             data.add(new SelectorElement(new String JavaDoc("0") , "Option 6"));
145             data.add(new SelectorElement(new String JavaDoc("1") , "Option 7"));
146             data.add(new SelectorElement(new String JavaDoc("2") , "Option 8"));
147             data.add(new SelectorElement(new String JavaDoc("0") , "Option 9"));
148             data.add(new SelectorElement(new String JavaDoc("1") , "Option 0"));
149             data.add(new SelectorElement(new String JavaDoc("2") , "Option 1"));
150             data.add(new SelectorElement(new String JavaDoc("0") , "Option 2"));
151             data.add(new SelectorElement(new String JavaDoc("1") , "Option 3"));
152             data.add(new SelectorElement(new String JavaDoc("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 // System.out.println("sql1 = " + sql);
166

167         ssRowset = new SSJdbcRowSetImpl();
168         ssRowset.setSSConnection(ssConnection);
169         
170         
171 // System.out.println("sql2 = " + sql);
172

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         //data.add(new SelectorElement(new String("-1"), selectText));
185

186         try {
187             ssRowset.beforeFirst();
188             while (ssRowset.next()) {
189                 String JavaDoc s1 = ssRowset.getString(1);
190                 String JavaDoc s2 = ssRowset.getString(2);
191 // System.out.println(s2);
192
data.add(new SelectorElement(s1,s2));
193             }
194         } catch (SQLException se) {
195             System.out.println("ssRowset.next()" + se);
196         } catch (java.lang.NullPointerException JavaDoc np) {
197             System.out.println(np);
198         }
199         
200         ssRowset = null;
201         ssConnection = null;
202     }
203     
204     public void setSelectedItem(String JavaDoc bdata) {
205         SelectorElement cual;
206         String JavaDoc 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                 // super.setSelectedItem(cual);
219
return;
220             }
221         }
222         System.out.println("parece que no encontro a '" + bdata +"'");
223     }
224     
225     /**
226      * Adds a PropertyChangeListener to the listener list.
227      * @param l The listener to add.
228      */

229     public void addPropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
230         propertyChangeSupport.addPropertyChangeListener(l);
231     }
232     
233     /**
234      * Removes a PropertyChangeListener from the listener list.
235      * @param l The listener to remove.
236      */

237     public void removePropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
238         propertyChangeSupport.removePropertyChangeListener(l);
239     }
240     
241     /**
242      * Getter for property dataColumn.
243      * @return Value of property dataColumn.
244      */

245     public String JavaDoc getDataColumn() {
246         return this.dataColumn;
247     }
248     
249     /**
250      * Setter for property dataColumn.
251      * @param dataColumn New value of property dataColumn.
252      */

253     public void setDataColumn(String JavaDoc dataColumn) {
254         String JavaDoc oldDataColumn = this.dataColumn;
255         this.dataColumn = dataColumn;
256         try {
257             propertyChangeSupport.firePropertyChange("dataColumn", oldDataColumn, dataColumn);
258         } catch(java.lang.NullPointerException JavaDoc npe) {
259             
260         }
261         //this.refresh();
262
}
263     
264     /**
265      * Getter for property listColumn.
266      * @return Value of property listColumn.
267      */

268     public String JavaDoc getListColumn() {
269         
270         return this.listColumn;
271     }
272     
273     /**
274      * Setter for property listColumn.
275      * @param listColumn New value of property listColumn.
276      */

277     public void setListColumn(String JavaDoc listColumn) {
278         String JavaDoc oldListColumn = this.listColumn;
279         this.listColumn = listColumn;
280         try {
281             propertyChangeSupport.firePropertyChange("listColumn", oldListColumn, listColumn);
282         } catch(java.lang.NullPointerException JavaDoc npe) {
283             
284         }
285         //this.refresh();
286

287     }
288     
289     /**
290      * Getter for property table.
291      * @return Value of property table.
292      */

293     public String JavaDoc getTable() {
294         return this.table;
295     }
296     
297     /**
298      * Setter for property table.
299      * @param table New value of property table.
300      */

301     public void setTable(String JavaDoc table) {
302         
303         String JavaDoc oldTable = this.table;
304         this.table = table;
305         
306         try {
307             propertyChangeSupport.firePropertyChange("table", oldTable, table);
308         } catch(java.lang.NullPointerException JavaDoc npe) {
309             
310         }
311         //this.refresh();
312
}
313     
314     /**
315      * Setter for property orderBy.
316      * @param orderBy New value of orderBy property
317      */

318     public void setOrderBy(String JavaDoc orderBy) {
319         
320         String JavaDoc oldorderBy = this.orderBy;
321         this.orderBy = orderBy;
322         
323         try {
324             propertyChangeSupport.firePropertyChange("orderBy", oldorderBy, orderBy);
325         } catch(java.lang.NullPointerException JavaDoc npe) {
326             
327         }
328         //this.refresh();
329
}
330
331     public String JavaDoc getOrderBy() {
332         return orderBy;
333     }
334
335     /**
336      * Getter for property selectText.
337      * @return Value of property selectText.
338      */

339     public String JavaDoc getSelectText() {
340         
341         return this.selectText;
342     }
343     
344     /**
345      * Setter for property selectText.
346      * @param selectText New value of property selectText.
347      */

348     public void setSelectText(String JavaDoc selectText) {
349         
350         String JavaDoc oldSelectText = this.selectText;
351         this.selectText = selectText;
352         try {
353             propertyChangeSupport.firePropertyChange("selectText", oldSelectText, selectText);
354         } catch (java.lang.NullPointerException JavaDoc npe) {
355             
356         }
357         //this.refresh();
358
}
359     
360     public void execute() {
361         refresh();
362     }
363     
364     /**
365      * Getter for property ssConnection.
366      * @return Value of property ssConnection.
367      */

368     public com.nqadmin.swingSet.datasources.SSConnection getSsConnection() {
369         
370         return this.ssConnection;
371     }
372     
373     /**
374      * Setter for property ssConnection.
375      * @param ssConnection New value of property ssConnection.
376      */

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 JavaDoc nop) {
385             
386         }
387     }
388     
389     public Object JavaDoc getElementAt(int index) {
390         return data.get(index);
391     }
392     
393     public int getSize() {
394         return data.size();
395     }
396 }
397
Popular Tags