KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > custom > tree > taglib > TreeSelectionListenerTag


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.custom.tree.taglib;
17
18 import javax.faces.component.UIComponent;
19 import javax.faces.context.FacesContext;
20 import javax.faces.el.ValueBinding;
21 import javax.faces.webapp.UIComponentTag;
22 import javax.servlet.jsp.JspException JavaDoc;
23 import javax.servlet.jsp.tagext.Tag JavaDoc;
24 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
25
26 import org.apache.myfaces.custom.tree.HtmlTree;
27 import org.apache.myfaces.custom.tree.event.TreeSelectionListener;
28 import org.apache.myfaces.util.ClassUtils;
29
30
31 /**
32  * Tag to add a tree selection listeners to a {@link HtmlTree]
33  *
34  * @author <a HREF="mailto:oliver@rossmueller.com">Oliver Rossmueller</a>
35  * @version $Revision: 1.3 $ $Date: 2004/10/13 11:50:58 $
36  * $Log: TreeSelectionListenerTag.java,v $
37  * Revision 1.3 2004/10/13 11:50:58 matze
38  * renamed packages to org.apache
39  *
40  * Revision 1.2 2004/07/01 21:53:06 mwessendorf
41  * ASF switch
42  *
43  * Revision 1.1 2004/04/22 21:14:55 o_rossmueller
44  * TreeSelectionListener support
45  *
46  */

47 public class TreeSelectionListenerTag extends TagSupport JavaDoc
48 {
49
50     private String JavaDoc type = null;
51
52
53     public TreeSelectionListenerTag()
54     {
55     }
56
57
58     public void setType(String JavaDoc type)
59     {
60         this.type = type;
61     }
62
63
64     public int doStartTag() throws JspException JavaDoc
65     {
66         if (type == null)
67         {
68             throw new JspException JavaDoc("type attribute not set");
69         }
70
71         //Find parent UIComponentTag
72
UIComponentTag componentTag = UIComponentTag.getParentUIComponentTag(pageContext);
73         if (componentTag == null)
74         {
75             throw new JspException JavaDoc("TreeSelectionListenerTag has no UIComponentTag ancestor");
76         }
77
78         if (componentTag.getCreated())
79         {
80             //Component was just created, so we add the Listener
81
UIComponent component = componentTag.getComponentInstance();
82             if (component instanceof HtmlTree)
83             {
84                 String JavaDoc className;
85                 if (UIComponentTag.isValueReference(type))
86                 {
87                     FacesContext facesContext = FacesContext.getCurrentInstance();
88                     ValueBinding valueBinding = facesContext.getApplication().createValueBinding(type);
89                     className = (String JavaDoc) valueBinding.getValue(facesContext);
90                 } else
91                 {
92                     className = type;
93                 }
94                 TreeSelectionListener listener = (TreeSelectionListener) ClassUtils.newInstance(className);
95                 ((HtmlTree) component).addTreeSelectionListener(listener);
96             } else
97             {
98                 throw new JspException JavaDoc("Component " + component.getId() + " is no HtmlTree");
99             }
100         }
101
102         return Tag.SKIP_BODY;
103     }
104 }
105
Popular Tags