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.slf4j.Logger; 021 import org.slf4j.LoggerFactory; 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 033 @SuppressWarnings("deprecation") 034 public class ValueBindingTabChangeListener implements TabChangeListener, StateHolder { 035 private static final Logger LOG = LoggerFactory.getLogger(ValueBindingTabChangeListener.class); 036 private String type; 037 private ValueBinding valueBinding; 038 039 public ValueBindingTabChangeListener() { 040 } 041 042 public ValueBindingTabChangeListener(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 }