| HierarchyJTreeDemo.java |
1 /****************************************************************************
2 **
3 ** This file is part of yFiles-2.9.
4 **
5 ** yWorks proprietary/confidential. Use is subject to license terms.
6 **
7 ** Redistribution of this file or of an unauthorized byte-code version
8 ** of this file is strictly forbidden.
9 **
10 ** Copyright (c) 2000-2011 by yWorks GmbH, Vor dem Kreuzberg 28,
11 ** 72070 Tuebingen, Germany. All rights reserved.
12 **
13 ***************************************************************************/
14 package demo.view.hierarchy;
15
16 import y.view.Graph2D;
17 import y.view.hierarchy.DefaultNodeChangePropagator;
18 import y.view.hierarchy.HierarchyJTree;
19 import y.view.hierarchy.HierarchyManager;
20 import y.view.hierarchy.HierarchyTreeModel;
21 import y.view.hierarchy.HierarchyTreeTransferHandler;
22
23 import javax.swing.JScrollPane;
24 import javax.swing.JSplitPane;
25 import javax.swing.JTree;
26 import java.awt.BorderLayout;
27 import java.awt.Dimension;
28 import java.awt.EventQueue;
29 import java.util.Locale;
30
31 /**
32 * This demo shows how to use class {@link y.view.hierarchy.HierarchyJTree} to display the hierarchical structure.
33 *
34 * HierarchyJTree provides a different view on the graph structure, as well as (optionally) navigational actions
35 * and support for changes in the hierarchical structure.
36 * @see <a href="http://docs.yworks.com/yfiles/doc/developers-guide/hier_mvc_view.html">Section View Implementations</a> in the yFiles for Java Developer's Guide
37 */
38 public class HierarchyJTreeDemo extends GroupingDemo {
39
40 /**
41 * Instantiates this demo. Builds the GUI.
42 */
43 public HierarchyJTreeDemo() {
44 JTree tree = configureHierarchyJTree();
45
46 //plug the gui elements together and add them to the pane
47 JScrollPane scrollPane = new JScrollPane(tree);
48 scrollPane.setPreferredSize(new Dimension(150, 0));
49 scrollPane.setMinimumSize(new Dimension(150, 0));
50 JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, scrollPane, view);
51 view.fitContent();
52 contentPane.add(splitPane, BorderLayout.CENTER);
53 }
54
55 protected JTree configureHierarchyJTree() {
56 Graph2D rootGraph = view.getGraph2D();
57
58 //propagates text label changes on nodes as change events
59 //on the hierarchy.
60 rootGraph.addGraph2DListener(new DefaultNodeChangePropagator());
61
62 //create a TreeModel, that represents the hierarchy of the nodes.
63 HierarchyManager hierarchy = getHierarchyManager();
64 HierarchyTreeModel htm = new HierarchyTreeModel(hierarchy);
65
66 //use a convenience comparator that sorts the elements in the tree model
67 //folder/group nodes will come before normal nodes
68 htm.setChildComparator(HierarchyTreeModel.createNodeStateComparator(hierarchy));
69
70 //display the graph hierarchy in a special JTree using the given TreeModel
71 JTree tree = new HierarchyJTree(hierarchy, htm);
72
73 //add a navigational action to the tree - when double clicking a node in the tree,
74 //it will be centered in the view (if necessary navigating into an inner graph of a folder node)
75 tree.addMouseListener(new HierarchyJTreeDoubleClickListener(view));
76
77 //add drag and drop functionality to HierarchyJTree. The drag and drop gesture
78 //will allow to reorganize the group structure using HierarchyJTree.
79 tree.setDragEnabled(true);
80 tree.setTransferHandler(new HierarchyTreeTransferHandler(hierarchy));
81 return tree;
82 }
83
84 /**
85 * Launches this demo.
86 */
87 public static void main(String[] args) {
88 EventQueue.invokeLater(new Runnable() {
89 public void run() {
90 Locale.setDefault(Locale.ENGLISH);
91 initLnF();
92 (new HierarchyJTreeDemo()).start();
93 }
94 });
95 }
96 }
97