KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: HelperPopup.java,v 1.5 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.formatting.SSFormattedTextField;
38 import java.awt.BorderLayout JavaDoc;
39 import java.awt.event.ActionListener JavaDoc;
40 import java.awt.event.FocusListener JavaDoc;
41 import java.awt.event.KeyEvent JavaDoc;
42 import java.awt.event.KeyListener JavaDoc;
43 import java.awt.event.MouseListener JavaDoc;
44 import javax.swing.BoxLayout JavaDoc;
45 import javax.swing.DefaultListSelectionModel JavaDoc;
46 import javax.swing.JButton JavaDoc;
47 import javax.swing.JPanel JavaDoc;
48 import javax.swing.JPopupMenu JavaDoc;
49 import javax.swing.JScrollPane JavaDoc;
50 import javax.swing.JTextField JavaDoc;
51
52 import javax.swing.event.ListSelectionListener JavaDoc;
53 import javax.swing.event.PopupMenuListener JavaDoc;
54
55 /**
56  *
57  * @author dags
58  */

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

152     }
153     
154     public void setTable(String JavaDoc table) {
155         this.table = table;
156         createHelper();
157     }
158     
159     public void setDataColumn(String JavaDoc dataColumn) {
160         this.dataColumn = dataColumn;
161         createHelper();
162     }
163     
164     public void setListColumn(String JavaDoc listColumn) {
165         this.listColumn = listColumn;
166         createHelper();
167     }
168     
169     public void setOrderBy(String JavaDoc orderBy) {
170         this.orderBy = orderBy;
171         createHelper();
172     }
173     
174     public void setConnection(SSConnection connection) {
175         this.connection = connection;
176         
177         try {
178             connection.createConnection();
179         } catch(java.lang.ClassNotFoundException JavaDoc nfe) {
180             
181         } catch(java.lang.Exception JavaDoc ex) {
182             
183         }
184         createHelper();
185     }
186     
187     public void setColumnType(int colType) {
188         this.colType = colType;
189     }
190     
191     private void createHelper() {
192         
193         if (connection == null || table == null || dataColumn == null || listColumn == null) return;
194         
195         try {
196             connection.createConnection();
197         } catch(java.lang.ClassNotFoundException JavaDoc nfe) {
198             
199         } catch(java.lang.Exception JavaDoc ex) {
200             
201         }
202         
203         model = new SelectorListModel(connection, table, dataColumn, listColumn, orderBy);
204         model.refresh();
205         
206         lista.setModel(model);
207         lista.getSelectionModel().addListSelectionListener(this);
208         lista.setVisibleRowCount(10);
209         pack();
210     }
211     
212     public void keyPressed(java.awt.event.KeyEvent JavaDoc e) {
213         System.out.println("keyPressed");
214         System.out.println("KeyCode = " + KeyEvent.getKeyText(e.getKeyCode()));
215         
216         if (e.getKeyCode() == KeyEvent.VK_ENTER) {
217             this.setVisible(false);
218         }
219     }
220     
221     public void keyTyped(java.awt.event.KeyEvent JavaDoc e) {
222         System.out.println("keyTyped");
223     }
224     
225     public void keyReleased(java.awt.event.KeyEvent JavaDoc e) {
226         System.out.println("keyReleased");
227     }
228     
229     public void mouseReleased(java.awt.event.MouseEvent JavaDoc e) {
230     }
231     
232     public void mousePressed(java.awt.event.MouseEvent JavaDoc e) {
233     }
234     
235     public void mouseExited(java.awt.event.MouseEvent JavaDoc e) {
236     }
237     
238     public void mouseEntered(java.awt.event.MouseEvent JavaDoc e) {
239     }
240     
241     public void mouseClicked(java.awt.event.MouseEvent JavaDoc e) {
242         System.out.println("mouseClicked");
243         
244         if (e.getClickCount() == 2) {
245             this.setVisible(false);
246         }
247         
248         if (e.getClickCount() == 1) {
249             this.setVisible(false);
250         }
251     }
252     
253     public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
254         
255         if (e.getSource().equals(searchButton)) {
256             System.out.println("searchButton");
257             search(e);
258         }
259         if (e.getSource().equals(closeButton)) {
260             System.out.println("closeButton");
261             this.setVisible(false);
262         }
263         if (e.getSource().equals(refreshButton)) {
264             System.out.println("refreshButton");
265             
266             model = new SelectorListModel(connection, table, dataColumn, listColumn, orderBy);
267             model.refresh();
268             lista.setModel(model);
269             lista.updateUI();
270             lista.getSelectionModel().addListSelectionListener(this);
271             lista.setVisibleRowCount(10);
272         }
273         
274         if (e.getSource().equals(helpButton)) {
275             int index = lista.getSelectedIndex();
276             lista.ensureIndexIsVisible(index);
277         }
278         
279         if (e.getSource().equals(searchText)) {
280             search(e);
281         }
282     }
283     
284     
285     private void search(java.awt.event.ActionEvent JavaDoc evt) {
286         
287         javax.swing.JTextField JavaDoc s;
288         int j, n;
289         
290         // text to find
291
String JavaDoc toFind = null;
292         
293         s= ((javax.swing.JTextField JavaDoc)evt.getSource());
294         toFind = s.getText().toUpperCase().trim();
295         s.setText(toFind);
296         System.out.println("Texto a buscar : " + toFind);
297         n = lista.getModel().getSize();
298         
299         /**
300          * Here implements list search logic.
301          *
302          *
303          */

304         
305         for (j= 0; j < n; j++) {
306             String JavaDoc texto = lista.getModel().getElementAt(j).toString().toUpperCase();
307             System.out.println("Comparando con " + texto);
308             if (texto.startsWith(toFind)) {
309                 lista.setSelectedIndex(j);
310                 lista.ensureIndexIsVisible(j);
311                 lista.requestFocus();
312                 break;
313             }
314         }
315     }
316     
317     public void valueChanged(javax.swing.event.ListSelectionEvent JavaDoc e) {
318         int desde;
319         int hasta;
320         int selected;
321         
322 // System.out.println("ValueIsAdjusting = " + e.getValueIsAdjusting());
323

324         if (e.getValueIsAdjusting() == false) {
325             desde = e.getFirstIndex();
326             hasta = e.getLastIndex();
327 // System.out.println("Desde " + desde + " hasta " + hasta);
328

329             DefaultListSelectionModel JavaDoc lm = ((DefaultListSelectionModel JavaDoc)e.getSource());
330             
331             selected = lm.getLeadSelectionIndex();
332             
333             System.out.println("--------------------- desde --------------------------------------");
334             SelectorElement se1 = (SelectorElement) (lista.getModel().getElementAt(desde));
335 // System.out.println("DataValue = " + se1.getDataValue().toString());
336
// System.out.println("ListValue = " + se1.getListValue().toString());
337

338             System.out.println("--------------------- hasta --------------------------------------");
339             SelectorElement se2 = (SelectorElement) (lista.getModel().getElementAt(hasta));
340 // System.out.println("DataValue = " + se2.getDataValue().toString());
341
// System.out.println("ListValue = " + se2.getListValue().toString());
342

343             System.out.println("--------------------- selected --------------------------------------");
344             SelectorElement se3 = (SelectorElement) (lista.getModel().getElementAt(selected));
345 // System.out.println("DataValue = " + se3.getDataValue().toString());
346
// System.out.println("ListValue = " + se3.getListValue().toString());
347

348         }
349     }
350     
351     public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent JavaDoc e) {
352         Object JavaDoc current = target.getValue();
353         Object JavaDoc dataval = null;
354         
355         System.out.println("popupMenuWillBecomeVisible();");
356         // trying to select current value
357
if (current == null) return;
358         
359         for (int j= 0; j < lista.getModel().getSize(); j++) {
360             dataval = ( (SelectorElement)lista.getModel().getElementAt(j) ).getDataValue();
361             
362             if (dataval.toString().equals(current.toString()) ) {
363                 lista.setSelectedIndex(j);
364                 lista.ensureIndexIsVisible(j);
365                 break;
366             }
367         }
368         //this.setSize(target.getWidth(), searchText.getHeight() * 15);
369
searchText.requestFocusInWindow();
370     }
371     
372     public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent JavaDoc e) {
373         int index;
374         System.out.println("popupMenuWillBecomeInvisible();");
375         index = lista.getSelectedIndex();
376         
377         if (index > -1 && target != null) {
378             SelectorElement se = (SelectorElement)(lista.getModel().getElementAt(index));
379             System.out.println("DataValue = " + se.getDataValue());
380             switch(colType) {
381                 case 0:
382                     target.setValue(new Integer JavaDoc((String JavaDoc)se.getDataValue()));
383                     break;
384                 case 1:
385                     target.setValue((String JavaDoc)se.getDataValue());
386                     break;
387             }
388         }
389     }
390     
391     public void popupMenuCanceled(javax.swing.event.PopupMenuEvent JavaDoc e) {
392         System.out.println("popupMenuCanceled();");
393     }
394
395     public void show(java.awt.Component JavaDoc invoker, int x, int y) {
396         System.out.println("show(" + x + "," + y + ")");
397         this.setSize(target.getWidth(), searchText.getHeight() * 15);
398         searchText.requestFocusInWindow();
399         super.show(invoker, x, y);
400     }
401
402     public void focusLost(java.awt.event.FocusEvent JavaDoc e) {
403         System.out.println("focusLost");
404     }
405
406     public void focusGained(java.awt.event.FocusEvent JavaDoc e) {
407         System.out.println("focusGained");
408     }
409 }
410
Popular Tags