--- /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.awt.Frame;\r
+import java.awt.GraphicsDevice;\r
+import java.awt.GraphicsEnvironment;\r
+import java.awt.Rectangle;\r
+\r
+import javax.swing.SwingUtilities;\r
+\r
+import org.simantics.g2d.element.IElement;\r
+\r
+/**\r
+ * AWT based tooltip.\r
+ * \r
+ * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
+ *\r
+ */\r
+public abstract class AWTTooltipProvider implements TooltipProvider {\r
+\r
+ private Frame frame;\r
+ \r
+ /**\r
+ * Construct popup UI.\r
+ * @param frame\r
+ * @param element\r
+ */\r
+ public abstract void constructPopup(Frame frame,IElement element);\r
+ \r
+ @Override\r
+ public void showTooltip(final IElement element, final int x, final int y) {\r
+ SwingUtilities.invokeLater(new Runnable() { \r
+ @Override\r
+ public void run() {\r
+ if (frame != null) {\r
+ frame.setVisible(false);\r
+ frame.dispose();\r
+ frame = null;\r
+ }\r
+ \r
+ frame = new Frame();\r
+ frame.setFocusableWindowState(false);\r
+ frame.setUndecorated(true);\r
+ \r
+ constructPopup(frame, element);\r
+ \r
+ frame.pack();\r
+ \r
+ setTooltipPosition(x,y);\r
+ \r
+ frame.setVisible(true);\r
+ }\r
+ });\r
+ }\r
+ \r
+ /**\r
+ * \r
+ * @param x\r
+ * @param y\r
+ */\r
+ private void setTooltipPosition(int x, int y) {\r
+\r
+ GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();\r
+ for (GraphicsDevice gd : ge.getScreenDevices()) {\r
+ Rectangle r = gd.getDefaultConfiguration().getBounds();\r
+ if (r.contains(x, y)) {\r
+ Rectangle ttr = new Rectangle(x, y, frame.getWidth(), frame.getHeight());\r
+ //ttr.y -= frame.getHeight();\r
+ ttr.y += 24;\r
+ if (!r.contains(ttr)) {\r
+ ttr.y = Math.max(r.y, ttr.y);\r
+ ttr.y = Math.min(r.y + r.height, ttr.y + ttr.height) - ttr.height;\r
+ ttr.x = Math.max(r.x, ttr.x);\r
+ ttr.x = Math.min(r.x + r.width, ttr.x + ttr.width) - ttr.width;\r
+ }\r
+ frame.setLocation(ttr.x,ttr.y);\r
+ return;\r
+ }\r
+ }\r
+ }\r
+ \r
+ \r
+ @Override\r
+ public void hideTooltip(IElement element) {\r
+ SwingUtilities.invokeLater(new Runnable() { \r
+ @Override\r
+ public void run() {\r
+ frame.setVisible(false);\r
+ frame.dispose();\r
+ frame = null;\r
+ \r
+ }\r
+ });\r
+ }\r
+}\r