KickJava   Java API By Example, From Geeks To Geeks.

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


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.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     /**
58      * Sets whether the editor should be hidden if the form is not new.
59      */

60     public void setHide(boolean hide) {
61         this.hide = hide;
62     }
63     
64     /**
65      * Implementation of the ContainerElement interface. This allows us to
66      * specify the editor as nested node in the XML configuration.
67      *
68      * @see org.riotfamily.riot.editor.xml.XmlEditorRepositoryDigester
69      * @see ContainerElement#addElement(Element)
70      */

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 JavaDoc getLabel() {
90         if (hide && !getForm().isNew()) {
91             return null;
92         }
93         return editor.getLabel();
94     }
95     
96     protected void renderInternal(PrintWriter JavaDoc writer) {
97         if (isEditable()) {
98             editor.render(writer);
99         }
100         else if (!hide) {
101             Object JavaDoc 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