alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.cvsclient.customizer;

import java.util.ArrayList;
import javax.swing.SwingUtilities;
import javax.accessibility.*;

import org.netbeans.modules.javacvs.passwd.*;
import org.netbeans.modules.javacvs.commands.ClientCreationException;
import org.openide.*;
import org.openide.DialogDisplayer;
import org.openide.util.*;

/**
 *
 * @author  Milos Kleint
 * @version
 */
public class LoginDialog extends javax.swing.JDialog {
    
    public final Object STATUS_CONNECTING = new Integer(0);
    public final Object STATUS_FAILED = new Integer(1);
    
    private static ArrayList lastSuccessfullLoggings = new ArrayList();
    
    private String connectStr = ""; // NOI18N
    private PasswdEntry entry = null;
    private boolean loggedIn = false;
    private boolean offline = false;
    private Thread loginThread = null;
    private CVSPasswd passwd = null;
    private int port;
    /** Creates new form LoginDialog */
    public LoginDialog(java.awt.Frame parent, boolean modal, String cvsRoot, CVSPasswd pass, int port) {
        super(parent, modal);
        initComponents();
        initAccessibility();
        connectStr = cvsRoot;
        passwd = pass;
        this.port = port;
        setTitle(java.util.ResourceBundle.getBundle("org/netbeans/modules/cvsclient/customizer/Bundle").getString("LoginDialog.title")); // NOI18N
        lblLogin.setText(java.text.MessageFormat.format(org.openide.util.NbBundle.getBundle(LoginDialog.class).getString("LoginDialog.lblLogin.text"), new Object[] { cvsRoot })); // NOI18N
        lblPasswd.setDisplayedMnemonic(java.util.ResourceBundle.getBundle("org/netbeans/modules/cvsclient/customizer/Bundle").getString("LoginDialog.lblPasswd.mnemonic").charAt(0)); // NOI18N
        lblPasswd.setLabelFor(txPasswd);
        btnLogin.setMnemonic(java.util.ResourceBundle.getBundle("org/netbeans/modules/cvsclient/customizer/Bundle").getString("LoginDialog.btnLogin.mnemonic").charAt(0));
        btnCancel.setMnemonic(java.util.ResourceBundle.getBundle("org/netbeans/modules/cvsclient/customizer/Bundle").getString("LoginDialog.btnCancel.mnemonic").charAt(0));
        pack();
        txPasswd.requestFocus();
        getRootPane().setDefaultButton(btnLogin);
    }
    
