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=7fe41aa4637b13fd8b42e3eb7097f6804bb8213f;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hp=54825895c0167be9a4ac66e59d24059489e8ee02;hpb=24e2b34260f219f0d1644ca7a138894980e25b14;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 index 54825895c..7fe41aa46 100644 --- a/bundles/org.simantics.g2d/src/org/simantics/g2d/tooltip/AWTTooltipProvider.java +++ b/bundles/org.simantics.g2d/src/org/simantics/g2d/tooltip/AWTTooltipProvider.java @@ -1,105 +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; - - } - }); - } -} +/******************************************************************************* + * 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; + + } + }); + } +}