KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > forms > element > PasswordField


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.forms.element;
25
26 import java.io.PrintWriter JavaDoc;
27
28 import org.riotfamily.common.markup.DocumentWriter;
29 import org.riotfamily.common.markup.Html;
30 import org.riotfamily.forms.TemplateUtils;
31
32 public class PasswordField extends TextField {
33
34     private static final String JavaDoc DEFAULT_CONFIRM_MESSAGE_KEY =
35             "label.passwordField.confirmInput";
36     
37     private static final String JavaDoc TOGGLE_PLAINTEXT_MESSAGE_KEY =
38             "label.passwordField.togglePlaintext";
39     
40     private boolean togglePlaintext;
41     
42     public PasswordField() {
43         super("password");
44     }
45
46     public void setTogglePlaintext(boolean togglePlaintext) {
47         this.togglePlaintext = togglePlaintext;
48     }
49
50     protected String JavaDoc getDefaultConfirmMessageKey() {
51         return DEFAULT_CONFIRM_MESSAGE_KEY;
52     }
53     
54     public String JavaDoc getButtonId() {
55         return getId() + "-toggleButton";
56     }
57     
58     public void renderInternal(PrintWriter JavaDoc writer) {
59         if (togglePlaintext) {
60             DocumentWriter doc = new DocumentWriter(writer);
61             doc.start(Html.DIV).body();
62             super.renderInternal(writer);
63             doc.start(Html.DIV)
64                     .attribute(Html.COMMON_CLASS, "toggle-plaintext")
65                     .body();
66             
67             doc.startEmpty(Html.INPUT)
68                 .attribute(Html.INPUT_TYPE, "checkbox")
69                 .attribute(Html.COMMON_ID, getButtonId())
70                 .end();
71             
72             String JavaDoc label = getFormContext().getMessageResolver().getMessage(
73                     TOGGLE_PLAINTEXT_MESSAGE_KEY);
74             
75             doc.start(Html.LABEL).attribute(Html.LABEL_FOR, getButtonId())
76                     .body(label).closeAll();
77         }
78         else {
79             super.renderInternal(writer);
80         }
81     }
82     
83     public String JavaDoc getInitScript() {
84         return togglePlaintext ? TemplateUtils.getInitScript(this) : null;
85     }
86     
87 }
88
Popular Tags