View Javadoc

1   /**
2    * This file is part of the equanda project.
3    *
4    * The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at http://www.mozilla.org/MPL/
7    *
8    * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
9    * ANY KIND, either express or implied. See the License for the specific language governing rights and
10   * limitations under the License.
11   *
12   * Alternatively, the contents of this file may be used under the terms of
13   * either the GNU General Public License Version 2 or later (the "GPL"), or
14   * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
15   * in which case the provisions of the GPL or the LGPL are applicable instead
16   * of those above. If you wish to allow use of your version of this file only
17   * under the terms of either the GPL or the LGPL, and not to allow others to
18   * use your version of this file under the terms of the MPL, indicate your
19   * decision by deleting the provisions above and replace them with the notice
20   * and other provisions required by the GPL or the LGPL. If you do not delete
21   * the provisions above, a recipient may use your version of this file under
22   * the terms of any one of the MPL, the GPL or the LGPL.
23   */
24  
25  package org.equanda.tapestry5.components;
26  
27  import org.apache.tapestry5.annotations.IncludeJavaScriptLibrary;
28  import org.apache.tapestry5.annotations.IncludeStylesheet;
29  import org.apache.tapestry5.annotations.Parameter;
30  import org.apache.tapestry5.annotations.SupportsInformalParameters;
31  import org.equanda.tapestry5.base.TitleContent;
32  import org.equanda.tapestry5.data.FlattenedTree;
33  import org.equanda.tapestry5.data.Tree;
34  
35  import java.util.ArrayList;
36  import java.util.List;
37  
38  /**
39   * TreeTable is a tapestry 5 component class. It is based upon <a href="http://sstree.tigris.org/">sstree</a>
40   *
41   * @author Geert Mergan
42   * @author <a href="mailto:joachim@progs.be">Joachim Van der Auwera</a>
43   */
44  @SupportsInformalParameters
45  @IncludeStylesheet( "classpath:/org/equanda/tapestry5/resources/treetable.css" )
46  @IncludeJavaScriptLibrary( { "${tapestry.scriptaculous}/prototype.js",
47                               "classpath:/org/equanda/tapestry5/resources/t5equanda.js" } )
48  public class TreeTable
49      extends TitleContent
50  {
51      /** Defines the source Tree to walk over. */
52      @Parameter( required = true )
53      private List<Tree> tree;
54  
55      @Parameter( required = true )
56      private Tree value;
57  
58      @Parameter()
59      private int unfoldLevel;
60  
61      private List<FlattenedTree> flattenedTreeList;
62  
63      /** Current node of the tree. */
64      private FlattenedTree flatValue;
65  
66      public boolean isUsingCookies()
67      {
68          return false; // no persistence 
69      }
70  
71      public FlattenedTree getFlatValue()
72      {
73          return flatValue;
74      }
75  
76      public void setFlatValue( FlattenedTree flatValue )
77      {
78          this.flatValue = flatValue;
79          value = flatValue.getTree();
80      }
81  
82      public List<FlattenedTree> getList()
83      {
84          if ( null == flattenedTreeList )
85          {
86              flattenedTreeList = new ArrayList<FlattenedTree>();
87              if ( null != tree )
88              {
89                  int i = 0;
90                  for ( Tree node : tree )
91                  {
92                      buildSourceList( flattenedTreeList, node, 0, Integer.toString( i ) );
93                      i++;
94                  }
95              }
96          }
97          return flattenedTreeList;
98      }
99  
100     public void setList( List<FlattenedTree> source )
101     {
102         this.flattenedTreeList = source;
103     }
104 
105     private void buildSourceList( List<FlattenedTree> list, Tree treeNode, int depth, String dotId )
106     {
107         list.add( new FlattenedTree( treeNode, depth, dotId ) );
108         List<Tree> children = treeNode.getChildren();
109         if ( children != null )
110         {
111             int i = 0;
112             for ( Tree node : children )
113             {
114                 // TODO check if stopping recursion works.. probably needs equals and hashCode method on FlattenedTree
115                 if ( !list.contains( node ) )
116                 {
117                     buildSourceList( list, node, depth + 1, dotId + "-" + i );
118                     i++;
119                 }
120             }
121         }
122     }
123 
124     public String getRowStyle()
125     {
126         if ( flatValue != null && flatValue.getDepth() > unfoldLevel ) return "display: none;";
127         return "";
128     }
129 
130     public String getFolderStyle()
131     {
132         if ( flatValue != null && flatValue.getDepth() > unfoldLevel ) return "folderclose";
133         return "folderopen";
134     }
135 }