View Javadoc

    1   /*
    2    * $Id: Tree.java 497654 2007-01-19 00:21:57Z rgielen $
    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  package org.apache.struts2.components;
    22
    23  import java.io.Writer;
    24
    25  import javax.servlet.http.HttpServletRequest;
    26  import javax.servlet.http.HttpServletResponse;
    27
    28  import org.apache.struts2.views.annotations.StrutsTag;
    29  import org.apache.struts2.views.annotations.StrutsTagAttribute;
    30
    31  import com.opensymphony.xwork2.util.ValueStack;
    32
    33  /***
    34   * <!-- START SNIPPET: javadoc -->
    35   *
    36   * Renders a tree widget with AJAX support.<p/>
    37   *
    38   * The id attribute is normally specified, such that it could be looked up using
    39   * javascript if necessary.<p/>
    40   *
    41   * <!-- END SNIPPET: javadoc -->
    42   *
    43   * <p/> <b>Examples</b>
    44   *
    45   * <pre>
    46   * <!-- START SNIPPET: example -->
    47   *
    48   * &lt-- statically --&gt;
    49   * &lt;s:tree id="..." label="..."&gt;
    50   *    &lt;s:treenode id="..." label="..." /&gt;
    51   *    &lt;s:treenode id="..." label="..."&gt;
    52   *        &lt;s:treenode id="..." label="..." /&gt;
    53   *        &lt;s:treenode id="..." label="..." /&gt;
    54   *    &;lt;/s:treenode&gt;
    55   *    &lt;s:treenode id="..." label="..." /&gt;
    56   * &lt;/s:tree&gt;
    57   *
    58   * &lt;-- dynamically --&gt;
    59   * &lt;s:tree
    60   *          id="..."
    61   *          rootNode="..."
    62   *          nodeIdProperty="..."
    63   *          nodeTitleProperty="..."
    64   *          childCollectionProperty="..." /&gt;
    65   *
    66   * <!-- END SNIPPET: example -->
    67   * </pre>
    68   *
    69   */
    70  @StrutsTag(name="tree", tldTagClass="org.apache.struts2.views.jsp.ui.TreeTag", description="Render a tree widget.")
    71  public class Tree extends ClosingUIBean {
    72
    73      private static final String TEMPLATE = "tree-close";
    74      private static final String OPEN_TEMPLATE = "tree";
    75
    76      private String toggle = "fade";
    77      private String treeSelectedTopic;
    78      private String treeExpandedTopic;
    79      private String treeCollapsedTopic;
    80      protected String rootNodeAttr;
    81      protected String childCollectionProperty;
    82      protected String nodeTitleProperty;
    83      protected String nodeIdProperty;
    84      private String showRootGrid;
    85
    86      private String showGrid;
    87      private String blankIconSrc;
    88      private String gridIconSrcL;
    89      private String gridIconSrcV;
    90      private String gridIconSrcP;
    91      private String gridIconSrcC;
    92      private String gridIconSrcX;
    93      private String gridIconSrcY;
    94      private String expandIconSrcPlus;
    95      private String expandIconSrcMinus;
    96      private String iconWidth;
    97      private String iconHeight;
    98      private String toggleDuration;
    99      private String templateCssPath;
    100
    101     public Tree(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
    102         super(stack, request, response);
    103     }
    104
    105     public boolean start(Writer writer) {
    106         boolean result = super.start(writer);
    107
    108         if (this.label == null) {
    109             if ((rootNodeAttr == null)
    110                     || (childCollectionProperty == null)
    111                     || (nodeTitleProperty == null)
    112                     || (nodeIdProperty == null)) {
    113                 fieldError("label","The TreeTag requires either a value for 'label' or ALL of 'rootNode', " +
    114                         "'childCollectionProperty', 'nodeTitleProperty', and 'nodeIdProperty'", null);
    115             }
    116         }
    117         return result;
    118     }
    119
    120     protected void evaluateExtraParams() {
    121         super.evaluateExtraParams();
    122
    123         if (toggle != null) {
    124             addParameter("toggle", findString(toggle));
    125         }
    126
    127         if (treeSelectedTopic != null) {
    128             addParameter("treeSelectedTopic", findString(treeSelectedTopic));
    129         }
    130
    131         if (treeExpandedTopic != null) {
    132             addParameter("treeExpandedTopic", findString(treeExpandedTopic));
    133         }
    134
    135         if (treeCollapsedTopic != null) {
    136             addParameter("treeCollapsedTopic", findString(treeCollapsedTopic));
    137         }
    138
    139         if (rootNodeAttr != null) {
    140             addParameter("rootNode", findValue(rootNodeAttr));
    141         }
    142
    143         if (childCollectionProperty != null) {
    144             addParameter("childCollectionProperty", findString(childCollectionProperty));
    145         }
    146
    147         if (nodeTitleProperty != null) {
    148             addParameter("nodeTitleProperty", findString(nodeTitleProperty));
    149         }
    150
    151         if (nodeIdProperty != null) {
    152             addParameter("nodeIdProperty", findString(nodeIdProperty));
    153         }
    154
    155         if (showRootGrid != null) {
    156             addParameter("showRootGrid", findValue(showRootGrid, Boolean.class));
    157         }
    158
    159
    160         if (showGrid != null) {
    161             addParameter("showGrid", findValue(showGrid, Boolean.class));
    162         }
    163
    164         if (blankIconSrc != null) {
    165             addParameter("blankIconSrc", findString(blankIconSrc));
    166         }
    167
    168         if (gridIconSrcL != null) {
    169             addParameter("gridIconSrcL", findString(gridIconSrcL));
    170         }
    171
    172         if (gridIconSrcV != null) {
    173             addParameter("gridIconSrcV", findString(gridIconSrcV));
    174         }
    175
    176         if (gridIconSrcP != null)  {
    177             addParameter("gridIconSrcP", findString(gridIconSrcP));
    178         }
    179
    180         if (gridIconSrcC != null) {
    181             addParameter("gridIconSrcC", findString(gridIconSrcC));
    182         }
    183
    184         if (gridIconSrcX != null) {
    185             addParameter("gridIconSrcX", findString(gridIconSrcX));
    186         }
    187
    188         if (gridIconSrcY != null) {
    189             addParameter("gridIconSrcY", findString(gridIconSrcY));
    190         }
    191
    192         if (expandIconSrcPlus != null) {
    193             addParameter("expandIconSrcPlus", findString(expandIconSrcPlus));
    194         }
    195
    196         if (expandIconSrcMinus != null) {
    197             addParameter("expandIconSrcMinus", findString(expandIconSrcMinus));
    198         }
    199
    200         if (iconWidth != null) {
    201             addParameter("iconWidth", findValue(iconWidth, Integer.class));
    202         }
    203         if (iconHeight != null) {
    204             addParameter("iconHeight", findValue(iconHeight, Integer.class));
    205         }
    206         if (toggleDuration != null) {
    207             addParameter("toggleDuration", findValue(toggleDuration, Integer.class));
    208         }
    209         if (templateCssPath != null) {
    210             addParameter("templateCssPath", findString(templateCssPath));
    211         }
    212     }
    213
    214     public String getDefaultOpenTemplate() {
    215         return OPEN_TEMPLATE;
    216     }
    217
    218     protected String getDefaultTemplate() {
    219         return TEMPLATE;
    220     }
    221
    222     public String getToggle() {
    223         return toggle;
    224     }
    225
    226     @StrutsTagAttribute(description="The toggle property (either 'explode' or 'fade')", defaultValue="fade")
    227     public void setToggle(String toggle) {
    228         this.toggle = toggle;
    229     }
    230
    231     public String getTreeSelectedTopic() {
    232         return treeSelectedTopic;
    233     }
    234
    235     @StrutsTagAttribute(description="The treeSelectedTopic property")
    236     public void setTreeSelectedTopic(String treeSelectedTopic) {
    237         this.treeSelectedTopic = treeSelectedTopic;
    238     }
    239
    240     public String getTreeExpandedTopic() {
    241         return treeExpandedTopic;
    242     }
    243
    244     @StrutsTagAttribute(description="The treeExpandedTopic property.")
    245     public void setTreeExpandedTopic(String treeExpandedTopic) {
    246         this.treeExpandedTopic = treeExpandedTopic;
    247     }
    248
    249     public String getTreeCollapsedTopic() {
    250         return treeCollapsedTopic;
    251     }
    252
    253     @StrutsTagAttribute(description="The treeCollapsedTopic property.")
    254     public void setTreeCollapsedTopic(String treeCollapsedTopic) {
    255         this.treeCollapsedTopic = treeCollapsedTopic;
    256     }
    257
    258     public String getRootNode() {
    259         return rootNodeAttr;
    260     }
    261
    262     @StrutsTagAttribute(description="The rootNode property.")
    263     public void setRootNode(String rootNode) {
    264         this.rootNodeAttr = rootNode;
    265     }
    266
    267     public String getChildCollectionProperty() {
    268         return childCollectionProperty;
    269     }
    270
    271     @StrutsTagAttribute(description="The childCollectionProperty property.")
    272     public void setChildCollectionProperty(String childCollectionProperty) {
    273         this.childCollectionProperty = childCollectionProperty;
    274     }
    275
    276     public String getNodeTitleProperty() {
    277         return nodeTitleProperty;
    278     }
    279
    280     @StrutsTagAttribute(description="The nodeTitleProperty property.")
    281     public void setNodeTitleProperty(String nodeTitleProperty) {
    282         this.nodeTitleProperty = nodeTitleProperty;
    283     }
    284
    285     public String getNodeIdProperty() {
    286         return nodeIdProperty;
    287     }
    288
    289     @StrutsTagAttribute(description="The nodeIdProperty property.")
    290     public void setNodeIdProperty(String nodeIdProperty) {
    291         this.nodeIdProperty = nodeIdProperty;
    292     }
    293
    294     @StrutsTagAttribute(description="The showRootGrid property (default true).")
    295     public void setShowRootGrid(String showRootGrid) {
    296         this.showRootGrid = showRootGrid;
    297     }
    298
    299     public String getShowRootGrid() {
    300         return showRootGrid;
    301     }
    302
    303     public String getBlankIconSrc() {
    304         return blankIconSrc;
    305     }
    306
    307     @StrutsTagAttribute(description="Blank icon image source.")
    308     public void setBlankIconSrc(String blankIconSrc) {
    309         this.blankIconSrc = blankIconSrc;
    310     }
    311
    312     public String getExpandIconSrcMinus() {
    313         return expandIconSrcMinus;
    314     }
    315
    316     @StrutsTagAttribute(description="Expand icon (-) image source.")
    317     public void setExpandIconSrcMinus(String expandIconSrcMinus) {
    318         this.expandIconSrcMinus = expandIconSrcMinus;
    319     }
    320
    321     public String getExpandIconSrcPlus() {
    322         return expandIconSrcPlus;
    323     }
    324
    325     @StrutsTagAttribute(description="Expand Icon (+) image source.")
    326     public void setExpandIconSrcPlus(String expandIconSrcPlus) {
    327         this.expandIconSrcPlus = expandIconSrcPlus;
    328     }
    329
    330     public String getGridIconSrcC() {
    331         return gridIconSrcC;
    332     }
    333
    334     @StrutsTagAttribute(description="Image source for under child item child icons.")
    335     public void setGridIconSrcC(String gridIconSrcC) {
    336         this.gridIconSrcC = gridIconSrcC;
    337     }
    338
    339     public String getGridIconSrcL() {
    340         return gridIconSrcL;
    341     }
    342
    343
    344     @StrutsTagAttribute(description=" Image source for last child grid.")
    345     public void setGridIconSrcL(String gridIconSrcL) {
    346         this.gridIconSrcL = gridIconSrcL;
    347     }
    348
    349     public String getGridIconSrcP() {
    350         return gridIconSrcP;
    351     }
    352
    353     @StrutsTagAttribute(description="Image source for under parent item child icons.")
    354     public void setGridIconSrcP(String gridIconSrcP) {
    355         this.gridIconSrcP = gridIconSrcP;
    356     }
    357
    358     public String getGridIconSrcV() {
    359         return gridIconSrcV;
    360     }
    361
    362     @StrutsTagAttribute(description="Image source for vertical line.")
    363     public void setGridIconSrcV(String gridIconSrcV) {
    364         this.gridIconSrcV = gridIconSrcV;
    365     }
    366
    367     public String getGridIconSrcX() {
    368         return gridIconSrcX;
    369     }
    370
    371     @StrutsTagAttribute(description="Image source for grid for sole root item.")
    372     public void setGridIconSrcX(String gridIconSrcX) {
    373         this.gridIconSrcX = gridIconSrcX;
    374     }
    375
    376     public String getGridIconSrcY() {
    377         return gridIconSrcY;
    378     }
    379
    380     @StrutsTagAttribute(description="Image source for grid for last root item.")
    381     public void setGridIconSrcY(String gridIconSrcY) {
    382         this.gridIconSrcY = gridIconSrcY;
    383     }
    384
    385     public String getIconHeight() {
    386         return iconHeight;
    387     }
    388
    389
    390     @StrutsTagAttribute(description="Icon height", defaultValue="18px")
    391     public void setIconHeight(String iconHeight) {
    392         this.iconHeight = iconHeight;
    393     }
    394
    395     public String getIconWidth() {
    396         return iconWidth;
    397     }
    398
    399     @StrutsTagAttribute(description="Icon width", defaultValue="19px")
    400     public void setIconWidth(String iconWidth) {
    401         this.iconWidth = iconWidth;
    402     }
    403
    404
    405
    406     public String getTemplateCssPath() {
    407         return templateCssPath;
    408     }
    409
    410     @StrutsTagAttribute(description="Template css path", defaultValue="{contextPath}/struts/tree.css.")
    411     public void setTemplateCssPath(String templateCssPath) {
    412         this.templateCssPath = templateCssPath;
    413     }
    414
    415     public String getToggleDuration() {
    416         return toggleDuration;
    417     }
    418
    419     @StrutsTagAttribute(description="Toggle duration in milliseconds", defaultValue="150")
    420     public void setToggleDuration(String toggleDuration) {
    421         this.toggleDuration = toggleDuration;
    422     }
    423
    424     public String getShowGrid() {
    425         return showGrid;
    426     }
    427
    428     @StrutsTagAttribute(description="Show grid", type="Boolean", defaultValue="true")
    429     public void setShowGrid(String showGrid) {
    430         this.showGrid = showGrid;
    431     }
    432 }
    433