Advertisement
zinch

Dynamic Tree View I

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