1 16 package org.outerj.daisy.frontend.admin; 17 18 import org.apache.cocoon.forms.binding.AbstractCustomBinding; 19 import org.apache.cocoon.forms.binding.BindingException; 20 import org.apache.cocoon.forms.formmodel.Widget; 21 import org.apache.cocoon.forms.formmodel.Repeater; 22 import org.apache.commons.jxpath.JXPathContext; 23 import org.outerj.daisy.repository.schema.DocumentType; 24 import org.outerj.daisy.repository.schema.FieldType; 25 import org.outerj.daisy.repository.schema.PartType; 26 import org.outerj.daisy.repository.LocaleHelper; 27 28 import java.util.Locale ; 29 30 public class LabelsAndDescriptionBinding extends AbstractCustomBinding { 31 32 protected void doLoad(Widget widget, JXPathContext jxPathContext) throws BindingException { 33 Repeater repeater = (Repeater)widget; 34 DisplayData displayData = getDisplayData(jxPathContext.getValue(".")); 35 for (int i = 0; i < repeater.getSize(); i++) { 36 Locale locale = LocaleHelper.parseLocale((String )repeater.getRow(i).getChild("locale").getValue()); 37 repeater.getRow(i).getChild("label").setValue(displayData.getLabelExact(locale)); 38 repeater.getRow(i).getChild("description").setValue(displayData.getDescriptionExact(locale)); 39 } 40 } 41 42 protected void doSave(Widget widget, JXPathContext jxPathContext) throws BindingException { 43 Repeater repeater = (Repeater)widget; 44 DisplayData displayData = getDisplayData(jxPathContext.getValue(".")); 45 for (int i = 0; i < repeater.getSize(); i++) { 46 Locale locale = LocaleHelper.parseLocale((String )repeater.getRow(i).getChild("locale").getValue()); 47 String label = (String )repeater.getRow(i).getChild("label").getValue(); 48 String description = (String )repeater.getRow(i).getChild("description").getValue(); 49 displayData.setLabel(locale, label); 50 displayData.setDescription(locale, description); 51 } 52 } 53 54 private DisplayData getDisplayData(Object object) { 55 if (object instanceof DocumentType) { 56 return new DocumentTypeDisplayData((DocumentType)object); 57 } else if (object instanceof PartType) { 58 return new PartTypeDisplayData((PartType)object); 59 } else if (object instanceof FieldType) { 60 return new FieldTypeDisplayData((FieldType)object); 61 } else { 62 throw new RuntimeException ("Unsupported object: " + object.getClass().getName()); 63 } 64 } 65 66 private interface DisplayData { 67 public String getLabelExact(Locale locale); 68 public String getDescriptionExact(Locale locale); 69 public void setLabel(Locale locale, String label); 70 public void setDescription(Locale locale, String description); 71 } 72 73 private static class DocumentTypeDisplayData implements DisplayData { 74 private DocumentType documentType; 75 76 public DocumentTypeDisplayData(DocumentType documentType) { 77 this.documentType = documentType; 78 } 79 80 public String getLabelExact(Locale locale) { 81 return documentType.getLabelExact(locale); 82 } 83 84 public String getDescriptionExact(Locale locale) { 85 return documentType.getDescriptionExact(locale); 86 } 87 88 public void setLabel(Locale locale, String label) { 89 documentType.setLabel(locale, label); 90 } 91 92 public void setDescription(Locale locale, String description) { 93 documentType.setDescription(locale, description); 94 } 95 } 96 97 private static class FieldTypeDisplayData implements DisplayData { 98 private FieldType fieldType; 99 100 public FieldTypeDisplayData(FieldType fieldType) { 101 this.fieldType = fieldType; 102 } 103 104 public String getLabelExact(Locale locale) { 105 return fieldType.getLabelExact(locale); 106 } 107 108 public String getDescriptionExact(Locale locale) { 109 return fieldType.getDescriptionExact(locale); 110 } 111 112 public void setLabel(Locale locale, String label) { 113 fieldType.setLabel(locale, label); 114 } 115 116 public void setDescription(Locale locale, String description) { 117 fieldType.setDescription(locale, description); 118 } 119 } 120 121 private static class PartTypeDisplayData implements DisplayData { 122 private PartType partType; 123 124 public PartTypeDisplayData(PartType partType) { 125 this.partType = partType; 126 } 127 128 public String getLabelExact(Locale locale) { 129 return partType.getLabelExact(locale); 130 } 131 132 public String getDescriptionExact(Locale locale) { 133 return partType.getDescriptionExact(locale); 134 } 135 136 public void setLabel(Locale locale, String label) { 137 partType.setLabel(locale, label); 138 } 139 140 public void setDescription(Locale locale, String description) { 141 partType.setDescription(locale, description); 142 } 143 } 144 } 145 | Popular Tags |