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.component.UIComponent;
22  import javax.faces.context.FacesContext;
23  import javax.faces.el.ValueBinding;
24  import javax.servlet.jsp.JspException;
25  
26  import org.apache.myfaces.custom.tree.HtmlTree;
27  import org.apache.myfaces.custom.tree.model.DefaultTreeModel;
28  import org.apache.myfaces.custom.tree.model.TreeModel;
29  import org.apache.myfaces.custom.tree.model.TreePath;
30  import org.apache.myfaces.shared_tomahawk.taglib.html.HtmlPanelGroupTag;
31  
32  /**
33   * <p>
34   * HtmlTree tag.
35   * </p>
36   * @since 1.1.7
37   * @author <a href="mailto:oliver@rossmueller.com">Oliver Rossmueller </a>
38   * @version $Revision: 888604 $ $Date: 2004/10/13 11:50:58
39   */
40  public class AbstractTreeTag extends HtmlPanelGroupTag {
41  
42      private String value;
43  
44      private String expandRoot;
45  
46      public String getComponentType() {
47          return "org.apache.myfaces.HtmlTree";
48      }
49  
50      public String getRendererType() {
51          return "org.apache.myfaces.HtmlTree";
52      }
53  
54      public String getValue() {
55          return value;
56      }
57  
58      public void setValue(String newValue) {
59          value = newValue;
60      }
61  
62      public String isExpandRoot() {
63          return expandRoot;
64      }
65  
66      public void setExpandRoot(String expandRoot) {
67          this.expandRoot = expandRoot;
68      }
69  
70      /**
71       * Obtain tree model or create a default model.
72       */
73      public int doStartTag() throws JspException {
74          FacesContext context = FacesContext.getCurrentInstance();
75  
76          if (value != null) {
77              ValueBinding valueBinding = context.getApplication()
78                      .createValueBinding(value);
79              TreeModel treeModel = (TreeModel) (valueBinding.getValue(context));
80  
81              if (treeModel == null) {
82                  // create default model
83                  treeModel = new DefaultTreeModel();
84                  valueBinding.setValue(context, treeModel);
85              }
86          }
87          int answer = super.doStartTag();
88          HtmlTree tree = (HtmlTree) getComponentInstance();
89  
90          if (getCreated() && parseBoolean(expandRoot)) {
91              // component was created, so expand the root node
92              TreeModel model = tree.getModel(context);
93  
94              if (model != null) {
95                  tree.expandPath(new TreePath(new Object[] { model.getRoot() }),
96                          context);
97              }
98          }
99  
100         tree.addToModelListeners();
101         return answer;
102     }
103     
104     private boolean parseBoolean(String s)
105     {
106         return ((s != null) && s.equalsIgnoreCase("true"));
107     }
108 
109     public void release() {
110         super.release();
111         value = null;
112         expandRoot = null;
113     }
114 
115     /**
116      * Applies attributes to the tree component
117      */
118     protected void setProperties(UIComponent component) {
119         super.setProperties(component);
120         FacesContext context = FacesContext.getCurrentInstance();
121 
122         if (value != null) {
123             if (isValueReference(value)) {
124                 ValueBinding binding = context.getApplication()
125                         .createValueBinding(value);
126                 component.setValueBinding("model", binding);
127             }
128         } else {
129             ValueBinding binding = component.getValueBinding("model");
130             if (binding == null) {
131                 binding = context.getApplication().createValueBinding(
132                         "#{sessionScope.tree}");
133             }
134             component.setValueBinding("model", binding);
135         }
136     }
137 }