Source code: org/schooltool/client/gui/dialogs/LoginDialog.java
1 package org.schooltool.client.gui.dialogs;
2
3 import javax.swing.JOptionPane;
4 import javax.swing.JDialog;
5 import javax.swing.JTextField;
6 import javax.swing.JLabel;
7 import javax.swing.JPasswordField;
8
9 import java.beans.*; //Property change stuff
10 import java.awt.*;
11 import java.awt.event.*;
12 import org.schooltool.ejb.session.client.access.*;
13 import org.schooltool.complextypes.access.*;
14 import org.schooltool.client.gui.dialogs.*;
15 import java.util.*;
16 import java.rmi.RemoteException;
17 import javax.swing.JComboBox;
18 import org.schooltool.client.gui.widgets.*;
19
20 public class LoginDialog extends JDialog {
21 private String userName = null;
22 private String password = null;
23 private long schoolProfilePK = 0;
24 private JOptionPane optionPane;
25 private accessModule accessModule;
26 private ComboBoxViewModel schoolModel;
27
28 private boolean loggedOn = false;
29
30 public String getUserName() {
31 return userName;
32 }
33
34 public String getPassword() {
35 return password;
36 }
37
38 public boolean isLoggedOn(){
39 return loggedOn;
40 }
41 private boolean doLogin(String userName, String password, long profilePK){
42 boolean returnValue = false;
43 try{
44 System.out.println("Checking again Profile : " + profilePK);
45 returnValue = accessModule.validateUser(userName, password, new Integer(1), new Long(profilePK));
46 }catch(Exception re){
47 System.out.println("Exception occurred " + re.getMessage());
48 }
49 return returnValue;
50 }
51
52 public LoginDialog(Frame aFrame, accessModule accessModule) {
53 super(aFrame, true);
54
55 setTitle("Login");
56
57 this.accessModule = accessModule;
58 JLabel usernameLabel = new JLabel("Username");
59 JLabel passwordLabel = new JLabel("Password");
60 JLabel schoolLabel = new JLabel("School ");
61
62 final JTextField usernameText = new JTextField(10);
63 final JPasswordField passwordField = new JPasswordField(10);
64 final JComboBox schoolCombo = new JComboBox();
65
66 try{
67 Collection records = this.accessModule.findAllSchoolProfile(1);
68 schoolModel = new ComboBoxViewModel(records, new String[]{"getSchoolName"}, 0);
69 schoolCombo.setModel(schoolModel);
70
71 Iterator iterator = records.iterator();
72 if (iterator.hasNext()){
73 SchoolProfile profile = (SchoolProfile)iterator.next();
74 schoolModel.setSelectedItemByPK(profile.getPrimaryKey());
75 }
76 }catch(Exception ex){
77 }
78
79 Object[] array = {usernameLabel, usernameText, passwordLabel, passwordField, schoolLabel, schoolCombo};
80 final String btnString1 = "Login";
81 final String btnString2 = "Cancel";
82 Object[] options = {btnString1, btnString2};
83
84 optionPane = new JOptionPane(array,
85 JOptionPane.QUESTION_MESSAGE,
86 JOptionPane.YES_NO_OPTION,
87 null,
88 options,
89 options[0]);
90 setContentPane(optionPane);
91 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
92 addWindowListener(new WindowAdapter() {
93 public void windowClosing(WindowEvent we) {
94 /*
95 * Instead of directly closing the window,
96 * we're going to change the JOptionPane's
97 * value property.
98 */
99 optionPane.setValue(new Integer(
100 JOptionPane.CLOSED_OPTION));
101 }
102 });
103
104 usernameText.addActionListener(new ActionListener() {
105 public void actionPerformed(ActionEvent e) {
106 optionPane.setValue(btnString1);
107 }
108 });
109
110 optionPane.addPropertyChangeListener(new PropertyChangeListener() {
111 public void propertyChange(PropertyChangeEvent e) {
112 String prop = e.getPropertyName();
113
114 if (isVisible() && (e.getSource() == optionPane) && (prop.equals(JOptionPane.VALUE_PROPERTY) || prop.equals(JOptionPane.INPUT_VALUE_PROPERTY))) {
115 Object value = optionPane.getValue();
116
117 if (value == JOptionPane.UNINITIALIZED_VALUE) {
118 //ignore reset
119 return;
120 }
121
122 // Reset the JOptionPane's value.
123 // If you don't do this, then if the user
124 // presses the same button next time, no
125 // property change event will be fired.
126 optionPane.setValue(JOptionPane.UNINITIALIZED_VALUE);
127
128 if (value.equals(btnString1)) {
129 userName = usernameText.getText();
130 password = new String(passwordField.getPassword());
131 schoolProfilePK = schoolModel.getPrimaryKey();
132
133 System.out.println("Profile : " + schoolProfilePK);
134
135 if (doLogin(userName, password, schoolProfilePK)) {
136 // we're done; dismiss the dialog
137 loggedOn = true;
138 setVisible(false);
139 } else {
140 // text was invalid
141 usernameText.selectAll();
142 JOptionPane.showMessageDialog(LoginDialog.this, "Invalid login and password combination", "Try again", JOptionPane.ERROR_MESSAGE);
143 userName = null;
144 }
145 } else { // user closed dialog or clicked cancel
146 setVisible(false);
147 }
148 }
149 }
150 });
151 }
152 }