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 javax.faces.application.Application;
22  import javax.faces.component.UIComponent;
23  import javax.faces.context.FacesContext;
24  import javax.faces.el.ValueBinding;
25  import javax.faces.webapp.UIComponentTag;
26  
27  import org.apache.myfaces.custom.tree.HtmlTree;
28  import org.apache.myfaces.custom.tree.model.DefaultTreeModel;
29  import org.apache.myfaces.custom.tree.model.TreeModel;
30  import org.apache.myfaces.custom.tree.model.TreePath;
31  
32  import com.sun.facelets.FaceletContext;
33  import com.sun.facelets.tag.TagAttribute;
34  import com.sun.facelets.tag.jsf.ComponentConfig;
35  import com.sun.facelets.tag.jsf.html.HtmlComponentHandler;
36  
37  /**
38   * 
39   * @since 1.1.7
40   */
41  public class TreeTagHandler extends HtmlComponentHandler
42  {
43  
44      private TagAttribute valueAttr;
45      private TagAttribute expandRootAttr;
46      
47      private boolean expandRoot;
48      
49      public TreeTagHandler(ComponentConfig config)
50      {
51          super(config);
52          valueAttr = getRequiredAttribute("value");
53          expandRootAttr = getRequiredAttribute("expandRoot");
54          expandRoot = false;
55      }
56      
57      protected void setAttributes(FaceletContext ctx, Object instance)
58      {
59          super.setAttributes(ctx, instance);
60          
61          Application app = ctx.getFacesContext().getApplication();
62          
63          HtmlTree tree = (HtmlTree) instance;
64          
65          if(valueAttr != null)
66          {
67              String value = valueAttr.getValue();
68              if (value != null && UIComponentTag.isValueReference(value))
69              {
70                  tree.setValueBinding("model", app.createValueBinding(valueAttr
71                          .getValue()));
72              }
73          }
74          else
75          {
76              ValueBinding binding = tree.getValueBinding("model");
77              if (binding == null) {
78                  binding = app.createValueBinding("#{sessionScope.tree}");
79              }
80              tree.setValueBinding("model", binding);            
81          }
82          
83          if (expandRootAttr != null)
84          {
85              expandRoot = expandRootAttr.getBoolean(ctx);
86          }
87      }
88      
89      protected void onComponentCreated(FaceletContext ctx,
90              UIComponent component, UIComponent parent)
91      {
92          FacesContext context = ctx.getFacesContext();
93          Application app = ctx.getFacesContext().getApplication();
94          
95          if (valueAttr != null) {
96              String value = valueAttr.getValue();
97              if (value != null){
98                  ValueBinding valueBinding = app.createValueBinding(value);
99                  TreeModel treeModel = (TreeModel) (valueBinding.getValue(context));
100         
101                 if (treeModel == null) {
102                     // create default model
103                     treeModel = new DefaultTreeModel();
104                     valueBinding.setValue(context, treeModel);
105                 }
106             }
107         }
108         
109         HtmlTree tree = (HtmlTree) component;
110         
111         if (expandRoot)
112         {
113             // component was created, so expand the root node
114             TreeModel model = tree.getModel(context);
115     
116             if (model != null) {
117                 tree.expandPath(new TreePath(new Object[] { model.getRoot() }),
118                         context);
119             }
120         }
121         tree.addToModelListeners();
122     }
123     
124 }