001// Copyright 2007, 2008, 2010 The Apache Software Foundation 002// 003// Licensed under the Apache License, Version 2.0 (the "License"); 004// you may not use this file except in compliance with the License. 005// You may obtain a copy of the License at 006// 007// http://www.apache.org/licenses/LICENSE-2.0 008// 009// Unless required by applicable law or agreed to in writing, software 010// distributed under the License is distributed on an "AS IS" BASIS, 011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012// See the License for the specific language governing permissions and 013// limitations under the License. 014 015package org.apache.tapestry5.internal.services; 016 017import org.apache.tapestry5.ComponentResources; 018import org.apache.tapestry5.internal.structure.Page; 019import org.apache.tapestry5.runtime.Component; 020import org.apache.tapestry5.runtime.RenderCommand; 021import org.apache.tapestry5.services.Ajax; 022import org.apache.tapestry5.services.ComponentEventResultProcessor; 023 024import java.io.IOException; 025 026/** 027 * Performs a partial page render based on a root component. If the component is actually a page, 028 * it will send an JSON reponse to rediret to the page. 029 */ 030@SuppressWarnings("all") 031public class AjaxComponentInstanceEventResultProcessor implements ComponentEventResultProcessor<Component> 032{ 033 private final RequestPageCache cache; 034 035 private final ComponentEventResultProcessor masterProcessor; 036 037 public AjaxComponentInstanceEventResultProcessor(RequestPageCache cache, @Ajax 038 ComponentEventResultProcessor masterProcessor) 039 { 040 this.cache = cache; 041 this.masterProcessor = masterProcessor; 042 } 043 044 public void processResultValue(Component value) throws IOException 045 { 046 ComponentResources resources = value.getComponentResources(); 047 048 boolean isPage = value == resources.getPage(); 049 050 String pageName = resources.getPageName(); 051 052 if (isPage) 053 { 054 // This will ultimately send a JSON response to redirect to the page 055 056 masterProcessor.processResultValue(pageName); 057 return; 058 } 059 060 // Otherwise, a component within a page. Components are transformed to implement RenderCommand, but if we just 061 // pass the component itself to the master processor, we'll get in a loop, so we instead 062 // pass the ComponentPageElement (which implements RenderCommand as well). 063 064 Page page = cache.get(pageName); 065 066 String nestedId = resources.getNestedId(); 067 068 RenderCommand command = page.getComponentElementByNestedId(nestedId); 069 070 masterProcessor.processResultValue(command); 071 } 072}