View Javadoc

1   /*
2    * @(#) StatisticsTree.java Oct 19, 2003
3    * 
4    * Copyright (c) 2002-2005 Delft University of Technology Jaffalaan 5, 2628 BX
5    * Delft, the Netherlands. All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the Lesser General Public License
9    */
10  package nl.tudelft.simulation.dsol.gui.statistics;
11  
12  import java.awt.dnd.DnDConstants;
13  import java.awt.dnd.DragGestureEvent;
14  import java.awt.dnd.DragGestureListener;
15  import java.awt.dnd.DragSource;
16  
17  import javax.naming.NamingException;
18  import javax.swing.JTree;
19  import javax.swing.tree.DefaultMutableTreeNode;
20  import javax.swing.tree.TreePath;
21  
22  import nl.tudelft.simulation.event.EventInterface;
23  import nl.tudelft.simulation.event.EventListenerInterface;
24  import nl.tudelft.simulation.jstats.Swingable;
25  import nl.tudelft.simulation.naming.InitialEventContext;
26  import nl.tudelft.simulation.naming.context.ContextTransferable;
27  import nl.tudelft.simulation.naming.context.ContextTreeModel;
28  import nl.tudelft.simulation.naming.context.SimpleDragSourceListener;
29  
30  /***
31   * The StatisticsTree extends a Tree and drags an object <br>
32   * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft
33   * University of Technology </a>, the Netherlands. <br>
34   * See for project information <a
35   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
36   * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
37   * General Public License (LGPL) </a>, no warranty.
38   * 
39   * @version $Revision$ $Date$
40   * @author <a href="http://www.tbm.tudelft.nl/webstaf/peterja">Peter Jacobs </a>
41   */
42  public class StatisticsTree extends JTree implements DragGestureListener,
43  		EventListenerInterface
44  {
45  
46  	/***
47  	 * constructs a new StatisticsTree
48  	 * 
49  	 * @throws NamingException on failure
50  	 */
51  	public StatisticsTree() throws NamingException
52  	{
53  		super(new ContextTreeModel(new InitialEventContext(),
54  				new Class[]{Swingable.class}, false));
55  		DragSource dragSource = DragSource.getDefaultDragSource();
56  		dragSource.createDefaultDragGestureRecognizer(this,
57  				DnDConstants.ACTION_COPY_OR_MOVE, this);
58  	}
59  
60  	/***
61  	 * @see java.awt.dnd.DragGestureListener
62  	 *      #dragGestureRecognized(java.awt.dnd.DragGestureEvent)
63  	 */
64  	public void dragGestureRecognized(final DragGestureEvent dge)
65  	{
66  		try
67  		{
68  			TreePath path = getLeadSelectionPath();
69  			DefaultMutableTreeNode node = (DefaultMutableTreeNode) path
70  					.getLastPathComponent();
71  			dge.startDrag(DragSource.DefaultCopyDrop, new ContextTransferable(
72  					node.getUserObject()), new SimpleDragSourceListener());
73  		} catch (Exception nullPointerException)
74  		{
75  			// Customer dragged without selection. Nor problem, we may ignore
76  			return;
77  		}
78  	}
79  
80  	/***
81  	 * @see nl.tudelft.simulation.event.EventListenerInterface
82  	 *      #notify(nl.tudelft.simulation.event.EventInterface)
83  	 */
84  	public void notify(final EventInterface event)
85  	{
86  		this.validate();
87  		this.repaint();
88  	}
89  }