]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/GraphExplorerToolTip.java
Adding column data to tooltip event could cause NPE
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / GraphExplorerToolTip.java
1 package org.simantics.browsing.ui.swt;
2
3 import org.eclipse.jface.window.ToolTip;
4 import org.eclipse.swt.graphics.Point;
5 import org.eclipse.swt.widgets.Composite;
6 import org.eclipse.swt.widgets.Event;
7 import org.eclipse.swt.widgets.Tree;
8 import org.eclipse.swt.widgets.TreeColumn;
9 import org.eclipse.swt.widgets.TreeItem;
10 import org.simantics.browsing.ui.BuiltinKeys;
11 import org.simantics.browsing.ui.NodeContext;
12 import org.simantics.browsing.ui.common.internal.GENodeQueryManager;
13 import org.simantics.browsing.ui.common.labelers.LabelerStub;
14 import org.simantics.browsing.ui.content.Labeler;
15 import org.simantics.browsing.ui.swt.GraphExplorerImpl.GraphExplorerContext;
16
17 public class GraphExplorerToolTip extends ToolTip {
18
19     private boolean DEBUG = false;
20     
21     private Tree parent;
22     private NodeContext nodeContext;
23     private Labeler labeler;
24
25     private GraphExplorerContext explorerContext;
26     
27     public GraphExplorerToolTip(GraphExplorerContext explorerContext, Tree parent) {
28         super(parent, NO_RECREATE, false);
29         setHideOnMouseDown(false);
30         setPopupDelay(400);
31         this.explorerContext = explorerContext;
32         this.parent = parent;
33         this.nodeContext = null;
34         if (DEBUG)
35             System.out.println("GraphExplorerToolTip constructor called for parent : " + parent + ", class : " + parent.getClass().toString());
36     }
37
38     @Override
39     protected Composite createToolTipContentArea(Event event, Composite parent) {
40         return ((LabelerStub) labeler).createToolTipContentArea(event, parent, nodeContext);
41     }
42     
43     @Override
44     protected boolean shouldCreateToolTip(Event event) {
45         TreeItem treeItem = parent.getItem(new Point(event.x, event.y));
46         if (treeItem == null)
47             return false;
48         // Locate the column, and add the column reference to Event object.
49         TreeColumn columns[] = parent.getColumns();
50         TreeColumn column = null;
51         int x = 0;
52         for (TreeColumn c : columns) {
53             int w = c.getWidth();
54             if (event.x >= x && event.x < (x+w)) {
55                 column = c;
56                 break;
57             }
58             x+=w;
59         }
60         if (column != null)
61                 event.data = column.getData();
62         
63         GENodeQueryManager manager = new GENodeQueryManager(explorerContext, null, null, TreeItemReference.create(treeItem.getParentItem()));
64         nodeContext = (NodeContext) treeItem.getData();
65         if (nodeContext != null)
66             labeler = manager.query(nodeContext, BuiltinKeys.SELECTED_LABELER);
67         if (nodeContext == null || !(labeler instanceof LabelerStub))
68             return false;
69         return ((LabelerStub) labeler).shouldCreateToolTip(event, nodeContext);
70     }
71
72     public void setGraphExplorerContext(GraphExplorerContext explorerContext) {
73         this.explorerContext = explorerContext;
74     }
75
76 }