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 /** 046 * Creates a new DirectCallback for the component. The parameters (which may be null) is 047 * retained, not copied. 048 */ 049 050 public DirectCallback(IDirect component, Object[] parameters) 051 { 052 Defense.notNull(component, "component"); 053 054 _pageName = component.getPage().getPageName(); 055 _componentIdPath = component.getIdPath(); 056 _parameters = parameters; 057 } 058 059 public String toString() 060 { 061 StringBuffer buffer = new StringBuffer("DirectCallback["); 062 063 buffer.append(_pageName); 064 buffer.append('/'); 065 buffer.append(_componentIdPath); 066 067 if (_parameters != null) 068 { 069 for (int i = 0; i < _parameters.length; i++) 070 { 071 buffer.append(i == 0 ? " " : ", "); 072 buffer.append(_parameters[i]); 073 } 074 } 075 076 buffer.append(']'); 077 078 return buffer.toString(); 079 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 }