KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > ui > DjListSelectionDialog


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

30 package com.genimen.djeneric.ui;
31
32 import java.awt.BorderLayout JavaDoc;
33 import java.awt.Dimension JavaDoc;
34 import java.awt.Frame JavaDoc;
35 import java.awt.Toolkit JavaDoc;
36 import java.awt.event.ActionEvent JavaDoc;
37 import java.awt.event.KeyEvent JavaDoc;
38 import java.util.ArrayList JavaDoc;
39
40 import javax.swing.DefaultComboBoxModel JavaDoc;
41 import javax.swing.JButton JavaDoc;
42 import javax.swing.JDialog JavaDoc;
43 import javax.swing.JList JavaDoc;
44 import javax.swing.JPanel JavaDoc;
45 import javax.swing.JScrollPane JavaDoc;
46 import javax.swing.ListSelectionModel JavaDoc;
47
48 import com.genimen.djeneric.language.Messages;
49
50 public class DjListSelectionDialog 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 _theList = new JList JavaDoc();
63
64   public DjListSelectionDialog(Frame JavaDoc mainFrame, ArrayList JavaDoc theList, String JavaDoc title)
65   {
66     super(mainFrame, title, true);
67     jbInit();
68     DefaultComboBoxModel JavaDoc mdl = new DefaultComboBoxModel JavaDoc(theList.toArray());
69     _theList.setModel(mdl);
70     pack();
71     setSize(500, 300);
72     Dimension JavaDoc screenSize = Toolkit.getDefaultToolkit().getScreenSize();
73     Dimension JavaDoc frameSize = getSize();
74     setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
75
76     _theList.setSelectedIndex(0);
77
78     if (theList.size() > 0) setVisible(true);
79   }
80
81   public Object JavaDoc getSelectedObject()
82   {
83     if (_theList.getSelectedIndex() == -1) return null;
84     return _theList.getSelectedValue();
85   }
86
87   void jbInit()
88   {
89     panel1.setLayout(borderLayout1);
90     jPanel3.setLayout(borderLayout2);
91     _butOk.setText(Messages.getString("global.Ok"));
92     _butOk.addKeyListener(new java.awt.event.KeyAdapter JavaDoc()
93     {
94       public void keyPressed(KeyEvent JavaDoc e)
95       {
96         _butOk_keyPressed(e);
97       }
98     });
99     _butOk.addActionListener(new java.awt.event.ActionListener JavaDoc()
100     {
101       public void actionPerformed(ActionEvent JavaDoc e)
102       {
103         _butOk_actionPerformed(e);
104       }
105     });
106     _butCancel.setText(Messages.getString("global.Cancel"));
107     _butCancel.addKeyListener(new java.awt.event.KeyAdapter JavaDoc()
108     {
109       public void keyPressed(KeyEvent JavaDoc e)
110       {
111         _butCancel_keyPressed(e);
112       }
113     });
114     _butCancel.addActionListener(new java.awt.event.ActionListener JavaDoc()
115     {
116       public void actionPerformed(ActionEvent JavaDoc e)
117       {
118         _butCancel_actionPerformed(e);
119       }
120     });
121     _theList.addKeyListener(new java.awt.event.KeyAdapter JavaDoc()
122     {
123       public void keyPressed(KeyEvent JavaDoc e)
124       {
125         _lstContext_keyPressed(e);
126       }
127     });
128     _theList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
129     getContentPane().add(panel1);
130     panel1.add(jPanel3, BorderLayout.SOUTH);
131     jPanel3.add(jPanel4, BorderLayout.EAST);
132     jPanel4.add(_butCancel, null);
133     jPanel4.add(_butOk, null);
134     panel1.add(jScrollPane1, BorderLayout.CENTER);
135     jScrollPane1.getViewport().add(_theList, null);
136     Util.sizeButtons(jPanel4);
137   }
138
139   void _butOk_actionPerformed(ActionEvent JavaDoc e)
140   {
141     _wasCancelled = false;
142     setVisible(false);
143   }
144
145   public boolean isCanceled()
146   {
147     return _wasCancelled;
148   }
149
150   void _butCancel_actionPerformed(ActionEvent JavaDoc e)
151   {
152     setVisible(false);
153   }
154
155   void _lstContext_keyPressed(KeyEvent JavaDoc e)
156   {
157     if (e.getKeyCode() == KeyEvent.VK_ENTER) _butOk_actionPerformed(null);
158     if (e.getKeyCode() == KeyEvent.VK_ESCAPE) _butCancel_actionPerformed(null);
159   }
160
161   void _butCancel_keyPressed(KeyEvent JavaDoc e)
162   {
163     _lstContext_keyPressed(e);
164   }
165
166   void _butOk_keyPressed(KeyEvent JavaDoc e)
167   {
168     _lstContext_keyPressed(e);
169   }
170 }
Popular Tags