Advertisement
zinch

Dynamic Tree View II

Feb 18th, 2014
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.49 KB | None | 0 0
  1. package stackoverflow;
  2.  
  3. import java.util.*;
  4.  
  5. import javafx.application.Application;
  6. import javafx.event.EventHandler;
  7. import javafx.scene.Scene;
  8. import javafx.scene.control.TreeItem;
  9. import javafx.scene.control.TreeItem.TreeModificationEvent;
  10. import javafx.scene.control.TreeView;
  11. import javafx.scene.layout.VBox;
  12. import javafx.stage.Stage;
  13.  
  14. public class DynamicTreeViewWaiter extends Application {
  15.  
  16.     private EventHandler<TreeModificationEvent<DynamicTreeNodeModel>> branchExpandedEventHandler;
  17.  
  18.     /**
  19.      * @param args the command line arguments
  20.      */
  21.     public static void main(String[] args) {
  22.         Application.launch(DynamicTreeViewWaiter.class, args);
  23.     }
  24.  
  25.     @Override
  26.     public void start(Stage primaryStage) {
  27.         primaryStage.setTitle("Example Dynamic Tree");
  28.         primaryStage.setResizable(true);
  29.         final VBox box = new VBox();
  30.         box.setFillWidth(false);
  31.         Scene scene = new Scene(box);
  32.         primaryStage.setScene(scene);
  33.         box.getChildren().add(this.getExampleTree());
  34.         primaryStage.show();
  35.     }
  36.  
  37.     private TreeView<DynamicTreeNodeModel> getExampleTree() {
  38.  
  39.         final TreeView<DynamicTreeNodeModel> treeView = new TreeView<DynamicTreeNodeModel>();
  40.         treeView.setPrefSize(1000, 750);
  41.  
  42.         branchExpandedEventHandler = new EventHandler<TreeModificationEvent<DynamicTreeNodeModel>>() {
  43.             public void handle(TreeModificationEvent<DynamicTreeNodeModel> event) {
  44. //                System.out.println("handling event " + event);
  45.                 TreeItem<DynamicTreeNodeModel> item = event.getTreeItem();
  46.                 populateTreeItem(item);
  47.             }
  48.         };
  49.  
  50.         DynamicTreeNodeModel rootNode = new MockTreeModel("Mock root");
  51.         MockTreeModel childNode = new MockTreeModel("Mock child");
  52.         rootNode.getChildren().add(childNode);
  53.  
  54.         final TreeItem rootItem = new TreeItem(rootNode);
  55.         rootItem.getChildren().add(new TreeItem<DynamicTreeNodeModel>(childNode));
  56.  
  57.         rootItem.addEventHandler(TreeItem.branchExpandedEvent(), new EventHandler<TreeModificationEvent<DynamicTreeNodeModel>>() {
  58.             @Override
  59.             public void handle(TreeModificationEvent<DynamicTreeNodeModel> dynamicTreeNodeModelTreeModificationEvent) {
  60.  
  61.                 DynamicTreeNodeModel rootNode = new RandomDynamicTreeNodeModel(null, "Root Node");
  62.                 TreeItem rootItem = new TreeItem(rootNode);
  63.                 rootItem.addEventHandler(TreeItem.branchExpandedEvent(), branchExpandedEventHandler);
  64.  
  65.                 treeView.setRoot(rootItem);
  66.                 populateTreeItem(rootItem);
  67.             }
  68.         });
  69.  
  70.         treeView.setShowRoot(true);
  71.         treeView.setRoot(rootItem);
  72.         rootItem.setExpanded(true);
  73. //        treeView.setCellFactory(new LearningTreeCellFactory());
  74.         return treeView;
  75.     }
  76.  
  77.     private void populateTreeItem(TreeItem<DynamicTreeNodeModel> item) {
  78.         DynamicTreeNodeModel node = item.getValue();
  79.         boolean isPopulated = node.isPopulated();
  80.         boolean areGrandChildrenPopulated = node.areChildenPopulated();
  81.         node.populateToDepth(2);
  82.         if (!isPopulated) {
  83.             for (DynamicTreeNodeModel childNode : node.getChildren()) {
  84.                 TreeItem childItem = new TreeItem(childNode);
  85.                 childItem.addEventHandler(TreeItem.branchExpandedEvent(), branchExpandedEventHandler);
  86.                 item.getChildren().add(childItem);
  87.             }
  88.         }
  89.         if (!areGrandChildrenPopulated) {
  90.             int i = 0;
  91.             for (TreeItem childItem : item.getChildren()) {
  92.                 // get cooresponding node in the model
  93.                 DynamicTreeNodeModel childNode = node.getChildren().get(i);
  94.                 i++;
  95.                 for (DynamicTreeNodeModel grandChildNode : childNode.getChildren()) {
  96.                     TreeItem grandChildItem = new TreeItem(grandChildNode);
  97.                     grandChildItem.addEventHandler(TreeItem.branchExpandedEvent(), branchExpandedEventHandler);
  98.                     childItem.getChildren().add(grandChildItem);
  99.                 }
  100.             }
  101.         }
  102.     }
  103.  
  104.     private static interface DynamicTreeNodeModel {
  105.  
  106.         public String getName();
  107.  
  108.         public void setName(String name);
  109.  
  110.         public boolean isPopulated();
  111.  
  112.         public boolean areChildenPopulated();
  113.  
  114.         public List<DynamicTreeNodeModel> getChildren();
  115.  
  116.         public void setChildren(List<DynamicTreeNodeModel> children);
  117.  
  118.         public DynamicTreeNodeModel getParent();
  119.  
  120.         public void setParent(DynamicTreeNodeModel parent);
  121.  
  122.         public void populateToDepth(int depth);
  123.  
  124.         @Override
  125.         public String toString();
  126.     }
  127.  
  128.     private static class RandomDynamicTreeNodeModel implements DynamicTreeNodeModel {
  129.  
  130.         private DynamicTreeNodeModel parent;
  131.         private String name;
  132.         private List<DynamicTreeNodeModel> children = null;
  133.  
  134.         public RandomDynamicTreeNodeModel(DynamicTreeNodeModel parent, String name) {
  135.             this.parent = parent;
  136.             this.name = name;
  137.         }
  138.  
  139.         @Override
  140.         public String getName() {
  141.             return name;
  142.         }
  143.  
  144.         @Override
  145.         public void setName(String name) {
  146.             this.name = name;
  147.         }
  148.  
  149.         @Override
  150.         public boolean isPopulated() {
  151.             if (children == null) {
  152.                 return false;
  153.             }
  154.             return true;
  155.         }
  156.  
  157.         @Override
  158.         public boolean areChildenPopulated() {
  159.             if (!this.isPopulated()) {
  160.                 return false;
  161.             }
  162.             for (DynamicTreeNodeModel child : this.children) {
  163.                 if (!child.isPopulated()) {
  164.                     return false;
  165.                 }
  166.             }
  167.             return true;
  168.         }
  169.  
  170.         @Override
  171.         public List<DynamicTreeNodeModel> getChildren() {
  172.             return children;
  173.         }
  174.  
  175.         @Override
  176.         public void setChildren(List<DynamicTreeNodeModel> children) {
  177.             this.children = children;
  178.         }
  179.  
  180.         @Override
  181.         public DynamicTreeNodeModel getParent() {
  182.             return parent;
  183.         }
  184.  
  185.         @Override
  186.         public void setParent(DynamicTreeNodeModel parent) {
  187.             this.parent = parent;
  188.         }
  189.         private static Random random = new Random();
  190.  
  191.         @Override
  192.         public void populateToDepth(int depth) {
  193.             if (depth <= 0) {
  194.                 return;
  195.             }
  196.             if (children == null) {
  197.                 int num = random.nextInt(5);
  198.                 System.out.println("got a random number " + num);
  199.                 children = new ArrayList(num);
  200.                 for (int i = 0; i < num; i++) {
  201.                     children.add(new RandomDynamicTreeNodeModel(this, "child " + i));
  202.                 }
  203.             }
  204.             int childDepth = depth - 1;
  205.             for (DynamicTreeNodeModel child : children) {
  206.                 child.populateToDepth(childDepth);
  207.             }
  208.         }
  209.  
  210.         @Override
  211.         public String toString() {
  212.             return this.name;
  213.         }
  214.     }
  215.  
  216.     private class MockTreeModel implements DynamicTreeNodeModel {
  217.  
  218.         private DynamicTreeNodeModel parent;
  219.         private String name = "mock";
  220.         private List<DynamicTreeNodeModel> children = new LinkedList<>();
  221.  
  222.         private MockTreeModel() {
  223.         }
  224.  
  225.         private MockTreeModel(String name) {
  226.  
  227.             this.name = name;
  228.         }
  229.  
  230.         @Override
  231.         public String getName() { return name; }
  232.  
  233.         @Override
  234.         public void setName(String name) {}
  235.  
  236.         @Override
  237.         public boolean isPopulated() { return !children.isEmpty(); }
  238.  
  239.         @Override
  240.         public boolean areChildenPopulated() {
  241.             return false;
  242.         }
  243.  
  244.         @Override
  245.         public List<DynamicTreeNodeModel> getChildren() { return children; }
  246.  
  247.         @Override
  248.         public void setChildren(List<DynamicTreeNodeModel> children) { this.children = children; }
  249.  
  250.         @Override
  251.         public DynamicTreeNodeModel getParent() { return parent; }
  252.  
  253.         @Override
  254.         public void setParent(DynamicTreeNodeModel parent) { this.parent = parent; }
  255.  
  256.         @Override
  257.         public void populateToDepth(int depth) { }
  258.  
  259.         @Override
  260.         public String toString() {
  261.             return "MockTreeModel{" +
  262.                     "name='" + name + '\'' +
  263.                     '}';
  264.         }
  265.     }
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement