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.TagGeneration;
26  import org.apache.myfaces.tobago.component.Attributes;
27  import org.apache.myfaces.tobago.event.PopupActionListener;
28  
29  import javax.el.ValueExpression;
30  import javax.faces.component.ActionSource;
31  import javax.faces.component.UIComponent;
32  import javax.faces.context.FacesContext;
33  import javax.faces.webapp.UIComponentClassicTagBase;
34  import javax.faces.webapp.UIComponentELTag;
35  import javax.servlet.jsp.JspException;
36  import javax.servlet.jsp.tagext.TagSupport;
37  
38  /**
39   * Register an PopupActionListener instance on the UIComponent
40   * associated with the closest parent UIComponent.
41   */
42  @Tag(name = "popupReference", bodyContent = BodyContent.EMPTY)
43  @TagGeneration(className = "org.apache.myfaces.tobago.internal.taglib.PopupReferenceTag")
44  public abstract class PopupReferenceTag extends TagSupport {
45  
46    private static final long serialVersionUID = 2L;
47  
48    private javax.el.ValueExpression forValue;
49  
50    public int doStartTag() throws JspException {
51  
52      // Locate our parent UIComponentTag
53      UIComponentClassicTagBase tag =
54          UIComponentELTag.getParentUIComponentClassicTagBase(pageContext);
55      if (tag == null) {
56        // TODO Message resource i18n
57        throw new JspException("Not nested in faces tag");
58      }
59  
60      if (!tag.getCreated()) {
61        return (SKIP_BODY);
62      }
63  
64      UIComponent component = tag.getComponentInstance();
65      if (component == null) {
66        // TODO Message resource i18n
67        throw new JspException("Component Instance is null");
68      }
69      if (!(component instanceof ActionSource)) {
70        // TODO Message resource i18n
71        throw new JspException("Component " + component.getClass().getName() + " is not instanceof ActionSource");
72      }
73      ActionSource actionSource = (ActionSource) component;
74  
75      component.setValueExpression(Attributes.FOR, forValue);
76  
77      if (forValue.isLiteralText()) {
78        actionSource.addActionListener(new PopupActionListener(
79            (String) forValue.getValue(FacesContext.getCurrentInstance().getELContext())));
80      } else {
81        component.setValueExpression(Attributes.FOR, forValue);
82      }
83      return (SKIP_BODY);
84    }
85  
86    /**
87     * The id of a Popup.
88     */
89    @TagAttribute(required = true, name = "for", type = "java.lang.String")
90    public void setFor(ValueExpression forValue) {
91      this.forValue = forValue;
92    }
93  
94  }