001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.framework.container;
016:
017: import java.util.Map;
018: import org.apache.commons.collections.map.LinkedMap;
019: import org.araneaframework.Environment;
020: import org.araneaframework.EnvironmentAwareCallback;
021: import org.araneaframework.InputData;
022: import org.araneaframework.OutputData;
023: import org.araneaframework.Path;
024: import org.araneaframework.Widget;
025: import org.araneaframework.core.Assert;
026: import org.araneaframework.core.BaseApplicationWidget;
027: import org.araneaframework.core.StandardEnvironment;
028: import org.araneaframework.framework.FlowContextWidget;
029: import org.araneaframework.framework.OverlayContext;
030: import org.araneaframework.framework.FlowContext.Configurator;
031: import org.araneaframework.framework.FlowContext.Handler;
032: import org.araneaframework.http.UpdateRegionContext;
033: import org.araneaframework.http.util.EnvironmentUtil;
034:
035: /**
036: * @author Alar Kvell (alar@araneaframework.org)
037: * @author Taimo Peelo (taimo@araneaframework.org)
038: * @since 1.1
039: */
040: public class StandardOverlayContainerWidget extends
041: BaseApplicationWidget implements OverlayContext {
042: /**
043: * <p>
044: * Map containing the default overlay presentation options.
045: * Default values are as follows:</p>
046: * <ul>
047: * <li>method: post</li>
048: * <li>overlayClose: false</li>
049: * <li>width: 800</li>
050: * <li>slideDownDuration: 0.0</li>
051: * <li>slideUpDuration: 0.0</li>
052: * <li>overlayDuration: 0.0</li>
053: * <li>resizeDuration: 0.0</li>
054: * </ul>
055: */
056: public static final Map DEFAULT_PRESENTATION_OPTIONS = new LinkedMap();
057: private static final String MAIN_CHILD_KEY = "m";
058: private static final String OVERLAY_CHILD_KEY = "o";
059:
060: protected Map presentationOptions = new LinkedMap();
061:
062: private Widget main;
063: private FlowContextWidget overlay;
064:
065: static {
066: DEFAULT_PRESENTATION_OPTIONS.put("method", "post");
067: DEFAULT_PRESENTATION_OPTIONS.put("overlayClose", Boolean.FALSE);
068: DEFAULT_PRESENTATION_OPTIONS.put("width", new Integer(800));
069: DEFAULT_PRESENTATION_OPTIONS.put("slideDownDuration", String
070: .valueOf(0.0));
071: DEFAULT_PRESENTATION_OPTIONS.put("slideUpDuration", String
072: .valueOf(0.0));
073: DEFAULT_PRESENTATION_OPTIONS.put("overlayDuration", String
074: .valueOf(0.0));
075: DEFAULT_PRESENTATION_OPTIONS.put("resizeDuration", String
076: .valueOf(0.0));
077: }
078:
079: {
080: presentationOptions.putAll(DEFAULT_PRESENTATION_OPTIONS);
081: }
082:
083: public void setMain(Widget main) {
084: this .main = main;
085: }
086:
087: public void setOverlay(FlowContextWidget overlay) {
088: this .overlay = overlay;
089: }
090:
091: public boolean isOverlayActive() {
092: return overlay.isNested();
093: }
094:
095: protected Environment getChildWidgetEnvironment() throws Exception {
096: return new StandardEnvironment(super
097: .getChildWidgetEnvironment(), OverlayContext.class,
098: this );
099: }
100:
101: protected void init() throws Exception {
102: super .init();
103: Assert.notNull(main);
104: Assert.notNull(overlay);
105: addWidget(MAIN_CHILD_KEY, main);
106: addWidget(OVERLAY_CHILD_KEY, overlay);
107: overlay.addNestedEnvironmentEntry(this ,
108: OverlayActivityMarkerContext.class,
109: new OverlayActivityMarkerContext() {
110: });
111: }
112:
113: protected void update(InputData input) throws Exception {
114: if (isOverlayActive())
115: overlay._getWidget().update(input);
116: else
117: main._getWidget().update(input);
118: }
119:
120: protected void event(Path path, InputData input) throws Exception {
121: if (path != null && path.hasNext()) {
122: Object next = path.getNext();
123: Assert.isTrue(!(isOverlayActive() ? MAIN_CHILD_KEY
124: : OVERLAY_CHILD_KEY).equals(next),
125: "Cannot deliver event to wrong hierarchy!");
126: }
127: super .event(path, input);
128: }
129:
130: protected void action(Path path, InputData input, OutputData output)
131: throws Exception {
132: if (path != null && path.hasNext()) {
133: Object next = path.getNext();
134: Assert.isTrue(!(isOverlayActive() ? MAIN_CHILD_KEY
135: : OVERLAY_CHILD_KEY).equals(next),
136: "Cannot deliver action to wrong hierarchy!");
137: }
138: super .action(path, input, output);
139: }
140:
141: protected void render(OutputData output) throws Exception {
142: if (output.getInputData().getGlobalData().containsKey(
143: OverlayContext.OVERLAY_REQUEST_KEY)) {
144: overlay._getWidget().render(output);
145: if (!isOverlayActive()) { // overlay has become inactive for some reason
146: UpdateRegionContext urCtx = EnvironmentUtil
147: .getUpdateRegionContext(getEnvironment());
148: urCtx.disableOnce();
149: }
150: } else
151: main._getWidget().render(output);
152: }
153:
154: // FlowContext methods
155: public void replace(Widget flow) {
156: overlay.replace(flow);
157: }
158:
159: public void replace(Widget flow, Configurator configurator) {
160: overlay.replace(flow, configurator);
161: }
162:
163: public void reset(EnvironmentAwareCallback callback) {
164: overlay.reset(callback);
165: }
166:
167: public void start(Widget flow, Configurator configurator,
168: Handler handler) {
169: overlay.start(flow, configurator, handler);
170: }
171:
172: public void start(Widget flow, Handler handler) {
173: overlay.start(flow, handler);
174: }
175:
176: public void start(Widget flow) {
177: overlay.start(flow);
178: }
179:
180: /* The presentation options of this overlay. */
181: public Map getOverlayOptions() {
182: return presentationOptions;
183: }
184:
185: public void setOverlayOptions(Map presentationOptions) {
186: this.presentationOptions = presentationOptions;
187: }
188: }
|