KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > riot > editor > AbstractObjectEditorDefinition


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.riot.editor;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.LinkedList JavaDoc;
29 import java.util.List JavaDoc;
30
31 import org.riotfamily.common.i18n.MessageResolver;
32 import org.riotfamily.common.util.FormatUtils;
33 import org.riotfamily.riot.editor.ui.EditorReference;
34
35 /**
36  * Abstract base class for editors that display a single object.
37  *
38  * @see org.riotfamily.riot.editor.FormDefinition
39  * @see org.riotfamily.riot.editor.ViewDefinition
40  */

41 public abstract class AbstractObjectEditorDefinition
42         extends AbstractEditorDefinition
43         implements ObjectEditorDefinition {
44
45     private Class JavaDoc beanClass;
46
47     private String JavaDoc labelProperty;
48
49     private List JavaDoc childEditorDefinitions = new LinkedList JavaDoc();
50
51     public Class JavaDoc getBeanClass() {
52         if (beanClass != null) {
53             return beanClass;
54         }
55         EditorDefinition parent = getParentEditorDefinition();
56         return parent != null ? parent.getBeanClass() : null;
57     }
58
59     public void setBeanClass(Class JavaDoc beanClass) {
60         this.beanClass = beanClass;
61     }
62
63     public String JavaDoc getLabelProperty() {
64         if (labelProperty != null) {
65             return labelProperty;
66         }
67         EditorDefinition parent = getParentEditorDefinition();
68         if (parent instanceof ListDefinition) {
69             ListDefinition listDef = (ListDefinition) parent;
70             return listDef.getListConfig().getFirstProperty();
71         }
72         return null;
73     }
74
75     public void setLabelProperty(String JavaDoc labelProperty) {
76         this.labelProperty = labelProperty;
77     }
78
79     protected String JavaDoc getConfiguredLabelProperty() {
80         return labelProperty;
81     }
82
83     public List JavaDoc getChildEditorDefinitions() {
84         return childEditorDefinitions;
85     }
86
87     public List JavaDoc getChildEditorReferences(Object JavaDoc object,
88             MessageResolver messageResolver) {
89
90         List JavaDoc refs = new ArrayList JavaDoc();
91         Iterator JavaDoc it = childEditorDefinitions.iterator();
92         while (it.hasNext()) {
93             EditorDefinition editorDef = (EditorDefinition) it.next();
94             editorDef.addReference(refs, this, object, messageResolver);
95         }
96         return refs;
97     }
98
99     public void addChildEditorDefinition(EditorDefinition editorDef) {
100         childEditorDefinitions.add(editorDef);
101         editorDef.setParentEditorDefinition(this);
102     }
103
104     protected Object JavaDoc loadBean(String JavaDoc objectId) {
105         return EditorDefinitionUtils.loadBean(this, objectId);
106     }
107
108     /**
109      * Returns a PathComponent for the given objectId and parentId that
110      * represents the complete path to the form.
111      *
112      * @param objectId Id of the object to be edited or <code>null</code> if a
113      * form for a new object is requested
114      * @param parentId Id of the the parent object or <code>null</code> if
115      * either an existing object is to be edited or the form belongs to a
116      * root list
117      */

118     public EditorReference createEditorPath(String JavaDoc objectId, String JavaDoc parentId,
119             MessageResolver messageResolver) {
120
121         if (objectId != null) {
122             //Editing an existing object
123
Object JavaDoc bean = loadBean(objectId);
124             return createEditorPath(bean, messageResolver);
125         }
126         else {
127             //Creating a new object - delegate the call to the parent editor
128
EditorReference parent = getParentEditorDefinition()
129                     .createEditorPath(null, parentId, messageResolver);
130
131             EditorReference component = createPathComponent(null, parentId);
132             component.setParent(parent);
133             return component;
134         }
135     }
136
137     /**
138      * Returns a PathComponent for the given bean that represents the complete
139      * path to the form.
140      */

141     public EditorReference createEditorPath(Object JavaDoc bean,
142             MessageResolver messageResolver) {
143
144         EditorReference component = null;
145         Object JavaDoc parentBean = null;
146         if (!(getParentEditorDefinition() instanceof ListDefinition)) {
147             String JavaDoc objectId = EditorDefinitionUtils.getObjectId(this, bean);
148             component = createReference(objectId, messageResolver);
149             parentBean = bean;
150         }
151         else {
152             component = createPathComponent(bean, null);
153             parentBean = EditorDefinitionUtils.getParent(this, bean);
154         }
155
156         //Create ancestors
157
EditorReference parent = getParentEditorDefinition()
158                 .createEditorPath(parentBean, messageResolver);
159
160         component.setParent(parent);
161         return component;
162     }
163
164     /**
165      * Creates a PathComponent for the given bean (or parentId). This method is
166      * not recursive and {@link EditorReference#getParent() getParent()} will
167      * always return <code>null</code>.
168      *
169      * @param bean The bean to be edited or <code>null</code> if a form for a
170      * new (unsaved) object is requested.
171      * @param parentId Id of the the parent object or <code>null</code> if
172      * either an existing object is to be edited or the form belongs to a
173      * root list
174      */

175     public EditorReference createPathComponent(Object JavaDoc bean, String JavaDoc parentId) {
176         EditorReference component = new EditorReference();
177         component.setEditorType(getEditorType());
178         component.setLabel(getLabel(bean));
179         component.setBean(bean);
180         String JavaDoc objectId = null;
181         if (bean != null) {
182              objectId = EditorDefinitionUtils.getObjectId(this, bean);
183              component.setObjectId(objectId);
184         }
185         component.setEditorUrl(getEditorUrl(objectId, parentId));
186         return component;
187     }
188
189     /**
190      *
191      */

192     public EditorReference createReference(String JavaDoc objectId,
193             MessageResolver messageResolver) {
194
195         EditorReference ref = new EditorReference();
196         ref.setEditorType(getEditorType());
197         String JavaDoc defaultLabel = FormatUtils.camelToTitleCase(getId());
198         ref.setLabel(messageResolver.getMessage(
199                 getMessageKey().toString(), null, defaultLabel));
200
201         ref.setObjectId(objectId);
202         ref.setEditorUrl(getEditorUrl(objectId, null));
203         return ref;
204     }
205     
206     public String JavaDoc getEditorUrl(String JavaDoc objectId, String JavaDoc parentId) {
207         return getEditorRepository().getRiotServletPrefix()
208                 + getEditorUrlWithinServlet(objectId, parentId);
209     }
210     
211     protected abstract String JavaDoc getEditorUrlWithinServlet(
212             String JavaDoc objectId, String JavaDoc parentId);
213     
214 }
215
Popular Tags