1 16 package org.apache.myfaces.taglib.core; 17 18 import org.apache.myfaces.util.ClassUtils; 19 20 import javax.faces.component.ActionSource; 21 import javax.faces.component.UIComponent; 22 import javax.faces.context.FacesContext; 23 import javax.faces.el.ValueBinding; 24 import javax.faces.event.ActionListener; 25 import javax.faces.webapp.UIComponentTag; 26 import javax.servlet.jsp.JspException ; 27 import javax.servlet.jsp.tagext.Tag ; 28 import javax.servlet.jsp.tagext.TagSupport ; 29 30 34 public class ActionListenerTag 35 extends TagSupport 36 { 37 39 private String _type = null; 40 41 public ActionListenerTag() 42 { 43 } 44 45 public void setType(String type) 46 { 47 _type = type; 48 } 49 50 51 public int doStartTag() throws JspException 52 { 53 if (_type == null) 54 { 55 throw new JspException ("type attribute not set"); 56 } 57 58 UIComponentTag componentTag = UIComponentTag.getParentUIComponentTag(pageContext); 60 if (componentTag == null) 61 { 62 throw new JspException ("ActionListenerTag has no UIComponentTag ancestor"); 63 } 64 65 if (componentTag.getCreated()) 66 { 67 UIComponent component = componentTag.getComponentInstance(); 69 if (component instanceof ActionSource) 70 { 71 String className; 72 if (UIComponentTag.isValueReference(_type)) 73 { 74 FacesContext facesContext = FacesContext.getCurrentInstance(); 75 ValueBinding vb = facesContext.getApplication().createValueBinding(_type); 76 className = (String )vb.getValue(facesContext); 77 } 78 else 79 { 80 className = _type; 81 } 82 ActionListener al = (ActionListener)ClassUtils.newInstance(className); 83 ((ActionSource)component).addActionListener(al); 84 } 85 else 86 { 87 throw new JspException ("Component " + component.getId() + " is no ActionSource"); 88 } 89 } 90 91 return Tag.SKIP_BODY; 92 } 93 } 94 | Popular Tags |