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 java.io.IOException;
22  
23  import javax.el.ELException;
24  import javax.faces.FacesException;
25  import javax.faces.component.UIComponent;
26  
27  import org.apache.myfaces.shared_tomahawk.util.ClassUtils;
28  
29  import com.sun.facelets.FaceletContext;
30  import com.sun.facelets.FaceletException;
31  import com.sun.facelets.tag.TagAttribute;
32  import com.sun.facelets.tag.TagConfig;
33  import com.sun.facelets.tag.TagHandler;
34  
35  /**
36   * Custom Facelets taghandler for the TabChangeListener class.
37   *  
38   * @since 1.1.7
39   */
40  public class TabChangeListenerTagHandler extends TagHandler
41  {
42      private final TagAttribute typeAttr;
43  
44      public TabChangeListenerTagHandler(TagConfig config)
45      {
46          super(config);
47          typeAttr = getRequiredAttribute("type");
48      }
49  
50      public void apply(FaceletContext faceletContext, UIComponent parent)
51              throws IOException, FacesException, FaceletException, ELException
52      {
53          // only process this tag if the parent has just been created..
54          if (parent.getParent() == null)
55          {
56              if (parent instanceof HtmlPanelTabbedPane)
57              {
58                  Object listenerRef;
59                  if (typeAttr.isLiteral())
60                  {
61                      listenerRef = typeAttr.getValue();
62                  }
63                  else
64                  {
65                      listenerRef = typeAttr.getObject(faceletContext);
66                  }
67                  
68                  if (listenerRef instanceof String)
69                  {
70                      String className = (String) listenerRef;
71                      TabChangeListener listener = (TabChangeListener) ClassUtils.newInstance(className);
72                      ((HtmlPanelTabbedPane) parent).addTabChangeListener(listener);
73                  }
74                  else if (listenerRef instanceof TabChangeListener)
75                  {
76                      TabChangeListener listener = (TabChangeListener) listenerRef;
77                      ((HtmlPanelTabbedPane) parent).addTabChangeListener(listener);
78                  }
79                  else if (listenerRef == null)
80                  {
81                      throw new FaceletException("Property 'type' must not be null.");
82                  }
83                  else
84                  {
85                      throw new FaceletException(
86                         "Property 'type' must be either a string (containing a class name) " +
87                         "or a TabChangeListener instance.");
88                  }
89              }
90              else
91              {
92                  throw new FaceletException(
93                          "Component " + parent.getId() + " is not of type HtmlPanelTabbedPane");
94              }
95          }
96      }
97  }