KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > authentication > gui > ChangePasswordPanel


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.him.authentication.gui;
20
21 import java.awt.Color JavaDoc;
22 import java.awt.Component JavaDoc;
23 import java.awt.Container JavaDoc;
24 import java.awt.Dimension JavaDoc;
25 import java.awt.Font JavaDoc;
26 import java.awt.LayoutManager JavaDoc;
27 import java.awt.event.KeyEvent JavaDoc;
28 import java.awt.event.KeyListener JavaDoc;
29
30 import javax.swing.BorderFactory JavaDoc;
31 import javax.swing.JLabel JavaDoc;
32 import javax.swing.JPanel JavaDoc;
33 import javax.swing.JPasswordField JavaDoc;
34 import javax.swing.JTextField JavaDoc;
35
36 /**
37  * Data entry panel for change password dialog.
38  *
39  * @author Matthew Large
40  * @version $Revision: 1.1 $
41  *
42  */

43 public class ChangePasswordPanel extends JPanel JavaDoc implements LayoutManager JavaDoc, KeyListener JavaDoc {
44
45     /**
46      * Label for username field.
47      */

48     private JLabel JavaDoc m_usernameLabel = null;
49     
50     /**
51      * Label for password field.
52      */

53     private JLabel JavaDoc m_passwordLabel = null;
54     
55     /**
56      * Username field.
57      */

58     private JTextField JavaDoc m_username = null;
59     
60     /**
61      * Password field.
62      */

63     private JPasswordField JavaDoc m_password = null;
64     
65     /**
66      * Label for first new password field.
67      */

68     private JLabel JavaDoc m_newPassword1Label = null;
69     
70     /**
71      * Label for second new password field.
72      */

73     private JLabel JavaDoc m_newPassword2Label = null;
74     
75     /**
76      * First new password field.
77      */

78     private JPasswordField JavaDoc m_newPassword1Password = null;
79     
80     /**
81      * Second new password field.
82      */

83     private JPasswordField JavaDoc m_newPassword2Password = null;
84     
85     /**
86      * true if the username field is to be shown.
87      */

88     private boolean m_bUsernameVisible = true;
89     
90     /**
91      * true if the original password field is to be shown.
92      */

93     private boolean m_bPasswordVisible = true;
94     
95     
96     /**
97      * Constructs a new change password data entry panel.
98      *
99      * @param dialog Dialog this panel will be part of
100      */

101     public ChangePasswordPanel(ChangePasswordDialog dialog) {
102         super();
103         this.setup(dialog);
104     }
105     
106     /**
107      * Configures this change password data entry panel.
108      *
109      * @param dialog Dialog this panel will be part of
110      */

111     private void setup(ChangePasswordDialog dialog) {
112         this.setLayout(this);
113         
114         String JavaDoc fontName = "Dialog";
115         int fontSize = 11;
116         Font JavaDoc font = new Font JavaDoc(fontName, Font.PLAIN, fontSize);
117         
118         m_usernameLabel = new JLabel JavaDoc("User name");
119         m_usernameLabel.setFont(font);
120         this.add(m_usernameLabel);
121         
122         m_passwordLabel = new JLabel JavaDoc("Password");
123         m_passwordLabel.setFont(font);
124         this.add(m_passwordLabel);
125
126         m_username = new JTextField JavaDoc();
127         m_username.addKeyListener(dialog);
128         this.add(m_username);
129         
130         m_password = new JPasswordField JavaDoc();
131         m_password.addKeyListener(dialog);
132         this.add(m_password);
133         
134         m_newPassword1Label = new JLabel JavaDoc("New Password");
135         m_newPassword1Label.setFont(font);
136         this.add(m_newPassword1Label);
137         
138         m_newPassword1Password = new JPasswordField JavaDoc();
139         m_newPassword1Password.setBorder(BorderFactory.createLineBorder(Color.RED));
140         m_newPassword1Password.addKeyListener(this);
141         m_newPassword1Password.addKeyListener(dialog);
142         this.add(m_newPassword1Password);
143         
144         m_newPassword2Label = new JLabel JavaDoc("Repeat New Password");
145         m_newPassword2Label.setFont(font);
146         this.add(m_newPassword2Label);
147         
148         m_newPassword2Password = new JPasswordField JavaDoc();
149         m_newPassword2Password.setBorder(BorderFactory.createLineBorder(Color.RED));
150         m_newPassword2Password.addKeyListener(this);
151         m_newPassword2Password.addKeyListener(dialog);
152         this.add(m_newPassword2Password);
153     }
154
155     /* (non-Javadoc)
156      * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
157      */

158     public void keyReleased(KeyEvent JavaDoc ke) {
159         if(ke.getSource()==this.m_newPassword1Password || ke.getSource()==this.m_newPassword2Password) {
160             this.checkPasswordsSame();
161         }
162     }
163     
164     /**
165      * Checks that both versions of the new password are the same.
166      *
167      * @return true if both versions of the new password are the same
168      */

169     public boolean checkPasswordsSame() {
170         this.m_newPassword1Password.setBorder(BorderFactory.createLineBorder(Color.BLACK));
171         this.m_newPassword2Password.setBorder(BorderFactory.createLineBorder(Color.BLACK));
172         
173         if(!this.m_newPassword1Password.getText().trim().equals(this.m_newPassword2Password.getText().trim()) || this.m_newPassword1Password.getText().trim().equals(getPassword().trim()) || this.m_newPassword2Password.getText().trim().equals(getPassword().trim())) {
174             this.m_newPassword1Password.setBorder(BorderFactory.createLineBorder(Color.RED));
175             this.m_newPassword2Password.setBorder(BorderFactory.createLineBorder(Color.RED));
176             return false;
177         } else {
178             return true;
179         }
180     }
181
182     /**
183      * @param arg0
184      */

185     private ChangePasswordPanel(boolean arg0) {
186         super(arg0);
187     }
188
189     /**
190      * @param arg0
191      */

192     private ChangePasswordPanel(LayoutManager JavaDoc arg0) {
193         super(arg0);
194     }
195
196     /**
197      * @param arg0
198      * @param arg1
199      */

200     private ChangePasswordPanel(LayoutManager JavaDoc arg0, boolean arg1) {
201         super(arg0, arg1);
202     }
203
204     /* (non-Javadoc)
205      * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
206      */

207     public void removeLayoutComponent(Component JavaDoc arg0) {
208         // TODO Auto-generated method stub
209

210     }
211
212     /* (non-Javadoc)
213      * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
214      */

215     public void layoutContainer(Container JavaDoc arg0) {
216         int nHeight = 50;
217         
218         if(this.m_bUsernameVisible) {
219             m_usernameLabel.setLocation(20,nHeight);
220             m_usernameLabel.setSize(110,20);
221         
222             m_username.setLocation(150, nHeight);
223             m_username.setSize(300,20);
224             nHeight = nHeight + 50;
225         }
226         
227         if(this.m_bPasswordVisible) {
228             m_passwordLabel.setLocation(20,nHeight);
229             m_passwordLabel.setSize(110,20);
230             m_password.setLocation(150, nHeight);
231             m_password.setSize(300,20);
232             nHeight = nHeight + 50;
233         }
234         
235         m_newPassword1Label.setLocation(20,nHeight);
236         m_newPassword1Label.setSize(110,20);
237         
238         m_newPassword1Password.setLocation(150, nHeight);
239         m_newPassword1Password.setSize(300,20);
240         nHeight = nHeight + 50;
241         
242         m_newPassword2Label.setLocation(20,nHeight);
243         m_newPassword2Label.setSize(120,20);
244         
245         m_newPassword2Password.setLocation(150, nHeight);
246         m_newPassword2Password.setSize(300,20);
247     }
248     
249     /**
250      * Returns the username.
251      *
252      * @return Username
253      */

254     public String JavaDoc getUsername() {
255         return this.m_username.getText().trim();
256     }
257     
258     /**
259      * Sets the username and pre-populates the username field.
260      *
261      * @param sUsername Username
262      */

263     public void setUsername(String JavaDoc sUsername) {
264         this.m_username.setText(sUsername);
265         this.m_username.setVisible(false);
266         this.m_usernameLabel.setVisible(false);
267         this.m_bUsernameVisible = false;
268     }
269     
270     /**
271      * Returns the original password.
272      *
273      * @return Password
274      */

275     public String JavaDoc getPassword() {
276         return this.m_password.getText().trim();
277     }
278     
279     /**
280      * Sets the original password and pre-populates the password
281      * field.
282      *
283      * @param sPassword Password
284      */

285     public void setPassword(String JavaDoc sPassword) {
286         this.m_password.setText(sPassword);
287         this.m_password.setVisible(false);
288         this.m_passwordLabel.setVisible(false);
289         this.m_bPasswordVisible = false;
290     }
291     
292     /**
293      * Returns the new password if both versions are the same.
294      *
295      * @return New password or empty string if both versions so not match
296      */

297     public String JavaDoc getNewPassword() {
298         if(this.checkPasswordsSame()) {
299             return this.m_newPassword1Password.getText().trim();
300         } else {
301             return "";
302         }
303     }
304
305     /* (non-Javadoc)
306      * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
307      */

308     public void addLayoutComponent(String JavaDoc arg0, Component JavaDoc arg1) {
309         // TODO Auto-generated method stub
310

311     }
312
313     /* (non-Javadoc)
314      * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
315      */

316     public Dimension JavaDoc minimumLayoutSize(Container JavaDoc arg0) {
317         return this.getPreferredSize();
318     }
319
320     /* (non-Javadoc)
321      * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
322      */

323     public Dimension JavaDoc preferredLayoutSize(Container JavaDoc arg0) {
324         return this.getPreferredSize();
325     }
326
327     /* (non-Javadoc)
328      * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
329      */

330     public void keyPressed(KeyEvent JavaDoc arg0) {
331     }
332
333     /* (non-Javadoc)
334      * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
335      */

336     public void keyTyped(KeyEvent JavaDoc arg0) {
337     }
338     
339     
340
341 }
342
Popular Tags