1 16 package org.apache.cocoon.faces.taglib; 17 18 import org.apache.cocoon.taglib.TagSupport; 19 20 import org.apache.cocoon.faces.FacesUtils; 21 import org.xml.sax.Attributes ; 22 import org.xml.sax.SAXException ; 23 24 import javax.faces.component.ActionSource; 25 import javax.faces.component.UIComponent; 26 import javax.faces.event.ActionListener; 27 28 31 public class ActionListenerTag extends TagSupport { 32 33 private String type; 34 35 public String getType() { 36 return this.type; 37 } 38 39 public void setType(String type) { 40 this.type = type; 41 } 42 43 public int doStartTag(String namespaceURI, String localName, String qName, Attributes atts) 44 throws SAXException { 45 UIComponentTag tag = FacesUtils.findParentUIComponentTag(this); 46 if (tag == null) { 47 throw new SAXException ("Tag <" + getClass().getName() + "> have to be nested within a UIComponentTag"); 48 } 49 50 if (!tag.getCreated()) { 51 return 0; 52 } 53 54 UIComponent component = tag.getComponentInstance(); 55 if (component == null) { 56 throw new SAXException ("Parent tag <" + tag.getClass().getName() + "> has no component instance"); 57 } 58 59 if (component instanceof ActionSource) { 60 String clazz = (String ) tag.createValueBinding(this.type).getValue(tag.getFacesContext()); 61 ActionListener handler = null; 62 try { 63 handler = (ActionListener) Class.forName(clazz).newInstance(); 64 } catch (Exception e) { 65 throw new SAXException ("Tag <" + tag.getClass().getName() + "> could not create action listener <" + clazz + ">", e); 66 } 67 ((ActionSource)component).addActionListener(handler); 68 } 69 70 return SKIP_BODY; 71 } 72 73 public void recycle() { 74 super.recycle(); 75 this.type = null; 76 } 77 } 78 | Popular Tags |