--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.g2d.tooltip;\r
+\r
+import java.util.concurrent.Future;\r
+import java.util.concurrent.TimeUnit;\r
+\r
+import javax.swing.ToolTipManager;\r
+\r
+import org.simantics.g2d.diagram.participant.AbstractDiagramParticipant;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.scenegraph.g2d.events.MouseEvent;\r
+import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;\r
+import org.simantics.scenegraph.g2d.events.MouseEvent.MouseButtonPressedEvent;\r
+import org.simantics.scenegraph.g2d.events.command.CommandEvent;\r
+import org.simantics.scenegraph.g2d.events.command.Commands;\r
+import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
+import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;\r
+import org.simantics.utils.threads.ThreadUtils;\r
+\r
+public abstract class BaseTooltipParticipant extends AbstractDiagramParticipant implements ITooltipParticipant {\r
+\r
+ // Element hint key for tooltips\r
+ public static Key TOOLTIP_KEY = new KeyOf(TooltipProvider.class, "TOOLTIP");\r
+\r
+ public int tooltipDelay = ToolTipManager.sharedInstance().getInitialDelay();\r
+\r
+ private IElement tooltipElement;\r
+ private TooltipProvider creator;\r
+ private boolean tooltipShowing = false;\r
+\r
+ private Future<?> delayRunnable = null;\r
+\r
+ private int x = 0;\r
+ private int y = 0;\r
+\r
+ @EventHandler(priority = 0)\r
+ public boolean handleKeyEvent(CommandEvent e) {\r
+ if (e.command == Commands.FOCUS_TOOLTIP) {\r
+ //System.out.println("Focus");\r
+ if (tooltipShowing && creator instanceof FocusableTooltipProvider) {\r
+ ((FocusableTooltipProvider)creator).focus();\r
+ }\r
+ }\r
+ return false;\r
+ }\r
+\r
+ @EventHandler(priority = Integer.MAX_VALUE)\r
+ public boolean handleMouseEvent(MouseEvent e) {\r
+ if (e.screenPosition != null) {\r
+ x = (int)e.screenPosition.getX();\r
+ y = (int)e.screenPosition.getY();\r
+ }\r
+ if (tooltipShowing) {\r
+\r
+ if (e instanceof MouseButtonPressedEvent) {\r
+ // this hides focused tooltip if mouse is clicked somewhere else that on the tooltip\r
+ hideTooltip();\r
+ }\r
+ }\r
+ return false;\r
+ }\r
+\r
+ /**\r
+ * Shows tooltip immediately\r
+ */\r
+ public synchronized void showTooltip() {\r
+ if (tooltipElement == null)\r
+ return;\r
+ creator.showTooltip(tooltipElement,x,y);\r
+ tooltipShowing = true;\r
+ }\r
+\r
+ private void launchThread() {\r
+ if (delayRunnable != null) {\r
+ delayRunnable.cancel(false);\r
+ delayRunnable = null;\r
+ }\r
+ delayRunnable = ThreadUtils.getNonBlockingWorkExecutor().schedule(new Runnable() {\r
+ @Override\r
+ public void run() {\r
+ showTooltip();\r
+ }\r
+ }, tooltipDelay, TimeUnit.MILLISECONDS);\r
+ }\r
+\r
+ /**\r
+ * Tries to show tooltip for given element\r
+ * Does not show tooltip instantly, but uses delay\r
+ * @param element\r
+ */\r
+ public synchronized void showTooltipFor(IElement element) {\r
+ //System.out.println("showTooltipFor " + element);\r
+ if (tooltipShowing && (creator instanceof FocusableTooltipProvider) && ((FocusableTooltipProvider)creator).hasFocus())\r
+ return;\r
+ creator = element.getHint(TOOLTIP_KEY);\r
+ if (creator != null) {\r
+ tooltipElement = element;\r
+ launchThread();\r
+ } else {\r
+ hideTooltip();\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Hides tooltip, if one is showing\r
+ */\r
+ public synchronized void hideTooltip() {\r
+ //System.out.println("hideTooltip");\r
+ if (tooltipShowing && (creator instanceof FocusableTooltipProvider) && ((FocusableTooltipProvider)creator).hasFocus())\r
+ return;\r
+ if (creator != null && tooltipShowing)\r
+ creator.hideTooltip(tooltipElement);\r
+ creator = null;\r
+ tooltipElement = null;\r
+ tooltipShowing = false;\r
+ }\r
+\r
+}\r