import java.awt.*; import java.awt.event.*; import javax.swing.*; /* * The login dialog for SCorTrap. It's on the screen on top of the main frame * whenever there is no operator logged in. It disappears only after a valid * login. * * I added an "exit" button to this dialog because it was a real nuisance to * have to log in just to end the program! This was not required. * * Part of sample solution for Assignment 4, CISC 323, Winter 2006. * author: M. Lamb *//* * */ public class LoginDialog extends JDialog implements ActionListener { // Fields for entering name and password. A JPasswordField is just like a // JTextField, except the input is echoed as stars. private JTextField nameField; private JPasswordField passwordField; // button to submit login information private JButton loginButton; // button to exit the program private JButton exitButton; // the parent frame (a SCorTrap frame) private SCorTrap parent; // the database for looking up operator names and passwords private SCorTrapDatabase database; // the name of the operator who logged on last time this dialog was used private String operatorName; // Accessor method for the name of the operator -- so other classes can // query this but not change it public String getOpName() { return operatorName; } /* * Constructor -- creates this dialog at the beginning of the program. * Does *not* make it visible. * Parameters: * parent: the main frame, frozen while this dialog is visible * database: a database object for looking up operator names & passwords */ public LoginDialog(SCorTrap parent, SCorTrapDatabase database) { super(parent, true); // set this up as a modal dialog, blocking the parent this.database = database; // remember for future use this.parent = parent; Container contents = getContentPane(); contents.setLayout(new GridLayout(0,1)); JLabel headerLabel = new JLabel("Please log in to SCorTrap"); headerLabel.setFont(new Font("Serif", Font.BOLD, 20)); JPanel nameRow = new JPanel(); nameRow.add(new JLabel("name: ")); nameField = new JTextField(20); nameRow.add(nameField); contents.add(nameRow); JPanel passwordRow = new JPanel(); passwordRow.add(new JLabel("password: ")); passwordField = new JPasswordField(10); passwordRow.add(passwordField); contents.add(passwordRow); JPanel buttonRow = new JPanel(); loginButton = new JButton("log in"); loginButton.addActionListener(this); buttonRow.add(loginButton); exitButton = new JButton("exit"); exitButton.addActionListener(this); buttonRow.add(exitButton); contents.add(buttonRow); setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); pack(); // don't make the dialog visible yet } // end constructor // This method is called when a button is clicked. // It looks up the name and password in the database. If they // match a real operator, this login dialog disappears. If not, // pops up an error message. public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == loginButton) { operatorName = nameField.getText(); // JPasswordField has a getText method, but for some reason it has // been "deprecated" meaning that they're advising you not to use it // in the newest versions of Java. Intead, they have a getPassword // method that returns an array of characters -- which I immediately // convert to a String in the following line. String password = new String(passwordField.getPassword()); Operator op = database.lookupOperator(operatorName); if (op == null || !(password.equals(op.password))) { JOptionPane.showMessageDialog(this, "Error: bad name or password"); } else { // Successful login. Clear out text fields for next use and disappear. nameField.setText(""); passwordField.setText(""); // Tell main frame if new operator is an administrator parent.setAdminStatus(op.isAdmin); setVisible(false); } // end if } else if (source == exitButton) { parent.endProgram(); } else JOptionPane.showMessageDialog(this, "internal error: unknown button"); } // end actionPerformed } //end class LoginDialog