<%@ page import="com.jenkov.prizetags.tree.impl.TreeNode,
                 com.jenkov.prizetags.tree.impl.Tree,
                 com.jenkov.prizetags.tree.itf.*"%>
<%@ taglib uri="/WEB-INF/treetag.tld" prefix="tree" %>
<%@ taglib uri="/WEB-INF/requesttags.tld" prefix="request" %>
<%
    //this Java code should be replaced by code that builds the tree from file system, xml document,
    //database etc. It could also be moved to a Struts action or Spring controller.
    if(session.getAttribute("tree.model") == null){

        //creating a simple tree model instance
        ITree       tree          = new Tree();
        tree.setSingleSelectionMode(true);

        //adding server side event listeners to the tree model instance
        tree.addExpandListener(new IExpandListener(){
           public void nodeExpanded(ITreeNode node, ITree tree){
               System.out.println("node " + node.getName() + " was expanded");
           }
        });

        tree.addCollapseListener(new ICollapseListener(){
           public void nodeCollapsed(ITreeNode node, ITree tree){
               System.out.println("node " + node.getName() + " was collapsed");
           }
        });

        tree.addSelectListener(new ISelectListener(){
            public void nodeSelected(ITreeNode node, ITree tree){
                System.out.println("node " + node.getName() + " was active");
            }
        });

        tree.addUnSelectListener(new IUnSelectListener(){
            public void nodeUnselected(ITreeNode node, ITree tree){
                System.out.println("node " + node.getName() + " was unselected");
            }
        });

        //adding tree nodes in a tree structure to the tree model instance.
        ITreeNode   movies        = new TreeNode("moviesId"     , "Reports"       , "movies");
        ITreeNode   thrillers     = new TreeNode("thrillersId"  , "Finance"    , "thrillers");
        ITreeNode   thrillersNew  = new TreeNode("newThrillId"  , "New Reports", "newThrill");
        ITreeNode   thrillersOld  = new TreeNode("oldThrillId"  , "Old Reports", "oldThrill");
        ITreeNode   fantasy       = new TreeNode("fantasyId"    , "Production"      , "fantasy");
        ITreeNode   comedy        = new TreeNode("comedyId"     , "Research"     , "comedies");
        ITreeNode   cartoons      = new TreeNode("cartoonsId"   , "MIS"     , "cartoons");
        ITreeNode   comedySub     = new TreeNode("comedySubId"    , "BI"   , "comedies");
        ITreeNode   comedySubSub  = new TreeNode("comedySubSubId" , "BI-on-ER","comedies");

        ITreeNode   basicInstinct = new TreeNode("basicInstinctId"  , "Report1"   , "thriller");
        ITreeNode   theFirm       = new TreeNode("theFirmId"        , "Report2"         , "thriller");
        ITreeNode   lordOfTheRings= new TreeNode("lordOfTheRingsId" , "Some report", "fantasy");
        ITreeNode   dumbNDumber   = new TreeNode("dumbNDumberId"    , "Another Report"    , "comedy");
        ITreeNode   lionKing      = new TreeNode("lionKingId"       , "Simple Report"        , "cartoon");
        ITreeNode   snowWhite     = new TreeNode("snowWhiteId"      , "Multi section report"       , "cartoon");

        //Tool Tips on some nodes...
        movies.setToolTip("Check out these hot thrillers & comedies!");

        movies.addChild(thrillers);
        movies.addChild(fantasy);
        movies.addChild(comedy);
        movies.addChild(cartoons);

        thrillers.addChild(thrillersNew);
        thrillers.addChild(thrillersOld);
        thrillersNew.addChild(theFirm);
        thrillersOld.addChild(basicInstinct);
        fantasy  .addChild(lordOfTheRings);
        cartoons .addChild(lionKing);
        cartoons .addChild(snowWhite);
        //comedy   .addChild(dumbNDumber);
        comedy   .addChild(comedySub);
        comedySub.addChild(comedySubSub);

        tree.setRoot(movies);
        tree.expand(movies.getId());
        tree.expand(cartoons.getId());

        tree.select(fantasy.getId());

        //attaching an object to a tree node. The attached object
        //can also be used when generating the HTML for the tree.
        thrillers.setObject("Attached Object");

        //storing the tree model instance in the session.
        //this is where the tags get the tree model from later.
        session.setAttribute("tree.model", tree);
    }
%>
<html>
<head>
    <link rel="stylesheet" href="stylesheet.css" type="text/css">
</head>

