001: /*
002: * Lucane - a collaborative platform
003: * Copyright (C) 2002 Gautier Ringeisen <gautier_ringeisen@hotmail.com>
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package org.lucane.client.widgets;
020:
021: import java.awt.*;
022: import java.awt.event.*;
023: import java.util.Vector;
024: import java.util.List;
025: import javax.swing.*;
026:
027: import org.lucane.client.Client;
028:
029: /**
030: * A ListBox dialog to select an element in a list
031: */
032: public class ListBox extends JDialog implements ActionListener,
033: MouseListener {
034: private Vector list;
035: private JButton btOK;
036: private JButton btCancel;
037: private JList lstSelection;
038: private JPanel pnlButton;
039: private JPanel pnlEast;
040: private JScrollPane pnlScroll;
041: private boolean accept;
042:
043: /**
044: * Creates a new ListBox object.
045: *
046: * @param owner the base JFrame (can be null)
047: * @param title the title of the dialog box
048: * @param message th emessage of the dialog box
049: * @param list a List of items
050: */
051: public ListBox(JFrame owner, String title, String message, List list) {
052: this (owner, title, message, new Vector(list));
053: }
054:
055: /**
056: * Creates a new ListBox object.
057: *
058: * @param owner the base JFrame (can be null)
059: * @param title the title of the dialog box
060: * @param message th emessage of the dialog box
061: * @param list a Vector of items
062: */
063: public ListBox(JFrame owner, String title, String message,
064: Vector list) {
065: super (owner, title, true);
066: this .list = list;
067: btOK = new JButton("OK", Client.getImageIcon("ok.png"));
068: btCancel = new JButton("Cancel", Client
069: .getImageIcon("cancel.png"));
070: lstSelection = new JList(list);
071: pnlEast = new JPanel(new BorderLayout());
072: pnlButton = new JPanel(new GridLayout(2, 0));
073: pnlScroll = new JScrollPane(lstSelection);
074:
075: pnlButton.add(btOK);
076: pnlButton.add(btCancel);
077:
078: pnlEast.add(pnlButton, BorderLayout.NORTH);
079:
080: btOK.addActionListener(this );
081: btCancel.addActionListener(this );
082: lstSelection.addMouseListener(this );
083:
084: this .setSize(400, 200);
085: this .getContentPane().setLayout(new BorderLayout(3, 3));
086: this .getContentPane().add(new JLabel(message),
087: BorderLayout.NORTH);
088: this .getContentPane().add(pnlScroll, BorderLayout.CENTER);
089: this .getContentPane().add(pnlEast, BorderLayout.EAST);
090: }
091:
092: /**
093: * Get the selected item index
094: *
095: * @return the selected index
096: */
097: public int selectItemByIndex() {
098: if (list.size() == 0)
099: return -1;
100:
101: lstSelection
102: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
103: this .show();
104:
105: if (accept)
106: return lstSelection.getSelectedIndex();
107: else
108: return -1;
109: }
110:
111: /**
112: * Get the selected items
113: *
114: * @return the selected items
115: */
116: public Object[] selectItems() {
117: if (list.size() == 0)
118: return new Object[0];
119:
120: lstSelection
121: .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
122: this .show();
123:
124: if (accept)
125: return lstSelection.getSelectedValues();
126: else
127: return null;
128: }
129:
130: /**
131: * A button has been clicked (oh my god!)
132: *
133: * @param ev yet another event...
134: */
135: public void actionPerformed(ActionEvent ev) {
136: if (ev.getSource() == btOK)
137: accept = true;
138: else
139: accept = false;
140:
141: this .dispose();
142: }
143:
144: //-- mouse listener
145:
146: public void mouseEntered(MouseEvent e) {
147: }
148:
149: public void mouseExited(MouseEvent e) {
150: }
151:
152: public void mousePressed(MouseEvent e) {
153: }
154:
155: public void mouseReleased(MouseEvent e) {
156: }
157:
158: public void mouseClicked(MouseEvent e) {
159: if (e.getClickCount() > 1
160: && lstSelection.getSelectedIndex() >= 0) {
161: accept = true;
162: this.dispose();
163: }
164: }
165: }
|