View Javadoc

1   /*
2    * @(#)$Id: ExplorerPopupDispatcher.java 761 2009-08-25 20:33:11Z kurmannu $
3    *
4    * Author   : Ueli Kurmann, igesture@uelikurmann.ch
5    *                                   
6    *                                   
7    * Purpose  : 
8    *
9    * -----------------------------------------------------------------------
10   *
11   * Revision Information:
12   *
13   * Date       Who     Reason
14   *
15   * 23.03.2008 ukurmann  Initial Release
16   *
17   * -----------------------------------------------------------------------
18   *
19   * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
20   *
21   * This software is the proprietary information of ETH Zurich.
22   * Use is subject to license terms.
23   * 
24   */
25  
26  package org.ximtec.igesture.tool.explorer;
27  
28  import java.awt.event.MouseAdapter;
29  import java.awt.event.MouseEvent;
30  import java.util.Map;
31  import java.util.logging.Logger;
32  
33  import javax.swing.JPopupMenu;
34  import javax.swing.JTree;
35  import javax.swing.tree.TreePath;
36  
37  import org.ximtec.igesture.tool.explorer.core.NodeInfo;
38  
39  /**
40   * Dispatches Popup Trigger Events and shows the corresponding menu.
41   *
42   * @author  UeliKurmann
43   * @version 1.0
44   * @since   igesture
45   */
46  public class ExplorerPopupDispatcher extends MouseAdapter {
47  
48     private static final Logger LOG = Logger.getLogger(ExplorerPopupDispatcher.class.getName());
49  
50     private Map<Class< ? >, NodeInfo> nodeInfos;
51  
52  
53     /**
54      * 
55      * @param nodeInfos
56      */
57     public ExplorerPopupDispatcher(Map<Class< ? >, NodeInfo> nodeInfos) {
58        this.nodeInfos = nodeInfos;
59     }
60     
61     @Override
62    public void mousePressed(MouseEvent e) {
63       handlePopUpTrigger(e);
64    }
65  
66  
67     @Override
68     public void mouseReleased(MouseEvent e) {
69        handlePopUpTrigger(e);
70  
71     } // mouseReleased
72  
73    private void handlePopUpTrigger(MouseEvent e) {
74      if (e.isPopupTrigger()) {
75  
76         // lookup selected tree node
77         JTree tree = (JTree)e.getSource();
78         TreePath closestPathForLocation = tree.getClosestPathForLocation(e
79               .getX(), e.getY());
80  
81         // select the nearest node if it is not active.
82         if (!tree.isPathSelected(closestPathForLocation)) {
83            tree.setSelectionPath(closestPathForLocation);
84         }
85  
86         // get the selected node
87         Object node = tree.getLastSelectedPathComponent();
88  
89         NodeInfo nodeInfo = nodeInfos.get(node.getClass());
90         if (nodeInfo != null) {
91            LOG.info("NodeInfo was found. Get the Popup Menu.");
92            // show Popup Menu
93            JPopupMenu popupMenu = nodeInfo.getPopupMenu(closestPathForLocation);
94            popupMenu.show(e.getComponent(), e.getX(), e.getY());
95         }
96      }
97    }
98  
99  }