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.internal.taglib.component;
21  
22  import org.apache.myfaces.tobago.apt.annotation.SimpleTag;
23  import org.apache.myfaces.tobago.apt.annotation.Tag;
24  import org.apache.myfaces.tobago.apt.annotation.TagAttribute;
25  import org.apache.myfaces.tobago.component.Attributes;
26  import org.apache.myfaces.tobago.event.ResetFormActionListener;
27  import org.apache.myfaces.tobago.event.ResetInputActionListener;
28  import org.apache.myfaces.tobago.event.ValueExpressionResetInputActionListener;
29  import org.apache.myfaces.tobago.util.ComponentUtils;
30  
31  import javax.el.ELContext;
32  import javax.el.ValueExpression;
33  import javax.faces.component.ActionSource;
34  import javax.faces.component.UIComponent;
35  import javax.faces.context.FacesContext;
36  import javax.faces.webapp.UIComponentClassicTagBase;
37  import javax.faces.webapp.UIComponentELTag;
38  import javax.servlet.jsp.JspException;
39  import javax.servlet.jsp.tagext.TagSupport;
40  
41  /**
42   * A ResetInputActionListener is a declarative way to allow an action source to reset all EditableValueHolder
43   * of a page or in a sub-form or part of the component tree.
44   */
45  @Tag(name = "resetInputActionListener")
46  @SimpleTag(
47      faceletHandler = "org.apache.myfaces.tobago.facelets.ResetInputActionListenerHandler")
48  public abstract class ResetInputActionListenerTag extends TagSupport {
49  
50    private static final long serialVersionUID = 2L;
51  
52    private ValueExpression execute;
53  
54    public int doStartTag() throws JspException {
55  
56      // Locate our parent UIComponentTag
57      final UIComponentClassicTagBase tag =
58          UIComponentELTag.getParentUIComponentClassicTagBase(pageContext);
59      if (tag == null) {
60        // TODO Message resource i18n
61        throw new JspException("Not nested in faces tag");
62      }
63  
64      if (!tag.getCreated()) {
65        return (SKIP_BODY);
66      }
67  
68      final UIComponent component = tag.getComponentInstance();
69      if (component == null) {
70        // TODO Message resource i18n
71        throw new JspException("Component Instance is null");
72      }
73      if (!(component instanceof ActionSource)) {
74        // TODO Message resource i18n
75        throw new JspException("Component " + component.getClass().getName() + " is not instanceof ActionSource");
76      }
77  
78      final ELContext elContext = FacesContext.getCurrentInstance().getELContext();
79  
80      final ActionSource actionSource = (ActionSource) component;
81      if (execute == null) {
82        actionSource.addActionListener(new ResetFormActionListener());
83      } else if (execute.isLiteralText()) {
84        actionSource.addActionListener(new ResetInputActionListener(
85            ComponentUtils.splitList((String) execute.getValue(elContext))));
86      } else {
87        actionSource.addActionListener(new ValueExpressionResetInputActionListener(execute));
88      }
89      return (SKIP_BODY);
90    }
91  
92    @Override
93    public void release() {
94      super.release();
95      execute = null;
96    }
97  
98    /**
99     * A list of ids of components. For each id, the surrounding (virtual) UIForm will be searched, and for each of
100    * them, all containing EditableValueHolder will be reset.
101    */
102   @TagAttribute(required = false, name = Attributes.EXECUTE, type = "java.lang.String")
103   public void setExecute(final javax.el.ValueExpression execute) {
104     this.execute = execute;
105   }
106 
107 }