1 24 package org.riotfamily.riot.editor; 25 26 import java.util.ArrayList ; 27 import java.util.Collection ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.TreeSet ; 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 discriminatorProperty; 42 43 private List formDefinitions = new ArrayList (); 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 getFormId() { 59 FormDefinition defaultFormDef = (FormDefinition) formDefinitions.get(0); 60 return defaultFormDef.getFormId(); 61 } 62 63 public Class getBeanClass() { 64 return getParentEditorDefinition().getBeanClass(); 65 } 66 67 protected String getDefaultName() { 68 return null; 69 } 70 71 protected FormDefinition getFormDefinition(String objectId) { 72 Object 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 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 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 bean) { 100 String discriminator = PropertyUtils.getPropertyAsString( 101 bean, discriminatorProperty); 102 103 Iterator 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 bean) { 114 TreeSet forms = new TreeSet (new FormDefinitionComparator(bean)); 115 forms.addAll(formDefinitions); 116 return (FormDefinition) forms.first(); 117 } 118 119 public String getEditorUrl(String objectId, String parentId) { 120 if (objectId != null) { 121 FormDefinition formDefinition = getFormDefinition(objectId); 122 return formDefinition.getEditorUrl(objectId, parentId); 123 } 124 else { 125 StringBuffer sb = new StringBuffer (); 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 createOptions(String parentId, 137 MessageResolver messageResolver) { 138 139 ArrayList options = new ArrayList (); 140 Iterator it = formDefinitions.iterator(); 141 while (it.hasNext()) { 142 FormDefinition option = (FormDefinition) it.next(); 143 String 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 idPrefix) { 153 FormChooserDefinition copy = (FormChooserDefinition) 154 super.copy(idPrefix); 155 156 copy.formDefinitions = new ArrayList (); 157 Iterator 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 bean) { 169 super(bean.getClass()); 170 } 171 172 public int compare(Object o1, Object 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 |