KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > books > frontend > PublicationTypesFormHelper


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.Arrays JavaDoc;
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 JavaDoc daisyContextPath;
43
44         public AddPublicationTypeAction(PublicationTypeInfo[] publicationTypeInfos, ServiceManager serviceManager, String JavaDoc 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 JavaDoc typeName = (String JavaDoc)form.lookupWidget("editors/gui/availablePublicationTypes").getValue();
53             Repeater publicationsRepeater = (Repeater)form.lookupWidget("editors/gui/publications");
54             Repeater.RepeaterRow row = publicationsRepeater.addRow();
55             String JavaDoc 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 JavaDoc getPublicationTypeLabel(String JavaDoc 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 JavaDoc publicationTypeName, ServiceManager serviceManager, String JavaDoc daisyContextPath) {
75         Map JavaDoc properties;
76         BookPublisher bookPublisher = null;
77         try {
78             bookPublisher = (BookPublisher)serviceManager.lookup(BookPublisher.ROLE);
79             properties = bookPublisher.getDefaultProperties(publicationTypeName, daisyContextPath);
80         } catch (Exception JavaDoc e) {
81             throw new RuntimeException JavaDoc(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 JavaDoc properties, Datatype datatype) {
92         StaticSelectionList selectionList = new StaticSelectionList(datatype);
93         Iterator JavaDoc propertiesIt = properties.entrySet().iterator();
94         while (propertiesIt.hasNext()) {
95             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)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 JavaDoc daisyContextPath) throws Exception JavaDoc {
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 JavaDoc daisyContextPath) throws Exception JavaDoc {
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 JavaDoc daisyContextPath) throws Exception JavaDoc {
123         PublicationTypeInfo[] publicationTypeInfos = getPublicationTypeInfos(serviceManager, daisyContextPath);
124         Repeater repeater = (Repeater)form.lookupWidget("editors/gui/publications");
125         repeater.clear(); // clear in case this is called on an existing form
126
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 JavaDoc propsIt = specs[i].getPublicationProperties().entrySet().iterator();
134             while (propsIt.hasNext()) {
135                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc)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 JavaDoc typeName = (String JavaDoc)row.getChild("typeName").getValue();
152             String JavaDoc outputName = (String JavaDoc)row.getChild("outputName").getValue();
153             String JavaDoc outputLabel = (String JavaDoc)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 JavaDoc name = (String JavaDoc)propRow.getChild("name").getValue();
166                     String JavaDoc value = (String JavaDoc)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