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.facelets;
21  
22  import org.apache.myfaces.tobago.component.Attributes;
23  import org.apache.myfaces.tobago.event.ResetFormActionListener;
24  import org.apache.myfaces.tobago.event.ResetInputActionListener;
25  import org.apache.myfaces.tobago.event.ValueExpressionResetInputActionListener;
26  import org.apache.myfaces.tobago.util.ComponentUtils;
27  
28  import javax.el.ELException;
29  import javax.el.ValueExpression;
30  import javax.faces.FacesException;
31  import javax.faces.component.ActionSource;
32  import javax.faces.component.UIComponent;
33  import javax.faces.view.facelets.ComponentHandler;
34  import javax.faces.view.facelets.FaceletContext;
35  import javax.faces.view.facelets.TagAttribute;
36  import javax.faces.view.facelets.TagConfig;
37  import javax.faces.view.facelets.TagException;
38  import javax.faces.view.facelets.TagHandler;
39  import java.io.IOException;
40  
41  public class ResetInputActionListenerHandler extends TagHandler {
42  
43    private final TagAttribute execute;
44  
45    public ResetInputActionListenerHandler(TagConfig config) {
46      super(config);
47      execute = getAttribute(Attributes.EXECUTE);
48    }
49  
50    public void apply(FaceletContext faceletContext, UIComponent parent)
51        throws IOException, FacesException, ELException {
52      if (parent instanceof ActionSource) {
53        if (ComponentHandler.isNew(parent)) {
54          ActionSource actionSource = (ActionSource) parent;
55          if (execute == null) {
56            actionSource.addActionListener(new ResetFormActionListener());
57          } else if (execute.isLiteral())  {
58            actionSource.addActionListener(new ResetInputActionListener(ComponentUtils.splitList(execute.getValue())));
59          } else {
60            ValueExpression forValueExpression = execute.getValueExpression(faceletContext, String.class);
61            actionSource.addActionListener(new ValueExpressionResetInputActionListener(forValueExpression));
62          }
63        }
64      } else {
65        throw new TagException(tag, "Parent is not of type ActionSource, type is: " + parent);
66      }
67    }
68  }
69  
70