Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/lucane/common/ListBox.java


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.common;
20  
21  import java.awt.*;
22  import java.awt.event.*;
23  import java.util.Vector;
24  import javax.swing.*;
25  
26  
27  /**
28   * A ListBox dialog to select an element in a list
29   */
30  class ListBox
31    extends JDialog
32    implements ActionListener
33  {
34    private Vector list;
35    private JButton btOK;
36    private JButton btCancel;
37    private JList lstSelection;
38    private JPanel pnlButton;
39    private JPanel pnlEast;
40    private JScrollPane pnlScroll;
41    private boolean accept;
42  
43    /**
44     * Creates a new ListBox object.
45     * 
46     * @param owner the base JFrame (can be null)
47     * @param title the title of the dialog box
48     * @param message th emessage of the dialog box
49     * @param list a Vector of items
50     */
51    public ListBox(JFrame owner, String title, String message, Vector list)
52    {
53      super(owner, title, true);
54      this.list = list;
55      btOK = new JButton("OK");
56      btCancel = new JButton("Cancel");
57      lstSelection = new JList(list);
58      pnlEast = new JPanel(new BorderLayout());
59      pnlButton = new JPanel(new GridLayout(2, 0));
60      pnlScroll = new JScrollPane(lstSelection);
61      pnlButton.add(btOK);
62      pnlButton.add(btCancel);
63      pnlEast.add(pnlButton, BorderLayout.NORTH);
64      btOK.addActionListener(this);
65      btCancel.addActionListener(this);
66      lstSelection.getSelectionModel().setSelectionMode(0);
67      this.setSize(400, 200);
68      this.getContentPane().setLayout(new BorderLayout(3, 3));
69      this.getContentPane().add(new JLabel(message), BorderLayout.NORTH);
70      this.getContentPane().add(pnlScroll, BorderLayout.CENTER);
71      this.getContentPane().add(pnlEast, BorderLayout.EAST);
72    }
73  
74    /**
75     * Get the selected item index
76     * 
77     * @return the selected index
78     */
79    public int selectItemByIndex()
80    {
81      if(list.size() == 0)
82        return -1;
83  
84      this.show();
85  
86      if(accept)
87        return lstSelection.getSelectedIndex();
88      else
89        return -1;
90    }
91  
92    /**
93     * A button has been clicked (oh my god!)
94     * 
95     * @param ev yet another event...
96     */
97    public void actionPerformed(ActionEvent ev)
98    {
99      if(ev.getSource() == btOK)
100       accept = true;
101     else
102       accept = false;
103 
104     this.hide();
105   }
106 }