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