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 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.custom.tree.HtmlTree;
28  import org.apache.myfaces.custom.tree.event.TreeSelectionListener;
29  import org.apache.myfaces.shared_tomahawk.util.ClassUtils;
30  
31  import com.sun.facelets.FaceletContext;
32  import com.sun.facelets.FaceletException;
33  import com.sun.facelets.tag.TagAttribute;
34  import com.sun.facelets.tag.TagConfig;
35  import com.sun.facelets.tag.TagHandler;
36  
37  /**
38   * 
39   * @since 1.1.7
40   */
41  public class TreeSelectionListenerTagHandler extends TagHandler
42  {
43  
44      private final TagAttribute typeAttr;
45      
46      public TreeSelectionListenerTagHandler(TagConfig config)
47      {
48          super(config);
49          typeAttr = getRequiredAttribute("type");
50      }
51  
52      public void apply(FaceletContext faceletContext, UIComponent parent)
53              throws IOException, FacesException, FaceletException, ELException
54      {
55          if (parent.getParent() == null)
56          {
57              if (parent instanceof HtmlTree)
58              {
59                  String className;
60                  if (!typeAttr.isLiteral())
61                  {
62                      className = typeAttr.getValue();
63                  }
64                  else
65                  {
66                      className = typeAttr.getValue(faceletContext);
67                  }
68                  TreeSelectionListener listener = (TreeSelectionListener) ClassUtils.newInstance(className);
69                  ((HtmlTree) parent).addTreeSelectionListener(listener);
70              }
71              else
72              {
73                  throw new FacesException(
74                          "Component is not HtmlTree children");
75              }            
76          }
77      }
78  }