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  
23  import org.apache.myfaces.tobago.internal.util.StringUtils;
24  import org.apache.myfaces.tobago.util.ComponentUtils;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  import javax.el.ValueExpression;
29  import javax.faces.component.StateHolder;
30  import javax.faces.component.UIComponent;
31  import javax.faces.component.UIComponentBase;
32  import javax.faces.context.FacesContext;
33  import javax.faces.event.ActionEvent;
34  
35  public class ValueExpressionResetInputActionListener extends AbstractResetInputActionListener implements StateHolder {
36  
37    private static final Logger LOG = LoggerFactory.getLogger(ValueExpressionResetInputActionListener.class);
38  
39    private ValueExpression clientIdsExpression;
40  
41    /**
42     * No-arg constructor used during restoreState
43     */
44    public ValueExpressionResetInputActionListener() {
45    }
46  
47    public ValueExpressionResetInputActionListener(final ValueExpression clientIdsExpression) {
48      this.clientIdsExpression = clientIdsExpression;
49    }
50  
51    @Override
52    public void processAction(final ActionEvent event) {
53      final Object obj = clientIdsExpression.getValue(FacesContext.getCurrentInstance().getELContext());
54      final String [] clientIds;
55      if (obj instanceof String[]) {
56        clientIds = (String[]) obj;
57      } else if (obj instanceof String) {
58        clientIds= StringUtils.split((String) obj, ", ");
59      } else {
60        LOG.error("Ignore unknown value of " + obj + " for reset.");
61        return;
62      }
63      for (final String clientId : clientIds) {
64        final UIComponent component = ComponentUtils.findComponent(event.getComponent(), clientId);
65        if (component != null) {
66          resetChildren(component);
67        }
68      }
69    }
70  
71    @Override
72    public boolean isTransient() {
73      return false;
74    }
75  
76    @Override
77    public void restoreState(final FacesContext context, final Object state) {
78      final Object[] values = (Object[]) state;
79      clientIdsExpression = (ValueExpression) UIComponentBase.restoreAttachedState(context, values[0]);
80    }
81  
82    @Override
83    public Object saveState(final FacesContext context) {
84      final Object[] values = new Object[1];
85      values[0] = UIComponentBase.saveAttachedState(context, clientIdsExpression);
86      return values;
87    }
88  
89  
90    @Override
91    public void setTransient(final boolean newTransientValue) {
92      // ignore
93    }
94  
95  }