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 org.apache.myfaces.shared_tomahawk.util.ClassUtils;
22  import org.apache.myfaces.custom.tree.HtmlTree;
23  import org.apache.myfaces.custom.tree.IconProvider;
24  
25  import javax.faces.component.UIComponent;
26  import javax.faces.context.FacesContext;
27  import javax.faces.el.ValueBinding;
28  import javax.faces.webapp.UIComponentTag;
29  import javax.servlet.jsp.JspException;
30  import javax.servlet.jsp.tagext.Tag;
31  import javax.servlet.jsp.tagext.TagSupport;
32  
33  
34  /**
35   * @JSFJspTag
36   *   name="t:iconProvider"
37   *   bodyContent="empty"
38   *   tagHandler="org.apache.myfaces.custom.tree.taglib.IconProviderTagHandler"
39   *   
40   * @author <a href="mailto:oliver@rossmueller.com">Oliver Rossmueller</a>
41   * @version $Revision: 666398 $ $Date: 2008-06-10 19:13:17 -0500 (Tue, 10 Jun 2008) $
42   */
43  public class IconProviderTag
44      extends TagSupport
45  {
46      private static final long serialVersionUID = -8851450834386187922L;
47      private String type = null;
48  
49  
50      public IconProviderTag()
51      {
52      }
53  
54      /**
55       * @JSFJspAttribute
56       *   required = "true"
57       */
58      public void setType(String type)
59      {
60          this.type = type;
61      }
62  
63  
64      public int doStartTag() throws JspException
65      {
66          if (type == null)
67          {
68              throw new JspException("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("IconProviderTag has no UIComponentTag ancestor");
76          }
77  
78          UIComponent component = componentTag.getComponentInstance();
79          if (component instanceof HtmlTree)
80          {
81              String className;
82              if (UIComponentTag.isValueReference(type))
83              {
84                  FacesContext facesContext = FacesContext.getCurrentInstance();
85                  ValueBinding vb = facesContext.getApplication().createValueBinding(type);
86                  className = (String) vb.getValue(facesContext);
87              } else
88              {
89                  className = type;
90              }
91              IconProvider provider = (IconProvider) ClassUtils.newInstance(className);
92              ((HtmlTree) component).setIconProvider(provider);
93          } else
94          {
95              throw new JspException("Component " + component.getId() + " is no HtmlTree");
96          }
97  
98          return Tag.SKIP_BODY;
99      }
100 }