01: /*
02: * Copyright 2004 Outerthought bvba and Schaubroeck nv
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.event.ValueChangedListener;
19: import org.apache.cocoon.forms.event.ValueChangedEvent;
20: import org.apache.cocoon.forms.formmodel.Form;
21: import org.apache.cocoon.forms.formmodel.Widget;
22:
23: /**
24: * Listener shared between forms that have a union to allow them to switch
25: * between XML and GUI editing modes.
26: */
27: public class EditModeListener implements ValueChangedListener {
28: private EditModeListenerConfig config;
29:
30: public EditModeListener(EditModeListenerConfig config) {
31: this .config = config;
32: }
33:
34: public void valueChanged(ValueChangedEvent valueChangedEvent) {
35: String editMode = (String) valueChangedEvent.getNewValue();
36: String oldEditMode = (String) valueChangedEvent.getOldValue();
37: if (oldEditMode == null)
38: return;
39:
40: Form form = valueChangedEvent.getSourceWidget().getForm();
41:
42: if (form.getAttribute("ignore-editmode-change") != null) {
43: form.removeAttribute("ignore-editmode-change");
44: return;
45:
46: }
47:
48: Widget widget = form
49: .lookupWidget(oldEditMode.equals("xml") ? config
50: .getXmlFieldPath() : "editors/gui");
51: boolean valid = widget.validate();
52: if (!valid) {
53: // stay in same mode
54: form.setAttribute("ignore-editmode-change", Boolean.TRUE);
55: form.getChild("editmode").setValue(oldEditMode);
56: } else {
57: if (editMode.equals("xml")) {
58: String xml = config.getXmlFromGuiEditor();
59: form.lookupWidget(config.getXmlFieldPath()).setValue(
60: xml);
61: } else if (editMode.equals("gui")) {
62: String value = (String) form.lookupWidget(
63: config.getXmlFieldPath()).getValue();
64: if (value != null) {
65: if (!config.loadGui(value)) {
66: // Stay in xml mode
67: form.setAttribute("ignore-editmode-change",
68: Boolean.TRUE);
69: form.getChild("editmode").setValue("xml");
70: }
71: } else {
72: config.clearGui();
73: }
74: }
75: }
76: }
77:
78: static interface EditModeListenerConfig {
79: public String getXmlFieldPath();
80:
81: public String getXmlFromGuiEditor();
82:
83: public boolean loadGui(String xml);
84:
85: public void clearGui();
86: }
87: }
|