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.FindComponentUtils;
24  import org.apache.myfaces.tobago.internal.util.StringUtils;
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    public void processAction(final ActionEvent event) {
52      final Object obj = clientIdsExpression.getValue(FacesContext.getCurrentInstance().getELContext());
53      final String [] clientIds;
54      if (obj instanceof String[]) {
55        clientIds = (String[]) obj;
56      } else if (obj instanceof String) {
57        clientIds= StringUtils.split((String) obj, ", ");
58      } else {
59        LOG.error("Ignore unknown value of " + obj + " for reset.");
60        return;
61      }
62      for (final String clientId : clientIds) {
63        final UIComponent component = FindComponentUtils.findComponent(event.getComponent(), clientId);
64        if (component != null) {
65          resetChildren(component);
66        }
67      }
68    }
69  
70    public boolean isTransient() {
71      return false;
72    }
73  
74    public void restoreState(final FacesContext context, final Object state) {
75      final Object[] values = (Object[]) state;
76      clientIdsExpression = (ValueExpression) UIComponentBase.restoreAttachedState(context, values[0]);
77    }
78  
79    public Object saveState(final FacesContext context) {
80      final Object[] values = new Object[1];
81      values[0] = UIComponentBase.saveAttachedState(context, clientIdsExpression);
82      return values;
83    }
84  
85  
86    public void setTransient(final boolean newTransientValue) {
87      // ignore
88    }
89  
90  }