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.listener; 016 017 import java.util.Map; 018 019 import ognl.ObjectPropertyAccessor; 020 import ognl.OgnlContext; 021 import ognl.OgnlException; 022 import ognl.OgnlRuntime; 023 import ognl.PropertyAccessor; 024 import ognl.enhance.ExpressionCompiler; 025 import ognl.enhance.UnsupportedCompilationException; 026 027 /** 028 * Exposes {@link org.apache.tapestry.IActionListener} listeners provided 029 * by the {@link org.apache.tapestry.listener.ListenerMap} as read-only 030 * properties of the map. 031 * 032 * @author Howard Lewis Ship 033 * @since 2.2 034 */ 035 036 public class ListenerMapPropertyAccessor extends ObjectPropertyAccessor implements PropertyAccessor 037 { 038 039 /** 040 * Checks to see if the ListenerMapImpl provides the named listener, 041 * returning the listener if it does. Otherwise, invokes the super 042 * implementation. 043 */ 044 045 public Object getProperty(Map context, Object target, Object name) 046 throws OgnlException 047 { 048 ListenerMap map = (ListenerMap) target; 049 String listenerName = (String) name; 050 051 if (map.canProvideListener(listenerName)) 052 return map.getListener(listenerName); 053 054 return super.getProperty(context, target, name); 055 } 056 057 /** 058 * Returns true if the ListenerMap contains the named listener, otherwise 059 * invokes super-implementation. 060 */ 061 062 public boolean hasGetProperty(Map context, Object target, Object oname) 063 throws OgnlException 064 { 065 ListenerMap map = (ListenerMap) target; 066 String listenerName = (String) oname; 067 068 if (map.canProvideListener(listenerName)) return true; 069 070 return super.hasGetProperty(context, target, oname); 071 } 072 073 public Class getPropertyClass(OgnlContext context, Object target, Object name) 074 { 075 ListenerMap map = (ListenerMap) target; 076 String listenerName = (String) name; 077 078 if (map.canProvideListener(listenerName)) 079 return map.getListener(listenerName).getClass(); 080 081 return super.getPropertyClass(context, target, name); 082 } 083 084 public String getSourceAccessor(OgnlContext context, Object target, Object name) 085 { 086 ListenerMap map = (ListenerMap) target; 087 String listenerName = ((String)name).replaceAll("\"", ""); 088 089 if (map.canProvideListener(listenerName)) { 090 091 Class type = OgnlRuntime.getCompiler().getInterfaceClass(map.getListener(listenerName).getClass()); 092 093 ExpressionCompiler.addCastString(context, "((" + type.getName() + ")"); 094 095 context.setCurrentAccessor(ListenerMap.class); 096 context.setCurrentType(type); 097 098 return ".getListener(" + name + "))"; 099 } 100 101 return super.getSourceAccessor(context, target, name); 102 } 103 104 public String getSourceSetter(OgnlContext context, Object target, Object name) 105 { 106 throw new UnsupportedCompilationException("Can't set listeners on ListenerMap."); 107 } 108 }