001 // Copyright 2004, 2005 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 015 package org.apache.tapestry.callback; 016 017 import org.apache.hivemind.ApplicationRuntimeException; 018 import org.apache.hivemind.util.Defense; 019 import org.apache.tapestry.IComponent; 020 import org.apache.tapestry.IDirect; 021 import org.apache.tapestry.IPage; 022 import org.apache.tapestry.IRequestCycle; 023 024 /** 025 * Simple callback for re-invoking a {@link IDirect} trigger. 026 * 027 * @author Howard Lewis Ship 028 * @since 0.2.9 029 */ 030 031 public class DirectCallback implements ICallback 032 { 033 /** 034 * @since 2.0.4 035 */ 036 037 private static final long serialVersionUID = -8888847655917503471L; 038 039 private String _pageName; 040 041 private String _componentIdPath; 042 043 private Object[] _parameters; 044 045 public String toString() 046 { 047 StringBuffer buffer = new StringBuffer("DirectCallback["); 048 049 buffer.append(_pageName); 050 buffer.append('/'); 051 buffer.append(_componentIdPath); 052 053 if (_parameters != null) 054 { 055 for (int i = 0; i < _parameters.length; i++) 056 { 057 buffer.append(i == 0 ? " " : ", "); 058 buffer.append(_parameters[i]); 059 } 060 } 061 062 buffer.append(']'); 063 064 return buffer.toString(); 065 066 } 067 068 /** 069 * Creates a new DirectCallback for the component. The parameters (which may be null) is 070 * retained, not copied. 071 */ 072 073 public DirectCallback(IDirect component, Object[] parameters) 074 { 075 Defense.notNull(component, "component"); 076 077 _pageName = component.getPage().getPageName(); 078 _componentIdPath = component.getIdPath(); 079 _parameters = parameters; 080 } 081 082 /** 083 * Locates the {@link IDirect}component that was previously identified (and whose page and id 084 * path were stored). Invokes {@link IRequestCycle#setListenerParameters(Object[])(Object[])}to 085 * restore the service parameters, then invokes {@link IDirect#trigger(IRequestCycle)}on the 086 * component. 087 */ 088 089 public void performCallback(IRequestCycle cycle) 090 { 091 IPage page = cycle.getPage(_pageName); 092 IComponent component = page.getNestedComponent(_componentIdPath); 093 IDirect direct = null; 094 095 try 096 { 097 direct = (IDirect) component; 098 } 099 catch (ClassCastException ex) 100 { 101 throw new ApplicationRuntimeException(CallbackMessages.componentNotDirect(component), 102 component, null, ex); 103 } 104 105 cycle.setListenerParameters(_parameters); 106 direct.trigger(cycle); 107 } 108 }