KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.TreeSet JavaDoc;
31
32 import org.riotfamily.common.beans.PropertyUtils;
33 import org.riotfamily.common.collection.TypeDifferenceComparator;
34 import org.riotfamily.common.i18n.MessageResolver;
35 import org.riotfamily.common.util.FormatUtils;
36 import org.riotfamily.riot.form.ui.FormOption;
37
38
39 public class FormChooserDefinition extends FormDefinition {
40
41     private String JavaDoc discriminatorProperty;
42
43     private List JavaDoc formDefinitions = new ArrayList JavaDoc();
44
45
46     public FormChooserDefinition(EditorRepository editorRepository) {
47         super(editorRepository);
48     }
49
50     public void addFormDefinition(FormDefinition formDef) {
51         formDefinitions.add(formDef);
52         formDef.setParentEditorDefinition(getParentEditorDefinition());
53         if (getConfiguredLabelProperty() != null && formDef.getConfiguredLabelProperty() == null) {
54             formDef.setLabelProperty(getConfiguredLabelProperty());
55         }
56     }
57
58     public String JavaDoc getFormId() {
59         FormDefinition defaultFormDef = (FormDefinition) formDefinitions.get(0);
60         return defaultFormDef.getFormId();
61     }
62
63     public Class JavaDoc getBeanClass() {
64         return getParentEditorDefinition().getBeanClass();
65     }
66
67     protected String JavaDoc getDefaultName() {
68         return null;
69     }
70
71     protected FormDefinition getFormDefinition(String JavaDoc objectId) {
72         Object JavaDoc bean = loadBean(objectId);
73         if (discriminatorProperty != null) {
74             return getFormDefinitionByDiscriminator(bean);
75         }
76         else {
77             return getNearestFormDefintionByClass(bean);
78         }
79     }
80
81     public void setParentEditorDefinition(EditorDefinition editorDef) {
82         super.setParentEditorDefinition(editorDef);
83         Iterator JavaDoc it = formDefinitions.iterator();
84         while (it.hasNext()) {
85             FormDefinition formDef = (FormDefinition) it.next();
86             formDef.setParentEditorDefinition(editorDef);
87         }
88     }
89
90     public void addChildEditorDefinition(EditorDefinition editorDef) {
91         super.addChildEditorDefinition(editorDef);
92         Iterator JavaDoc it = formDefinitions.iterator();
93         while (it.hasNext()) {
94             FormDefinition formDef = (FormDefinition) it.next();
95             formDef.getChildEditorDefinitions().add(editorDef);
96         }
97     }
98
99     protected FormDefinition getFormDefinitionByDiscriminator(Object JavaDoc bean) {
100         String JavaDoc discriminator = PropertyUtils.getPropertyAsString(
101                 bean, discriminatorProperty);
102
103         Iterator JavaDoc it = formDefinitions.iterator();
104         while (it.hasNext()) {
105             FormDefinition formDefinition = (FormDefinition) it.next();
106             if (formDefinition.getDiscriminatorValue().equals(discriminator)) {
107                 return formDefinition;
108             }
109         }
110         return null;
111     }
112
113     protected FormDefinition getNearestFormDefintionByClass(Object JavaDoc bean) {
114         TreeSet JavaDoc forms = new TreeSet JavaDoc(new FormDefinitionComparator(bean));
115         forms.addAll(formDefinitions);
116         return (FormDefinition) forms.first();
117     }
118
119     public String JavaDoc getEditorUrl(String JavaDoc objectId, String JavaDoc parentId) {
120         if (objectId != null) {
121             FormDefinition formDefinition = getFormDefinition(objectId);
122             return formDefinition.getEditorUrl(objectId, parentId);
123         }
124         else {
125             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
126             sb.append(getEditorRepository().getRiotServletPrefix());
127             sb.append("/form-chooser/").append(getId());
128             sb.append("?form=").append(getFormId());
129             if (parentId != null) {
130                 sb.append("&parentId=").append(parentId);
131             }
132             return sb.toString();
133         }
134     }
135
136     public Collection JavaDoc createOptions(String JavaDoc parentId,
137             MessageResolver messageResolver) {
138
139         ArrayList JavaDoc options = new ArrayList JavaDoc();
140         Iterator JavaDoc it = formDefinitions.iterator();
141         while (it.hasNext()) {
142             FormDefinition option = (FormDefinition) it.next();
143             String JavaDoc label = messageResolver.getMessage(
144                     option.getMessageKey().toString(), null,
145                     FormatUtils.camelToTitleCase(option.getFormId()));
146
147             options.add(new FormOption(label, option.getFormId()));
148         }
149         return options;
150     }
151
152     public FormDefinition copy(String JavaDoc idPrefix) {
153         FormChooserDefinition copy = (FormChooserDefinition)
154                 super.copy(idPrefix);
155
156         copy.formDefinitions = new ArrayList JavaDoc();
157         Iterator JavaDoc it = formDefinitions.iterator();
158         while (it.hasNext()) {
159             FormDefinition formDefinition = (FormDefinition) it.next();
160             copy.formDefinitions.add(formDefinition.copy(idPrefix));
161         }
162         return copy;
163     }
164
165     protected static class FormDefinitionComparator
166             extends TypeDifferenceComparator {
167
168         public FormDefinitionComparator(Object JavaDoc bean) {
169             super(bean.getClass());
170         }
171
172         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
173             FormDefinition fd1 = (FormDefinition) o1;
174             FormDefinition fd2 = (FormDefinition) o2;
175             return super.compare(fd1.getBeanClass(), fd2.getBeanClass());
176         }
177     }
178 }
179
Popular Tags