HierarchyJTreeDoubleClickListener.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 javax.swing.tree.*; 31 import javax.swing.*; 32 import java.awt.event.*; 33 import y.view.*; 34 import y.base.*; 35 36 /** 37 * A MouseListener that listens for double click events on a JTree. 38 * The node item that was clicked will be focused in an 39 * associated Graph2DView. 40 */ 41 public class HierarchyJTreeDoubleClickListener extends MouseAdapter 42 { 43 Graph2DView view; 44 45 public HierarchyJTreeDoubleClickListener(Graph2DView view) 46 { 47 this.view = view; 48 } 49 50 public void mouseClicked(MouseEvent e) 51 { 52 JTree tree =(JTree)e.getSource(); 53 54 if(e.getClickCount() == 2) 55 { 56 //D.bug("right mouse pressed"); 57 58 int y = e.getY(); 59 int x = e.getX(); 60 TreePath path = tree.getPathForLocation(x,y); 61 if(path != null) 62 { 63 Object last = path.getLastPathComponent(); 64 Graph2D focusedGraph = null; 65 Node v = null; 66 67 if(last instanceof Node) 68 { 69 v = (Node)last; 70 focusedGraph = (Graph2D)v.getGraph(); 71 } 72 else if(last instanceof Graph2D) //root 73 { 74 focusedGraph = (Graph2D)last; 75 } 76 77 if(focusedGraph != null) 78 { 79 view.setGraph2D(focusedGraph); 80 if(v != null) 81 { 82 view.setCenter(focusedGraph.getCenterX(v),focusedGraph.getCenterY(v)); 83 } 84 view.updateView(); 85 } 86 } 87 } 88 } 89 } 90