| HierarchyJTreeDemo.java |
1 /****************************************************************************
2 * This demo file is part of yFiles for Java 2.14.
3 * Copyright (c) 2000-2017 by yWorks GmbH, Vor dem Kreuzberg 28,
4 * 72070 Tuebingen, Germany. All rights reserved.
5 *
6 * yFiles demo files exhibit yFiles for Java functionalities. Any redistribution
7 * of demo files in source code or binary form, with or without
8 * modification, is not permitted.
9 *
10 * Owners of a valid software license for a yFiles for Java version that this
11 * demo is shipped with are allowed to use the demo source code as basis
12 * for their own yFiles for Java powered applications. Use of such programs is
13 * governed by the rights and conditions as set out in the yFiles for Java
14 * license agreement.
15 *
16 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
19 * NO EVENT SHALL yWorks BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 ***************************************************************************/
28 package demo.view.hierarchy;
29
30 import y.view.Graph2D;
31 import y.view.hierarchy.DefaultNodeChangePropagator;
32 import y.view.hierarchy.HierarchyJTree;
33 import y.view.hierarchy.HierarchyManager;
34 import y.view.hierarchy.HierarchyTreeModel;
35 import y.view.hierarchy.HierarchyTreeTransferHandler;
36
37 import javax.swing.JScrollPane;
38 import javax.swing.JSplitPane;
39 import javax.swing.JTree;
40 import java.awt.BorderLayout;
41 import java.awt.Dimension;
42 import java.awt.EventQueue;
43 import java.util.Locale;
44
45 /**
46 * This demo shows how to use class {@link y.view.hierarchy.HierarchyJTree} to display the hierarchical structure.
47 *
48 * HierarchyJTree provides a different view on the graph structure, as well as (optionally) navigational actions
49 * and support for changes in the hierarchical structure.
50 * @see <a href="http://docs.yworks.com/yfiles/doc/api/index.html#/dguide/hier_mvc_view" target="_blank">Section View Implementations</a> in the yFiles for Java Developer's Guide
51 */
52 public class HierarchyJTreeDemo extends GroupingDemo {
53
54 /**
55 * Instantiates this demo. Builds the GUI.
56 */
57 public HierarchyJTreeDemo() {
58 JTree tree = configureHierarchyJTree();
59
60 //plug the gui elements together and add them to the pane
61 JScrollPane scrollPane = new JScrollPane(tree);
62 scrollPane.setPreferredSize(new Dimension(150, 0));
63 scrollPane.setMinimumSize(new Dimension(150, 0));
64 JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, scrollPane, view);
65 view.fitContent();
66 contentPane.add(splitPane, BorderLayout.CENTER);
67 }
68
69 protected JTree configureHierarchyJTree() {
70 Graph2D rootGraph = view.getGraph2D();
71
72 //propagates text label changes on nodes as change events
73 //on the hierarchy.
74 rootGraph.addGraph2DListener(new DefaultNodeChangePropagator());
75
76 //create a TreeModel, that represents the hierarchy of the nodes.
77 HierarchyManager hierarchy = getHierarchyManager();
78 HierarchyTreeModel htm = new HierarchyTreeModel(hierarchy);
79
80 //use a convenience comparator that sorts the elements in the tree model
81 //folder/group nodes will come before normal nodes
82 htm.setChildComparator(HierarchyTreeModel.createNodeStateComparator(hierarchy));
83
84 //display the graph hierarchy in a special JTree using the given TreeModel
85 JTree tree = new HierarchyJTree(hierarchy, htm);
86
87 //add a navigational action to the tree - when double clicking a node in the tree,
88 //it will be centered in the view (if necessary navigating into an inner graph of a folder node)
89 tree.addMouseListener(new HierarchyJTreeDoubleClickListener(view));
90
91 //add drag and drop functionality to HierarchyJTree. The drag and drop gesture
92 //will allow to reorganize the group structure using HierarchyJTree.
93 tree.setDragEnabled(true);
94 tree.setTransferHandler(new HierarchyTreeTransferHandler(hierarchy));
95 return tree;
96 }
97
98 /**
99 * Launches this demo.
100 */
101 public static void main(String[] args) {
102 EventQueue.invokeLater(new Runnable() {
103 public void run() {
104 Locale.setDefault(Locale.ENGLISH);
105 initLnF();
106 (new HierarchyJTreeDemo()).start();
107 }
108 });
109 }
110 }
111