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.tree2;
20  
21  import java.util.Map;
22  
23  import javax.faces.component.UICommand;
24  import javax.faces.component.html.HtmlCommandLink;
25  import javax.faces.context.FacesContext;
26  import javax.faces.el.MethodBinding;
27  
28  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFComponent;
29  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFProperty;
30  import org.apache.myfaces.component.LibraryLocationAware;
31  import org.apache.myfaces.component.LocationAware;
32  
33  /**
34   * Represents "tree data" in an HTML format.  Also provides a mechanism for maintaining expand/collapse
35   * state of the nodes in the tree.
36   *
37   * A component that provides an HTML-based tree from data supplied by a 
38   * backing bean. The tree is highly customizable and allows for 
39   * fine-grained control over the appearance of each of the nodes 
40   * depending on their type. 
41   * 
42   * Almost any type of JSF component (text, image, checkbox, etc.) can 
43   * be rendered inside the nodes and there is an option for client-side 
44   * or server-side toggling of the expand/collapse state. 
45   * 
46   * Unless otherwise specified, all attributes accept static values or EL expressions.
47   * 
48   * @since 1.1.7
49   * @author Sean Schofield
50   */
51  @JSFComponent(
52      name = "t:tree2",
53      clazz = "org.apache.myfaces.custom.tree2.HtmlTree",
54      tagClass = "org.apache.myfaces.custom.tree2.TreeTag",
55      tagHandler = "org.apache.myfaces.custom.tree2.HtmlTreeTagHandler")
56  public abstract class AbstractHtmlTree extends UITreeData
57      implements LocationAware, LibraryLocationAware
58  {
59      public static final String COMPONENT_TYPE = "org.apache.myfaces.HtmlTree2";
60      private static final String DEFAULT_RENDERER_TYPE = "org.apache.myfaces.HtmlTree2";
61  
62      private UICommand _expandControl = null;
63  
64      /**
65       * Perform client-side toggling of expand/collapse state via javascript (default is true.)
66       * 
67       * @return  the new clientSideToggle value
68       */
69      @JSFProperty(defaultValue = "true")
70      public boolean isClientSideToggle()
71      {
72          return (Boolean) getStateHelper().eval(PropertyKeys.clientSideToggle, true);
73      }
74      
75      /**
76       * Sets 
77       * 
78       * @param clientSideToggle  the new clientSideToggle value
79       */
80      public void setClientSideToggle(boolean clientSideToggle)
81      {
82          getStateHelper().put(PropertyKeys.clientSideToggle, clientSideToggle ); 
83      }  
84          
85      /**
86       * @see org.apache.myfaces.custom.tree2.UITreeData#processNodes(javax.faces.context.FacesContext, int, org.apache.myfaces.custom.tree2.TreeWalker)
87       */
88      protected void processNodes(FacesContext context, int processAction, TreeWalker walker)
89      {
90          if (isClientSideToggle()) {
91              walker.setCheckState(false);
92          }
93          super.processNodes(context, processAction, walker);
94      }
95      
96      //public abstract String getLocalVarNodeToggler();
97  
98      // see superclass for documentation
99      public void setNodeId(String nodeId)
100     {
101         super.setNodeId(nodeId);
102 
103         String varNodeToggler = (String) getStateHelper().get(PropertyKeys.varNodeToggler);
104         if (varNodeToggler != null)
105         {
106             Map requestMap = getFacesContext().getExternalContext().getRequestMap();
107             requestMap.put(varNodeToggler, this);
108         }
109     }
110 
111     /**
112      * Gets the expand/collapse control that can be used to handle expand/collapse nodes.  This is only used in server-side
113      * mode.  It allows the nagivation controls (if any) to be clickable as well as any commandLinks the user has set up in
114      * their JSP.
115      * 
116      * @return UICommand
117      */
118     public UICommand getExpandControl()
119     {
120         if (_expandControl == null){
121             _expandControl = new HtmlCommandLink();
122             _expandControl.setParent(this);
123         }
124         return _expandControl;
125     }
126     
127     /**
128      * Gets 
129      *
130      * @return  the new varNodeToggler value
131      */
132     @JSFProperty(literalOnly=true)
133     public String getVarNodeToggler()
134     {
135         return (String) getStateHelper().get(PropertyKeys.varNodeToggler);
136     }
137     
138     public void setVarNodeToggler(String varNodeToggler)
139     {
140         getStateHelper().put(PropertyKeys.varNodeToggler, varNodeToggler );
141 
142         // create a method binding for the expand control
143         String bindingString = "#{" + varNodeToggler + ".toggleExpanded}";
144         MethodBinding actionBinding = FacesContext.getCurrentInstance().getApplication().createMethodBinding(bindingString, null);
145         getExpandControl().setAction(actionBinding);
146     }
147         
148     /**
149      * Show the "plus" and "minus" navigation icons (default is true.) 
150      * Value is ignored if clientSideToggle is true.
151      * 
152      */
153     @JSFProperty(defaultValue="true")
154     public abstract boolean isShowNav();
155 
156     /**
157      * Show the connecting lines (default is true.)
158      * 
159      */
160     @JSFProperty(defaultValue="true")
161     public abstract boolean isShowLines();
162 
163     /**
164      * Include the root node when rendering the tree (default is true.)
165      * 
166      */
167     @JSFProperty(defaultValue="true")
168     public abstract boolean isShowRootNode();
169 
170     /**
171      * Preserve changes in client-side toggle information between 
172      * requests (default is true.)
173      * 
174      */
175     @JSFProperty(defaultValue="true")
176     public abstract boolean isPreserveToggle();
177 
178     protected enum PropertyKeys
179     {
180          clientSideToggle
181         , varNodeToggler
182     }
183 }