<%@ 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" %>
<%@ taglib uri="/WEB-INF/ajaxtags.tld" prefix="ajax" %>
<%
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Pragma", "no-cache");
%>

<%
    //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"     , "Movies"       , "movies");
        ITreeNode   thrillers     = new TreeNode("thrillersId"  , "Thrillers"    , "thrillers");
        ITreeNode   thrillersNew  = new TreeNode("newThrillId"  , "New Thrillers", "newThrill");
        ITreeNode   thrillersOld  = new TreeNode("oldThrillId"  , "Old Thrillers", "oldThrill");
        ITreeNode   fantasy       = new TreeNode("fantasyId"    , "Fantasy"      , "fantasy");
        ITreeNode   comedy        = new TreeNode("comedyId"     , "Comedies"     , "comedies");
        ITreeNode   cartoons      = new TreeNode("cartoonsId"   , "Cartoons"     , "cartoons");
        ITreeNode   comedySub     = new TreeNode("comedySubId"    , "Comedy Sub"   , "comedies");
        ITreeNode   comedySubSub  = new TreeNode("comedySubSubId" , "Comedy Sub Sub","comedies");

        ITreeNode   basicInstinct = new TreeNode("basicInstinctId"  , "Basic Instinct"   , "thriller");
        ITreeNode   theFirm       = new TreeNode("theFirmId"        , "The Firm"         , "thriller");
        ITreeNode   lordOfTheRings= new TreeNode("lordOfTheRingsId" , "Lord of The Rings", "fantasy");
        ITreeNode   dumbNDumber   = new TreeNode("dumbNDumberId"    , "Dumb'n Dumber"    , "comedy");
        ITreeNode   lionKing      = new TreeNode("lionKingId"       , "Lion King"        , "cartoon");
        ITreeNode   snowWhite     = new TreeNode("snowWhiteId"      , "Snow White"       , "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);
    }
%>
<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><ajax:link targetElement="treeCell"><a href="treeAjaxTree.jsp?expand=<tree:nodeId node="tree.node"/>"><img src="../images/collapsedMidNode.gif" border="0"></a></ajax:link><img src="../images/closedFolder.gif"></td></tree:nodeMatch>
    <tree:nodeMatch    node="tree.node" expanded="true"  hasChildren="true"  isLastChild="false"><td><ajax:link targetElement="treeCell"><a href="treeAjaxTree.jsp?collapse=<tree:nodeId node="tree.node"/>"><img src="../images/expandedMidNode.gif"  border="0"></a></ajax:link><img src="../images/openFolder.gif"></td></tree:nodeMatch>
    <tree:nodeMatch    node="tree.node" expanded="false" hasChildren="true"  isLastChild="true" ><td><ajax:link targetElement="treeCell"><a href="treeAjaxTree.jsp?expand=<tree:nodeId node="tree.node"/>"><img src="../images/collapsedLastNode.gif"  border="0"></a></ajax:link><img src="../images/closedFolder.gif"></td></tree:nodeMatch>
    <tree:nodeMatch    node="tree.node" expanded="true"  hasChildren="true"  isLastChild="true" ><td><ajax:link targetElement="treeCell"><a href="treeAjaxTree.jsp?collapse=<tree:nodeId node="tree.node"/>"><img src="../images/expandedLastNode.gif" border="0"></a></ajax:link><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"><ajax:link targetElement="detailCell"><a title="<tree:nodeToolTip node="tree.node" />" href="treeAjaxDetail.jsp?select=<tree:nodeId node="tree.node"/>"><span style="Font-Size: 12px;"><tree:nodeName      node="tree.node"/></span></a></ajax:link></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>
