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.el.ValueExpression;
26  import javax.faces.component.StateHolder;
27  import javax.faces.component.UIComponentBase;
28  import javax.faces.context.FacesContext;
29  
30  public class ValueExpressionSheetStateChangeListener implements SheetStateChangeListener, StateHolder {
31  
32    private static final Logger LOG = LoggerFactory.getLogger(ValueExpressionSheetStateChangeListener.class);
33  
34    private String type;
35    private ValueExpression valueExpression;
36  
37    public ValueExpressionSheetStateChangeListener() {
38    }
39  
40    public ValueExpressionSheetStateChangeListener(final String type, final ValueExpression valueExpression) {
41      this.type = type;
42      this.valueExpression = valueExpression;
43    }
44  
45    @Override
46    public void processSheetStateChange(final SheetStateChangeEvent sheetStateChangeEvent) {
47      final SheetStateChangeListener handler = getSheetStateChangeListener();
48      if (handler != null) {
49        handler.processSheetStateChange(sheetStateChangeEvent);
50      } else {
51        LOG.error("Ignoring SheetStateChangeEvent. No SheetStateChangeListener found.");
52      }
53    }
54  
55    @Override
56    public Object saveState(final FacesContext context) {
57      final Object[] state = new Object[2];
58      state[0] = UIComponentBase.saveAttachedState(context, valueExpression);
59      state[1] = type;
60      return state;
61    }
62  
63    @Override
64    public void restoreState(final FacesContext context, final Object state) {
65      final Object[] values = (Object[]) state;
66      valueExpression = (ValueExpression) UIComponentBase.restoreAttachedState(context, values[0]);
67      type = (String) values[1];
68  
69    }
70  
71    @Override
72    public boolean isTransient() {
73      return false;
74    }
75  
76    @Override
77    public void setTransient(final boolean newTransientValue) {
78      // ignore
79    }
80  
81    private SheetStateChangeListener getSheetStateChangeListener() {
82      SheetStateChangeListener handler = null;
83      if (valueExpression != null) {
84        final Object obj = valueExpression.getValue(FacesContext.getCurrentInstance().getELContext());
85        if (obj != null && obj instanceof SheetStateChangeListener) {
86          handler = (SheetStateChangeListener) obj;
87        }
88      }
89      if (handler == null && type != null) {
90        handler = createSheetStateChangeListener(type);
91        if (handler != null && valueExpression != null) {
92          valueExpression.setValue(FacesContext.getCurrentInstance().getELContext(), handler);
93        }
94      }
95      return handler;
96    }
97  
98    private SheetStateChangeListener createSheetStateChangeListener(final String className) {
99      try {
100       final Class clazz = getClass().getClassLoader().loadClass(className);
101       return ((SheetStateChangeListener) clazz.newInstance());
102     } catch (final Exception e) {
103       LOG.error("", e);
104     }
105     return null;
106   }
107 }