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