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.apache.myfaces.tobago.internal.component.AbstractUIPopup;
23  import org.apache.myfaces.tobago.internal.util.FindComponentUtils;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  import javax.el.ValueExpression;
28  import javax.faces.component.StateHolder;
29  import javax.faces.component.UIComponent;
30  import javax.faces.component.UIComponentBase;
31  import javax.faces.context.FacesContext;
32  import javax.faces.event.ActionEvent;
33  
34  
35  public class ValueExpressionPopupActionListener extends AbstractPopupActionListener implements StateHolder {
36  
37    private static final Logger LOG = LoggerFactory.getLogger(ValueExpressionPopupActionListener.class);
38  
39    private ValueExpression popupIdExpression;
40  
41    public ValueExpressionPopupActionListener(ValueExpression expression) {
42      popupIdExpression = expression;
43    }
44  
45    /**
46     * @deprecated Since 2.0.0, please use the other constructor with explicit type
47     */
48    @Deprecated
49    public ValueExpressionPopupActionListener(Object expression) {
50      popupIdExpression = (ValueExpression) expression;
51    }
52  
53    /**
54     * No-arg constructor used during restoreState
55     */
56    public ValueExpressionPopupActionListener() {
57    }
58  
59    @Override
60    protected AbstractUIPopup getPopup(ActionEvent actionEvent) {
61      String id = (String) popupIdExpression.getValue(FacesContext.getCurrentInstance().getELContext());
62      UIComponent popup = FindComponentUtils.findComponent(actionEvent.getComponent(), id);
63      if (popup instanceof AbstractUIPopup) {
64        return (AbstractUIPopup) popup;
65      } else {
66        LOG.error("Found no popup for \""
67            + popupIdExpression.getExpressionString() + "\" := \""
68            + id + "\"! Search base componentId : "
69            + actionEvent.getComponent().getClientId(FacesContext.getCurrentInstance()));
70        return null;
71      }
72    }
73  
74    public boolean isTransient() {
75      return false;
76    }
77  
78    public void restoreState(FacesContext context, Object state) {
79      Object[] values = (Object[]) state;
80      popupIdExpression = (ValueExpression) UIComponentBase.restoreAttachedState(context, values[0]);
81    }
82  
83    public Object saveState(FacesContext context) {
84      Object[] values = new Object[1];
85      values[0] = UIComponentBase.saveAttachedState(context, popupIdExpression);
86      return values;
87    }
88  
89  
90    public void setTransient(boolean newTransientValue) {
91      // ignore
92    }
93  }