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    public void processSheetStateChange(final SheetStateChangeEvent sheetStateChangeEvent) {
46      final SheetStateChangeListener handler = getSheetStateChangeListener();
47      if (handler != null) {
48        handler.processSheetStateChange(sheetStateChangeEvent);
49      } else {
50        LOG.error("Ignoring SheetStateChangeEvent. No SheetStateChangeListener found.");
51      }
52    }
53  
54    public Object saveState(final FacesContext context) {
55      final Object[] state = new Object[2];
56      state[0] = UIComponentBase.saveAttachedState(context, valueExpression);
57      state[1] = type;
58      return state;
59    }
60  
61    public void restoreState(final FacesContext context, final Object state) {
62      final Object[] values = (Object[]) state;
63      valueExpression = (ValueExpression) UIComponentBase.restoreAttachedState(context, values[0]);
64      type = (String) values[1];
65  
66    }
67  
68    public boolean isTransient() {
69      return false;
70    }
71  
72    public void setTransient(final boolean newTransientValue) {
73      // ignore
74    }
75  
76    private SheetStateChangeListener getSheetStateChangeListener() {
77      SheetStateChangeListener handler = null;
78      if (valueExpression != null) {
79        final Object obj = valueExpression.getValue(FacesContext.getCurrentInstance().getELContext());
80        if (obj != null && obj instanceof SheetStateChangeListener) {
81          handler = (SheetStateChangeListener) obj;
82        }
83      }
84      if (handler == null && type != null) {
85        handler = createSheetStateChangeListener(type);
86        if (handler != null && valueExpression != null) {
87          valueExpression.setValue(FacesContext.getCurrentInstance().getELContext(), handler);
88        }
89      }
90      return handler;
91    }
92  
93    private SheetStateChangeListener createSheetStateChangeListener(final String className) {
94      try {
95        final Class clazz = getClass().getClassLoader().loadClass(className);
96        return ((SheetStateChangeListener) clazz.newInstance());
97      } catch (final Exception e) {
98        LOG.error("", e);
99      }
100     return null;
101   }
102 }