KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > frontend > admin > LabelsAndDescriptionBinding


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.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 JavaDoc;
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 JavaDoc locale = LocaleHelper.parseLocale((String JavaDoc)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 JavaDoc locale = LocaleHelper.parseLocale((String JavaDoc)repeater.getRow(i).getChild("locale").getValue());
47           String JavaDoc label = (String JavaDoc)repeater.getRow(i).getChild("label").getValue();
48           String JavaDoc description = (String JavaDoc)repeater.getRow(i).getChild("description").getValue();
49           displayData.setLabel(locale, label);
50           displayData.setDescription(locale, description);
51         }
52     }
53
54     private DisplayData getDisplayData(Object JavaDoc 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 JavaDoc("Unsupported object: " + object.getClass().getName());
63         }
64     }
65
66     private interface DisplayData {
67         public String JavaDoc getLabelExact(Locale JavaDoc locale);
68         public String JavaDoc getDescriptionExact(Locale JavaDoc locale);
69         public void setLabel(Locale JavaDoc locale, String JavaDoc label);
70         public void setDescription(Locale JavaDoc locale, String JavaDoc 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 JavaDoc getLabelExact(Locale JavaDoc locale) {
81             return documentType.getLabelExact(locale);
82         }
83
84         public String JavaDoc getDescriptionExact(Locale JavaDoc locale) {
85             return documentType.getDescriptionExact(locale);
86         }
87
88         public void setLabel(Locale JavaDoc locale, String JavaDoc label) {
89             documentType.setLabel(locale, label);
90         }
91
92         public void setDescription(Locale JavaDoc locale, String JavaDoc 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 JavaDoc getLabelExact(Locale JavaDoc locale) {
105             return fieldType.getLabelExact(locale);
106         }
107
108         public String JavaDoc getDescriptionExact(Locale JavaDoc locale) {
109             return fieldType.getDescriptionExact(locale);
110         }
111
112         public void setLabel(Locale JavaDoc locale, String JavaDoc label) {
113             fieldType.setLabel(locale, label);
114         }
115
116         public void setDescription(Locale JavaDoc locale, String JavaDoc 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 JavaDoc getLabelExact(Locale JavaDoc locale) {
129             return partType.getLabelExact(locale);
130         }
131
132         public String JavaDoc getDescriptionExact(Locale JavaDoc locale) {
133             return partType.getDescriptionExact(locale);
134         }
135
136         public void setLabel(Locale JavaDoc locale, String JavaDoc label) {
137             partType.setLabel(locale, label);
138         }
139
140         public void setDescription(Locale JavaDoc locale, String JavaDoc description) {
141             partType.setDescription(locale, description);
142         }
143     }
144 }
145
Popular Tags