X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.g2d%2Fsrc%2Forg%2Fsimantics%2Fg2d%2Ftooltip%2FAWTTooltipProvider.java;fp=bundles%2Forg.simantics.g2d%2Fsrc%2Forg%2Fsimantics%2Fg2d%2Ftooltip%2FAWTTooltipProvider.java;h=54825895c0167be9a4ac66e59d24059489e8ee02;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/tooltip/AWTTooltipProvider.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/tooltip/AWTTooltipProvider.java new file mode 100644 index 000000000..54825895c --- /dev/null +++ b/bundles/org.simantics.g2d/src/org/simantics/g2d/tooltip/AWTTooltipProvider.java @@ -0,0 +1,105 @@ +/******************************************************************************* + * 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.awt.Frame; +import java.awt.GraphicsDevice; +import java.awt.GraphicsEnvironment; +import java.awt.Rectangle; + +import javax.swing.SwingUtilities; + +import org.simantics.g2d.element.IElement; + +/** + * AWT based tooltip. + * + * @author Marko Luukkainen + * + */ +public abstract class AWTTooltipProvider implements TooltipProvider { + + private Frame frame; + + /** + * Construct popup UI. + * @param frame + * @param element + */ + public abstract void constructPopup(Frame frame,IElement element); + + @Override + public void showTooltip(final IElement element, final int x, final int y) { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + if (frame != null) { + frame.setVisible(false); + frame.dispose(); + frame = null; + } + + frame = new Frame(); + frame.setFocusableWindowState(false); + frame.setUndecorated(true); + + constructPopup(frame, element); + + frame.pack(); + + setTooltipPosition(x,y); + + frame.setVisible(true); + } + }); + } + + /** + * + * @param x + * @param y + */ + private void setTooltipPosition(int x, int y) { + + GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); + for (GraphicsDevice gd : ge.getScreenDevices()) { + Rectangle r = gd.getDefaultConfiguration().getBounds(); + if (r.contains(x, y)) { + Rectangle ttr = new Rectangle(x, y, frame.getWidth(), frame.getHeight()); + //ttr.y -= frame.getHeight(); + ttr.y += 24; + if (!r.contains(ttr)) { + ttr.y = Math.max(r.y, ttr.y); + ttr.y = Math.min(r.y + r.height, ttr.y + ttr.height) - ttr.height; + ttr.x = Math.max(r.x, ttr.x); + ttr.x = Math.min(r.x + r.width, ttr.x + ttr.width) - ttr.width; + } + frame.setLocation(ttr.x,ttr.y); + return; + } + } + } + + + @Override + public void hideTooltip(IElement element) { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + frame.setVisible(false); + frame.dispose(); + frame = null; + + } + }); + } +}