KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.riotfamily.forms.AbstractEditorBase;
27 import org.riotfamily.forms.BeanEditor;
28 import org.riotfamily.forms.Container;
29 import org.riotfamily.forms.ContainerElement;
30 import org.riotfamily.forms.Editor;
31 import org.riotfamily.forms.EditorBinder;
32 import org.riotfamily.forms.Element;
33 import org.riotfamily.forms.MessageUtils;
34 import org.riotfamily.forms.TemplateUtils;
35 import org.riotfamily.forms.event.Button;
36 import org.riotfamily.forms.event.JavaScriptEvent;
37 import org.riotfamily.forms.request.FormRequest;
38 import org.riotfamily.forms.support.MapOrBeanWrapper;
39 import org.springframework.util.Assert;
40
41 /**
42  * Element to edit nested beans.
43  */

44 public class NestedForm extends TemplateElement implements
45         ContainerElement, Editor, BeanEditor {
46         
47     private EditorBinder editorBinder;
48
49     private Container elements = new Container();
50     
51     private boolean present;
52     
53     private boolean indent = true;
54     
55     private String JavaDoc buttonLabelKeySet = "form.nestedForm.set";
56     
57     private String JavaDoc buttonLabelKeyRemove = "form.nestedForm.remove";
58     
59     public NestedForm() {
60         super("form");
61         addComponent("elements", elements);
62         addComponent("toggleButton", new ToggleButton());
63     }
64     
65     public void setIndent(boolean indent) {
66         this.indent = indent;
67         setTemplate(TemplateUtils.getTemplatePath(NestedForm.class,
68                 indent ? null : "_noindent"));
69     }
70
71     protected void setEditorBinder(EditorBinder editorBinder) {
72         this.editorBinder = editorBinder;
73     }
74     
75     public void setBeanClass(Class JavaDoc beanClass) {
76         EditorBinder editorBinder = new EditorBinder(new MapOrBeanWrapper(beanClass));
77         setEditorBinder(editorBinder);
78     }
79     
80     /**
81      * Invoked by {@link AbstractEditorBase#setEditorBinding} when the nested
82      * form is bound to a property.
83      */

84     protected void afterBindingSet() {
85         if (editorBinder == null) {
86             setBeanClass(getEditorBinding().getPropertyType());
87         }
88         editorBinder.setParent(getEditorBinding());
89     }
90     
91     public Editor getEditor(String JavaDoc property) {
92         return editorBinder.getEditor(property);
93     }
94     
95     protected void afterFormContextSet() {
96         editorBinder.registerPropertyEditors(
97                 getFormContext().getPropertyEditorRegistrars());
98     }
99     
100     public String JavaDoc getLabel() {
101         return indent ? super.getLabel() : null;
102     }
103     
104     public boolean isPresent() {
105         return present;
106     }
107     
108     protected void toggle() {
109         present = !present;
110         if (getFormListener() != null) {
111             getFormListener().elementChanged(this);
112         }
113     }
114     
115     public void processRequest(FormRequest request) {
116         if (present || isRequired()) {
117             super.processRequest(request);
118         }
119     }
120     
121     /**
122      * Sets the given value as backingObject on the internal EditorBinder and
123      * initializes the bound editors.
124      *
125      * @see org.riotfamily.forms.Editor#setValue(java.lang.Object)
126      */

127     public void setValue(Object JavaDoc value) {
128         this.present = isRequired() || value != null;
129         editorBinder.setBackingObject(value);
130         editorBinder.initEditors();
131     }
132
133     /**
134      * Invokes the EditorBinder to populate the backingObject and returns the
135      * populated instance.
136      *
137      * @see org.riotfamily.forms.Editor#getValue()
138      */

139     public Object JavaDoc getValue() {
140         // TODO: Revisit, present is false if used as DynamicList Element
141
if (present || isRequired()) {
142             return editorBinder.populateBackingObject();
143         }
144         return null;
145     }
146
147     public void bind(Editor editor, String JavaDoc property) {
148         Assert.notNull(editorBinder, "The NestedForm must either be bound " +
149                 "to a property or setBeanClass() must be invoked before " +
150                 "nested editors can be added.");
151         
152         editorBinder.bind(editor, property);
153     }
154
155     public void addElement(Element element) {
156         elements.addElement(element);
157     }
158     
159     public void addElement(Editor element, String JavaDoc property) {
160         addElement(element);
161         bind(element, property);
162     }
163     
164     public void removeElement(Element element) {
165         elements.removeElement(element);
166     }
167
168     public String JavaDoc getProperty() {
169         if (getEditorBinding() == null) {
170             return null;
171         }
172         return getEditorBinding().getProperty();
173     }
174
175     private class ToggleButton extends Button {
176         
177         private ToggleButton() {
178             setCssClass("button button-toggle");
179             setTabIndex(2);
180         }
181         
182         public String JavaDoc getLabel() {
183             String JavaDoc key = present ? buttonLabelKeyRemove : buttonLabelKeySet;
184             String JavaDoc label = MessageUtils.getMessage(this, key);
185             if (label == null) {
186                 label = present ? "Remove" : "Set";
187             }
188             return label;
189         }
190
191         protected void onClick() {
192             toggle();
193         }
194         
195         public int getEventTypes() {
196             return JavaScriptEvent.ON_CLICK;
197         }
198     }
199 }
Popular Tags