1 33 34 package com.icesoft.faces.facelets; 35 36 import com.sun.facelets.FaceletContext; 37 import com.sun.facelets.tag.TagAttribute; 38 import com.sun.facelets.tag.TagAttributeException; 39 import com.sun.facelets.tag.TagConfig; 40 import com.sun.facelets.tag.TagException; 41 import com.sun.facelets.tag.TagHandler; 42 import com.sun.facelets.tag.jsf.ComponentSupport; 43 44 import javax.el.ELException; 45 import javax.faces.FacesException; 46 import javax.faces.component.UIComponent; 47 import java.io.IOException ; 48 import java.lang.reflect.Method ; 49 50 54 public class TabChangeListenerHandler extends TagHandler { 55 private static Class panelTabSetClass; 56 private static Class tabChangeListenerClass; 57 private static Method addTabChangeListenerMethod; 58 private static boolean triedGettingICEfacesComponentClasses; 59 60 private static synchronized boolean isICEfacesComponentClassesPresent() { 61 if( !triedGettingICEfacesComponentClasses ) { 62 try { 63 panelTabSetClass = Class.forName( 64 "com.icesoft.faces.component.paneltabset.PanelTabSet"); 65 tabChangeListenerClass = Class.forName( 66 "com.icesoft.faces.component.paneltabset.TabChangeListener"); 67 addTabChangeListenerMethod = panelTabSetClass.getMethod( 68 "addTabChangeListener", new Class []{tabChangeListenerClass}); 69 } 70 catch (Exception e) { 71 } 72 triedGettingICEfacesComponentClasses = true; 73 } 74 return (panelTabSetClass != null && 75 tabChangeListenerClass != null && 76 addTabChangeListenerMethod != null); 77 } 78 79 80 private final TagAttribute typeTagAttribute; 81 82 85 public TabChangeListenerHandler(TagConfig config) { 86 super(config); 87 88 typeTagAttribute = getRequiredAttribute("type"); 89 } 90 91 95 public void apply(FaceletContext ctx, UIComponent parent) 96 throws IOException , FacesException, ELException { 97 if( !isICEfacesComponentClassesPresent() ) { 98 throw new TagException( 99 tag, 100 "ICEfaces components classes not found, can not use this tag"); 101 } 102 if( parent == null ) { 103 throw new TagException(tag, "Parent UIComponent was null"); 104 } 105 if( !panelTabSetClass.isAssignableFrom(parent.getClass()) ) { 106 throw new TagException( 107 tag, 108 "Parent UIComponent must be a "+ 109 "com.icesoft.faces.component.paneltabset.PanelTabSet"); 110 } 111 if( !typeTagAttribute.isLiteral() ) { 112 throw new TagAttributeException( 113 tag, typeTagAttribute, 114 "The class, as given by tabChangeListener tag's type " + 115 "attribute, must be literal, and not a value expression: " + 116 typeTagAttribute.getValue()); 117 } 118 String listenerClassName = typeTagAttribute.getValue(ctx); 119 try { 120 Class listenerClass = Class.forName( listenerClassName ); 121 if( !tabChangeListenerClass.isAssignableFrom(listenerClass) ) { 122 throw new TagAttributeException( 123 tag, typeTagAttribute, 124 "The class, as given by tabChangeListener tag's type " + 125 "attribute, must implement TabChangeListener: " + 126 listenerClassName); 127 } 128 129 if( ComponentSupport.isNew(parent) ) { 130 Object listenerObject = listenerClass.newInstance(); 132 addTabChangeListenerMethod.invoke( 133 parent, new Object []{listenerObject} ); 134 } 135 } 136 catch(Exception e) { 137 throw new TagAttributeException( 138 tag, typeTagAttribute, 139 "Could not either find, or instantiate, or add as a " + 140 "listener, the class described by tabChangeListener " + 141 "tag's type attribute: " + listenerClassName, e); 142 } 143 144 } 146 } 147 | Popular Tags |