KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > client > widgets > ListBox


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2002 Gautier Ringeisen <gautier_ringeisen@hotmail.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.client.widgets;
20
21 import java.awt.*;
22 import java.awt.event.*;
23 import java.util.Vector JavaDoc;
24 import java.util.List JavaDoc;
25 import javax.swing.*;
26
27 import org.lucane.client.Client;
28
29
30 /**
31  * A ListBox dialog to select an element in a list
32  */

33 public class ListBox extends JDialog
34   implements ActionListener, MouseListener
35 {
36   private Vector JavaDoc list;
37   private JButton btOK;
38   private JButton btCancel;
39   private JList lstSelection;
40   private JPanel pnlButton;
41   private JPanel pnlEast;
42   private JScrollPane pnlScroll;
43   private boolean accept;
44
45   /**
46    * Creates a new ListBox object.
47    *
48    * @param owner the base JFrame (can be null)
49    * @param title the title of the dialog box
50    * @param message th emessage of the dialog box
51    * @param list a List of items
52    */

53   public ListBox(JFrame owner, String JavaDoc title, String JavaDoc message, List JavaDoc list)
54   {
55       this(owner, title, message, new Vector JavaDoc(list));
56   }
57
58
59   /**
60    * Creates a new ListBox object.
61    *
62    * @param owner the base JFrame (can be null)
63    * @param title the title of the dialog box
64    * @param message th emessage of the dialog box
65    * @param list a Vector of items
66    */

67   public ListBox(JFrame owner, String JavaDoc title, String JavaDoc message, Vector JavaDoc list)
68   {
69     super(owner, title, true);
70     this.list = list;
71     btOK = new JButton("OK", Client.getImageIcon("ok.png"));
72     btCancel = new JButton("Cancel", Client.getImageIcon("cancel.png"));
73     lstSelection = new JList(list);
74     pnlEast = new JPanel(new BorderLayout());
75     pnlButton = new JPanel(new GridLayout(2, 0));
76     pnlScroll = new JScrollPane(lstSelection);
77
78     pnlButton.add(btOK);
79     pnlButton.add(btCancel);
80     
81     pnlEast.add(pnlButton, BorderLayout.NORTH);
82     
83     btOK.addActionListener(this);
84     btCancel.addActionListener(this);
85     lstSelection.addMouseListener(this);
86     
87     this.setSize(400, 200);
88     this.getContentPane().setLayout(new BorderLayout(3, 3));
89     this.getContentPane().add(new JLabel(message), BorderLayout.NORTH);
90     this.getContentPane().add(pnlScroll, BorderLayout.CENTER);
91     this.getContentPane().add(pnlEast, BorderLayout.EAST);
92   }
93
94   /**
95    * Get the selected item index
96    *
97    * @return the selected index
98    */

99   public int selectItemByIndex()
100   {
101     if(list.size() == 0)
102       return -1;
103
104     lstSelection.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
105     this.show();
106
107     if(accept)
108       return lstSelection.getSelectedIndex();
109     else
110       return -1;
111   }
112
113   /**
114    * Get the selected items
115    *
116    * @return the selected items
117    */

118   public Object JavaDoc[] selectItems()
119   {
120     if(list.size() == 0)
121         return new Object JavaDoc[0];
122
123     lstSelection.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
124     this.show();
125
126     if(accept)
127         return lstSelection.getSelectedValues();
128     else
129         return null;
130   }
131
132   /**
133    * A button has been clicked (oh my god!)
134    *
135    * @param ev yet another event...
136    */

137   public void actionPerformed(ActionEvent ev)
138   {
139     if(ev.getSource() == btOK)
140       accept = true;
141     else
142       accept = false;
143
144     this.dispose();
145   }
146   
147   //-- mouse listener
148

149     public void mouseEntered(MouseEvent e) {}
150     public void mouseExited(MouseEvent e) {}
151     public void mousePressed(MouseEvent e) {}
152     public void mouseReleased(MouseEvent e) {}
153     public void mouseClicked(MouseEvent e)
154     {
155         if(e.getClickCount() > 1 && lstSelection.getSelectedIndex() >= 0)
156         {
157             accept = true;
158             this.dispose();
159         }
160     }
161 }
Popular Tags