]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/tooltip/BaseTooltipParticipant.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / tooltip / BaseTooltipParticipant.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.g2d.tooltip;\r
13 \r
14 import java.util.concurrent.Future;\r
15 import java.util.concurrent.TimeUnit;\r
16 \r
17 import javax.swing.ToolTipManager;\r
18 \r
19 import org.simantics.g2d.diagram.participant.AbstractDiagramParticipant;\r
20 import org.simantics.g2d.element.IElement;\r
21 import org.simantics.scenegraph.g2d.events.MouseEvent;\r
22 import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;\r
23 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseButtonPressedEvent;\r
24 import org.simantics.scenegraph.g2d.events.command.CommandEvent;\r
25 import org.simantics.scenegraph.g2d.events.command.Commands;\r
26 import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
27 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;\r
28 import org.simantics.utils.threads.ThreadUtils;\r
29 \r
30 public abstract class BaseTooltipParticipant extends AbstractDiagramParticipant implements ITooltipParticipant {\r
31 \r
32     // Element hint key for tooltips\r
33     public static Key TOOLTIP_KEY = new KeyOf(TooltipProvider.class, "TOOLTIP");\r
34 \r
35     public int tooltipDelay = ToolTipManager.sharedInstance().getInitialDelay();\r
36 \r
37     private IElement tooltipElement;\r
38     private TooltipProvider creator;\r
39     private boolean tooltipShowing = false;\r
40 \r
41     private Future<?> delayRunnable = null;\r
42 \r
43     private int x = 0;\r
44     private int y = 0;\r
45 \r
46     @EventHandler(priority = 0)\r
47     public boolean handleKeyEvent(CommandEvent e) {\r
48         if (e.command == Commands.FOCUS_TOOLTIP) {\r
49             //System.out.println("Focus");\r
50             if (tooltipShowing && creator instanceof FocusableTooltipProvider) {\r
51                 ((FocusableTooltipProvider)creator).focus();\r
52             }\r
53         }\r
54         return false;\r
55     }\r
56 \r
57     @EventHandler(priority = Integer.MAX_VALUE)\r
58     public boolean handleMouseEvent(MouseEvent e) {\r
59         if (e.screenPosition != null) {\r
60             x = (int)e.screenPosition.getX();\r
61             y = (int)e.screenPosition.getY();\r
62         }\r
63         if (tooltipShowing) {\r
64 \r
65             if (e instanceof MouseButtonPressedEvent) {\r
66                 // this hides focused tooltip if mouse is clicked somewhere else that on the tooltip\r
67                 hideTooltip();\r
68             }\r
69         }\r
70         return  false;\r
71     }\r
72 \r
73     /**\r
74      * Shows tooltip immediately\r
75      */\r
76     public synchronized void showTooltip() {\r
77         if (tooltipElement == null)\r
78             return;\r
79         creator.showTooltip(tooltipElement,x,y);\r
80         tooltipShowing = true;\r
81     }\r
82 \r
83     private void launchThread() {\r
84         if (delayRunnable != null) {\r
85             delayRunnable.cancel(false);\r
86             delayRunnable = null;\r
87         }\r
88         delayRunnable = ThreadUtils.getNonBlockingWorkExecutor().schedule(new Runnable() {\r
89             @Override\r
90             public void run() {\r
91                 showTooltip();\r
92             }\r
93         }, tooltipDelay, TimeUnit.MILLISECONDS);\r
94     }\r
95 \r
96     /**\r
97      * Tries to show tooltip for given element\r
98      * Does not show tooltip instantly, but uses delay\r
99      * @param element\r
100      */\r
101     public synchronized void showTooltipFor(IElement element) {\r
102         //System.out.println("showTooltipFor " + element);\r
103         if (tooltipShowing && (creator instanceof FocusableTooltipProvider) && ((FocusableTooltipProvider)creator).hasFocus())\r
104             return;\r
105         creator = element.getHint(TOOLTIP_KEY);\r
106         if (creator != null) {\r
107             tooltipElement = element;\r
108             launchThread();\r
109         } else {\r
110             hideTooltip();\r
111         }\r
112     }\r
113 \r
114     /**\r
115      * Hides tooltip, if one is showing\r
116      */\r
117     public synchronized void hideTooltip() {\r
118         //System.out.println("hideTooltip");\r
119         if (tooltipShowing && (creator instanceof FocusableTooltipProvider) && ((FocusableTooltipProvider)creator).hasFocus())\r
120             return;\r
121         if (creator != null && tooltipShowing)\r
122             creator.hideTooltip(tooltipElement);\r
123         creator = null;\r
124         tooltipElement = null;\r
125         tooltipShowing = false;\r
126     }\r
127 \r
128 }\r