View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.custom.tree.taglib;
20  
21  import javax.faces.component.UIComponent;
22  import javax.faces.context.FacesContext;
23  import javax.faces.el.ValueBinding;
24  import javax.faces.webapp.UIComponentTag;
25  import javax.servlet.jsp.JspException;
26  import javax.servlet.jsp.tagext.Tag;
27  import javax.servlet.jsp.tagext.TagSupport;
28  
29  import org.apache.myfaces.custom.tree.HtmlTree;
30  import org.apache.myfaces.custom.tree.event.TreeSelectionListener;
31  import org.apache.myfaces.shared_tomahawk.util.ClassUtils;
32  
33  
34  /**
35   * Tag to add a tree selection listeners to a {@link HtmlTree}
36   *
37   * @JSFJspTag
38   *   name="t:treeSelectionListener"
39   *   bodyContent="empty"
40   *   tagHandler="org.apache.myfaces.custom.tree.taglib.TreeSelectionListenerTagHandler"
41   *   
42   * @author <a href="mailto:oliver@rossmueller.com">Oliver Rossmueller</a>
43   * @version $Revision: 666398 $ $Date: 2008-06-10 19:13:17 -0500 (Tue, 10 Jun 2008) $
44   */
45  public class TreeSelectionListenerTag extends TagSupport
46  {
47      private static final long serialVersionUID = 7322612767746076403L;
48      private String type = null;
49  
50  
51      public TreeSelectionListenerTag()
52      {
53      }
54  
55  
56      /**
57       * @JSFJspAttribute
58       *   required = "true"
59       */
60      public void setType(String type)
61      {
62          this.type = type;
63      }
64  
65  
66      public int doStartTag() throws JspException
67      {
68          if (type == null)
69          {
70              throw new JspException("type attribute not set");
71          }
72  
73          //Find parent UIComponentTag
74          UIComponentTag componentTag = UIComponentTag.getParentUIComponentTag(pageContext);
75          if (componentTag == null)
76          {
77              throw new JspException("TreeSelectionListenerTag has no UIComponentTag ancestor");
78          }
79  
80          if (componentTag.getCreated())
81          {
82              //Component was just created, so we add the Listener
83              UIComponent component = componentTag.getComponentInstance();
84              if (component instanceof HtmlTree)
85              {
86                  String className;
87                  if (UIComponentTag.isValueReference(type))
88                  {
89                      FacesContext facesContext = FacesContext.getCurrentInstance();
90                      ValueBinding valueBinding = facesContext.getApplication().createValueBinding(type);
91                      className = (String) valueBinding.getValue(facesContext);
92                  } else
93                  {
94                      className = type;
95                  }
96                  TreeSelectionListener listener = (TreeSelectionListener) ClassUtils.newInstance(className);
97                  ((HtmlTree) component).addTreeSelectionListener(listener);
98              } else
99              {
100                 throw new JspException("Component " + component.getId() + " is no HtmlTree");
101             }
102         }
103 
104         return Tag.SKIP_BODY;
105     }
106 }