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.el.ValueExpression; 024 import javax.faces.component.StateHolder; 025 import javax.faces.component.UIComponentBase; 026 import javax.faces.context.FacesContext; 027 028 public class ValueExpressionTabChangeListener implements TabChangeListener, StateHolder { 029 030 private static final Logger LOG = LoggerFactory.getLogger(ValueExpressionTabChangeListener.class); 031 032 private String type; 033 private ValueExpression valueExpression; 034 035 public ValueExpressionTabChangeListener() { 036 } 037 038 public ValueExpressionTabChangeListener(String type, ValueExpression valueExpression) { 039 this.type = type; 040 this.valueExpression = valueExpression; 041 } 042 043 public void processTabChange(TabChangeEvent tabChangeEvent) { 044 TabChangeListener handler = getTabChangeListener(); 045 if (handler != null) { 046 handler.processTabChange(tabChangeEvent); 047 } else { 048 LOG.error("Ignoring TabChangeEvent. No TabChangeListener found."); 049 } 050 } 051 052 public Object saveState(FacesContext context) { 053 Object[] state = new Object[2]; 054 state[0] = UIComponentBase.saveAttachedState(context, valueExpression); 055 state[1] = type; 056 return state; 057 } 058 059 public void restoreState(FacesContext context, Object state) { 060 Object[] values = (Object[]) state; 061 valueExpression = (ValueExpression) UIComponentBase.restoreAttachedState(context, values[0]); 062 type = (String) values[1]; 063 064 } 065 066 public boolean isTransient() { 067 return false; 068 } 069 070 public void setTransient(boolean newTransientValue) { 071 // ignore 072 } 073 074 private TabChangeListener getTabChangeListener() { 075 TabChangeListener handler = null; 076 if (valueExpression != null) { 077 Object obj = valueExpression.getValue(FacesContext.getCurrentInstance().getELContext()); 078 if (obj != null && obj instanceof TabChangeListener) { 079 handler = (TabChangeListener) obj; 080 } 081 } 082 if (handler == null && type != null) { 083 handler = createTabChangeListener(type); 084 if (handler != null && valueExpression != null) { 085 valueExpression.setValue(FacesContext.getCurrentInstance().getELContext(), handler); 086 } 087 } 088 return handler; 089 } 090 091 private TabChangeListener createTabChangeListener(String className) { 092 try { 093 Class clazz = getClass().getClassLoader().loadClass(className); 094 return ((TabChangeListener) clazz.newInstance()); 095 } catch (Exception e) { 096 LOG.error("", e); 097 } 098 return null; 099 } 100 }