1 24 package org.riotfamily.forms.element; 25 26 import java.io.PrintWriter ; 27 28 import org.riotfamily.common.markup.Html; 29 import org.riotfamily.common.markup.TagWriter; 30 import org.riotfamily.forms.AbstractElement; 31 import org.riotfamily.forms.ContainerElement; 32 import org.riotfamily.forms.Editor; 33 import org.riotfamily.forms.Element; 34 import org.riotfamily.forms.request.FormRequest; 35 import org.springframework.util.Assert; 36 37 public abstract class AbstractConditionalElement extends AbstractElement 38 implements ContainerElement { 39 40 private Editor editor; 41 42 private boolean hide; 43 44 protected void afterFormSet() { 45 Assert.notNull(editor, "An editor must be set."); 46 getForm().registerElement(editor); 47 } 48 49 public void setEditor(Editor editor) { 50 this.editor = editor; 51 } 52 53 protected Editor getEditor() { 54 return editor; 55 } 56 57 60 public void setHide(boolean hide) { 61 this.hide = hide; 62 } 63 64 71 public void addElement(Element element) { 72 Assert.isInstanceOf(Editor.class, element, "Element must be an Editor."); 73 Assert.isTrue(editor == null, "Only one child element is allowed."); 74 setEditor((Editor) element); 75 } 76 77 public void removeElement(Element element) { 78 if (element == editor) { 79 setEditor(null); 80 } 81 } 82 83 public void processRequest(FormRequest request) { 84 if (isEditable()) { 85 editor.processRequest(request); 86 } 87 } 88 89 public String getLabel() { 90 if (hide && !getForm().isNew()) { 91 return null; 92 } 93 return editor.getLabel(); 94 } 95 96 protected void renderInternal(PrintWriter writer) { 97 if (isEditable()) { 98 editor.render(writer); 99 } 100 else if (!hide) { 101 Object value = editor.getValue(); 102 if (value != null) { 103 TagWriter tag = new TagWriter(writer); 104 tag.start(Html.SPAN); 105 tag.attribute(Html.COMMON_CLASS, "read-only"); 106 tag.body(value.toString()); 107 tag.end(); 108 } 109 } 110 } 111 112 public boolean isCompositeElement() { 113 return editor.isCompositeElement(); 114 } 115 116 protected abstract boolean isEditable(); 117 118 } 119 | Popular Tags |