1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g2d.tooltip;
14 import java.awt.Frame;
15 import java.awt.GraphicsDevice;
16 import java.awt.GraphicsEnvironment;
17 import java.awt.Rectangle;
19 import javax.swing.SwingUtilities;
21 import org.simantics.g2d.element.IElement;
26 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
29 public abstract class AWTTooltipProvider implements TooltipProvider {
38 public abstract void constructPopup(Frame frame,IElement element);
41 public void showTooltip(final IElement element, final int x, final int y) {
42 SwingUtilities.invokeLater(new Runnable() {
46 frame.setVisible(false);
52 frame.setFocusableWindowState(false);
53 frame.setUndecorated(true);
55 constructPopup(frame, element);
59 setTooltipPosition(x,y);
61 frame.setVisible(true);
71 private void setTooltipPosition(int x, int y) {
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();
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;
86 frame.setLocation(ttr.x,ttr.y);
94 public void hideTooltip(IElement element) {
95 SwingUtilities.invokeLater(new Runnable() {
98 frame.setVisible(false);