View Javadoc

1   /*
2    * jcurl java curling software framework https://JCurl.mro.name
3    * Copyright (C) 2005-2009 M. Rohrmoser
4    * 
5    * This program is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License as published by the
7    * Free Software Foundation; either version 2 of the License, or (at your
8    * option) any later version.
9    * 
10   * This program is distributed in the hope that it will be useful, but
11   * WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13   * Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License along
16   * with this program; if not, write to the Free Software Foundation, Inc.,
17   * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18   */
19  package org.jcurl.demo.jtree;
20  
21  import java.awt.Container;
22  import java.awt.GridLayout;
23  import java.awt.Insets;
24  import java.awt.Point;
25  import java.awt.Rectangle;
26  import java.awt.dnd.DnDConstants;
27  import java.util.Enumeration;
28  
29  import javax.swing.JFrame;
30  import javax.swing.JScrollPane;
31  import javax.swing.JTree;
32  import javax.swing.UIManager;
33  import javax.swing.tree.DefaultMutableTreeNode;
34  import javax.swing.tree.DefaultTreeModel;
35  import javax.swing.tree.TreeSelectionModel;
36  
37  /**
38   * http://forum.java.sun.com/thread.jspa?threadID=296255&start=0
39   * 
40   * @author <a href="http://forum.java.sun.com/profile.jspa?userID=82795">Deudeu</a>
41   * @version $Id: DNDTree.java 1031 2009-07-23 15:06:05Z mro $
42   */
43  public class DNDTree extends JTree {
44  
45  	private static final long serialVersionUID = 4923607215510457402L;
46  
47  	public static DefaultMutableTreeNode createTree() {
48  		final DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
49  		final DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("node1");
50  		final DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("node2");
51  		root.add(node1);
52  		root.add(node2);
53  		node1.add(new DefaultMutableTreeNode("sub1_1"));
54  		node1.add(new DefaultMutableTreeNode("sub1_2"));
55  		node1.add(new DefaultMutableTreeNode("sub1_3"));
56  		node2.add(new DefaultMutableTreeNode("sub2_1"));
57  		node2.add(new DefaultMutableTreeNode("sub2_2"));
58  		node2.add(new DefaultMutableTreeNode("sub2_3"));
59  		return root;
60  	}
61  
62  	public static void main(final String[] args) {
63  		try {
64  			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
65  			final JFrame frame = new JFrame();
66  			final Container contentPane = frame.getContentPane();
67  			contentPane.setLayout(new GridLayout(1, 2));
68  			final DefaultMutableTreeNode root1 = DNDTree.createTree();
69  			final DNDTree tree1 = new DNDTree(root1);
70  			final DefaultMutableTreeNode root2 = DNDTree.createTree();
71  			final DNDTree tree2 = new DNDTree(root2);
72  			contentPane.add(new JScrollPane(tree1));
73  			contentPane.add(new JScrollPane(tree2));
74  			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
75  			frame.setSize(400, 400);
76  			frame.setVisible(true);
77  		} catch (final Exception e) {
78  			System.out.println(e);
79  		}
80  	}
81  
82  	public static DefaultMutableTreeNode makeDeepCopy(
83  			final DefaultMutableTreeNode node) {
84  		final DefaultMutableTreeNode copy = new DefaultMutableTreeNode(node
85  				.getUserObject());
86  		for (final Enumeration e = node.children(); e.hasMoreElements();)
87  			copy.add(makeDeepCopy((DefaultMutableTreeNode) e.nextElement()));
88  		return copy;
89  	}
90  
91  	Insets autoscrollInsets = new Insets(20, 20, 20, 20); // insets
92  
93  	public DNDTree(final DefaultMutableTreeNode root) {
94  		setAutoscrolls(true);
95  		final DefaultTreeModel treemodel = new DefaultTreeModel(root);
96  		setModel(treemodel);
97  		setRootVisible(true);
98  		setShowsRootHandles(false);// to show the root icon
99  		getSelectionModel().setSelectionMode(
100 				TreeSelectionModel.SINGLE_TREE_SELECTION); // set
101 		// single
102 		// selection
103 		// for
104 		// the
105 		// Tree
106 		setEditable(false);
107 		new DefaultTreeTransferHandler(this, DnDConstants.ACTION_COPY_OR_MOVE);
108 	}
109 
110 	public void autoscroll(final Point cursorLocation) {
111 		final Insets insets = getAutoscrollInsets();
112 		final Rectangle outer = getVisibleRect();
113 		final Rectangle inner = new Rectangle(outer.x + insets.left, outer.y
114 				+ insets.top, outer.width - (insets.left + insets.right),
115 				outer.height - (insets.top + insets.bottom));
116 		if (!inner.contains(cursorLocation)) {
117 			final Rectangle scrollRect = new Rectangle(cursorLocation.x
118 					- insets.left, cursorLocation.y - insets.top, insets.left
119 					+ insets.right, insets.top + insets.bottom);
120 			scrollRectToVisible(scrollRect);
121 		}
122 	}
123 
124 	public Insets getAutoscrollInsets() {
125 		return autoscrollInsets;
126 	}
127 }