1 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 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 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 loadBean(String objectId) { 92 return EditorDefinitionUtils.loadBean(targetListDefinition, objectId); 93 } 94 95 protected String getDisplayName(Object object) { 96 return object != null ? targetEditorDefinition.getLabel(object) : null; 97 } 98 99 protected String getChooserUrl() { 100 return targetListDefinition.getEditorUrl(null, null) 101 + "?choose=" + targetEditorDefinition.getId(); 102 } 103 104 } 105 | Popular Tags |