package jetbrains.mps.ide.tooltips; /*Generated by MPS */ import java.awt.Color; import com.intellij.codeInsight.hint.HintUtil; import java.awt.Frame; import java.awt.Point; import java.awt.Window; import java.awt.Component; import java.awt.event.FocusListener; import java.awt.event.FocusAdapter; import java.awt.event.FocusEvent; import java.awt.event.MouseListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.KeyListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.JScrollPane; import com.intellij.ui.ScrollPaneFactory; import javax.swing.border.LineBorder; import java.awt.Rectangle; import jetbrains.mps.util.WindowsUtil; public class ToolTip { public static final Color BACKGROUND_COLOR = HintUtil.INFORMATION_COLOR; private static final int X_OFFSET = 13; private static final int Y_OFFSET = 3; private ToolTip.MyDialog myDialog; private ToolTipData myHintInformation; private boolean myRigthAligned; /*package*/ ToolTip(boolean rightAligned) { myRigthAligned = rightAligned; } /*package*/ void show(Frame owner, Point location, ToolTipData hintInformation) { myHintInformation = hintInformation; location = new Point(location.x + ((myRigthAligned ? -X_OFFSET : X_OFFSET)), location.y + Y_OFFSET); myDialog = new ToolTip.MyDialog(owner, location, myRigthAligned, hintInformation); myDialog.setVisible(true); } /*package*/ void hide() { if (myDialog != null) { myDialog.dispose(); myDialog = null; } } /*package*/ String getText() { if (myHintInformation == null) { return null; } return myHintInformation.getText(); } /*package*/ static class MyDialog extends Window { private Component myPrevFocusOwner; private FocusListener myOwnerFocusListener = new FocusAdapter() { @Override public void focusLost(FocusEvent p0) { dispose(); } }; private MouseListener myOwnerMouseListener = new MouseAdapter() { @Override public void mousePressed(MouseEvent p0) { dispose(); } }; private KeyListener myOwnerKeyListener = new KeyAdapter() { @Override public void keyPressed(KeyEvent p0) { dispose(); } }; /*package*/ MyDialog(Frame owner, Point location, boolean rightAligned, ToolTipData toolTipData) { super(owner); myPrevFocusOwner = owner.getFocusOwner(); setFocusableWindowState(false); JScrollPane scrollPane = ScrollPaneFactory.createScrollPane(toolTipData.getComponent()); scrollPane.setBorder(new LineBorder(Color.BLACK)); add(scrollPane); pack(); if (rightAligned) { location.x = Math.max(0, location.x - getWidth()); } Rectangle rect = WindowsUtil.findDeviceBoundsAt(location); if (rect.x + rect.width < location.x + getWidth()) { location.x = Math.max(0, rect.x + rect.width - getWidth()); } if (rect.y + rect.height < location.y + getHeight()) { location.y = Math.max(0, rect.y + rect.height - getHeight()); } setLocation(location); addListeners(); } private void addListeners() { if (myPrevFocusOwner != null) { myPrevFocusOwner.addFocusListener(myOwnerFocusListener); myPrevFocusOwner.addMouseListener(myOwnerMouseListener); myPrevFocusOwner.addKeyListener(myOwnerKeyListener); } super.dispose(); } @Override public void dispose() { if (myPrevFocusOwner != null) { myPrevFocusOwner.removeFocusListener(myOwnerFocusListener); myPrevFocusOwner.removeMouseListener(myOwnerMouseListener); myPrevFocusOwner.removeKeyListener(myOwnerKeyListener); } super.dispose(); } } }