001: /* ====================================================================
002: * The QueryForm License, Version 1.1
003: *
004: * Copyright (c) 1998 - 2003 David F. Glasser. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution,
020: * if any, must include the following acknowledgment:
021: * "This product includes software developed by
022: * David F. Glasser."
023: * Alternately, this acknowledgment may appear in the software itself,
024: * if and wherever such third-party acknowledgments normally appear.
025: *
026: * 4. The names "QueryForm" and "David F. Glasser" must
027: * not be used to endorse or promote products derived from this
028: * software without prior written permission. For written
029: * permission, please contact dglasser@pobox.com.
030: *
031: * 5. Products derived from this software may not be called "QueryForm",
032: * nor may "QueryForm" appear in their name, without prior written
033: * permission of David F. Glasser.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL DAVID F. GLASSER, THE APACHE SOFTWARE
039: * FOUNDATION OR ITS CONTRIBUTORS, OR ANY AUTHORS OR DISTRIBUTORS
040: * OF THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This product includes software developed by the
051: * Apache Software Foundation (http://www.apache.org/).
052: *
053: * ====================================================================
054: *
055: * $Source: /cvsroot/qform/qform/src/org/glasser/swing/LoginDialog.java,v $
056: * $Revision: 1.2 $
057: * $Author: dglasser $
058: * $Date: 2003/08/11 00:59:24 $
059: *
060: * --------------------------------------------------------------------
061: */
062: package org.glasser.swing;
063:
064: import java.awt.*;
065: import java.awt.event.*;
066: import javax.swing.*;
067: import javax.swing.text.JTextComponent;
068: import javax.swing.border.EmptyBorder;
069: import org.glasser.util.*;
070: import org.glasser.sql.LoginHandler;
071: import org.glasser.sql.LoginHandlerException;
072:
073: public class LoginDialog extends JDialog implements ActionListener {
074:
075: private Object loginResult = null;
076:
077: private JTextField txtUserId = new JTextField();
078:
079: private JPasswordField txtPassword = new JPasswordField();
080:
081: private JButton btnSubmit = new JButton("Submit");
082:
083: private JButton btnCancel = new JButton("Cancel");
084:
085: private LoginHandler loginHandler = null;
086:
087: public Object getLoginResult() {
088: return loginResult;
089: }
090:
091: public void setLoginHandler(LoginHandler loginHandler) {
092: this .loginHandler = loginHandler;
093: }
094:
095: public LoginHandler getLoginHandler() {
096: return loginHandler;
097: }
098:
099: public void setUserId(String userID) {
100: this .txtUserId.setText(userID);
101: }
102:
103: public void setPassword(String password) {
104: txtPassword.setText(password);
105: }
106:
107: public LoginDialog() {
108: this (null);
109: }
110:
111: public LoginDialog(Frame parent) {
112: super (parent);
113:
114: setModal(true);
115:
116: JPanel panel = new JPanel();
117: panel.setBorder(new EmptyBorder(10, 10, 10, 10));
118: GridBagConstraints gc = new GridBagConstraints();
119: panel.setLayout(new GridBagLayout());
120: JLabel idLbl = new JLabel("User ID:");
121: JLabel pwLbl = new JLabel("Password:");
122: Insets labelInsets = new Insets(0, 0, 0, 0);
123: Insets fieldInsets = new Insets(0, 5, 5, 0);
124:
125: // add the User ID label
126: gc.anchor = gc.NORTH;
127: gc.fill = gc.HORIZONTAL;
128: gc.insets = labelInsets;
129: panel.add(idLbl, gc);
130:
131: // add the user ID textfield.
132: gc.gridx = 1;
133: gc.weightx = 1;
134: gc.insets = fieldInsets;
135: panel.add(this .txtUserId, gc);
136:
137: // add the password label
138: gc.gridx = 0;
139: gc.gridy = 1;
140: gc.weightx = 0;
141: gc.insets = labelInsets;
142: panel.add(pwLbl, gc);
143:
144: // now add the password field
145: gc.gridx = 1;
146: gc.weightx = 1;
147: gc.insets = fieldInsets;
148: panel.add(this .txtPassword, gc);
149:
150: // add the button panel
151: JPanel buttonPanel = new JPanel();
152: gc.gridx = 0;
153: gc.gridwidth = 2;
154: gc.gridy = 2;
155: // make this row "stretchable"
156: gc.weighty = 1;
157: gc.ipadx = 20;
158: panel.add(buttonPanel, gc);
159:
160: // add the buttons to the button panel
161: buttonPanel.setLayout(new FlowLayout());
162: buttonPanel.add(btnSubmit);
163: buttonPanel.add(btnCancel);
164:
165: btnSubmit.addActionListener(this );
166: btnCancel.addActionListener(this );
167:
168: GUIHelper.enterPressesWhenFocused(btnSubmit);
169: GUIHelper.enterPressesWhenFocused(btnCancel);
170:
171: // instantiate a key listener for the text boxes. This
172: // will be used to make the focus move to their next focusable
173: // component when the enter key is pressed.
174: KeyHandler keyHandler = new KeyHandler();
175: txtUserId.addKeyListener(keyHandler);
176: txtPassword.addKeyListener(keyHandler);
177: setTitle("Login");
178:
179: // this WindowListener will place the cursor into the first
180: // blank textfield when the dialog is opened.
181: this .addWindowListener(new WindowAdapter() {
182: /**
183: * Focus the first blank field when the dialog is opened.
184: */
185: public void windowOpened(WindowEvent e) {
186: placeCursor();
187: }
188:
189: /**
190: * Focus the first blank field when the dialog is opened.
191: */
192: public void windowActivated(WindowEvent e) {
193: placeCursor();
194: }
195: });
196:
197: setContentPane(panel);
198: pack();
199:
200: }
201:
202: /**
203: * This method is called when the dialog is opened. It places the
204: * cursor in the first blank textbox it finds.
205: *
206: * @return true if a blank field was found and a call to its requestFocus()
207: * method was made, false otherwise.
208: */
209: private boolean placeCursor() {
210: String s = txtUserId.getText();
211:
212: // if the userID field is blank, focus it.
213: if (s == null || s.trim().length() == 0) {
214: txtUserId.requestFocus();
215: return true;
216: } else {
217:
218: // if the password field is blank, focus it.
219: s = null;
220: char[] pw = txtPassword.getPassword();
221: if (pw != null) {
222: s = new String(txtPassword.getPassword());
223: }
224: if (s == null || s.trim().length() == 0) {
225: txtPassword.requestFocus();
226: return true;
227: }
228: }
229: return false;
230: }
231:
232: public void actionPerformed(ActionEvent e) {
233:
234: if (e.getSource() == btnSubmit) {
235: try {
236: if (loginHandler == null) {
237: errMsg(
238: "No LoginHandler has been set for this LoginDialog.",
239: "Application Error");
240: return;
241: }
242:
243: String userID = this .txtUserId.getText();
244: if (userID != null && userID.trim().length() == 0) {
245: userID = null;
246: }
247:
248: char[] pwChars = txtPassword.getPassword();
249:
250: String pw = null;
251: if (pwChars != null)
252: pw = new String(pwChars);
253:
254: loginResult = loginHandler.login(userID, pw);
255:
256: txtUserId.setText(null);
257: txtPassword.setText(null);
258: setVisible(false);
259: } catch (Exception ex) {
260: errMsg(ex.getMessage(), "Login Error");
261: }
262: } else if (e.getSource() == btnCancel) {
263: setVisible(false);
264: }
265: }
266:
267: public void errMsg(String msg, String title) {
268: if (title == null)
269: title = "QueryForm";
270: Toolkit.getDefaultToolkit().beep();
271: JOptionPane.showMessageDialog(null, msg, title,
272: JOptionPane.ERROR_MESSAGE);
273: }
274:
275: /**
276: * This will select all of the text in the given JTextField object.
277: */
278: public void selectAll(JTextComponent field) {
279:
280: if (field == null)
281: return;
282: if (field.getText() == null)
283: return;
284:
285: field.setSelectionStart(0);
286: field.setSelectionEnd(field.getText().length());
287: field.requestFocus();
288:
289: }
290:
291: class KeyHandler extends KeyAdapter {
292:
293: public void keyReleased(KeyEvent e) {
294: if (e.getKeyCode() == e.VK_ENTER) {
295: if (e.getSource() == txtUserId) {
296: selectAll(txtPassword);
297: } else if (e.getSource() == txtPassword) {
298: btnSubmit.doClick();
299: }
300: } else if (e.getKeyCode() == e.VK_ESCAPE) {
301: txtUserId.setText(null);
302: txtPassword.setText(null);
303: setVisible(false);
304: }
305: }
306: }
307:
308: }
|