KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > generator > dialogs > ListSelectionDlg


1 /*
2  Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  Redistribution and use in source and binary forms, with or without modification, is permitted
4  provided that the following conditions are met:
5  - Redistributions of source code must retain the above copyright notice, this list of conditions
6  and the following disclaimer.
7  - Redistributions in binary form must reproduce the above copyright notice, this list of
8  conditions and the following disclaimer in the documentation and/or other materials
9  provided with the distribution.
10  - All advertising materials mentioning features or use of this software must display the
11  following acknowledgment: "This product includes Djeneric."
12  - Products derived from this software may not be called "Djeneric" nor may
13  "Djeneric" appear in their names without prior written permission of Genimen BV.
14  - Redistributions of any form whatsoever must retain the following acknowledgment: "This
15  product includes Djeneric."
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
17  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG,
20  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */

28 package com.genimen.djeneric.tools.generator.dialogs;
29
30 import java.awt.BorderLayout JavaDoc;
31 import java.awt.Dialog JavaDoc;
32 import java.awt.Dimension JavaDoc;
33 import java.awt.Frame JavaDoc;
34 import java.awt.Toolkit JavaDoc;
35 import java.awt.event.ActionEvent JavaDoc;
36 import java.awt.event.KeyEvent JavaDoc;
37 import java.awt.event.MouseEvent JavaDoc;
38 import java.awt.event.WindowEvent JavaDoc;
39 import java.util.Vector JavaDoc;
40
41 import javax.swing.DefaultComboBoxModel JavaDoc;
42 import javax.swing.JButton JavaDoc;
43 import javax.swing.JDialog JavaDoc;
44 import javax.swing.JList JavaDoc;
45 import javax.swing.JPanel JavaDoc;
46 import javax.swing.JScrollPane JavaDoc;
47
48 import com.genimen.djeneric.util.DjLogger;
49
50 public class ListSelectionDlg extends JDialog JavaDoc
51 {
52   private static final long serialVersionUID = 1L;
53   JPanel JavaDoc panel1 = new JPanel JavaDoc();
54   BorderLayout JavaDoc borderLayout1 = new BorderLayout JavaDoc();
55   JPanel JavaDoc jPanel3 = new JPanel JavaDoc();
56   BorderLayout JavaDoc borderLayout2 = new BorderLayout JavaDoc();
57   JPanel JavaDoc jPanel4 = new JPanel JavaDoc();
58   JButton JavaDoc _butOk = new JButton JavaDoc();
59   JButton JavaDoc _butCancel = new JButton JavaDoc();
60   boolean _wasCancelled = true;
61   JScrollPane JavaDoc jScrollPane1 = new JScrollPane JavaDoc();
62   JList JavaDoc _lstList = new JList JavaDoc();
63
64   public ListSelectionDlg(Dialog JavaDoc dialog, String JavaDoc title, Vector JavaDoc lst)
65   {
66     super(dialog, title, true);
67     init(lst);
68   }
69
70   public ListSelectionDlg(Frame JavaDoc frame, String JavaDoc title, Vector JavaDoc lst)
71   {
72     super(frame, title, true);
73     init(lst);
74   }
75
76   protected void init(Vector JavaDoc lst)
77   {
78     try
79     {
80       jbInit();
81       DefaultComboBoxModel JavaDoc mdl = new DefaultComboBoxModel JavaDoc(lst);
82       _lstList.setModel(mdl);
83       pack();
84       setSize(300, 300);
85       Dimension JavaDoc screenSize = Toolkit.getDefaultToolkit().getScreenSize();
86       Dimension JavaDoc frameSize = getSize();
87       setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
88
89       if (lst.size() > 0) _lstList.setSelectedIndex(0);
90
91       setVisible(true);
92     }
93     catch (Exception JavaDoc ex)
94     {
95       DjLogger.log(ex);
96     }
97   }
98
99   public Object JavaDoc[] getSelection()
100   {
101     if (_lstList.getSelectedIndex() == -1) return new Object JavaDoc[0];
102     return _lstList.getSelectedValues();
103   }
104
105   void jbInit() throws Exception JavaDoc
106   {
107     panel1.setLayout(borderLayout1);
108     jPanel3.setLayout(borderLayout2);
109     _butOk.setPreferredSize(new Dimension JavaDoc(73, 27));
110     _butOk.setText("OK");
111     _butOk.addKeyListener(new java.awt.event.KeyAdapter JavaDoc()
112     {
113       public void keyPressed(KeyEvent JavaDoc e)
114       {
115         handleKeys(e);
116       }
117     });
118     _butOk.addActionListener(new java.awt.event.ActionListener JavaDoc()
119     {
120       public void actionPerformed(ActionEvent JavaDoc e)
121       {
122         _butOk_actionPerformed(e);
123       }
124     });
125     _butCancel.setText("Cancel");
126     _butCancel.addKeyListener(new java.awt.event.KeyAdapter JavaDoc()
127     {
128       public void keyPressed(KeyEvent JavaDoc e)
129       {
130         handleKeys(e);
131       }
132     });
133     _butCancel.addActionListener(new java.awt.event.ActionListener JavaDoc()
134     {
135       public void actionPerformed(ActionEvent JavaDoc e)
136       {
137         _butCancel_actionPerformed(e);
138       }
139     });
140     _lstList.addKeyListener(new java.awt.event.KeyAdapter JavaDoc()
141     {
142       public void keyPressed(KeyEvent JavaDoc e)
143       {
144         handleKeys(e);
145       }
146     });
147     _lstList.addMouseListener(new java.awt.event.MouseAdapter JavaDoc()
148     {
149       public void mouseClicked(MouseEvent JavaDoc e)
150       {
151         _lstList_mouseClicked(e);
152       }
153     });
154     this.addWindowListener(new java.awt.event.WindowAdapter JavaDoc()
155     {
156       public void windowOpened(WindowEvent JavaDoc e)
157       {
158         this_windowOpened(e);
159       }
160     });
161     getContentPane().add(panel1);
162     panel1.add(jPanel3, BorderLayout.SOUTH);
163     jPanel3.add(jPanel4, BorderLayout.EAST);
164     jPanel4.add(_butCancel, null);
165     jPanel4.add(_butOk, null);
166     panel1.add(jScrollPane1, BorderLayout.CENTER);
167     jScrollPane1.getViewport().add(_lstList, null);
168   }
169
170   void _butOk_actionPerformed(ActionEvent JavaDoc e)
171   {
172     _wasCancelled = false;
173     setVisible(false);
174   }
175
176   void _butCancel_actionPerformed(ActionEvent JavaDoc e)
177   {
178     setVisible(false);
179   }
180
181   void handleKeys(KeyEvent JavaDoc e)
182   {
183     if (e.getKeyCode() == KeyEvent.VK_ENTER) _butOk_actionPerformed(null);
184     if (e.getKeyCode() == KeyEvent.VK_ESCAPE) _butCancel_actionPerformed(null);
185   }
186
187   void _lstList_mouseClicked(MouseEvent JavaDoc e)
188   {
189     if (e.getClickCount() > 1) _butOk_actionPerformed(null);
190   }
191
192   void this_windowOpened(WindowEvent JavaDoc e)
193   {
194     _lstList.requestFocus();
195
196   }
197
198   public boolean wasCancelled()
199   {
200     return _wasCancelled;
201   }
202 }
Popular Tags