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.awt.BorderLayout;
022 import java.awt.GridBagConstraints;
023 import java.awt.GridBagLayout;
024 import java.awt.event.ActionEvent;
025 import java.awt.event.ActionListener;
026 import java.util.Locale;
027 import java.util.ResourceBundle;
028
029 import javax.swing.BorderFactory;
030 import javax.swing.Box;
031 import javax.swing.BoxLayout;
032 import javax.swing.JButton;
033 import javax.swing.JDialog;
034 import javax.swing.JLabel;
035 import javax.swing.JPanel;
036 import javax.swing.JPasswordField;
037
038 import org.jivesoftware.smack.AccountManager;
039 import org.jivesoftware.smack.XMPPException;
040
041 import com.valhalla.gui.DialogTracker;
042 import com.valhalla.gui.Standard;
043 import com.valhalla.gui.WaitDialog;
044 import com.valhalla.misc.SimpleXOR;
045 import com.valhalla.settings.Settings;
046
047 /**
048 * Displays a Dialog allowing the user to change his passowrd on the Jabber
049 * server
050 *
051 * @author Adam Olsen
052 * @version 1.0
053 */
054 public class ChangePasswordDialog extends JDialog {
055 private ResourceBundle resources = ResourceBundle.getBundle(
056 "JBotherBundle", Locale.getDefault());
057
058 private JPanel mainPanel;
059
060 private JButton okButton = new JButton(resources.getString("okButton"));
061
062 private JButton cancelButton = new JButton(resources
063 .getString("cancelButton"));
064
065 private JPasswordField passwordField = new JPasswordField(16);
066
067 private JPasswordField verifyPasswordField = new JPasswordField(16);
068
069 private WaitDialog wait = new WaitDialog(this, null, resources
070 .getString("pleaseWait"));
071
072 /**
073 * Sets up the dialog
074 */
075 public ChangePasswordDialog() {
076 super(BuddyList.getInstance().getContainerFrame());
077 setTitle(resources.getString("changePassword"));
078 initComponents();
079 DialogTracker.addDialog(this, true, true);
080 pack();
081 setLocationRelativeTo(null);
082 }
083
084 /**
085 * Sets up the various visual components
086 */
087 private void initComponents() {
088 mainPanel = (JPanel) getContentPane();
089
090 mainPanel.setBorder(BorderFactory.createTitledBorder(resources
091 .getString("changePassword")));
092 mainPanel.setLayout(new BorderLayout());
093
094 JPanel buttonPanel = new JPanel();
095 buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
096 buttonPanel.add(Box.createHorizontalGlue());
097 buttonPanel.add(cancelButton);
098 buttonPanel.add(okButton);
099 buttonPanel.add(Box.createHorizontalGlue());
100 buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
101
102 mainPanel.add(buttonPanel, BorderLayout.SOUTH);
103
104 JPanel innerPanel = new JPanel();
105 GridBagLayout grid = new GridBagLayout();
106 innerPanel.setLayout(grid);
107 GridBagConstraints c = new GridBagConstraints();
108
109 c.gridx = 0;
110 c.gridy = 0;
111 c.anchor = GridBagConstraints.WEST;
112
113 JLabel pLabel = new JLabel(resources.getString("newPassword") + ": ");
114 grid.setConstraints(pLabel, c);
115 innerPanel.add(pLabel);
116 c.gridx++;
117 grid.setConstraints(passwordField, c);
118 innerPanel.add(passwordField);
119
120 JLabel vLabel = new JLabel(resources.getString("verifyPassword") + ": ");
121 c.gridy++;
122 c.gridx = 0;
123 grid.setConstraints(vLabel, c);
124 innerPanel.add(vLabel);
125 c.gridx++;
126 grid.setConstraints(verifyPasswordField, c);
127 innerPanel.add(verifyPasswordField);
128
129 // we have to set the JPasswordField fonts manually for some reason
130 passwordField.setFont(okButton.getFont());
131 verifyPasswordField.setFont(okButton.getFont());
132
133 passwordField.grabFocus();
134
135 mainPanel.add(innerPanel, BorderLayout.CENTER);
136 addListeners();
137 }
138
139 /**
140 * Adds listeners to the dialogs buttons
141 */
142 private void addListeners() {
143 cancelButton.addActionListener(new ActionListener() {
144 public void actionPerformed(ActionEvent e) {
145 DialogTracker.removeDialog(ChangePasswordDialog.this);
146 }
147 });
148
149 okButton.addActionListener(new ActionListener() {
150 public void actionPerformed(ActionEvent e) {
151 okHandler();
152 }
153 });
154
155 }
156
157 /**
158 * Checks the information and runs the PasswordChangeThread
159 */
160 private void okHandler() {
161 String pass = new String(passwordField.getPassword());
162 String verify = new String(verifyPasswordField.getPassword());
163
164 if (pass.equals("")) {
165 Standard.warningMessage(this,
166 resources.getString("changePassword"), resources
167 .getString("passwordRequired"));
168 return;
169 }
170
171 if (!verify.equals(pass)) {
172 Standard.warningMessage(this,
173 resources.getString("changePassword"), resources
174 .getString("verificationMatch"));
175 return;
176
177 }
178
179 wait.setVisible(true);
180 setVisible(false);
181 Thread thread = new Thread(new PasswordChangeThread(pass));
182 thread.start();
183 }
184
185 /**
186 * Sends the new password to the server and gets the response
187 *
188 * @author Adam Olsen
189 * @version 1.0
190 */
191 class PasswordChangeThread implements Runnable {
192 private String newPass;
193
194 public PasswordChangeThread(String p) {
195 newPass = p;
196 }
197
198 public void run() {
199 String errorMessage = null;
200
201 if (BuddyList.getInstance().checkConnection()) {
202 AccountManager manager = BuddyList.getInstance()
203 .getConnection().getAccountManager();
204 try {
205 manager.changePassword(newPass);
206 } catch (XMPPException e) {
207 if (e.getXMPPError() == null)
208 errorMessage = e.getMessage();
209 else
210 errorMessage = resources.getString("xmppError"
211 + e.getXMPPError().getCode());
212 }
213 } else
214 errorMessage = resources.getString("notConnected");
215
216 wait.dispose();
217
218 if (errorMessage == null) {
219 if (Settings.getInstance().getProperty("password") != null)
220 Settings.getInstance().setProperty("password",
221 SimpleXOR.encrypt(newPass, "JBother rules!"));
222 Standard.noticeMessage(ChangePasswordDialog.this, resources
223 .getString("changePassword"), resources
224 .getString("passwordChanged"));
225 } else {
226 Standard.warningMessage(ChangePasswordDialog.this, resources
227 .getString("changePassword"), errorMessage);
228 }
229
230 DialogTracker.removeDialog(ChangePasswordDialog.this);
231 }
232 }
233 }