KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > frontend > editor > BookPublicationTypesPartEditor


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.frontend.editor;
17
18 import org.apache.cocoon.forms.formmodel.Form;
19 import org.apache.cocoon.forms.formmodel.Field;
20 import org.apache.cocoon.forms.formmodel.Repeater;
21 import org.apache.cocoon.forms.validation.ValidationErrorAware;
22 import org.apache.cocoon.forms.validation.ValidationError;
23 import org.apache.cocoon.components.ContextHelper;
24 import org.apache.avalon.framework.service.ServiceManager;
25 import org.apache.avalon.framework.context.Context;
26 import org.apache.xmlbeans.XmlOptions;
27 import org.outerj.daisy.repository.schema.PartTypeUse;
28 import org.outerj.daisy.repository.Repository;
29 import org.outerj.daisy.repository.Document;
30 import org.outerj.daisy.repository.Part;
31 import org.outerj.daisy.frontend.util.FormHelper;
32 import org.outerj.daisy.frontend.WikiHelper;
33 import org.outerj.daisy.books.publisher.PublicationSpec;
34 import org.outerj.daisy.books.publisher.PublicationSpecBuilder;
35 import org.outerj.daisy.books.frontend.PublicationTypesFormHelper;
36 import org.outerx.daisy.x10Bookpubspecs.PublicationSpecsDocument;
37
38 import java.io.StringReader JavaDoc;
39 import java.util.Map JavaDoc;
40
41 public class BookPublicationTypesPartEditor implements PartEditor {
42     private final ServiceManager serviceManager;
43     private final Context context;
44
45     private BookPublicationTypesPartEditor(ServiceManager serviceManager, Context context) {
46         this.serviceManager = serviceManager;
47         this.context = context;
48     }
49
50     public static class Factory implements PartEditorFactory {
51         public PartEditor getPartEditor(Map JavaDoc properties, ServiceManager serviceManager, Context context) {
52             return new BookPublicationTypesPartEditor(serviceManager, context);
53         }
54     }
55
56     public Form getForm(PartTypeUse partTypeUse, DocumentEditorForm documentEditorForm, Repository repository) throws Exception JavaDoc {
57         final Form form = FormHelper.createForm(serviceManager, "books/resources/form/selectpublicationtypes_definition.xml");
58
59         Field editMode = (Field)form.getChild("editmode");
60         editMode.setValue("gui");
61         editMode.addValueChangedListener(new EditModeListener(new EditModeListener.EditModeListenerConfig() {
62             public String JavaDoc getXmlFieldPath() {
63                 return "editors/xml/xmlText";
64             }
65
66             public String JavaDoc getXmlFromGuiEditor() {
67                 return getPublicationTypesXml(form);
68             }
69
70             public boolean loadGui(String JavaDoc xml) {
71                 PublicationSpec[] specs;
72                 try {
73                     specs = PublicationSpecBuilder.build(new StringReader JavaDoc(xml));
74                 } catch (Throwable JavaDoc e) {
75                     // TODO nicer error message?
76
((ValidationErrorAware)form.lookupWidget(getXmlFieldPath())).setValidationError(new ValidationError(e.toString(), false));
77                     return false;
78                 }
79                 try {
80                     PublicationTypesFormHelper.loadPublicationSpecs(form, specs, serviceManager, WikiHelper.getDaisyContextPath(ContextHelper.getRequest(context)));
81                 } catch (Throwable JavaDoc e) {
82                     // TODO nicer error message?
83
((ValidationErrorAware)form.lookupWidget(getXmlFieldPath())).setValidationError(new ValidationError(e.toString(), false));
84                     return false;
85                 }
86                 return true;
87             }
88
89             public void clearGui() {
90                 Repeater repeater = (Repeater)form.lookupWidget("editors/gui/publications");
91                 repeater.clear();
92             }
93         }));
94
95         PublicationTypesFormHelper.initPublicationsForm(form, serviceManager, WikiHelper.getDaisyContextPath(ContextHelper.getRequest(context)));
96         return form;
97     }
98
99     public String JavaDoc getFormTemplate() {
100         return "resources/form/parteditor_bookpublicationtypes_template.xml";
101     }
102
103     public void load(Form form, Document document, Part part, Repository repository) throws Exception JavaDoc {
104         byte[] data = part.getData();
105
106         PublicationSpec[] specs = null;
107         try {
108             specs = PublicationSpecBuilder.build(part.getDataStream());
109         } catch (Throwable JavaDoc e) {
110             // ignore
111
}
112
113         if (specs != null) {
114             try {
115                 PublicationTypesFormHelper.loadPublicationSpecs(form, specs, serviceManager, WikiHelper.getDaisyContextPath(ContextHelper.getRequest(context)));
116             } catch (Throwable JavaDoc e) {
117                 // TODO nicer error message?
118
((ValidationErrorAware)form.lookupWidget("editors/xml/xmlText")).setValidationError(new ValidationError(e.toString(), false));
119                 specs = null;
120             }
121         }
122
123         if (specs == null) {
124             // parsing failed
125
form.setAttribute("ignore-editmode-change", Boolean.TRUE);
126             form.getChild("editmode").setValue("xml");
127             // TODO might want to do smarter encoding detection
128
form.lookupWidget("editors/xml/xmlText").setValue(new String JavaDoc(data, "UTF-8"));
129         }
130     }
131
132     private String JavaDoc getPublicationTypesXml(Form form) {
133         PublicationSpecsDocument publicationSpecsDocument = PublicationTypesFormHelper.getXml(form);
134         XmlOptions xmlOptions = new XmlOptions();
135         xmlOptions.setSavePrettyPrint();
136         xmlOptions.setCharacterEncoding("UTF-8");
137         return publicationSpecsDocument.xmlText(xmlOptions);
138     }
139
140     public void save(Form form, Document document) throws Exception JavaDoc {
141         Field editMode = (Field)form.getChild("editmode");
142         if (editMode.getValue().equals("xml")) {
143             PartEditorHelper.save(form, document, "editors/xml/xmlText", "text/xml");
144         } else if (editMode.getValue().equals("gui")) {
145             String JavaDoc xml = getPublicationTypesXml(form);
146             PartEditorHelper.save(form, document, null, xml.getBytes("UTF-8"), "text/xml");
147         }
148     }
149 }
150
Popular Tags