1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
41
42
43
44
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
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 }
72
73 private void handlePopUpTrigger(MouseEvent e) {
74 if (e.isPopupTrigger()) {
75
76
77 JTree tree = (JTree)e.getSource();
78 TreePath closestPathForLocation = tree.getClosestPathForLocation(e
79 .getX(), e.getY());
80
81
82 if (!tree.isPathSelected(closestPathForLocation)) {
83 tree.setSelectionPath(closestPathForLocation);
84 }
85
86
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
93 JPopupMenu popupMenu = nodeInfo.getPopupMenu(closestPathForLocation);
94 popupMenu.show(e.getComponent(), e.getX(), e.getY());
95 }
96 }
97 }
98
99 }