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