1 14 15 package com.sun.facelets.tag.jsf.core; 16 17 import java.io.IOException ; 18 19 import javax.el.ELException; 20 import javax.el.ValueExpression; 21 import javax.faces.FacesException; 22 import javax.faces.component.ActionSource; 23 import javax.faces.component.UIComponent; 24 import javax.faces.event.ActionListener; 25 26 import com.sun.facelets.FaceletContext; 27 import com.sun.facelets.FaceletException; 28 import com.sun.facelets.tag.TagAttribute; 29 import com.sun.facelets.tag.TagAttributeException; 30 import com.sun.facelets.tag.TagConfig; 31 import com.sun.facelets.tag.TagException; 32 import com.sun.facelets.tag.TagHandler; 33 34 45 public final class ActionListenerHandler extends TagHandler { 46 47 private Class listenerType; 48 49 private final TagAttribute type; 50 51 private final TagAttribute binding; 52 53 56 public ActionListenerHandler(TagConfig config) { 57 super(config); 58 this.binding = this.getAttribute("binding"); 59 this.type = this.getAttribute("type"); 60 if (type != null) { 61 if (!type.isLiteral()) { 62 throw new TagAttributeException(this.tag, this.type, "Must be literal"); 63 } 64 try { 65 this.listenerType = Class.forName(type.getValue()); 66 } catch (Exception e) { 67 throw new TagAttributeException(this.tag, this.type, e); 68 } 69 } 70 } 71 72 78 public void apply(FaceletContext ctx, UIComponent parent) 79 throws IOException , FacesException, FaceletException, ELException { 80 if (parent instanceof ActionSource) { 81 if (parent.getParent() == null) { 83 ActionSource src = (ActionSource) parent; 84 ActionListener listener = null; 85 ValueExpression ve = null; 86 if (this.binding != null) { 87 ve = this.binding.getValueExpression(ctx, 88 ActionListener.class); 89 listener = (ActionListener) ve.getValue(ctx); 90 } 91 if (listener == null) { 92 try { 93 listener = (ActionListener) listenerType.newInstance(); 94 } catch (Exception e) { 95 throw new TagAttributeException(this.tag, this.type, e.getCause()); 96 } 97 if (ve != null) { 98 ve.setValue(ctx, ve); 99 } 100 } 101 src.addActionListener(listener); 102 } 103 } else { 104 throw new TagException(this.tag, 105 "Parent is not of type ActionSource, type is: " + parent); 106 } 107 } 108 } 109 | Popular Tags |