01: // Copyright 2006, 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.tapestry.internal.services;
16:
17: import org.apache.tapestry.ComponentEventHandler;
18: import org.apache.tapestry.Link;
19: import org.apache.tapestry.TapestryConstants;
20: import org.apache.tapestry.internal.structure.ComponentPageElement;
21: import org.apache.tapestry.internal.structure.Page;
22: import org.apache.tapestry.internal.util.Holder;
23: import org.apache.tapestry.runtime.Component;
24: import org.apache.tapestry.services.ActionResponseGenerator;
25: import org.apache.tapestry.services.ComponentActionRequestHandler;
26: import org.apache.tapestry.services.ComponentEventResultProcessor;
27:
28: public class ComponentActionRequestHandlerImpl implements
29: ComponentActionRequestHandler {
30: private final ComponentEventResultProcessor _resultProcessor;
31:
32: private final RequestPageCache _cache;
33:
34: private final LinkFactory _linkFactory;
35:
36: public ComponentActionRequestHandlerImpl(
37: ComponentEventResultProcessor resultProcessor,
38: RequestPageCache cache, LinkFactory linkFactory) {
39: _resultProcessor = resultProcessor;
40: _cache = cache;
41: _linkFactory = linkFactory;
42: }
43:
44: public ActionResponseGenerator handle(String logicalPageName,
45: String nestedComponentId, String eventType,
46: String[] context, String[] activationContext) {
47: Page page = _cache.get(logicalPageName);
48:
49: // This is the active page, until we know better.
50:
51: ComponentPageElement element = page
52: .getComponentElementByNestedId(nestedComponentId);
53:
54: final Holder<ActionResponseGenerator> holder = new Holder<ActionResponseGenerator>();
55:
56: ComponentEventHandler handler = new ComponentEventHandler() {
57: @SuppressWarnings("unchecked")
58: public boolean handleResult(Object result,
59: Component component, String methodDescription) {
60: ActionResponseGenerator generator = _resultProcessor
61: .processComponentEvent(result, component,
62: methodDescription);
63:
64: holder.put(generator);
65:
66: return true;
67: }
68: };
69:
70: // If activating the page returns a "navigational result", then don't trigger the action
71: // on the component.
72:
73: page.getRootElement().triggerEvent(
74: TapestryConstants.ACTIVATE_EVENT, activationContext,
75: handler);
76:
77: if (holder.hasValue())
78: return holder.get();
79:
80: element.triggerEvent(eventType, context, handler);
81:
82: ActionResponseGenerator result = holder.get();
83:
84: if (result == null) {
85: Link link = _linkFactory.createPageLink(page, false);
86:
87: result = new LinkActionResponseGenerator(link);
88: }
89:
90: return result;
91: }
92: }
|