View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.event;
21  
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  import javax.faces.component.StateHolder;
26  import javax.faces.component.UIComponentBase;
27  import javax.faces.context.FacesContext;
28  import javax.faces.el.ValueBinding;
29  
30  /**
31   * @deprecated Since 2.0.0, please use ValueExpressionTabChangeListener
32   */
33  @Deprecated
34  public class ValueBindingTabChangeListener implements TabChangeListener, StateHolder {
35    private static final Logger LOG = LoggerFactory.getLogger(ValueBindingTabChangeListener.class);
36    private String type;
37    private ValueBinding valueBinding;
38  
39    public ValueBindingTabChangeListener() {
40    }
41  
42    public ValueBindingTabChangeListener(final String type, final ValueBinding valueBinding) {
43      this.type = type;
44      this.valueBinding = valueBinding;
45    }
46  
47    public void processTabChange(final TabChangeEvent tabChangeEvent) {
48      final TabChangeListener handler = getTabChangeListener();
49      if (handler != null) {
50        handler.processTabChange(tabChangeEvent);
51      } else {
52        LOG.error("Ignoring TabChangeEvent. No TabChangeListener found.");
53      }
54    }
55  
56    public Object saveState(final FacesContext context) {
57      final Object[] state = new Object[2];
58      state[0] = UIComponentBase.saveAttachedState(context, valueBinding);
59      state[1] = type;
60      return state;
61    }
62  
63    public void restoreState(final FacesContext context, final Object state) {
64      final Object[] values = (Object[]) state;
65      valueBinding = (ValueBinding) UIComponentBase.restoreAttachedState(context, values[0]);
66      type = (String) values[1];
67  
68    }
69  
70    public boolean isTransient() {
71      return false;
72    }
73  
74    public void setTransient(final boolean newTransientValue) {
75      // ignore
76    }
77  
78    private TabChangeListener getTabChangeListener() {
79      TabChangeListener handler = null;
80      if (valueBinding != null) {
81        final Object obj = valueBinding.getValue(FacesContext.getCurrentInstance());
82        if (obj != null && obj instanceof TabChangeListener) {
83          handler = (TabChangeListener) obj;
84        }
85      }
86      if (handler == null && type != null) {
87        handler = createTabChangeListener(type);
88        if (handler != null && valueBinding != null) {
89          valueBinding.setValue(FacesContext.getCurrentInstance(), handler);
90        }
91      }
92      return handler;
93    }
94  
95    private TabChangeListener createTabChangeListener(final String className) {
96      try {
97        final Class clazz = getClass().getClassLoader().loadClass(className);
98        return ((TabChangeListener) clazz.newInstance());
99      } catch (final Exception e) {
100       LOG.error("", e);
101     }
102     return null;
103   }
104 
105 }