import java.awt.*; import java.awt.event.*; import javax.swing.*; /* * A dialog for creating a new operator. Part of the SCorTrap program. * * Part of sample solution for Assignment 4, CISC 323, Winter 2006. * author: M. Lamb */ public class NewOperatorDialog extends JDialog implements ActionListener { // the database for storing operator names and passwords private SCorTrapDatabase database; // fields for entering information private JTextField nameField; private JPasswordField passwordField; private JButton adminButton; // create operator as an administrator private JButton nonAdminButton; // create operator as a non-administrator private JButton cancelButton; // cancel without creating operator /* * 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 NewOperatorDialog(SCorTrap parent, SCorTrapDatabase database) { super(parent, true); // set up as a modal dialog this.database = database; // remember the database for future use Container contents = getContentPane(); contents.setLayout(new GridLayout(0,1)); contents.add(new JLabel("Please enter information about new operator")); 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); adminButton = new JButton("create as administrator"); JPanel buttonRow1 = new JPanel(); buttonRow1.add(adminButton); contents.add(buttonRow1); nonAdminButton = new JButton("create as non-administrator"); JPanel buttonRow2 = new JPanel(); buttonRow2.add(nonAdminButton); contents.add(buttonRow2); cancelButton = new JButton("cancel"); JPanel buttonRow3 = new JPanel(); buttonRow3.add(cancelButton); contents.add(buttonRow3); adminButton.addActionListener(this); nonAdminButton.addActionListener(this); cancelButton.addActionListener(this); setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); pack(); // don't make the dialog visible yet } // end NewOperatorDialog /* * This method is called when the user clicks a button */ public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == adminButton) { createOperator(true); } else if (source == nonAdminButton) { createOperator(false); } else if (source == cancelButton) { setVisible(false); } else { JOptionPane.showMessageDialog(this, "internal error: unknown button"); } // end if } //end actionPerformed /* * Creates a new operator from the data in the text fields * Parameter: true if new operator should be an administrator */ public void createOperator(boolean isAdmin) { String name = 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()); if (database.lookupOperator(name) != null) { JOptionPane.showMessageDialog(this, "Error: there is already an operator named " + name); } else { Operator newOp = new Operator(name, password, isAdmin); database.updateOperator(newOp); setVisible(false); } } // end createOperator } // end class NewOperatorDialog