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: LoginDialog.java,v $
023   Revision 1.12  2004/05/31 07:42:09  markl
024   Transfer focus back to username field after failed login.
025
026   Revision 1.11  2004/05/05 23:20:24  markl
027   comment block updates
028
029   Revision 1.10  2004/03/16 06:43:39  markl
030   LocaleManager method change
031
032   Revision 1.9  2003/01/19 09:41:00  markl
033   Javadoc & comment header updates.
034
035   Revision 1.8  2002/03/08 22:50:15  markl
036   Added new methods.
037
038   Revision 1.7  2001/03/20 00:54:55  markl
039   Fixed deprecated calls.
040
041   Revision 1.6  2001/03/12 09:56:55  markl
042   KLabel/KLabelArea changes.
043
044   Revision 1.5  2001/03/12 05:19:59  markl
045   Source code cleanup.
046
047   Revision 1.4  1999/06/03 06:48:58  markl
048   Minor tweaks.
049
050   Revision 1.3  1999/04/19 06:06:24  markl
051   I18N changes, new constructor.
052
053   Revision 1.2  1999/01/10 03:22:17  markl
054   added GPL header & RCS tag
055   ----------------------------------------------------------------------------
056*/
057
058package kiwi.ui.dialog;
059
060import java.awt.*;
061import java.awt.event.*;
062import javax.swing.*;
063import javax.swing.border.EmptyBorder;
064
065import kiwi.util.*;
066import kiwi.ui.*;
067import kiwi.event.*;
068
069/** A general-purpose login dialog. The login dialog includes text fields for
070  * the entry of a username and password and <i>OK</i> and <i>Cancel</i>
071  * buttons. Validation is performed using the provided
072  * <code>LoginValidator</code>. If a validation succeeds, the login dialog
073  * disappears. Otherwise, a warning dialog is displayed, and once it's
074  * dismissed by the user, control returns to the login dialog. The dialog is
075  * unconditionally modal.
076  *
077  * <p><center>
078  * <img src="snapshot/LoginDialog.gif"><br>
079  * <i>An example LoginDialog.</i>
080  * </center>
081  *
082  * @author Mark Lindner
083  */
084
085public class LoginDialog extends ComponentDialog
086  {
087  private LoginValidator validator;
088  private JTextField t_user;
089  private JPasswordField t_passwd;
090  private DialogSet dialogs;
091  private String loginFailedMessage;
092  private KLabel l_username, l_password;
093
094  /** Construct a new <code>LoginDialog</code> with a default title.
095    *
096    * @param parent The parent frame.
097    * @param comment A comment string to display in the upper portion of the
098    * window.
099    * @param validator The validator to check username/password pairs against.
100    */
101
102  public LoginDialog(Frame parent, String comment, LoginValidator validator)
103    {
104    this(parent, "", comment, validator);
105    }
106  
107  /** Construct a new <code>LoginDialog</code>.
108    *
109    * @param parent The parent frame.
110    * @param title The dialog window's title.
111    * @param comment A comment string to display in the upper portion of the
112    * window.
113    * @param validator The validator to check username/password pairs against.
114    */
115
116  public LoginDialog(Frame parent, String title, String comment,
117                     LoginValidator validator)
118    {
119    super(parent, title, true);
120    this.validator = validator;
121    setResizable(false);
122
123    setComment(comment);
124    
125    setIcon(KiwiUtils.getResourceManager().getIcon("keylock.gif"));
126    
127    dialogs = new DialogSet(this, DialogSet.CENTER_PLACEMENT);
128    }
129
130  /*
131   */
132
133  protected Component buildDialogUI()
134    {
135    LocaleData loc = LocaleManager.getDefault().getLocaleData("KiwiDialogs");
136
137    loginFailedMessage = loc.getMessage("kiwi.dialog.message.login_failed");
138    
139    KPanel main = new KPanel();
140    GridBagLayout gb = new GridBagLayout();
141    main.setLayout(gb);
142
143    GridBagConstraints gbc = new GridBagConstraints();
144    gbc.anchor = gbc.NORTHWEST;
145    gbc.fill = gbc.HORIZONTAL;
146    gbc.gridwidth = 1;
147    Insets insets0 = new Insets(0, 0, 5, 5);
148    Insets insets1 = new Insets(0, 0, 5, 0);
149    
150    l_username = new KLabel(loc.getMessage("kiwi.dialog.prompt.username"));
151    gbc.gridwidth = gbc.RELATIVE;
152    gbc.insets = insets0;
153    main.add(l_username, gbc);
154
155    gbc.gridwidth = gbc.REMAINDER;
156    gbc.weightx = 1;
157    t_user = new JTextField(15);
158    t_user.addActionListener(new ActionListener()
159      {
160      public void actionPerformed(ActionEvent evt)
161        {
162        t_passwd.requestFocus();
163        }
164      });
165    gbc.insets = insets1;
166    main.add(t_user, gbc);
167
168    gbc.gridwidth = gbc.RELATIVE;
169    gbc.weightx = 0;
170    gbc.insets = insets0;    
171    l_password = new KLabel(loc.getMessage("kiwi.dialog.prompt.password"));
172    main.add(l_password, gbc);
173
174    gbc.gridwidth = gbc.REMAINDER;
175    gbc.weightx = 1;
176    gbc.insets = insets1;    
177    t_passwd = new JPasswordField(15);
178    registerTextInputComponent(t_passwd);
179    main.add(t_passwd, gbc);
180
181    if(getTitle().length() == 0)
182      setTitle(loc.getMessage("kiwi.dialog.title.login"));
183    
184    return(main);
185    }
186
187  /*
188   */
189  
190  protected boolean accept()
191    {
192    boolean ok = validator.validate(t_user.getText(),
193                                    new String(t_passwd.getPassword()));
194    if(ok)
195      return(true);
196
197    dialogs.showMessageDialog(loginFailedMessage);
198    t_user.requestFocus();
199    return(false);
200    }
201
202  /*
203   */
204  
205  protected void cancel()
206    {
207    validator.validationCancelled();
208    }
209
210  /** Show or hide the dialog. */
211
212  public void setVisible(boolean flag)
213    {
214    if(flag)
215      {
216      t_user.setText("");
217      t_passwd.setText("");
218      t_user.requestFocus();
219      }
220    super.setVisible(flag);
221    }
222
223  /** Set the text for the username prompt label.
224   *
225   * @param text The new text for the label
226   * @since Kiwi 1.3.3
227   */
228
229  public void setUsernamePromptText(String text)
230    {
231    if(text == null)
232      return;
233
234    l_username.setText(text);
235    pack();
236    }
237  
238  /** Set the text for the password prompt label.
239   *
240   * @param text The new text for the label
241   * @since Kiwi 1.3.3
242   */
243
244  public void setPasswordPromptText(String text)
245    {
246    if(text == null)
247      return;
248    
249    l_password.setText(text);
250    pack();
251    }  
252  
253  }
254
255/* end of source file */