1 24 package org.riotfamily.forms.element; 25 26 import java.io.PrintWriter ; 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 DEFAULT_CONFIRM_MESSAGE_KEY = 35 "label.passwordField.confirmInput"; 36 37 private static final String 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 getDefaultConfirmMessageKey() { 51 return DEFAULT_CONFIRM_MESSAGE_KEY; 52 } 53 54 public String getButtonId() { 55 return getId() + "-toggleButton"; 56 } 57 58 public void renderInternal(PrintWriter 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 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 getInitScript() { 84 return togglePlaintext ? TemplateUtils.getInitScript(this) : null; 85 } 86 87 } 88 | Popular Tags |