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.binding; 016 017 import org.apache.hivemind.Location; 018 import org.apache.hivemind.util.Defense; 019 import org.apache.tapestry.BindingException; 020 import org.apache.tapestry.IActionListener; 021 import org.apache.tapestry.IComponent; 022 import org.apache.tapestry.IRequestCycle; 023 import org.apache.tapestry.PageRedirectException; 024 import org.apache.tapestry.RedirectException; 025 import org.apache.tapestry.coerce.ValueConverter; 026 027 /** 028 * @author Howard M. Lewis Ship 029 * @since 4.0 030 */ 031 public class ListenerMethodBinding extends AbstractBinding implements IActionListener 032 { 033 private final IComponent _component; 034 035 private final String _methodName; 036 037 // We have to defer obtaining the listener until after the page is loaded, because it is 038 // (currently) reliant on the page's engine property to gain access to the 039 // ListenerMapSource. I'd prefer it if this was a final field, resolved by the constructor, 040 // but that will involve injecting the ListenerMapSource into AbstractComponent. 041 042 private IActionListener _listener; 043 044 public ListenerMethodBinding(String description, ValueConverter valueConverter, Location location, 045 IComponent component, String methodName) 046 { 047 super(description, valueConverter, location); 048 049 Defense.notNull(component, "component"); 050 Defense.notNull(methodName, "methodName"); 051 052 _component = component; 053 _methodName = methodName; 054 } 055 056 public Object getComponent() 057 { 058 return _component; 059 } 060 061 /** 062 * Returns this binding object; the binding object delegates to the actual listener. This allows 063 * us to intercept errors and report the location of the binding. 064 */ 065 public Object getObject() 066 { 067 return this; 068 } 069 070 public void actionTriggered(IComponent component, IRequestCycle cycle) 071 { 072 try 073 { 074 if (_listener == null) 075 _listener = _component.getListeners().getListener(_methodName); 076 077 _listener.actionTriggered(component, cycle); 078 } 079 catch (PageRedirectException ex) 080 { 081 throw ex; 082 } 083 catch (RedirectException ex) 084 { 085 throw ex; 086 } 087 catch (RuntimeException ex) 088 { 089 throw new BindingException(BindingMessages.listenerMethodFailure( 090 _component, 091 _methodName, 092 ex), _component, getLocation(), this, ex); 093 } 094 } 095 096 protected void extendDescription(StringBuffer buffer) 097 { 098 buffer.append(", component="); 099 buffer.append(_component.getExtendedId()); 100 buffer.append(", methodName="); 101 buffer.append(_methodName); 102 } 103 104 }