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.tabbedpane;
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.shared_tomahawk.util.ClassUtils;
30  
31  
32  /**
33   * Tag to add a tab change listeners to a {@link org.apache.myfaces.custom.tabbedpane.HtmlPanelTabbedPane}
34   *
35   * @JSFJspTag
36   *   name="t:tabChangeListener"
37   *   bodyContent="empty"
38   *   tagHandler = "org.apache.myfaces.custom.tabbedpane.TabChangeListenerTagHandler"
39   *   
40   * @author <a href="mailto:oliver@rossmueller.com">Oliver Rossmueller</a>
41   * @version $Revision: 692184 $ $Date: 2008-09-04 13:21:52 -0500 (Thu, 04 Sep 2008) $
42   */
43  public class TabChangeListenerTag extends TagSupport
44  {
45      private static final long serialVersionUID = -6903749011638483023L;
46      private String type = null;
47  
48  
49      public TabChangeListenerTag()
50      {
51      }
52  
53      /**
54       * @JSFJspAttribute
55       *   required = "true"
56       */
57      public void setType(String type)
58      {
59          this.type = type;
60      }
61  
62  
63      public int doStartTag() throws JspException
64      {
65          if (type == null)
66          {
67              throw new JspException("type attribute not set");
68          }
69  
70          //Find parent UIComponentTag
71          UIComponentTag componentTag = UIComponentTag.getParentUIComponentTag(pageContext);
72          if (componentTag == null)
73          {
74              throw new JspException("TabChangeListenerTag has no UIComponentTag ancestor");
75          }
76  
77          if (componentTag.getCreated())
78          {
79              //Component was just created, so we add the Listener
80              UIComponent component = componentTag.getComponentInstance();
81              if (component instanceof HtmlPanelTabbedPane)
82              {
83                  Object listenerRef = type;
84                  if (UIComponentTag.isValueReference(type))
85                  {
86                      FacesContext facesContext = FacesContext.getCurrentInstance();
87                      ValueBinding valueBinding = facesContext.getApplication().createValueBinding(type);
88                      listenerRef = valueBinding.getValue(facesContext);
89                  }
90  
91                  if(listenerRef instanceof String)
92                  {
93                      String className = (String) listenerRef;
94                      TabChangeListener listener = (TabChangeListener) ClassUtils.newInstance(className);
95                      ((HtmlPanelTabbedPane) component).addTabChangeListener(listener);
96                  }
97                  else if(listenerRef instanceof TabChangeListener)
98                  {
99                      TabChangeListener listener = (TabChangeListener) listenerRef;
100                     ((HtmlPanelTabbedPane) component).addTabChangeListener(listener);
101 
102                 }
103                 else
104                     throw new JspException("type is neither a 'String' nor a value-binding to a 'String' or a 'TabChangeListener' instance.");
105             }
106             else
107             {
108                 throw new JspException("Component " + component.getId() + " is no HtmlPanelTabbedPane");
109             }
110         }
111 
112         return Tag.SKIP_BODY;
113     }
114 }