Save This Page
Home » j2ssh-0.2.9-src » com.sshtools.common.authentication » [javadoc | source]
    1   /*
    2    *  SSHTools - Java SSH2 API
    3    *
    4    *  Copyright (C) 2002-2003 Lee David Painter and Contributors.
    5    *
    6    *  Contributions made by:
    7    *
    8    *  Brett Smith
    9    *  Richard Pernavas
   10    *  Erwin Bolwidt
   11    *
   12    *  This program is free software; you can redistribute it and/or
   13    *  modify it under the terms of the GNU General Public License
   14    *  as published by the Free Software Foundation; either version 2
   15    *  of the License, or (at your option) any later version.
   16    *
   17    *  This program is distributed in the hope that it will be useful,
   18    *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   19    *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   20    *  GNU General Public License for more details.
   21    *
   22    *  You should have received a copy of the GNU General Public License
   23    *  along with this program; if not, write to the Free Software
   24    *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
   25    */
   26   package com.sshtools.common.authentication;
   27   
   28   import com.sshtools.common.ui.IconWrapperPanel;
   29   import com.sshtools.common.ui.ResourceIcon;
   30   import com.sshtools.common.ui.SshToolsConnectionHostTab;
   31   import com.sshtools.common.ui.UIUtil;
   32   
   33   import com.sshtools.j2ssh.authentication.PasswordChangePrompt;
   34   
   35   import java.awt.BorderLayout;
   36   import java.awt.Component;
   37   import java.awt.Dialog;
   38   import java.awt.FlowLayout;
   39   import java.awt.Frame;
   40   import java.awt.GridBagConstraints;
   41   import java.awt.GridBagLayout;
   42   import java.awt.GridLayout;
   43   import java.awt.Insets;
   44   import java.awt.Window;
   45   import java.awt.event.ActionEvent;
   46   import java.awt.event.ActionListener;
   47   
   48   import javax.swing.BorderFactory;
   49   import javax.swing.JButton;
   50   import javax.swing.JDialog;
   51   import javax.swing.JLabel;
   52   import javax.swing.JOptionPane;
   53   import javax.swing.JPanel;
   54   import javax.swing.JPasswordField;
   55   import javax.swing.SwingConstants;
   56   import javax.swing.SwingUtilities;
   57   
   58   
   59   /**
   60    *
   61    *
   62    * @author $author$
   63    * @version $Revision: 1.15 $
   64    */
   65   public class PasswordChange implements PasswordChangePrompt {
   66       //
   67   
   68       /**  */
   69       public final static String PASSWORD_ICON = "/com/sshtools/common/authentication/largepassword.png";
   70   
   71       //
   72       private static PasswordChange instance;
   73   
   74       //
   75       private Component parent;
   76   
   77       private PasswordChange() {
   78       }
   79   
   80       /**
   81   *
   82   *
   83   * @param parent
   84   */
   85       public void setParentComponent(Component parent) {
   86           this.parent = parent;
   87       }
   88   
   89       /**
   90   *
   91   *
   92   * @param prompt
   93   *
   94   * @return
   95   */
   96       public String changePassword(String prompt) {
   97           Window w = (parent == null) ? null
   98                                       : (Window) SwingUtilities.getAncestorOfClass(Window.class,
   99                   parent);
  100           PasswordChangeDialog dialog = null;
  101   
  102           if (w instanceof Frame) {
  103               dialog = new PasswordChangeDialog((Frame) w, prompt);
  104           } else if (w instanceof Dialog) {
  105               dialog = new PasswordChangeDialog((Dialog) w, prompt);
  106           } else {
  107               dialog = new PasswordChangeDialog(prompt);
  108           }
  109   
  110           char[] p = dialog.getPassword();
  111   
  112           return (p == null) ? null : new String(p);
  113       }
  114   
  115       /**
  116   *
  117   *
  118   * @return
  119   */
  120       public static PasswordChange getInstance() {
  121           if (instance == null) {
  122               instance = new PasswordChange();
  123           }
  124   
  125           return instance;
  126       }
  127   
  128       class PasswordChangeDialog extends JDialog {
  129           JLabel promptLabel = new JLabel();
  130           JPasswordField password = new JPasswordField(15);
  131           JPasswordField confirm = new JPasswordField(15);
  132           boolean cancelled;
  133   
  134           PasswordChangeDialog(String prompt) {
  135               super((Frame) null, "Password Change", true);
  136               init(prompt);
  137           }
  138   
  139           PasswordChangeDialog(Frame frame, String prompt) {
  140               super(frame, "Password Change", true);
  141               init(prompt);
  142           }
  143   
  144           PasswordChangeDialog(Dialog dialog, String prompt) {
  145               super(dialog, "Password Change", true);
  146               init(prompt);
  147           }
  148   
  149           char[] getPassword() {
  150               return (cancelled == true) ? null : password.getPassword();
  151           }
  152   
  153           void init(String prompt) {
  154               setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  155   
  156               JPanel g = new JPanel(new GridBagLayout());
  157               GridBagConstraints gbc = new GridBagConstraints();
  158               gbc.insets = new Insets(0, 0, 2, 2);
  159               gbc.anchor = GridBagConstraints.WEST;
  160               gbc.fill = GridBagConstraints.HORIZONTAL;
  161               gbc.weightx = 0.0;
  162               UIUtil.jGridBagAdd(g, new JLabel("Password: "), gbc,
  163                   GridBagConstraints.RELATIVE);
  164               gbc.weightx = 1.0;
  165               UIUtil.jGridBagAdd(g, password, gbc, GridBagConstraints.REMAINDER);
  166               gbc.weightx = 0.0;
  167               UIUtil.jGridBagAdd(g, new JLabel("Confirm: "), gbc,
  168                   GridBagConstraints.RELATIVE);
  169               gbc.weightx = 1.0;
  170               UIUtil.jGridBagAdd(g, confirm, gbc, GridBagConstraints.REMAINDER);
  171   
  172               //
  173               promptLabel.setHorizontalAlignment(JLabel.CENTER);
  174   
  175               //  Main panel
  176               JPanel centerPanel = new JPanel(new BorderLayout());
  177               centerPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
  178               centerPanel.add(promptLabel, BorderLayout.NORTH);
  179               centerPanel.add(g, BorderLayout.CENTER);
  180   
  181               //  Create the bottom button panel
  182               JButton ok = new JButton("Ok");
  183               ok.setMnemonic('o');
  184               ok.setDefaultCapable(true);
  185               ok.addActionListener(new ActionListener() {
  186                       public void actionPerformed(ActionEvent evt) {
  187                           if (!new String(password.getPassword()).equals(
  188                                       new String(confirm.getPassword()))) {
  189                               JOptionPane.showMessageDialog(PasswordChangeDialog.this,
  190                                   "Passwords do not match. Please try again.",
  191                                   "Passwords do not match",
  192                                   JOptionPane.ERROR_MESSAGE);
  193                           } else {
  194                             setVisible(false);
  195                           }
  196                       }
  197                   });
  198               getRootPane().setDefaultButton(ok);
  199   
  200               JButton cancel = new JButton("Cancel");
  201               cancel.setMnemonic('c');
  202               cancel.addActionListener(new ActionListener() {
  203                       public void actionPerformed(ActionEvent evt) {
  204                           cancelled = true;
  205                           setVisible(false);
  206                       }
  207                   });
  208   
  209               JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 0, 0));
  210               southPanel.setBorder(BorderFactory.createEmptyBorder(4, 0, 0, 0));
  211               southPanel.add(cancel);
  212               southPanel.add(ok);
  213   
  214               //  Create the center banner panel
  215               IconWrapperPanel iconPanel = new IconWrapperPanel(new ResourceIcon(
  216                           SshToolsConnectionHostTab.AUTH_ICON), centerPanel);
  217               iconPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
  218   
  219               //  The main panel contains everything and is surrounded by a border
  220               JPanel mainPanel = new JPanel(new BorderLayout());
  221               mainPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
  222               mainPanel.add(iconPanel, BorderLayout.CENTER);
  223               mainPanel.add(southPanel, BorderLayout.SOUTH);
  224   
  225               //  Build the main panel
  226               getContentPane().setLayout(new GridLayout(1, 1));
  227               getContentPane().add(mainPanel);
  228               pack();
  229               toFront();
  230               UIUtil.positionComponent(SwingConstants.CENTER, this);
  231               setVisible(true);
  232           }
  233       }
  234   }

Save This Page
Home » j2ssh-0.2.9-src » com.sshtools.common.authentication » [javadoc | source]