KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > riot > form > element > ObjectChooser


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.form.element;
25
26 import org.riotfamily.forms.element.select.AbstractChooser;
27 import org.riotfamily.riot.editor.EditorDefinition;
28 import org.riotfamily.riot.editor.EditorDefinitionUtils;
29 import org.riotfamily.riot.editor.EditorRepository;
30 import org.riotfamily.riot.editor.ListDefinition;
31 import org.springframework.beans.BeansException;
32 import org.springframework.beans.factory.BeanFactory;
33 import org.springframework.beans.factory.BeanFactoryAware;
34 import org.springframework.beans.factory.BeanFactoryUtils;
35 import org.springframework.beans.factory.ListableBeanFactory;
36 import org.springframework.util.Assert;
37
38 public class ObjectChooser extends AbstractChooser
39         implements BeanFactoryAware {
40
41     private String JavaDoc targetEditorId;
42         
43     private BeanFactory beanFactory;
44     
45     private EditorRepository editorRepository;
46     
47     private ListDefinition targetListDefinition;
48     
49     private EditorDefinition targetEditorDefinition;
50     
51     
52     public void setTargetEditorId(String JavaDoc targetEditorId) {
53         this.targetEditorId = targetEditorId;
54     }
55
56     public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
57         this.beanFactory = beanFactory;
58     }
59     
60     protected void afterFormSet() {
61         if (editorRepository == null) {
62             Assert.isInstanceOf(ListableBeanFactory.class, beanFactory,
63                     "Not a ListableBeanFactory");
64             
65             ListableBeanFactory lbf = (ListableBeanFactory) beanFactory;
66             editorRepository = (EditorRepository)
67                     BeanFactoryUtils.beanOfTypeIncludingAncestors(
68                     lbf, EditorRepository.class);
69             
70             Assert.notNull(editorRepository,
71                     "No EditorRepository found in BeanFactory");
72         }
73         
74         log.debug("Looking up editor: " + targetEditorId);
75         targetEditorDefinition = editorRepository.getEditorDefinition(
76                 targetEditorId);
77         
78         Assert.notNull(targetEditorDefinition, "No such EditorDefinition: "
79                     + targetEditorId);
80
81         if (targetEditorDefinition instanceof ListDefinition) {
82             targetListDefinition = (ListDefinition) targetEditorDefinition;
83             targetEditorDefinition = targetListDefinition.getDisplayDefinition();
84         }
85         else {
86             targetListDefinition = EditorDefinitionUtils
87                     .getParentListDefinition(targetEditorDefinition);
88         }
89     }
90
91     protected Object JavaDoc loadBean(String JavaDoc objectId) {
92         return EditorDefinitionUtils.loadBean(targetListDefinition, objectId);
93     }
94     
95     protected String JavaDoc getDisplayName(Object JavaDoc object) {
96         return object != null ? targetEditorDefinition.getLabel(object) : null;
97     }
98
99     protected String JavaDoc getChooserUrl() {
100         return targetListDefinition.getEditorUrl(null, null)
101                 + "?choose=" + targetEditorDefinition.getId();
102     }
103     
104 }
105
Popular Tags