]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/tooltip/AWTTooltipProvider.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / tooltip / AWTTooltipProvider.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.awt.Frame;\r
15 import java.awt.GraphicsDevice;\r
16 import java.awt.GraphicsEnvironment;\r
17 import java.awt.Rectangle;\r
18 \r
19 import javax.swing.SwingUtilities;\r
20 \r
21 import org.simantics.g2d.element.IElement;\r
22 \r
23 /**\r
24  * AWT based tooltip.\r
25  * \r
26  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
27  *\r
28  */\r
29 public abstract class AWTTooltipProvider implements TooltipProvider {\r
30 \r
31         private Frame frame;\r
32         \r
33         /**\r
34          * Construct popup UI.\r
35          * @param frame\r
36          * @param element\r
37          */\r
38         public abstract void constructPopup(Frame frame,IElement element);\r
39         \r
40         @Override\r
41         public void showTooltip(final IElement element, final int x, final int y) {\r
42                 SwingUtilities.invokeLater(new Runnable() {     \r
43                         @Override\r
44                         public void run() {\r
45                                 if (frame != null) {\r
46                                         frame.setVisible(false);\r
47                                         frame.dispose();\r
48                                         frame = null;\r
49                                 }\r
50                                 \r
51                                 frame = new Frame();\r
52                                 frame.setFocusableWindowState(false);\r
53                                 frame.setUndecorated(true);\r
54                                 \r
55                                 constructPopup(frame, element);\r
56                                 \r
57                                 frame.pack();\r
58                                 \r
59                                 setTooltipPosition(x,y);\r
60                                 \r
61                                 frame.setVisible(true);\r
62                         }\r
63                 });\r
64         }\r
65         \r
66         /**\r
67          * \r
68          * @param x\r
69          * @param y\r
70          */\r
71         private void setTooltipPosition(int x, int y) {\r
72 \r
73                 GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();\r
74                 for (GraphicsDevice gd : ge.getScreenDevices()) {\r
75                         Rectangle r = gd.getDefaultConfiguration().getBounds();\r
76                         if (r.contains(x, y)) {\r
77                                 Rectangle ttr = new Rectangle(x, y, frame.getWidth(), frame.getHeight());\r
78                                 //ttr.y -= frame.getHeight();\r
79                                 ttr.y += 24;\r
80                                 if (!r.contains(ttr)) {\r
81                                         ttr.y = Math.max(r.y, ttr.y);\r
82                                         ttr.y = Math.min(r.y + r.height, ttr.y + ttr.height) - ttr.height;\r
83                                         ttr.x = Math.max(r.x, ttr.x);\r
84                                         ttr.x = Math.min(r.x + r.width, ttr.x + ttr.width) - ttr.width;\r
85                                 }\r
86                                 frame.setLocation(ttr.x,ttr.y);\r
87                                 return;\r
88                         }\r
89                 }\r
90         }\r
91         \r
92         \r
93         @Override\r
94         public void hideTooltip(IElement element) {\r
95                 SwingUtilities.invokeLater(new Runnable() {     \r
96                         @Override\r
97                         public void run() {\r
98                                 frame.setVisible(false);\r
99                                 frame.dispose();\r
100                                 frame = null;\r
101                                 \r
102                         }\r
103                 });\r
104         }\r
105 }\r