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.SimpleTag;
24  import org.apache.myfaces.tobago.apt.annotation.Tag;
25  import org.apache.myfaces.tobago.apt.annotation.TagAttribute;
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  @SimpleTag(
44      faceletHandler = "org.apache.myfaces.tobago.facelets.PopupReferenceHandler")
45  public abstract class PopupReferenceTag extends TagSupport {
46  
47    private static final long serialVersionUID = 2L;
48  
49    private javax.el.ValueExpression forValue;
50  
51    public int doStartTag() throws JspException {
52  
53      // Locate our parent UIComponentTag
54      final UIComponentClassicTagBase tag =
55          UIComponentELTag.getParentUIComponentClassicTagBase(pageContext);
56      if (tag == null) {
57        // TODO Message resource i18n
58        throw new JspException("Not nested in faces tag");
59      }
60  
61      if (!tag.getCreated()) {
62        return (SKIP_BODY);
63      }
64  
65      final UIComponent component = tag.getComponentInstance();
66      if (component == null) {
67        // TODO Message resource i18n
68        throw new JspException("Component Instance is null");
69      }
70      if (!(component instanceof ActionSource)) {
71        // TODO Message resource i18n
72        throw new JspException("Component " + component.getClass().getName() + " is not instanceof ActionSource");
73      }
74      final ActionSource actionSource = (ActionSource) component;
75  
76      component.setValueExpression(Attributes.FOR, forValue);
77  
78      if (forValue.isLiteralText()) {
79        actionSource.addActionListener(new PopupActionListener(
80            (String) forValue.getValue(FacesContext.getCurrentInstance().getELContext())));
81      } else {
82        component.setValueExpression(Attributes.FOR, forValue);
83      }
84      return (SKIP_BODY);
85    }
86  
87    /**
88     * The id of a Popup.
89     */
90    @TagAttribute(required = true, name = "for", type = "java.lang.String")
91    public void setFor(final ValueExpression forValue) {
92      this.forValue = forValue;
93    }
94  
95  }