001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.him.authentication.gui;
020:
021: import java.awt.Color;
022: import java.awt.Component;
023: import java.awt.Container;
024: import java.awt.Dimension;
025: import java.awt.Font;
026: import java.awt.LayoutManager;
027: import java.awt.event.KeyEvent;
028: import java.awt.event.KeyListener;
029:
030: import javax.swing.BorderFactory;
031: import javax.swing.JLabel;
032: import javax.swing.JPanel;
033: import javax.swing.JPasswordField;
034: import javax.swing.JTextField;
035:
036: /**
037: * Data entry panel for change password dialog.
038: *
039: * @author Matthew Large
040: * @version $Revision: 1.1 $
041: *
042: */
043: public class ChangePasswordPanel extends JPanel implements
044: LayoutManager, KeyListener {
045:
046: /**
047: * Label for username field.
048: */
049: private JLabel m_usernameLabel = null;
050:
051: /**
052: * Label for password field.
053: */
054: private JLabel m_passwordLabel = null;
055:
056: /**
057: * Username field.
058: */
059: private JTextField m_username = null;
060:
061: /**
062: * Password field.
063: */
064: private JPasswordField m_password = null;
065:
066: /**
067: * Label for first new password field.
068: */
069: private JLabel m_newPassword1Label = null;
070:
071: /**
072: * Label for second new password field.
073: */
074: private JLabel m_newPassword2Label = null;
075:
076: /**
077: * First new password field.
078: */
079: private JPasswordField m_newPassword1Password = null;
080:
081: /**
082: * Second new password field.
083: */
084: private JPasswordField m_newPassword2Password = null;
085:
086: /**
087: * true if the username field is to be shown.
088: */
089: private boolean m_bUsernameVisible = true;
090:
091: /**
092: * true if the original password field is to be shown.
093: */
094: private boolean m_bPasswordVisible = true;
095:
096: /**
097: * Constructs a new change password data entry panel.
098: *
099: * @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 fontName = "Dialog";
115: int fontSize = 11;
116: Font font = new Font(fontName, Font.PLAIN, fontSize);
117:
118: m_usernameLabel = new JLabel("User name");
119: m_usernameLabel.setFont(font);
120: this .add(m_usernameLabel);
121:
122: m_passwordLabel = new JLabel("Password");
123: m_passwordLabel.setFont(font);
124: this .add(m_passwordLabel);
125:
126: m_username = new JTextField();
127: m_username.addKeyListener(dialog);
128: this .add(m_username);
129:
130: m_password = new JPasswordField();
131: m_password.addKeyListener(dialog);
132: this .add(m_password);
133:
134: m_newPassword1Label = new JLabel("New Password");
135: m_newPassword1Label.setFont(font);
136: this .add(m_newPassword1Label);
137:
138: m_newPassword1Password = new JPasswordField();
139: m_newPassword1Password.setBorder(BorderFactory
140: .createLineBorder(Color.RED));
141: m_newPassword1Password.addKeyListener(this );
142: m_newPassword1Password.addKeyListener(dialog);
143: this .add(m_newPassword1Password);
144:
145: m_newPassword2Label = new JLabel("Repeat New Password");
146: m_newPassword2Label.setFont(font);
147: this .add(m_newPassword2Label);
148:
149: m_newPassword2Password = new JPasswordField();
150: m_newPassword2Password.setBorder(BorderFactory
151: .createLineBorder(Color.RED));
152: m_newPassword2Password.addKeyListener(this );
153: m_newPassword2Password.addKeyListener(dialog);
154: this .add(m_newPassword2Password);
155: }
156:
157: /* (non-Javadoc)
158: * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
159: */
160: public void keyReleased(KeyEvent ke) {
161: if (ke.getSource() == this .m_newPassword1Password
162: || ke.getSource() == this .m_newPassword2Password) {
163: this .checkPasswordsSame();
164: }
165: }
166:
167: /**
168: * Checks that both versions of the new password are the same.
169: *
170: * @return true if both versions of the new password are the same
171: */
172: public boolean checkPasswordsSame() {
173: this .m_newPassword1Password.setBorder(BorderFactory
174: .createLineBorder(Color.BLACK));
175: this .m_newPassword2Password.setBorder(BorderFactory
176: .createLineBorder(Color.BLACK));
177:
178: if (!this .m_newPassword1Password.getText().trim().equals(
179: this .m_newPassword2Password.getText().trim())
180: || this .m_newPassword1Password.getText().trim().equals(
181: getPassword().trim())
182: || this .m_newPassword2Password.getText().trim().equals(
183: getPassword().trim())) {
184: this .m_newPassword1Password.setBorder(BorderFactory
185: .createLineBorder(Color.RED));
186: this .m_newPassword2Password.setBorder(BorderFactory
187: .createLineBorder(Color.RED));
188: return false;
189: } else {
190: return true;
191: }
192: }
193:
194: /**
195: * @param arg0
196: */
197: private ChangePasswordPanel(boolean arg0) {
198: super (arg0);
199: }
200:
201: /**
202: * @param arg0
203: */
204: private ChangePasswordPanel(LayoutManager arg0) {
205: super (arg0);
206: }
207:
208: /**
209: * @param arg0
210: * @param arg1
211: */
212: private ChangePasswordPanel(LayoutManager arg0, boolean arg1) {
213: super (arg0, arg1);
214: }
215:
216: /* (non-Javadoc)
217: * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
218: */
219: public void removeLayoutComponent(Component arg0) {
220: // TODO Auto-generated method stub
221:
222: }
223:
224: /* (non-Javadoc)
225: * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
226: */
227: public void layoutContainer(Container arg0) {
228: int nHeight = 50;
229:
230: if (this .m_bUsernameVisible) {
231: m_usernameLabel.setLocation(20, nHeight);
232: m_usernameLabel.setSize(110, 20);
233:
234: m_username.setLocation(150, nHeight);
235: m_username.setSize(300, 20);
236: nHeight = nHeight + 50;
237: }
238:
239: if (this .m_bPasswordVisible) {
240: m_passwordLabel.setLocation(20, nHeight);
241: m_passwordLabel.setSize(110, 20);
242: m_password.setLocation(150, nHeight);
243: m_password.setSize(300, 20);
244: nHeight = nHeight + 50;
245: }
246:
247: m_newPassword1Label.setLocation(20, nHeight);
248: m_newPassword1Label.setSize(110, 20);
249:
250: m_newPassword1Password.setLocation(150, nHeight);
251: m_newPassword1Password.setSize(300, 20);
252: nHeight = nHeight + 50;
253:
254: m_newPassword2Label.setLocation(20, nHeight);
255: m_newPassword2Label.setSize(120, 20);
256:
257: m_newPassword2Password.setLocation(150, nHeight);
258: m_newPassword2Password.setSize(300, 20);
259: }
260:
261: /**
262: * Returns the username.
263: *
264: * @return Username
265: */
266: public String getUsername() {
267: return this .m_username.getText().trim();
268: }
269:
270: /**
271: * Sets the username and pre-populates the username field.
272: *
273: * @param sUsername Username
274: */
275: public void setUsername(String sUsername) {
276: this .m_username.setText(sUsername);
277: this .m_username.setVisible(false);
278: this .m_usernameLabel.setVisible(false);
279: this .m_bUsernameVisible = false;
280: }
281:
282: /**
283: * Returns the original password.
284: *
285: * @return Password
286: */
287: public String getPassword() {
288: return this .m_password.getText().trim();
289: }
290:
291: /**
292: * Sets the original password and pre-populates the password
293: * field.
294: *
295: * @param sPassword Password
296: */
297: public void setPassword(String sPassword) {
298: this .m_password.setText(sPassword);
299: this .m_password.setVisible(false);
300: this .m_passwordLabel.setVisible(false);
301: this .m_bPasswordVisible = false;
302: }
303:
304: /**
305: * Returns the new password if both versions are the same.
306: *
307: * @return New password or empty string if both versions so not match
308: */
309: public String getNewPassword() {
310: if (this .checkPasswordsSame()) {
311: return this .m_newPassword1Password.getText().trim();
312: } else {
313: return "";
314: }
315: }
316:
317: /* (non-Javadoc)
318: * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
319: */
320: public void addLayoutComponent(String arg0, Component arg1) {
321: // TODO Auto-generated method stub
322:
323: }
324:
325: /* (non-Javadoc)
326: * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
327: */
328: public Dimension minimumLayoutSize(Container arg0) {
329: return this .getPreferredSize();
330: }
331:
332: /* (non-Javadoc)
333: * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
334: */
335: public Dimension preferredLayoutSize(Container arg0) {
336: return this .getPreferredSize();
337: }
338:
339: /* (non-Javadoc)
340: * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
341: */
342: public void keyPressed(KeyEvent arg0) {
343: }
344:
345: /* (non-Javadoc)
346: * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
347: */
348: public void keyTyped(KeyEvent arg0) {
349: }
350:
351: }
|