001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *   http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    package org.apache.myfaces.tobago.taglib.component;
021    
022    import org.apache.myfaces.tobago.apt.annotation.Tag;
023    import org.apache.myfaces.tobago.apt.annotation.BodyContent;
024    import org.apache.myfaces.tobago.apt.annotation.TagAttribute;
025    import org.apache.myfaces.tobago.event.PopupActionListener;
026    
027    import javax.servlet.jsp.tagext.TagSupport;
028    import javax.servlet.jsp.JspException;
029    import javax.faces.webapp.UIComponentTag;
030    import javax.faces.component.UIComponent;
031    import javax.faces.component.ActionSource;
032    
033    /*
034     * Date: Jan 3, 2007
035     * Time: 10:42:11 PM
036     */
037    
038    /**
039     * Register an PopupActionListener instance on the UIComponent
040     * associated with the closest parent UIComponent.
041     */
042    @Tag(name = "popupReference", bodyContent = BodyContent.EMPTY)
043    public class PopupReferenceTag extends TagSupport {
044    
045      private static final long serialVersionUID = -8444689365088370011L;
046    
047      private String forComponent;
048    
049      /**
050       * The id of a Popup.
051       */
052      @TagAttribute
053      public void setFor(String popupId) {
054        this.forComponent = popupId;
055      }
056    
057      public int doStartTag() throws JspException {
058    
059        // Locate our parent UIComponentTag
060        UIComponentTag tag =
061            UIComponentTag.getParentUIComponentTag(pageContext);
062        if (tag == null) {
063          // TODO Message resource i18n
064          throw new JspException("Not nested in faces tag");
065        }
066    
067        if (!tag.getCreated()) {
068          return (SKIP_BODY);
069        }
070    
071        UIComponent component = tag.getComponentInstance();
072        if (component == null) {
073          // TODO Message resource i18n
074          throw new JspException("Component Instance is null");
075        }
076        if (!(component instanceof ActionSource)) {
077          // TODO Message resource i18n
078          throw new JspException("Component " + component.getClass().getName() + " is not instanceof ActionSource");
079        }
080        ActionSource actionSource = (ActionSource) component;
081        actionSource.addActionListener(new PopupActionListener(forComponent));
082        return (SKIP_BODY);
083      }
084    
085      /**
086       * <p>Release references to any acquired resources.
087       */
088      public void release() {
089        this.forComponent = null;
090      }
091    }