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