001 /* 002 Copyright (C) 2003 Adam Olsen 003 004 This program is free software; you can redistribute it and/or modify 005 it under the terms of the GNU General Public License as published by 006 the Free Software Foundation; either version 1, or (at your option) 007 any later version. 008 009 This program is distributed in the hope that it will be useful, 010 but WITHOUT ANY WARRANTY; without even the implied warranty of 011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 GNU General Public License for more details. 013 014 You should have received a copy of the GNU General Public License 015 along with this program; if not, write to the Free Software 016 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 017 */ 018 019 package com.valhalla.jbother; 020 021 import java.io.File; 022 import java.util.*; 023 import org.jivesoftware.smack.*; 024 import com.valhalla.settings.*; 025 026 import com.valhalla.gui.*; 027 import javax.swing.*; 028 import java.awt.*; 029 import java.awt.event.*; 030 import java.lang.reflect.*; 031 import javax.swing.border.*; 032 import com.valhalla.jbother.*; 033 034 /** 035 * Displays a Dialog allowing the user to change his passowrd on the Jabber server 036 * 037 * @author Adam Olsen 038 * @version 1.0 039 */ 040 public class ChangePasswordDialog extends JDialog 041 { 042 private ResourceBundle resources = ResourceBundle.getBundle( "JBotherBundle", Locale.getDefault() ); 043 private JPanel mainPanel; 044 private JButton okButton = new JButton( resources.getString( "okButton" ) ); 045 private JButton cancelButton = new JButton( resources.getString( "cancelButton" ) ); 046 private JPasswordField passwordField = new JPasswordField( 16 ); 047 private JPasswordField verifyPasswordField = new JPasswordField( 16 ); 048 private ChangePasswordDialog thisPointer = this; 049 private WaitDialog wait = new WaitDialog( resources.getString( "pleaseWait" ) ); 050 051 /** 052 * Sets up the dialog 053 */ 054 public ChangePasswordDialog() 055 { 056 setTitle( resources.getString( "changePassword" ) ); 057 initComponents(); 058 DialogTracker.addDialog( this, true, true ); 059 pack(); 060 setLocationRelativeTo( null ); 061 } 062 063 /** 064 * Sets up the various visual components 065 */ 066 private void initComponents() 067 { 068 mainPanel = (JPanel)getContentPane(); 069 070 mainPanel.setBorder( BorderFactory.createTitledBorder( resources.getString( "changePassword" ) ) ); 071 mainPanel.setLayout( new BorderLayout() ); 072 073 JPanel buttonPanel = new JPanel(); 074 buttonPanel.setLayout( new BoxLayout( buttonPanel, BoxLayout.X_AXIS ) ); 075 buttonPanel.add( Box.createHorizontalGlue() ); 076 buttonPanel.add( cancelButton ); 077 buttonPanel.add( okButton ); 078 buttonPanel.add( Box.createHorizontalGlue() ); 079 buttonPanel.setBorder( BorderFactory.createEmptyBorder( 5, 5, 5, 5 ) ); 080 081 mainPanel.add( buttonPanel, BorderLayout.SOUTH ); 082 083 JPanel innerPanel = new JPanel(); 084 innerPanel.setLayout( new BoxLayout( innerPanel, BoxLayout.Y_AXIS ) ); 085 086 JPanel passwordPanel = new JPanel(); 087 passwordPanel.setLayout( new BoxLayout( passwordPanel, BoxLayout.X_AXIS ) ); 088 passwordPanel.add( new JLabel( resources.getString( "newPassword" ) + ": ", SwingConstants.RIGHT ) ); 089 passwordPanel.add( passwordField ); 090 passwordPanel.setBorder( BorderFactory.createEmptyBorder( 0, 5, 2, 5 ) ); 091 innerPanel.add( passwordPanel ); 092 093 JPanel verifyPanel = new JPanel(); 094 verifyPanel.setLayout( new BoxLayout( verifyPanel, BoxLayout.X_AXIS ) ); 095 verifyPanel.add( new JLabel( resources.getString( "verifyPassword" ) + ": ", SwingConstants.RIGHT ) ); 096 verifyPanel.add( verifyPasswordField ); 097 verifyPanel.setBorder( BorderFactory.createEmptyBorder( 0, 5, 5, 5 ) ); 098 innerPanel.add( verifyPanel ); 099 100 // we have to set the JPasswordField fonts manually for some reason 101 passwordField.setFont( okButton.getFont() ); 102 verifyPasswordField.setFont( okButton.getFont() ); 103 104 passwordField.grabFocus(); 105 106 mainPanel.add( innerPanel, BorderLayout.CENTER ); 107 addListeners(); 108 } 109 110 /** 111 * Adds listeners to the dialogs buttons 112 */ 113 private void addListeners() 114 { 115 cancelButton.addActionListener( new ActionListener() 116 { 117 public void actionPerformed( ActionEvent e ) { DialogTracker.removeDialog( thisPointer ); } 118 } ); 119 120 okButton.addActionListener( new ActionListener() 121 { 122 public void actionPerformed( ActionEvent e ) { okHandler(); } 123 } ); 124 125 } 126 127 /** 128 * Checks the information and runs the PasswordChangeThread 129 */ 130 private void okHandler() 131 { 132 String pass = new String( passwordField.getPassword() ); 133 String verify = new String( verifyPasswordField.getPassword() ); 134 135 if( pass.equals( "" ) ) 136 { 137 Standard.warningMessage( this, resources.getString( "changePassword" ), resources.getString( "passwordRequired" ) ); 138 return; 139 } 140 141 if( !verify.equals( pass ) ) 142 { 143 Standard.warningMessage( this, resources.getString( "changePassword" ), resources.getString( "verificationMatch" ) ); 144 return; 145 146 } 147 148 wait.show(); 149 setVisible( false ); 150 Thread thread = new Thread( new PasswordChangeThread( pass ) ); 151 thread.start(); 152 } 153 154 /** 155 * Sends the new password to the server and gets the response 156 * @author Adam Olsen 157 * @version 1.0 158 */ 159 class PasswordChangeThread implements Runnable 160 { 161 private String newPass; 162 public PasswordChangeThread( String p ) { newPass = p; } 163 public void run() 164 { 165 String errorMessage = null; 166 167 if( BuddyList.getInstance().checkConnection() ) 168 { 169 AccountManager manager = BuddyList.getInstance().getConnection().getAccountManager(); 170 try { 171 manager.changePassword( newPass ); 172 } 173 catch( XMPPException e ) 174 { 175 errorMessage = e.getMessage(); 176 } 177 } 178 else errorMessage = resources.getString( "notConnected" ); 179 180 wait.dispose(); 181 182 if( errorMessage == null ) 183 { 184 if( Settings.getInstance().getProperty( "password" ) != null ) 185 Settings.getInstance().setProperty( "password", newPass ); 186 Standard.noticeMessage( thisPointer, resources.getString( "changePassword" ), resources.getString( "passwordChanged" ) ); 187 } 188 else { 189 Standard.warningMessage( thisPointer, resources.getString( "changePassword" ), errorMessage ); 190 } 191 192 DialogTracker.removeDialog( thisPointer ); 193 } 194 } 195 }