001    package org.apache.myfaces.tobago.event;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    import org.apache.commons.logging.Log;
021    import org.apache.commons.logging.LogFactory;
022    
023    import javax.faces.component.StateHolder;
024    import javax.faces.component.UIComponentBase;
025    import javax.faces.context.FacesContext;
026    import javax.faces.el.ValueBinding;
027    
028    /*
029     * Date: Jan 9, 2007
030     * Time: 8:08:01 PM
031     */
032    public class TabChangeListenerValueBindingDelegate implements TabChangeListener, StateHolder {
033      private static final Log LOG = LogFactory.getLog(TabChangeListenerValueBindingDelegate.class);
034      private String type;
035      private ValueBinding valueBinding;
036    
037      public TabChangeListenerValueBindingDelegate() {
038      }
039    
040      public TabChangeListenerValueBindingDelegate(String type, ValueBinding valueBinding) {
041        this.type = type;
042        this.valueBinding = valueBinding;
043      }
044    
045      public void processTabChange(TabChangeEvent tabChangeEvent) {
046        TabChangeListener handler = getTabChangeListener();
047        if (handler != null) {
048          handler.processTabChange(tabChangeEvent);
049        } else {
050          LOG.error("Ignoring TabChangeEvent. No TabChangeListener found.");
051        }
052      }
053    
054      public Object saveState(FacesContext context) {
055        Object[] state = new Object[2];
056        state[0] = UIComponentBase.saveAttachedState(context, valueBinding);
057        state[1] = type;
058        return state;
059      }
060    
061      public void restoreState(FacesContext context, Object state) {
062        Object[] values = (Object[]) state;
063        valueBinding = (ValueBinding) UIComponentBase.restoreAttachedState(context, values[0]);
064        type = (String) values[1];
065    
066      }
067    
068      public boolean isTransient() {
069        return false;
070      }
071    
072      public void setTransient(boolean newTransientValue) {
073        // ignore
074      }
075    
076      private TabChangeListener getTabChangeListener() {
077        TabChangeListener handler = null;
078        if (valueBinding != null) {
079          Object obj = valueBinding.getValue(FacesContext.getCurrentInstance());
080          if (obj != null && obj instanceof TabChangeListener) {
081            handler = (TabChangeListener) obj;
082          }
083        }
084        if (handler == null && type != null) {
085          handler = createTabChangeListener(type);
086          if (handler != null && valueBinding != null) {
087            valueBinding.setValue(FacesContext.getCurrentInstance(), handler);
088          }
089        }
090        return handler;
091      }
092    
093      private TabChangeListener createTabChangeListener(String className) {
094        try {
095          Class clazz = getClass().getClassLoader().loadClass(className);
096          return ((TabChangeListener) clazz.newInstance());
097        } catch (Exception e) {
098          LOG.error("", e);
099        }
100        return null;
101      }
102    
103    }