Save This Page
Home » struts-2.1.8.1-src » org.apache.struts2.dojo » components » [javadoc | source]
    1   /*
    2    * $Id: TreeNode.java 651946 2008-04-27 13:41:38Z apetrelli $
    3    *
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
   10    * with the License.  You may obtain a copy of the License at
   11    *
   12    *  http://www.apache.org/licenses/LICENSE-2.0
   13    *
   14    * Unless required by applicable law or agreed to in writing,
   15    * software distributed under the License is distributed on an
   16    * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   17    * KIND, either express or implied.  See the License for the
   18    * specific language governing permissions and limitations
   19    * under the License.
   20    */
   21   
   22   package org.apache.struts2.dojo.components;
   23   
   24   import java.util.Random;
   25   
   26   import javax.servlet.http.HttpServletRequest;
   27   import javax.servlet.http.HttpServletResponse;
   28   
   29   import org.apache.struts2.components.ClosingUIBean;
   30   import org.apache.struts2.views.annotations.StrutsTag;
   31   import org.apache.struts2.views.annotations.StrutsTagAttribute;
   32   import org.apache.struts2.views.annotations.StrutsTagSkipInheritance;
   33   
   34   import com.opensymphony.xwork2.util.ValueStack;
   35   
   36   /**
   37    * <!-- START SNIPPET: javadoc -->
   38    *
   39    * Renders a tree node within a tree widget with AJAX support.<p/>
   40    *
   41    * Either of the following combinations should be used depending on if the tree
   42    * is to be constructed dynamically or statically. <p/>
   43    *
   44    * <b>Dynamically:</b>
   45    * <ul>
   46    *      <li>id - id of this tree node</li>
   47    *      <li>title - label to be displayed for this tree node</li>
   48    * </ul>
   49    *
   50    * <b>Statically:</b>
   51    * <ul>
   52    *      <li>rootNode - the parent node of which this tree is derived from</li>
   53    *      <li>nodeIdProperty - property to obtained this current tree node's id</li>
   54    *      <li>nodeTitleProperty - property to obtained this current tree node's title</li>
   55    *      <li>childCollectionProperty - property that returnds this current tree node's children</li>
   56    * </ul>
   57    *
   58    * <!-- END SNIPPET: javadoc -->
   59    *
   60    * <p/> <b>Examples</b>
   61    *
   62    * <pre>
   63    * <!-- START SNIPPET: example -->
   64    *
   65    * &lt;-- Creating tree statically using hard-coded data. --&gt;
   66    * &lt;s:tree id="..." label="..."&gt;
   67    *    &lt;s:treenode id="..." label="..." /&gt;
   68    *    &lt;s:treenode id="..." label="..."&gt;
   69    *        &lt;s:treenode id="..." label="..." /&gt;
   70    *        &lt;s:treenode id="..." label="..." /&gt;
   71    *    &lt;/s:treenode&gt;
   72    *    &lt;s:treenode id="..." label="..." /&gt;
   73    * &lt;/s:tree&gt;
   74    *
   75    * &lt;-- Creating tree dynamically using data from backing action. --&gt;
   76    * &lt;s:tree
   77    *          id="..."
   78    *          rootNode="..."
   79    *          nodeIdProperty="..."
   80    *          nodeTitleProperty="..."
   81    *          childCollectionProperty="..." /&gt;
   82    *
   83    * <!-- END SNIPPET: example -->
   84    * </pre>
   85    *
   86    */
   87   @StrutsTag(name="treenode", tldTagClass="org.apache.struts2.dojo.views.jsp.ui.TreeNodeTag", description="Render a tree node within a tree widget.")
   88   public class TreeNode extends ClosingUIBean {
   89       private static final String TEMPLATE = "treenode-close";
   90       private static final String OPEN_TEMPLATE = "treenode";
   91       private final static transient Random RANDOM = new Random();    
   92   
   93       public TreeNode(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
   94           super(stack, request, response);
   95       }
   96   
   97       @Override
   98       @StrutsTagSkipInheritance
   99       public void setTheme(String theme) {
  100           super.setTheme(theme);
  101       }
  102       
  103       @Override
  104       public String getTheme() {
  105           return "ajax";
  106       }
  107   
  108       public String getDefaultOpenTemplate() {
  109           return OPEN_TEMPLATE;
  110       }
  111   
  112       protected String getDefaultTemplate() {
  113           return TEMPLATE;
  114       }
  115   
  116       protected void evaluateExtraParams() {
  117           super.evaluateExtraParams();
  118           
  119           // generate a random ID if not explicitly set and not parsing the content
  120           Boolean parseContent = (Boolean)stack.getContext().get(Head.PARSE_CONTENT);
  121           boolean generateId = (parseContent != null ? !parseContent : true);
  122   
  123           addParameter("pushId", generateId);
  124           if ((this.id == null || this.id.length() == 0) && generateId) {
  125               // resolves Math.abs(Integer.MIN_VALUE) issue reported by FindBugs 
  126               // http://findbugs.sourceforge.net/bugDescriptions.html#RV_ABSOLUTE_VALUE_OF_RANDOM_INT
  127               int nextInt = RANDOM.nextInt();
  128               nextInt = nextInt == Integer.MIN_VALUE ? Integer.MAX_VALUE : Math.abs(nextInt);  
  129               this.id = "widget_" + String.valueOf(nextInt);
  130               addParameter("id", this.id);
  131           }
  132           
  133           Tree parentTree = (Tree) findAncestor(Tree.class);
  134           parentTree.addChildrenId(this.id);
  135       }
  136       
  137       @StrutsTagAttribute(description="Label expression used for rendering tree node label.", required=true)
  138       public void setLabel(String label) {
  139           super.setLabel(label);
  140       }
  141       
  142       @StrutsTagAttribute(description="The css class to use for element")
  143       public void setCssClass(String cssClass) {
  144           super.setCssClass(cssClass);
  145       }
  146   
  147       @StrutsTagAttribute(description="The css style to use for element")
  148       public void setCssStyle(String cssStyle) {
  149           super.setCssStyle(cssStyle);
  150       }
  151   
  152       @StrutsTagAttribute(description="The id to use for the element")
  153       public void setId(String id) {
  154           super.setId(id);
  155       }
  156   
  157       @StrutsTagAttribute(description="The name to set for element")
  158       public void setName(String name) {
  159           super.setName(name);
  160       }
  161   }

Save This Page
Home » struts-2.1.8.1-src » org.apache.struts2.dojo » components » [javadoc | source]