001/* ----------------------------------------------------------------------------
002   The Kiwi Toolkit - A Java Class Library
003   Copyright (C) 1998-2004 Mark A. Lindner
004
005   This library is free software; you can redistribute it and/or
006   modify it under the terms of the GNU General Public License as
007   published by the Free Software Foundation; either version 2 of the
008   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   General Public License for more details.
014
015   You should have received a copy of the GNU General Public License
016   along with this library; if not, write to the Free Software
017   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018   02111-1307, USA.
019 
020   The author may be contacted at: mark_a_lindner@yahoo.com
021   ----------------------------------------------------------------------------
022   $Log: ItemChooserDialog.java,v $
023   Revision 1.7  2004/05/05 23:20:24  markl
024   comment block updates
025
026   Revision 1.6  2004/03/16 06:43:39  markl
027   LocaleManager method change
028
029   Revision 1.5  2003/01/19 09:40:15  markl
030   Respond to a double-click on a list item as a selection event. Added new
031   constructors that take a dialog parent.
032
033   Revision 1.4  2001/03/20 00:54:55  markl
034   Fixed deprecated calls.
035
036   Revision 1.3  2001/03/12 05:19:58  markl
037   Source code cleanup.
038
039   Revision 1.2  1999/06/28 09:36:32  markl
040   I18N.
041
042   Revision 1.1  1999/04/19 06:06:44  markl
043   Initial revision
044
045   Revision 1.1  1999/04/18 10:26:12  markl
046   Initial revision
047   ----------------------------------------------------------------------------
048*/
049
050package kiwi.ui.dialog;
051
052import java.awt.*;
053import java.util.*;
054import javax.swing.*;
055
056import kiwi.event.*;
057import kiwi.ui.*;
058import kiwi.util.*;
059
060/** A dialog that presents a list of items, one of which must be selected
061 * before the dialog may be dismissed. An item may be selected either by
062 * single-clicking on the item and then clicking on the <i>OK</i> button,
063 * or by double-clicking on the item.
064 *
065 * <p><center>
066 * <img src="snapshot/ItemChooserDialog.gif"><br>
067 * <i>An example ItemChooserDialog.</i>
068 * </center>
069 *
070 * @author Mark Lindner
071 */
072
073public class ItemChooserDialog extends ComponentDialog
074  {
075  /** The <code>JList</code> used by this dialog. */
076  protected JList list;
077  private DefaultListModel model;
078  private String selectError;
079  private ListItemMouseAdapter mouseAdapter;
080
081  /** Construct a new <code>ItemChooserDialog</code>.
082   *
083   * @param parent The parent window for this dialog.
084   * @param title The title for the dialog.
085   * @param comment The comment string for this dialog.
086   */
087  
088  public ItemChooserDialog(Frame parent, String title, String comment)
089    {
090    super(parent, title, true);
091    _init(comment);
092    }
093
094  /** Construct a new <code>ItemChooserDialog</code>.
095   *
096   * @param parent The parent window for this dialog.
097   * @param title The title for the dialog.
098   * @param comment The comment string for this dialog.
099   *
100   * @since Kiwi 1.4
101   */
102  
103  public ItemChooserDialog(Dialog parent, String title, String comment)
104    {
105    super(parent, title, true);
106    _init(comment);
107    }
108  
109  /*
110   */
111
112  private void _init(String comment)
113    {
114    setComment(comment);
115
116    LocaleData loc = LocaleManager.getDefault().getLocaleData("KiwiDialogs");
117    selectError = loc.getMessage("kiwi.dialog.message.select_item");
118    }
119
120  /** Build the dialog UI. */
121  
122  protected Component buildDialogUI()
123    {
124    list = new JList();
125    model = new DefaultListModel();
126    list.setModel(model);
127
128    mouseAdapter = new ListItemMouseAdapter()
129        {
130        public void itemDoubleClicked(int row)
131          {
132          doAccept();
133          }
134        };
135
136    list.addMouseListener(mouseAdapter);
137
138    return(new KScrollPane(list));
139    }
140
141  /** Accept the input.
142   *
143   * @return <code>true</code> if an item is selected in the list,
144   * <code>false</code> otherwise.
145   */
146  
147  protected boolean accept()
148    {
149    if(list.getSelectedIndex() < 0)
150      {
151      DialogSet.getInstance().showMessageDialog(selectError);
152      return(false);
153      }
154
155    else
156      return(true);    
157    }
158
159  /** Set the list of items to be displayed by this dialog.
160   *
161   * @param items The items.
162   */
163  
164  public void setItems(Enumeration items)
165    {
166    model.clear();
167    while(items.hasMoreElements())
168      model.addElement(items.nextElement());
169
170    list.setSelectedIndex(0);
171    }
172
173  /** Set the list of items to be displayed by this dialog.
174   *
175   * @param items The items.
176   *
177   * @since Kiwi 1.4
178   */
179
180  public void setItems(Iterator items)
181    {
182    model.clear();
183
184    while(items.hasNext())
185      model.addElement(items.next());
186
187    list.setSelectedIndex(0);
188    }
189    
190
191  /** Get the currently selected object in the list.
192   *
193   * @return The selected item, or <code>null</code> if no item is currently
194   * selected.
195   */
196  
197  public Object getSelectedItem()
198    {
199    return(list.getSelectedValue());
200    }
201  
202  }
203
204/* end of source file */