KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: RowSetHelperPopup.java,v 1.4 2005/02/04 22:42:14 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 com.nqadmin.swingSet.datasources.SSConnection;
37 import com.nqadmin.swingSet.datasources.SSRowSet;
38 import com.nqadmin.swingSet.formatting.SSFormattedTextField;
39 import java.awt.BorderLayout JavaDoc;
40 import java.awt.event.ActionListener JavaDoc;
41 import java.awt.event.FocusListener JavaDoc;
42 import java.awt.event.KeyEvent JavaDoc;
43 import java.awt.event.KeyListener JavaDoc;
44 import java.awt.event.MouseListener JavaDoc;
45 import java.sql.SQLException JavaDoc;
46 import javax.swing.BoxLayout JavaDoc;
47 import javax.swing.DefaultListSelectionModel JavaDoc;
48 import javax.swing.DefaultListModel JavaDoc;
49 import javax.swing.JButton JavaDoc;
50 import javax.swing.JPanel JavaDoc;
51 import javax.swing.JPopupMenu JavaDoc;
52 import javax.swing.JScrollPane JavaDoc;
53 import javax.swing.JTextField JavaDoc;
54
55 import javax.swing.event.ListSelectionListener JavaDoc;
56 import javax.swing.event.PopupMenuListener JavaDoc;
57
58 /**
59  *
60  * @author dags
61  */

