1 24 package org.riotfamily.riot.editor; 25 26 import java.util.ArrayList ; 27 import java.util.Iterator ; 28 import java.util.LinkedList ; 29 import java.util.List ; 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 41 public abstract class AbstractObjectEditorDefinition 42 extends AbstractEditorDefinition 43 implements ObjectEditorDefinition { 44 45 private Class beanClass; 46 47 private String labelProperty; 48 49 private List childEditorDefinitions = new LinkedList (); 50 51 public Class 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 beanClass) { 60 this.beanClass = beanClass; 61 } 62 63 public String 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 labelProperty) { 76 this.labelProperty = labelProperty; 77 } 78 79 protected String getConfiguredLabelProperty() { 80 return labelProperty; 81 } 82 83 public List getChildEditorDefinitions() { 84 return childEditorDefinitions; 85 } 86 87 public List getChildEditorReferences(Object object, 88 MessageResolver messageResolver) { 89 90 List refs = new ArrayList (); 91 Iterator 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 loadBean(String objectId) { 105 return EditorDefinitionUtils.loadBean(this, objectId); 106 } 107 108 118 public EditorReference createEditorPath(String objectId, String parentId, 119 MessageResolver messageResolver) { 120 121 if (objectId != null) { 122 Object bean = loadBean(objectId); 124 return createEditorPath(bean, messageResolver); 125 } 126 else { 127 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 141 public EditorReference createEditorPath(Object bean, 142 MessageResolver messageResolver) { 143 144 EditorReference component = null; 145 Object parentBean = null; 146 if (!(getParentEditorDefinition() instanceof ListDefinition)) { 147 String 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 EditorReference parent = getParentEditorDefinition() 158 .createEditorPath(parentBean, messageResolver); 159 160 component.setParent(parent); 161 return component; 162 } 163 164 175 public EditorReference createPathComponent(Object bean, String parentId) { 176 EditorReference component = new EditorReference(); 177 component.setEditorType(getEditorType()); 178 component.setLabel(getLabel(bean)); 179 component.setBean(bean); 180 String 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 192 public EditorReference createReference(String objectId, 193 MessageResolver messageResolver) { 194 195 EditorReference ref = new EditorReference(); 196 ref.setEditorType(getEditorType()); 197 String 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 getEditorUrl(String objectId, String parentId) { 207 return getEditorRepository().getRiotServletPrefix() 208 + getEditorUrlWithinServlet(objectId, parentId); 209 } 210 211 protected abstract String getEditorUrlWithinServlet( 212 String objectId, String parentId); 213 214 } 215 | Popular Tags |