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