1 package com.sri.emo.controller;
2
3 import com.jcorporate.expresso.core.controller.ExpressoResponse;
4 import com.sri.common.taglib.TreeNode;
5 import com.sri.emo.dbobj.Node;
6 import com.sri.emo.dbobj.model_tree.DefaultModelFactory;
7 import com.sri.emo.dbobj.model_tree.Model;
8
9 /***
10 * A factory for building the data structure needed for a tree view. It does
11 * so by first building a 'tree' model of the database node, and then using
12 * a visitor to transform it into something useful for the TreeView
13 * control.
14 *
15 * @author Michael Rimov
16 */
17 public class TreeViewFactory {
18
19 /***
20 * Root node instance to the hierarchy.
21 */
22 private final Node root;
23
24 /***
25 * ControllerResponse used to set for Transitions.
26 */
27 private final ExpressoResponse response;
28
29 /***
30 * Maximum levels to recurse.
31 */
32 private final int MAX_LEVELS;
33
34 /***
35 * Constructs a tree view factory.
36 *
37 * @param rootNode Node
38 * @param response ControllerResponse
39 * @param maxNestingLevels int
40 */
41 public TreeViewFactory(final Node rootNode, final ExpressoResponse response, int maxNestingLevels) {
42 super();
43 assert maxNestingLevels > 0;
44 assert rootNode != null;
45 assert response != null;
46
47 MAX_LEVELS = maxNestingLevels;
48 this.response = response;
49 root = rootNode;
50 }
51
52 /***
53 * Builds a Tree for the Tree node. It is the callers responsibility to
54 * save the Nodes to the request, session or wherever.
55 *
56 * @return TreeNode the tree node.
57 */
58 public TreeNode buildTree() {
59 DefaultModelFactory modelFactory = new DefaultModelFactory(root, response, MAX_LEVELS);
60 Model model = modelFactory.buildModel();
61 TreeViewVisitor visitor = new TreeViewVisitor(response);
62
63 return visitor.traverseModelTree(model);
64 }
65
66
67 }