package ciscoroutertool.gui; import javax.swing.JOptionPane; /** * The dialog that forces users to log in to the application * @version 0.01ALPHA * @author Andrew Johnston */ public class AuthDialog extends javax.swing.JFrame { /** * The parent window */ private MainGUI parent; /** * Creates new form AuthDialog */ public AuthDialog(MainGUI _parent) { initComponents(); parent = _parent; parent.setVisible(false); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { lblPrompt = new javax.swing.JLabel(); lblUsername = new javax.swing.JLabel(); lblPassword = new javax.swing.JLabel(); fieldPassword = new javax.swing.JPasswordField(); fieldUsername = new javax.swing.JTextField(); btnClose = new javax.swing.JButton(); btnLogin = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Please Log In"); lblPrompt.setText("You must provide authentication to use this application!"); lblUsername.setText("Username: "); lblPassword.setText("Password: "); btnClose.setText("Close"); btnClose.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnCloseActionPerformed(evt); } }); btnLogin.setText("Log In"); btnLogin.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnLoginActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(lblPassword) .addComponent(lblUsername)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(fieldPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE) .addComponent(fieldUsername))) .addComponent(lblPrompt, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(35, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(btnLogin) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnClose) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(lblPrompt) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(lblUsername) .addComponent(fieldUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(lblPassword) .addComponent(fieldPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnClose) .addComponent(btnLogin)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); pack(); }// </editor-fold>//GEN-END:initComponents /** * Closes the Authentication dialog (closing the application). * @param evt The ActionEvent object with relevant data */ private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed System.exit(0); }//GEN-LAST:event_btnCloseActionPerformed /** * Checks the credentials and allows login if the credentials are correct. * @param evt The ActionEvent object with relevant data */ private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginActionPerformed String username = fieldUsername.getText().trim(); String password = new String(fieldPassword.getPassword()); if (MainGUI.settingsManager.checkAuth(username, password)) { parent.setVisible(true); parent.setEnabled(true); this.dispose(); } else { JOptionPane.showMessageDialog(this, "Please reenter your credentials", "Invalid ID or Password", JOptionPane.ERROR_MESSAGE); } }//GEN-LAST:event_btnLoginActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnClose; private javax.swing.JButton btnLogin; private javax.swing.JPasswordField fieldPassword; private javax.swing.JTextField fieldUsername; private javax.swing.JLabel lblPassword; private javax.swing.JLabel lblPrompt; private javax.swing.JLabel lblUsername; // End of variables declaration//GEN-END:variables }