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.EditableValueHolder; 23 import javax.faces.component.UIComponent; 24 import javax.faces.event.ValueChangeListener; 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 43 public final class ValueChangeListenerHandler extends TagHandler { 44 45 private Class listenerType; 46 47 private final TagAttribute type; 48 49 private final TagAttribute binding; 50 51 public ValueChangeListenerHandler(TagConfig config) { 52 super(config); 53 this.binding = this.getAttribute("binding"); 54 this.type = this.getAttribute("type"); 55 if (type != null) { 56 try { 57 this.listenerType = Class.forName(type.getValue()); 58 } catch (Exception e) { 59 throw new TagAttributeException(this.tag, this.type, e); 60 } 61 } 62 } 63 64 70 public void apply(FaceletContext ctx, UIComponent parent) 71 throws IOException , FacesException, FaceletException, ELException { 72 if (parent instanceof EditableValueHolder) { 73 if (parent.getParent() == null) { 75 EditableValueHolder evh = (EditableValueHolder) parent; 76 ValueChangeListener listener = null; 77 ValueExpression ve = null; 78 if (this.binding != null) { 79 ve = this.binding.getValueExpression(ctx, 80 ValueChangeListener.class); 81 listener = (ValueChangeListener) ve.getValue(ctx); 82 } 83 if (listener == null && this.listenerType != null) { 84 try { 85 listener = (ValueChangeListener) listenerType 86 .newInstance(); 87 } catch (Exception e) { 88 throw new TagAttributeException(this.tag, this.type, e); 89 } 90 if (ve != null) { 91 ve.setValue(ctx, ve); 92 } 93 } else { 94 throw new TagAttributeException(this.tag, this.binding, 95 "Binding evaluated to null, and there wasn't a 'type' Attribute Specified"); 96 } 97 evh.addValueChangeListener(listener); 98 } 99 } else { 100 throw new TagException(this.tag, 101 "Parent is not of type EditableValueHolder, type is: " 102 + parent); 103 } 104 } 105 106 } 107 | Popular Tags |