1 25 package org.riotfamily.riot.editor; 26 27 import java.util.List ; 28 29 import org.riotfamily.common.i18n.MessageResolver; 30 import org.riotfamily.riot.editor.ui.EditorReference; 31 import org.springframework.util.Assert; 32 33 public class TreeDefinition extends ListDefinition { 34 35 protected static final String TYPE_TREE = "tree"; 36 37 private Class branchClass; 38 39 private FormDefinition formDefinition; 40 41 private ListDefinition nodeListDefinition; 42 43 44 public TreeDefinition(EditorRepository editorRepository) { 45 super(editorRepository); 46 } 47 48 private boolean isNode(Object bean) { 49 if (bean == null) { 50 return false; 51 } 52 if (!nodeListDefinition.getBeanClass().isInstance(bean)) { 53 return false; 54 } 55 56 EditorDefinition parentDef = getParentEditorDefinition(); 57 58 if (parentDef != null && !(parentDef instanceof GroupDefinition)) { 59 Class parentClass = parentDef.getBeanClass(); 60 if (parentClass.isInstance(bean)) { 61 return false; 62 } 63 } 64 return true; 65 } 66 67 68 public void setBranchClass(Class nodeClass) { 69 this.branchClass = nodeClass; 70 } 71 72 public ListDefinition getNodeListDefinition() { 73 return this.nodeListDefinition; 74 } 75 76 public void setDisplayDefinition(final EditorDefinition def) { 77 Assert.isInstanceOf(FormDefinition.class, def); 78 super.setDisplayDefinition(def); 79 80 formDefinition = (FormDefinition) def; 81 nodeListDefinition = new NodeListDefinition(); 82 83 getEditorRepository().addEditorDefinition(nodeListDefinition); 84 formDefinition.getChildEditorDefinitions().add(0, nodeListDefinition); 85 86 FormDefinition nodeForm = formDefinition.copy("node-"); 87 nodeForm.setParentEditorDefinition(nodeListDefinition); 88 nodeListDefinition.setDisplayDefinition(nodeForm); 89 nodeListDefinition.setParentEditorDefinition(nodeForm); 90 } 91 92 public EditorReference createEditorPath(Object bean, 93 MessageResolver messageResolver) { 94 95 if (isNode(bean)) { 96 return nodeListDefinition.createEditorPath(bean, messageResolver); 97 } 98 99 return super.createEditorPath(bean, messageResolver); 100 } 101 102 private class NodeListDefinition extends ListDefinition { 103 104 NodeListDefinition() { 105 super(TreeDefinition.this, TreeDefinition.this.getEditorRepository()); 106 } 107 108 public String getId() { 109 return "node-" + super.getId(); 110 } 111 112 private EditorReference stripListIfTreeIsHomogeneous( 113 EditorReference path) { 114 115 if (formDefinition.getChildEditorDefinitions().size() == 1) { 116 EditorReference parent = path.getParent(); 117 parent.setEditorUrl(path.getEditorUrl()); 118 parent.setEditorType("node"); 119 return parent; 120 } 121 return path; 122 } 123 124 public EditorReference createEditorPath(String objectId, 125 String parentId, MessageResolver messageResolver) { 126 127 return stripListIfTreeIsHomogeneous(super.createEditorPath( 128 objectId, parentId, messageResolver)); 129 } 130 131 public EditorReference createEditorPath(Object bean, 132 MessageResolver messageResolver) { 133 134 if (isNode(bean)) { 135 return stripListIfTreeIsHomogeneous(super.createEditorPath( 136 bean, messageResolver)); 137 } 138 else { 139 return TreeDefinition.this.createEditorPath( 140 bean, messageResolver); 141 } 142 } 143 144 public void addReference(List refs, EditorDefinition parentDef, 145 Object parent, MessageResolver messageResolver) { 146 147 if (branchClass == null || branchClass.isInstance(parent)) { 148 super.addReference(refs, parentDef, parent, messageResolver); 149 } 150 } 151 } 152 153 } 154 | Popular Tags |