62 public class RowSetHelperPopup extends JPopupMenu JavaDoc implements MouseListener JavaDoc, KeyListener JavaDoc, ActionListener JavaDoc, ListSelectionListener JavaDoc, PopupMenuListener JavaDoc, FocusListener JavaDoc {
63     
64     private JPanel JavaDoc spane;
65     private JPanel JavaDoc buttons;
66     private JPanel JavaDoc tpane;
67     private JButton JavaDoc searchButton;
68     private JButton JavaDoc closeButton;
69     private JButton JavaDoc refreshButton;
70     private JButton JavaDoc helpButton;
71     private JTextField JavaDoc searchText;
72     
73     private String JavaDoc table = null;
74     private String JavaDoc dataColumn = null;
75     private String JavaDoc listColumn = null;
76     private String JavaDoc orderBy = null;
77     private String JavaDoc query = null;
78     
79     private SSFormattedTextField target = null;
80     private JScrollPane JavaDoc sc;
81     private SSConnection connection;
82     private DefaultListModel JavaDoc model = null;
83     private SelectorList lista;
84     
85     private SSRowSet rowset = null;
86     
87     /** Creates a new instance of HelperPopup */
88     public RowSetHelperPopup() {
89         
90         // main panel
91
spane = new JPanel JavaDoc();
92         spane.setLayout(new BorderLayout JavaDoc());
93         spane.setBorder(new javax.swing.border.TitledBorder JavaDoc(" RowSet Helper "));
94         
95         // search text panel
96
tpane = new JPanel JavaDoc();
97         tpane.setLayout(new BorderLayout JavaDoc());
98         
99         // button bar panel
100
buttons = new JPanel JavaDoc();
101         buttons.setLayout(new BoxLayout JavaDoc(buttons, BoxLayout.X_AXIS));
102         
103         searchButton = new JButton JavaDoc("Search");
104         searchButton.addActionListener(this);
105         
106         closeButton = new JButton JavaDoc("Close");
107         closeButton.addActionListener(this);
108         
109         refreshButton = new JButton JavaDoc("Refresh");
110         refreshButton.addActionListener(this);
111         
112         helpButton = new JButton JavaDoc("Help");
113         helpButton.addActionListener(this);
114         
115         searchText = new JTextField JavaDoc();
116         searchText.setColumns(20);
117         searchText.addActionListener(this);
118         searchText.addFocusListener(this);
119         tpane.add(searchText, BorderLayout.NORTH);
120         
121         buttons.add(searchButton);
122         buttons.add(closeButton);
123         buttons.add(refreshButton);
124         buttons.add(helpButton);
125         
126         tpane.add(buttons, BorderLayout.SOUTH);
127         
128         lista = new SelectorList();
129         lista.addKeyListener(this);
130         lista.addMouseListener(this);
131         lista.setVisibleRowCount(10);
132         
133         sc = new JScrollPane JavaDoc(lista);
134         
135         spane.add(tpane, BorderLayout.NORTH);
136         spane.add(sc , BorderLayout.CENTER);
137         
138         this.add(spane);
139         this.addPopupMenuListener(this);
140         this.setEnabled(true);
141         this.setFocusable(true);
142         this.addFocusListener(this);
143         this.pack();
144     }
145     
146     public void setRowSet(SSRowSet rowset) {
147         this.rowset = rowset;
148     }
149     
150     public void setModel(DefaultListModel JavaDoc model) {
151         this.model = model;
152         lista.setModel(model);
153     }
154     
155     public void setTarget(SSFormattedTextField target) {
156         this.target = target;
157         
158         //if (target != null) this.setPreferredSize(new Dimension(target.getWidth(),300));
159

160     }
161     
162     public void setTable(String JavaDoc table) {
163         this.table = table;
164     }
165     
166     public void setDataColumn(String JavaDoc dataColumn) {
167         this.dataColumn = dataColumn;
168     }
169     
170     public void setListColumn(String JavaDoc listColumn) {
171         this.listColumn = listColumn;
172     }
173     
174     public void setOrderBy(String JavaDoc orderBy) {
175         this.orderBy = orderBy;
176     }
177     
178     public void setConnection(SSConnection connection) {
179         this.connection = connection;
180         
181         try {
182             connection.createConnection();
183         } catch(java.lang.ClassNotFoundException JavaDoc nfe) {
184             
185         } catch(java.lang.Exception JavaDoc ex) {
186             
187         }
188     }
189     
190     public void createHelper() {
191         /*
192         try {
193             connection.createConnection();
194         } catch(java.lang.ClassNotFoundException nfe) {
195             
196         } catch(java.lang.Exception ex) {
197             
198         }
199         */

200         model = new DefaultListModel JavaDoc();
201         
202          try {
203             System.out.println("beforeFirst();");
204             System.out.println("dataColumn = " + dataColumn);
205             System.out.println("listColumn = " + listColumn);
206             
207             rowset.beforeFirst();
208             while (rowset.next()) {
209                 System.out.println("rowset.next() =" + rowset.getString(listColumn));
210                 String JavaDoc s1 = rowset.getString(dataColumn);
211                 String JavaDoc s2 = rowset.getString(listColumn);
212                 model.addElement(new SelectorElement(s1,s2));
213             }
214         } catch (SQLException JavaDoc se) {
215             System.out.println("rowset.next()" + se);
216         } catch (java.lang.NullPointerException JavaDoc np) {
217             System.out.println(np);
218         }
219         
220         lista.setModel(model);
221         lista.getSelectionModel().addListSelectionListener(this);
222         lista.setVisibleRowCount(10);
223         pack();
224     }
225     
226     public void keyPressed(java.awt.event.KeyEvent JavaDoc e) {
227         System.out.println("keyPressed");
228         System.out.println("KeyCode = " + KeyEvent.getKeyText(e.getKeyCode()));
229         
230         if (e.getKeyCode() == KeyEvent.VK_ENTER) {
231             this.setVisible(false);
232         }
233     }
234     
235     public void keyTyped(java.awt.event.KeyEvent JavaDoc e) {
236         System.out.println("keyTyped");
237     }
238     
239     public void keyReleased(java.awt.event.KeyEvent JavaDoc e) {
240         System.out.println("keyReleased");
241     }
242     
243     public void mouseReleased(java.awt.event.MouseEvent JavaDoc e) {
244     }
245     
246     public void mousePressed(java.awt.event.MouseEvent JavaDoc e) {
247     }
248     
249     public void mouseExited(java.awt.event.MouseEvent JavaDoc e) {
250     }
251     
252     public void mouseEntered(java.awt.event.MouseEvent JavaDoc e) {
253     }
254     
255     public void mouseClicked(java.awt.event.MouseEvent JavaDoc e) {
256         System.out.println("mouseClicked");
257         
258         if (e.getClickCount() == 2) {
259             this.setVisible(false);
260         }
261         
262         if (e.getClickCount() == 321) {
263             this.setVisible(false);
264         }
265     }
266     
267     public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
268         
269         if (e.getSource().equals(searchButton)) {
270             System.out.println("searchButton");
271             search();
272         }
273         if (e.getSource().equals(closeButton)) {
274             System.out.println("closeButton");
275             this.setVisible(false);
276         }
277         if (e.getSource().equals(refreshButton)) {
278             System.out.println("refreshButton");
279             createHelper();
280         }
281         
282         if (e.getSource().equals(helpButton)) {
283             int index = lista.getSelectedIndex();
284             lista.ensureIndexIsVisible(index);
285         }
286         
287         if (e.getSource().equals(searchText)) {
288             search();
289         }
290     }
291     
292     
293     private void search() {
294         
295         int j, n;
296         
297         // text to find
298
String JavaDoc toFind = searchText.getText().toUpperCase().trim();
299         searchText.setText(toFind);
300         System.out.println("Texto a buscar : " + toFind);
301         n = lista.getModel().getSize();
302         
303         /**
304          * Here implements list search logic.
305          *
306          *
307          */

308         j = lista.getSelectedIndex() + 1;
309         
310         System.out.println("j = " + j + " n = " + n);
311         for (; j < n; j++) {
312             String JavaDoc texto = lista.getModel().getElementAt(j).toString().toUpperCase();
313             System.out.println("Comparando con " + texto);
314             
315             if (texto.indexOf(toFind) != -1) { // texto.contains(toFind) in jdk5
316
lista.setSelectedIndex(j);
317                 lista.ensureIndexIsVisible(j);
318                 lista.requestFocus();
319                 break;
320             }
321         }
322     }
323     
324     public void valueChanged(javax.swing.event.ListSelectionEvent JavaDoc e) {
325         int desde;
326         int hasta;
327         int selected;
328         
329         // System.out.println("ValueIsAdjusting = " + e.getValueIsAdjusting());
330

331         if (e.getValueIsAdjusting() == false) {
332             desde = e.getFirstIndex();
333             hasta = e.getLastIndex();
334             // System.out.println("Desde " + desde + " hasta " + hasta);
335

336             DefaultListSelectionModel JavaDoc lm = ((DefaultListSelectionModel JavaDoc)e.getSource());
337             
338             selected = lm.getLeadSelectionIndex();
339             
340             System.out.println("--------------------- desde --------------------------------------");
341             SelectorElement se1 = (SelectorElement) (lista.getModel().getElementAt(desde));
342             // System.out.println("DataValue = " + se1.getDataValue().toString());
343
// System.out.println("ListValue = " + se1.getListValue().toString());
344

345             System.out.println("--------------------- hasta --------------------------------------");
346             SelectorElement se2 = (SelectorElement) (lista.getModel().getElementAt(hasta));
347             // System.out.println("DataValue = " + se2.getDataValue().toString());
348
// System.out.println("ListValue = " + se2.getListValue().toString());
349

350             System.out.println("--------------------- selected --------------------------------------");
351             SelectorElement se3 = (SelectorElement) (lista.getModel().getElementAt(selected));
352             // System.out.println("DataValue = " + se3.getDataValue().toString());
353
// System.out.println("ListValue = " + se3.getListValue().toString());
354

355         }
356     }
357     
358     public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent JavaDoc e) {
359         
360         int index = -1;
361         
362         try {
363             index = rowset.getRow() -1;
364         }
365         catch(java.sql.SQLException JavaDoc se) {
366             
367         }
368
369         lista.setSelectedIndex(index);
370         lista.ensureIndexIsVisible(index);
371         searchText.requestFocusInWindow();
372         searchText.selectAll();
373     }
374     
375     public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent JavaDoc e) {
376
377         System.out.println("popupMenuWillBecomeInvisible();");
378         int index = lista.getSelectedIndex() + 1;
379         
380         if (index > -1 && target != null) {
381             try {
382                 rowset.absolute(index);
383             }
384             catch(java.sql.SQLException JavaDoc se) {
385                 
386             }
387         }
388     }
389     
390     public void popupMenuCanceled(javax.swing.event.PopupMenuEvent JavaDoc e) {
391         System.out.println("popupMenuCanceled();");
392     }
393     
394     public void show(java.awt.Component JavaDoc invoker, int x, int y) {
395         System.out.println("show(" + x + "," + y + ")");
396         this.setSize(target.getWidth(), searchText.getHeight() * 15);
397         searchText.requestFocusInWindow();
398         super.show(invoker, x, y);
399     }
400     
401     public void focusLost(java.awt.event.FocusEvent JavaDoc e) {
402         System.out.println("focusLost");
403     }
404     
405     public void focusGained(java.awt.event.FocusEvent JavaDoc e) {
406         System.out.println("focusGained");
407     }
408 }
409
Popular Tags