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.el.ValueExpression;
22  import javax.faces.component.UIComponent;
23  import javax.faces.context.FacesContext;
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   *
37   * @author <a href="mailto:oliver@rossmueller.com">Oliver Rossmueller </a>
38   * @version $Revision: 664402 $ $Date: 2004/10/13 11:50:58
39   */
40  public class AbstractTreeTag extends HtmlPanelGroupTag {
41  
42      private ValueExpression value;
43  
44      private boolean 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 ValueExpression getValue() {
55          return value;
56      }
57  
58      public void setValue(ValueExpression newValue) {
59          value = newValue;
60      }
61  
62      public boolean isExpandRoot() {
63          return expandRoot;
64      }
65  
66      public void setExpandRoot(boolean 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              TreeModel treeModel = (TreeModel) (value.getValue(context.getELContext()));
78  
79              if (treeModel == null) {
80                  // create default model
81                  treeModel = new DefaultTreeModel();
82                  value.setValue(context.getELContext(), treeModel);
83              }
84          }
85          int answer = super.doStartTag();
86          HtmlTree tree = (HtmlTree) getComponentInstance();
87  
88          if (getCreated() && expandRoot) {
89              // component was created, so expand the root node
90              TreeModel model = tree.getModel(context);
91  
92              if (model != null) {
93                  tree.expandPath(new TreePath(new Object[] { model.getRoot() }),
94                          context);
95              }
96          }
97  
98          tree.addToModelListeners();
99          return answer;
100     }
101 
102     public void release() {
103         super.release();
104         value = null;
105         expandRoot = false;
106     }
107 
108     /**
109      * Applies attributes to the tree component
110      */
111     protected void setProperties(UIComponent component) {
112         super.setProperties(component);
113         FacesContext context = FacesContext.getCurrentInstance();
114 
115         
116         if (value != null) {
117             if (!value.isLiteralText()) {
118                 component.setValueExpression("model", value);
119             }
120         } else {
121             ValueExpression binding = component.getValueExpression("model");
122             if (binding == null) {
123                 binding = context.getApplication().
124                     getExpressionFactory().createValueExpression(
125                     context.getELContext(),"#{sessionScope.tree}", Object.class);
126             }
127             component.setValueExpression("model", binding);
128         }
129     }
130 }