001    // Copyright 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.enhance;
016    
017    import org.apache.hivemind.ErrorLog;
018    import org.apache.hivemind.util.Defense;
019    import org.apache.tapestry.internal.event.IComponentEventInvoker;
020    import org.apache.tapestry.spec.IComponentSpecification;
021    
022    /**
023     * Injects the component's event invoker as the
024     * {@link org.apache.tapestry.IComponent#getEventInvoker() eventInvoker}property.
025     * 
026     * @author jkuhnert
027     * @since 4.1
028     */
029    public class InjectEventInvokerWorker implements EnhancementWorker
030    {
031    
032        public static final String PROPERTY_NAME = "eventInvoker";
033    
034        private ErrorLog _errorLog;
035    
036        private IComponentEventInvoker _invoker;
037        
038        public void performEnhancement(EnhancementOperation op, IComponentSpecification spec)
039        {
040            try
041            {
042                injectEventInvoker(op, spec);
043            }
044            catch (Exception ex)
045            {
046                _errorLog.error(EnhanceMessages.errorAddingProperty(
047                        PROPERTY_NAME, op.getBaseClass(), ex), spec
048                        .getLocation(), ex);
049            }
050        }
051    
052        public void injectEventInvoker(EnhancementOperation op, IComponentSpecification spec)
053        {
054            Defense.notNull(op, "op");
055            Defense.notNull(spec, "spec");
056            
057            op.claimReadonlyProperty(PROPERTY_NAME);
058            
059            String fieldName = op.addInjectedField("_$"
060                    + PROPERTY_NAME, IComponentEventInvoker.class,
061                    _invoker);
062            
063            EnhanceUtils.createSimpleAccessor(op, fieldName,
064                    PROPERTY_NAME, IComponentEventInvoker.class,
065                    spec.getLocation());
066        }
067    
068        public void setErrorLog(ErrorLog errorLog)
069        {
070            _errorLog = errorLog;
071        }
072        
073        public void setComponentEventInvoker(IComponentEventInvoker invoker)
074        {
075            _invoker = invoker;
076        }
077    }