1   /****************************************************************************
2    **
3    ** This file is part of yFiles-2.6. 
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-2008 by yWorks GmbH, Vor dem Kreuzberg 28, 
11   ** 72070 Tuebingen, Germany. All rights reserved.
12   **
13   ***************************************************************************/
14  package demo.view.hierarchy;
15  
16  import javax.swing.tree.*;
17  import javax.swing.*;
18  import java.awt.event.*;
19  import y.view.*;
20  import y.base.*;
21  
22  /**
23   * A MouseListener that listens for double click events on a JTree.
24   * The node item that was clicked will be focused in an 
25   * associated Graph2DView.
26   */ 
27  public class HierarchyJTreeDoubleClickListener extends MouseAdapter
28  {
29    Graph2DView view;
30    
31    public HierarchyJTreeDoubleClickListener(Graph2DView view)
32    {
33      this.view = view;
34    }
35    
36    public void mouseClicked(MouseEvent e)
37    {
38      JTree tree =(JTree)e.getSource();
39      
40      if(e.getClickCount() == 2)
41      {
42        //D.bug("right mouse pressed");
43        
44        int y = e.getY();
45        int x = e.getX();
46        TreePath path = tree.getPathForLocation(x,y);
47        if(path != null)
48        {
49          Object last =  path.getLastPathComponent();
50          Graph2D focusedGraph = null;
51          Node v = null;
52          
53          if(last instanceof Node)
54          {
55            v = (Node)last;
56            focusedGraph = (Graph2D)v.getGraph();
57          }
58          else if(last instanceof Graph2D) //root
59          {
60            focusedGraph = (Graph2D)last;
61          }
62          
63          if(focusedGraph != null)
64          {
65            view.setGraph2D(focusedGraph);
66            if(v != null)
67            {
68              view.setCenter(focusedGraph.getCenterX(v),focusedGraph.getCenterY(v));
69            }
70            view.updateView();
71          }
72        }
73      }
74    }
75  }
76