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 * Created by IntelliJ IDEA. 033 * User: bommel 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 private String forComponent; 045 046 /** 047 * The id of a Popup. 048 */ 049 @TagAttribute 050 public void setFor(String popupId) { 051 this.forComponent = popupId; 052 } 053 054 public int doStartTag() throws JspException { 055 056 // Locate our parent UIComponentTag 057 UIComponentTag tag = 058 UIComponentTag.getParentUIComponentTag(pageContext); 059 if (tag == null) { 060 // TODO Message resource i18n 061 throw new JspException("Not nested in faces tag"); 062 } 063 064 if (!tag.getCreated()) { 065 return (SKIP_BODY); 066 } 067 068 UIComponent component = tag.getComponentInstance(); 069 if (component == null) { 070 // TODO Message resource i18n 071 throw new JspException("Component Instance is null"); 072 } 073 if (!(component instanceof ActionSource)) { 074 // TODO Message resource i18n 075 throw new JspException("Component "+ component.getClass().getName() + " is not instanceof ActionSource"); 076 } 077 ActionSource actionSource = (ActionSource) component; 078 actionSource.addActionListener(new PopupActionListener(forComponent)); 079 return (SKIP_BODY); 080 } 081 082 /** 083 * <p>Release references to any acquired resources. 084 */ 085 public void release() { 086 this.forComponent = null; 087 } 088 }