<body>
<%-- adding client side event listeners.
     If your tree is big, and slow to generate
     it can be a good idea to move these event listeners to the top of the JSP page,
     so they are the first thing run. That way, if you use the event listeners to
     reload another frame, like they do here, the other frame will be asked to
     reload itself as soon as possible. This will make the application feel faster.
     Otherwise the frame-reload command is not executed until after the whole
     tree is generated, making the frame-reload feel slower.
--%>
<tree:onExpand>
  <script>top.window.main.location.href="detail.jsp?expand=<tree:expandedNodeId />"; </script>
</tree:onExpand>

<tree:onCollapse>
  <script>top.window.main.location.href="detail.jsp?collapse=<tree:collapsedNodeId />"; </script>
</tree:onCollapse>

<tree:onSelect>
  <script>top.window.main.location.href="detail.jsp?select=<tree:selectedNodeId />"; </script>
</tree:onSelect>

<tree:onUnselect>
  <script>top.window.main.location.href="detail.jsp?unselect=<tree:unselectedNodeId />"; </script>
</tree:onUnselect>



<%-- Generating the Tree HTML --%>
<table cellspacing="0" cellpadding="0" border="0">
<tree:tree tree="tree.model" node="tree.node" >
    <tr><td
    ><table cellspacing="0" cellpadding="0" border="0">
    <tr><td><tree:nodeIndent    node="tree.node" indentationType="type"><tree:nodeIndentVerticalLine indentationType="type" ><img src="images/verticalLine.gif"></tree:nodeIndentVerticalLine><tree:nodeIndentBlankSpace   indentationType="type" ><img src="images/blankSpace.gif"></tree:nodeIndentBlankSpace></tree:nodeIndent></td>
    <tree:nodeMatch    node="tree.node" expanded="false" hasChildren="true"  isLastChild="false"><td><a href="<request:requestUri/>?expand=<tree:nodeId node="tree.node"/>"><img src="images/collapsedMidNode.gif" border="0"></a><img src="images/closedFolder.gif"></td></tree:nodeMatch>
    <tree:nodeMatch    node="tree.node" expanded="true"  hasChildren="true"  isLastChild="false"><td><a href="<request:requestUri/>?collapse=<tree:nodeId node="tree.node"/>"><img src="images/expandedMidNode.gif"  border="0"></a><img src="images/openFolder.gif"></td></tree:nodeMatch>
    <tree:nodeMatch    node="tree.node" expanded="false" hasChildren="true"  isLastChild="true" ><td><a href="<request:requestUri/>?expand=<tree:nodeId node="tree.node"/>"><img src="images/collapsedLastNode.gif"  border="0"></a><img src="images/closedFolder.gif"></td></tree:nodeMatch>
    <tree:nodeMatch    node="tree.node" expanded="true"  hasChildren="true"  isLastChild="true" ><td><a href="<request:requestUri/>?collapse=<tree:nodeId node="tree.node"/>"><img src="images/expandedLastNode.gif" border="0"></a><img src="images/openFolder.gif"></td></tree:nodeMatch>
    <tree:nodeMatch    node="tree.node" expanded="false" hasChildren="false" isLastChild="false"><td><img src="images/noChildrenMidNode.gif"><img src="images/nonFolder.gif"></td></tree:nodeMatch>
    <tree:nodeMatch    node="tree.node" expanded="false" hasChildren="false" isLastChild="true" ><td><img src="images/noChildrenLastNode.gif"><img src="images/nonFolder.gif"></td></tree:nodeMatch>

    <td valign="top">
    <tree:nodeMatch node="tree.node" selected="true"><span style="Font-Size: 12px;"><b><tree:nodeName      node="tree.node"/></b></span></tree:nodeMatch>
    <tree:nodeMatch node="tree.node" selected="false"><a title="<tree:nodeToolTip node="tree.node" />" href="<request:requestUri/>?select=<tree:nodeId node="tree.node"/>"><span style="Font-Size: 12px;"><tree:nodeName      node="tree.node"/></span></a></tree:nodeMatch>

    <%-- using the attached object of a node.
         The node object is now available in the http request attribute with the
         name given in the detachedObject attribute. You can then use the JSTL
         or Struts <bean:write id="theNodeObject" /> etc. tags to display information from the attached
         object.
    --%>
    <tree:detachNodeObject node="tree.node" detachedObject="theNodeObject"/>
    <%  if(request.getAttribute("theNodeObject") != null){
           out.print(request.getAttribute("theNodeObject"));
        }
    %>

    </td>
    </tr>
    </table></td></tr>
</tree:tree>
</table>

</body>

</html>