001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2006
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.forms.element;
025:
026: import java.io.PrintWriter;
027: import java.util.Collections;
028: import java.util.List;
029:
030: import org.riotfamily.common.markup.Html;
031: import org.riotfamily.common.markup.TagWriter;
032: import org.riotfamily.forms.AbstractElement;
033: import org.riotfamily.forms.ContainerElement;
034: import org.riotfamily.forms.Editor;
035: import org.riotfamily.forms.Element;
036: import org.riotfamily.forms.request.FormRequest;
037: import org.springframework.util.Assert;
038:
039: public abstract class AbstractConditionalElement extends
040: AbstractElement implements ContainerElement {
041:
042: private Editor editor;
043:
044: private boolean hide;
045:
046: protected void afterFormSet() {
047: Assert.notNull(editor, "An editor must be set.");
048: getForm().registerElement(editor);
049: }
050:
051: public void setEditor(Editor editor) {
052: this .editor = editor;
053: }
054:
055: protected Editor getEditor() {
056: return editor;
057: }
058:
059: /**
060: * Sets whether the editor should be hidden if the form is not new.
061: */
062: public void setHide(boolean hide) {
063: this .hide = hide;
064: }
065:
066: /**
067: * Implementation of the ContainerElement interface. This allows us to
068: * specify the editor as nested node in the XML configuration.
069: *
070: * @see org.riotfamily.riot.editor.xml.XmlEditorRepositoryDigester
071: * @see ContainerElement#addElement(Element)
072: */
073: public void addElement(Element element) {
074: Assert.isInstanceOf(Editor.class, element,
075: "Element must be an Editor.");
076: Assert.isTrue(editor == null,
077: "Only one child element is allowed.");
078: setEditor((Editor) element);
079: }
080:
081: public void removeElement(Element element) {
082: if (element == editor) {
083: setEditor(null);
084: }
085: }
086:
087: public List getElements() {
088: return Collections.singletonList(editor);
089: }
090:
091: public void processRequest(FormRequest request) {
092: if (isEditable()) {
093: editor.processRequest(request);
094: }
095: }
096:
097: public String getLabel() {
098: if (hide && !getForm().isNew()) {
099: return null;
100: }
101: return editor.getLabel();
102: }
103:
104: protected void renderInternal(PrintWriter writer) {
105: if (isEditable()) {
106: editor.render(writer);
107: } else if (!hide) {
108: Object value = editor.getValue();
109: if (value != null) {
110: TagWriter tag = new TagWriter(writer);
111: tag.start(Html.SPAN);
112: tag.attribute(Html.COMMON_CLASS, "read-only");
113: tag.body(value.toString());
114: tag.end();
115: }
116: }
117: }
118:
119: public boolean isCompositeElement() {
120: return editor.isCompositeElement();
121: }
122:
123: protected abstract boolean isEditable();
124:
125: }
|