    /** 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 FormEditor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        loginPanel = new javax.swing.JPanel();
        lblLogin = new javax.swing.JLabel();
        lblPasswd = new javax.swing.JLabel();
        txPasswd = new javax.swing.JPasswordField();
        buttonPanel = new javax.swing.JPanel();
        btnLogin = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        statusPanel = new javax.swing.JPanel();
        statusLabel = new javax.swing.JLabel();

        getContentPane().setLayout(new java.awt.GridBagLayout());

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        loginPanel.setLayout(new java.awt.GridBagLayout());

        lblLogin.setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/cvsclient/customizer/Bundle").getString("LoginDialog.lblLogin.text"));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
        loginPanel.add(lblLogin, gridBagConstraints);

        lblPasswd.setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/cvsclient/customizer/Bundle").getString("LoginDialog.lblPasswd.text"));
        lblPasswd.setLabelFor(txPasswd);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 12);
        loginPanel.add(lblPasswd, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        loginPanel.add(txPasswd, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(12, 12, 12, 11);
        getContentPane().add(loginPanel, gridBagConstraints);

        buttonPanel.setLayout(new java.awt.GridBagLayout());

        btnLogin.setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/cvsclient/customizer/Bundle").getString("LoginDialog.btnLogin.text"));
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginButtonActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 1.0;
        buttonPanel.add(btnLogin, gridBagConstraints);

        btnCancel.setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/cvsclient/customizer/Bundle").getString("LoginDialog.btnCancel.text"));
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                offlineButtonActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
        buttonPanel.add(btnCancel, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 12, 12, 11);
        getContentPane().add(buttonPanel, gridBagConstraints);

        statusPanel.setLayout(new java.awt.GridBagLayout());

        statusPanel.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.LOWERED));
        statusLabel.setText(" ");
        statusLabel.setEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        statusPanel.add(statusLabel, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 12, 17, 11);
        getContentPane().add(statusPanel, gridBagConstraints);

    }//GEN-END:initComponents
    
  private void offlineButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_offlineButtonActionPerformed
      // Add your handling code here:
      loggedIn = false;
      if (loginThread != null) {
          if (loginThread.isAlive()) {
              loginThread.interrupt();
          }
      }
      offline = true;
      closeDialog(null);
  }//GEN-LAST:event_offlineButtonActionPerformed
  
  private void loginButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginButtonActionPerformed
      // Add your handling code here:
      loginThread = new Thread("CVS_AUTHORIZING_THREAD") { // NOI18N
          public void run() {
              //entry = new PasswdEntry();
              //              passwd.remove(connectStr);
              //              pasFile.add(connectStr, password); //CVSPasswd.scramble(password));
              //              pasFile.savePassFile();
              //boolean setRight = entry.setEntry(connectStr + " " + CVSPasswd.scramble(password));
              //if (!setRight) {D("wrongly set entry.");}
              checkLogin();
          }
      };
      loginThread.start();
  }//GEN-LAST:event_loginButtonActionPerformed
  
  /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog
/*
    private boolean wasLoggedIn() {
        return lastSuccessfullLoggings.contains(connectStr);
    }
 */
    /**
     * Show the login dialog and start the login process immediately.
     */
    public void showLogging() {
/*
 if (wasLoggedIn()) {
            loggedIn = true; // believe, that the user is still correctly logged in
            return;
        }
 */
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        Runnable login = new Runnable() {
                            public void run() {
                                checkLogin(); // this should be called after show();
                            }
                        };
                        new Thread(login, "CVS_AUTHORIZING_THREAD").start(); // NOI18N
                    }
                });
            }
        });
        show();
    }
    
    private void checkLogin() {
        lastSuccessfullLoggings.remove(connectStr);
        txPasswd.setEnabled(false);
        btnLogin.setEnabled(false);
        setStatus(STATUS_CONNECTING, null);
        StringBuffer message = new StringBuffer();
        String password = null;
        loggedIn = false;
        try {
            PasswdEntry entry = new PasswdEntry();
            password = new String(txPasswd.getPassword());
            entry.setEntry(connectStr + " " + CVSPasswd.scramble(password)); // NOI18N
            loggedIn = passwd.checkServer(entry, port);
        } catch (org.netbeans.lib.cvsclient.connection.AuthenticationException exc1) {
            if (exc1.getUnderlyingThrowable() instanceof java.net.UnknownHostException) {
                setStatus(org.openide.util.NbBundle.getBundle(LoginDialog.class).getString(
                "LoginDialog.unknownHost")); // NOI18N
                return;
            }
            if (exc1.getUnderlyingThrowable() instanceof java.io.IOException) {
                setStatus(org.openide.util.NbBundle.getBundle(LoginDialog.class).getString(
                "LoginDialog.connectionIOError")); // NOI18N
                return;
            }
        } finally {
            txPasswd.setEnabled(true);
            btnLogin.setEnabled(true);
        }
        if (!loggedIn) {
            setStatus(STATUS_FAILED, message.toString());
            txPasswd.requestFocus();
            txPasswd.setSelectionStart(0);
            txPasswd.setSelectionEnd(txPasswd.getText().length());
        } else {
            //           lastSuccessfullLoggings.add(connectStr);
            //            closeDialog(null);
            setStatus(org.openide.util.NbBundle.getBundle(LoginDialog.class).getString(
            "LoginDialog.status.succeeded")); // NOI18N
            passwd.add(connectStr, new String(txPasswd.getPassword()));
            try {
                passwd.savePassFile();
            } catch (ClientCreationException exc) {
                    NotifyDescriptor mess = new NotifyDescriptor.Message(exc.getLocalizedMessage(),
                    NotifyDescriptor.ERROR_MESSAGE);
                    DialogDisplayer.getDefault().notify(mess);
//OLD way                    TopManager.getDefault().notifyException(exc);
            }
            setVisible(false);
            dispose();
        }
    }
    
    public void setConnectString(String connectStr) {
        this.connectStr = connectStr;
    }
    
    public void setPserverName(String pserverName) {
        lblLogin.setText(java.text.MessageFormat.format(org.openide.util.NbBundle.getBundle(LoginDialog.class).getString("LoginDialog.loginLabel.text"), new Object[] { pserverName })); // NOI18N
        pack();
    }
    
    public void setStatus(Object status) {
        setStatus(status, null);
    }
    
    public void setStatus(Object status, String message) {
        if (status.equals(STATUS_FAILED)) {
            statusLabel.setText(org.openide.util.NbBundle.getBundle(LoginDialog.class).getString("LoginDialog.status.failed")); // NOI18N
            if (message != null) statusLabel.setToolTipText(message);
        } else if (status.equals(STATUS_CONNECTING)) {
            statusLabel.setText(org.openide.util.NbBundle.getBundle(LoginDialog.class).getString("LoginDialog.status.connecting")); // NOI18N
        } else if (status instanceof String) {
            statusLabel.setText((String) status);
            //            statusLabel.setText();
        }
    }
    
    public boolean isLoggedIn() {
        return loggedIn;
    }
    
    public boolean isOffline() {
        return offline;
    }
    
    public String getPassword() {
        return new String(txPasswd.getPassword());
    }
    
    public static LoginDialog createDialog(String cvsRoot, CVSPasswd pass, int port) {
        LoginDialog dialog = new LoginDialog(new javax.swing.JFrame(), true, cvsRoot, pass, port);
        return dialog;
    }
    
    /**
     * @param args the command line arguments
     *
     * public static void main (String args[]) {
     * new LoginDialog (new javax.swing.JFrame (), true, null).show ();
     * }
     */
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JPanel loginPanel;
    private javax.swing.JPanel statusPanel;
    private javax.swing.JButton btnLogin;
    private javax.swing.JLabel lblLogin;
    private javax.swing.JLabel lblPasswd;
    private javax.swing.JLabel statusLabel;
    private javax.swing.JButton btnCancel;
    private javax.swing.JPasswordField txPasswd;
    // End of variables declaration//GEN-END:variables
    private static final java.util.ResourceBundle bundle = NbBundle.getBundle(LoginDialog.class);
    
    private void initAccessibility() {
        
        AccessibleContext context = this.getAccessibleContext();
        context.setAccessibleDescription(bundle.getString("ACSD_LoginDialog"));
        
        context = statusLabel.getAccessibleContext();
        context.setAccessibleDescription(bundle.getString("ACSD_LoginDialog.statusLabel"));
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.