1 16 package org.outerj.daisy.books.frontend; 17 18 import org.apache.cocoon.forms.event.ActionListener; 19 import org.apache.cocoon.forms.event.ActionEvent; 20 import org.apache.cocoon.forms.formmodel.Form; 21 import org.apache.cocoon.forms.formmodel.Repeater; 22 import org.apache.cocoon.forms.formmodel.Field; 23 import org.apache.cocoon.forms.formmodel.Action; 24 import org.apache.cocoon.forms.datatype.SelectionList; 25 import org.apache.cocoon.forms.datatype.Datatype; 26 import org.apache.cocoon.forms.datatype.StaticSelectionList; 27 import org.apache.cocoon.forms.util.StringMessage; 28 import org.apache.avalon.framework.service.ServiceManager; 29 import org.outerj.daisy.books.publisher.BookPublisher; 30 import org.outerj.daisy.books.publisher.PublicationSpec; 31 import org.outerj.daisy.books.publisher.PublicationTypeInfo; 32 import org.outerx.daisy.x10Bookpubspecs.PublicationSpecsDocument; 33 34 import java.util.Map ; 35 import java.util.Iterator ; 36 import java.util.Arrays ; 37 38 public class PublicationTypesFormHelper { 39 private static class AddPublicationTypeAction implements ActionListener { 40 private final PublicationTypeInfo[] publicationTypeInfos; 41 private final ServiceManager serviceManager; 42 private final String daisyContextPath; 43 44 public AddPublicationTypeAction(PublicationTypeInfo[] publicationTypeInfos, ServiceManager serviceManager, String daisyContextPath) { 45 this.publicationTypeInfos = publicationTypeInfos; 46 this.serviceManager = serviceManager; 47 this.daisyContextPath = daisyContextPath; 48 } 49 50 public void actionPerformed(ActionEvent actionEvent) { 51 Form form = actionEvent.getSourceWidget().getForm(); 52 String typeName = (String )form.lookupWidget("editors/gui/availablePublicationTypes").getValue(); 53 Repeater publicationsRepeater = (Repeater)form.lookupWidget("editors/gui/publications"); 54 Repeater.RepeaterRow row = publicationsRepeater.addRow(); 55 String typeLabel = getPublicationTypeLabel(typeName, publicationTypeInfos); 56 row.getChild("typeLabel").setValue(typeLabel); 57 row.getChild("typeName").setValue(typeName); 58 row.getChild("outputName").setValue(typeName); 59 row.getChild("outputLabel").setValue(typeLabel); 60 61 loadDefaultProperties(row, typeName, serviceManager, daisyContextPath); 62 } 63 } 64 65 private static String getPublicationTypeLabel(String name, PublicationTypeInfo[] publicationTypeInfos) { 66 for (int i = 0; i < publicationTypeInfos.length; i++) { 67 if (publicationTypeInfos[i].getName().equals(name)) { 68 return publicationTypeInfos[i].getLabel(); 69 } 70 } 71 return "(error: specified publication type unknown)"; 72 } 73 74 private static void loadDefaultProperties(Repeater.RepeaterRow row, String publicationTypeName, ServiceManager serviceManager, String daisyContextPath) { 75 Map properties; 76 BookPublisher bookPublisher = null; 77 try { 78 bookPublisher = (BookPublisher)serviceManager.lookup(BookPublisher.ROLE); 79 properties = bookPublisher.getDefaultProperties(publicationTypeName, daisyContextPath); 80 } catch (Exception e) { 81 throw new RuntimeException (e); 82 } finally { 83 if (bookPublisher != null) 84 serviceManager.release(bookPublisher); 85 } 86 Field defaultPropertiesField = (Field)row.getChild("defaultProperties"); 87 defaultPropertiesField.setSelectionList(buildDefaultPropertiesSelectionList(properties, defaultPropertiesField.getDatatype())); 88 defaultPropertiesField.setAttribute("properties", properties); 89 } 90 91 private static SelectionList buildDefaultPropertiesSelectionList(Map properties, Datatype datatype) { 92 StaticSelectionList selectionList = new StaticSelectionList(datatype); 93 Iterator propertiesIt = properties.entrySet().iterator(); 94 while (propertiesIt.hasNext()) { 95 Map.Entry entry = (Map.Entry )propertiesIt.next(); 96 selectionList.addItem(entry.getKey(), new StringMessage(entry.getKey() + " = " + entry.getValue())); 97 } 98 return selectionList; 99 } 100 101 private static PublicationTypeInfo[] getPublicationTypeInfos(ServiceManager serviceManager, String daisyContextPath) throws Exception { 102 BookPublisher bookPublisher = null; 103 try { 104 bookPublisher = (BookPublisher)serviceManager.lookup(BookPublisher.ROLE); 105 return bookPublisher.getAvailablePublicationTypes(daisyContextPath); 106 } finally { 107 if (bookPublisher != null) 108 serviceManager.release(bookPublisher); 109 } 110 } 111 112 public static void initPublicationsForm(Form form, ServiceManager serviceManager, 113 String daisyContextPath) throws Exception { 114 PublicationTypeInfo[] publicationTypeInfos = getPublicationTypeInfos(serviceManager, daisyContextPath); 115 Arrays.sort(publicationTypeInfos); 116 Field availablePublicationTypesField = (Field)form.lookupWidget("editors/gui/availablePublicationTypes"); 117 availablePublicationTypesField.setSelectionList(publicationTypeInfos, "name", "label"); 118 ((Action)form.lookupWidget("editors/gui/addPublicationType")).addActionListener(new AddPublicationTypeAction(publicationTypeInfos, serviceManager, daisyContextPath)); 119 } 120 121 public static void loadPublicationSpecs(Form form, PublicationSpec[] specs, 122 ServiceManager serviceManager, String daisyContextPath) throws Exception { 123 PublicationTypeInfo[] publicationTypeInfos = getPublicationTypeInfos(serviceManager, daisyContextPath); 124 Repeater repeater = (Repeater)form.lookupWidget("editors/gui/publications"); 125 repeater.clear(); for (int i = 0; i < specs.length; i++) { 127 Repeater.RepeaterRow row = repeater.addRow(); 128 row.getChild("typeLabel").setValue(getPublicationTypeLabel(specs[i].getPublicationTypeName(), publicationTypeInfos)); 129 row.getChild("typeName").setValue(specs[i].getPublicationTypeName()); 130 row.getChild("outputName").setValue(specs[i].getPublicationOutputName()); 131 row.getChild("outputLabel").setValue(specs[i].getPublicationOutputLabel()); 132 Repeater propertiesRepeater = (Repeater)row.getChild("properties"); 133 Iterator propsIt = specs[i].getPublicationProperties().entrySet().iterator(); 134 while (propsIt.hasNext()) { 135 Map.Entry entry = (Map.Entry )propsIt.next(); 136 Repeater.RepeaterRow propRow = propertiesRepeater.addRow(); 137 propRow.getChild("name").setValue(entry.getKey()); 138 propRow.getChild("value").setValue(entry.getValue()); 139 } 140 loadDefaultProperties(row, specs[i].getPublicationTypeName(), serviceManager, daisyContextPath); 141 } 142 } 143 144 public static PublicationSpecsDocument getXml(Form form) { 145 PublicationSpecsDocument publicationSpecsDocument = PublicationSpecsDocument.Factory.newInstance(); 146 PublicationSpecsDocument.PublicationSpecs publicationSpecsXml = publicationSpecsDocument.addNewPublicationSpecs(); 147 148 Repeater repeater = (Repeater)form.lookupWidget("editors/gui/publications"); 149 for (int i = 0; i < repeater.getSize(); i++) { 150 Repeater.RepeaterRow row = repeater.getRow(i); 151 String typeName = (String )row.getChild("typeName").getValue(); 152 String outputName = (String )row.getChild("outputName").getValue(); 153 String outputLabel = (String )row.getChild("outputLabel").getValue(); 154 155 PublicationSpecsDocument.PublicationSpecs.PublicationSpec publicationSpecXml = publicationSpecsXml.addNewPublicationSpec(); 156 publicationSpecXml.setName(outputName); 157 publicationSpecXml.setType(typeName); 158 publicationSpecXml.setLabel(outputLabel); 159 160 Repeater propertiesRepeater = (Repeater)row.getChild("properties"); 161 if (propertiesRepeater.getSize() > 0) { 162 PublicationSpecsDocument.PublicationSpecs.PublicationSpec.Properties propertiesXml = publicationSpecXml.addNewProperties(); 163 for (int k = 0; k < propertiesRepeater.getSize(); k++) { 164 Repeater.RepeaterRow propRow = propertiesRepeater.getRow(k); 165 String name = (String )propRow.getChild("name").getValue(); 166 String value = (String )propRow.getChild("value").getValue(); 167 if (name != null) { 168 PublicationSpecsDocument.PublicationSpecs.PublicationSpec.Properties.Entry entryXml = propertiesXml.addNewEntry(); 169 entryXml.setKey(name); 170 if (value != null) 171 entryXml.setStringValue(value); 172 } 173 } 174 } 175 } 176 177 return publicationSpecsDocument; 178 } 179 } 180 | Popular Tags |