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