001: /*
002: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003: *
004: * "The contents of this file are subject to the Mozilla Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
011: * License for the specific language governing rights and limitations under
012: * the License.
013: *
014: * The Original Code is ICEfaces 1.5 open source software code, released
015: * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
016: * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
017: * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
018: *
019: * Contributor(s): _____________________.
020: *
021: * Alternatively, the contents of this file may be used under the terms of
022: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
023: * License), in which case the provisions of the LGPL License are
024: * applicable instead of those above. If you wish to allow use of your
025: * version of this file only under the terms of the LGPL License and not to
026: * allow others to use your version of this file under the MPL, indicate
027: * your decision by deleting the provisions above and replace them with
028: * the notice and other provisions required by the LGPL License. If you do
029: * not delete the provisions above, a recipient may use your version of
030: * this file under either the MPL or the LGPL License."
031: *
032: */
033:
034: package com.icesoft.faces.facelets;
035:
036: import com.sun.facelets.FaceletContext;
037: import com.sun.facelets.tag.TagAttribute;
038: import com.sun.facelets.tag.TagAttributeException;
039: import com.sun.facelets.tag.TagConfig;
040: import com.sun.facelets.tag.TagException;
041: import com.sun.facelets.tag.TagHandler;
042: import com.sun.facelets.tag.jsf.ComponentSupport;
043:
044: import javax.el.ELException;
045: import javax.faces.FacesException;
046: import javax.faces.component.UIComponent;
047: import java.io.IOException;
048: import java.lang.reflect.Method;
049:
050: /**
051: * @author Mark Collette
052: * @since 1.1
053: */
054: public class TabChangeListenerHandler extends TagHandler {
055: private static Class panelTabSetClass;
056: private static Class tabChangeListenerClass;
057: private static Method addTabChangeListenerMethod;
058: private static boolean triedGettingICEfacesComponentClasses;
059:
060: private static synchronized boolean isICEfacesComponentClassesPresent() {
061: if (!triedGettingICEfacesComponentClasses) {
062: try {
063: panelTabSetClass = Class
064: .forName("com.icesoft.faces.component.paneltabset.PanelTabSet");
065: tabChangeListenerClass = Class
066: .forName("com.icesoft.faces.component.paneltabset.TabChangeListener");
067: addTabChangeListenerMethod = panelTabSetClass
068: .getMethod("addTabChangeListener",
069: new Class[] { tabChangeListenerClass });
070: } catch (Exception e) {
071: }
072: triedGettingICEfacesComponentClasses = true;
073: }
074: return (panelTabSetClass != null
075: && tabChangeListenerClass != null && addTabChangeListenerMethod != null);
076: }
077:
078: private final TagAttribute typeTagAttribute;
079:
080: /**
081: * @param config
082: */
083: public TabChangeListenerHandler(TagConfig config) {
084: super (config);
085:
086: typeTagAttribute = getRequiredAttribute("type");
087: }
088:
089: /**
090: * Threadsafe Method for controlling evaluation of its child tags,
091: * represented by "nextHandler"
092: */
093: public void apply(FaceletContext ctx, UIComponent parent)
094: throws IOException, FacesException, ELException {
095: if (!isICEfacesComponentClassesPresent()) {
096: throw new TagException(tag,
097: "ICEfaces components classes not found, can not use this tag");
098: }
099: if (parent == null) {
100: throw new TagException(tag, "Parent UIComponent was null");
101: }
102: if (!panelTabSetClass.isAssignableFrom(parent.getClass())) {
103: throw new TagException(
104: tag,
105: "Parent UIComponent must be a "
106: + "com.icesoft.faces.component.paneltabset.PanelTabSet");
107: }
108: if (!typeTagAttribute.isLiteral()) {
109: throw new TagAttributeException(
110: tag,
111: typeTagAttribute,
112: "The class, as given by tabChangeListener tag's type "
113: + "attribute, must be literal, and not a value expression: "
114: + typeTagAttribute.getValue());
115: }
116: String listenerClassName = typeTagAttribute.getValue(ctx);
117: try {
118: Class listenerClass = Class.forName(listenerClassName);
119: if (!tabChangeListenerClass.isAssignableFrom(listenerClass)) {
120: throw new TagAttributeException(
121: tag,
122: typeTagAttribute,
123: "The class, as given by tabChangeListener tag's type "
124: + "attribute, must implement TabChangeListener: "
125: + listenerClassName);
126: }
127:
128: if (ComponentSupport.isNew(parent)) {
129: // TabChangeListener instance
130: Object listenerObject = listenerClass.newInstance();
131: addTabChangeListenerMethod.invoke(parent,
132: new Object[] { listenerObject });
133: }
134: } catch (Exception e) {
135: throw new TagAttributeException(
136: tag,
137: typeTagAttribute,
138: "Could not either find, or instantiate, or add as a "
139: + "listener, the class described by tabChangeListener "
140: + "tag's type attribute: "
141: + listenerClassName, e);
142: }
143:
144: //TODO Use nextHandler? ice:tabChangeListener can't have children
145: }
146: }
|