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