]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/tooltip/AWTTooltipProvider.java
G2DParentNode handles "undefined" child bounds separately
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / tooltip / AWTTooltipProvider.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.g2d.tooltip;
13
14 import java.awt.Frame;
15 import java.awt.GraphicsDevice;
16 import java.awt.GraphicsEnvironment;
17 import java.awt.Rectangle;
18
19 import javax.swing.SwingUtilities;
20
21 import org.simantics.g2d.element.IElement;
22
23 /**
24  * AWT based tooltip.
25  * 
26  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
27  *
28  */
29 public abstract class AWTTooltipProvider implements TooltipProvider {
30
31         private Frame frame;
32         
33         /**
34          * Construct popup UI.
35          * @param frame
36          * @param element
37          */
38         public abstract void constructPopup(Frame frame,IElement element);
39         
40         @Override
41         public void showTooltip(final IElement element, final int x, final int y) {
42                 SwingUtilities.invokeLater(new Runnable() {     
43                         @Override
44                         public void run() {
45                                 if (frame != null) {
46                                         frame.setVisible(false);
47                                         frame.dispose();
48                                         frame = null;
49                                 }
50                                 
51                                 frame = new Frame();
52                                 frame.setFocusableWindowState(false);
53                                 frame.setUndecorated(true);
54                                 
55                                 constructPopup(frame, element);
56                                 
57                                 frame.pack();
58                                 
59                                 setTooltipPosition(x,y);
60                                 
61                                 frame.setVisible(true);
62                         }
63                 });
64         }
65         
66         /**
67          * 
68          * @param x
69          * @param y
70          */
71         private void setTooltipPosition(int x, int y) {
72
73                 GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
74                 for (GraphicsDevice gd : ge.getScreenDevices()) {
75                         Rectangle r = gd.getDefaultConfiguration().getBounds();
76                         if (r.contains(x, y)) {
77                                 Rectangle ttr = new Rectangle(x, y, frame.getWidth(), frame.getHeight());
78                                 //ttr.y -= frame.getHeight();
79                                 ttr.y += 24;
80                                 if (!r.contains(ttr)) {
81                                         ttr.y = Math.max(r.y, ttr.y);
82                                         ttr.y = Math.min(r.y + r.height, ttr.y + ttr.height) - ttr.height;
83                                         ttr.x = Math.max(r.x, ttr.x);
84                                         ttr.x = Math.min(r.x + r.width, ttr.x + ttr.width) - ttr.width;
85                                 }
86                                 frame.setLocation(ttr.x,ttr.y);
87                                 return;
88                         }
89                 }
90         }
91         
92         
93         @Override
94         public void hideTooltip(IElement element) {
95                 SwingUtilities.invokeLater(new Runnable() {     
96                         @Override
97                         public void run() {
98                                 frame.setVisible(false);
99                                 frame.dispose();
100                                 frame = null;
101                                 
102                         }
103                 });
104         }
105 }