SCViewer/plugins/com.minres.scviewer.databas.../src/com/minres/scviewer/database/ui/swt/internal/ToolTipHandler.java

138 lines
4.4 KiB
Java
Raw Normal View History

2020-07-15 21:42:10 +02:00
package com.minres.scviewer.database.ui.swt.internal;
2020-03-21 06:28:28 +01:00
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
2020-03-24 07:43:10 +01:00
import org.eclipse.swt.layout.GridLayout;
2020-03-21 06:28:28 +01:00
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
2020-03-21 06:28:28 +01:00
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
2020-03-21 06:28:28 +01:00
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Widget;
2020-07-15 21:42:10 +02:00
import com.minres.scviewer.database.ui.swt.Constants;
2021-01-09 23:25:31 +01:00
import com.minres.scviewer.database.ui.swt.IToolTipContentProvider;
import com.minres.scviewer.database.ui.swt.IToolTipHelpTextProvider;
2020-03-21 06:28:28 +01:00
class ToolTipHandler {
2020-03-21 07:31:14 +01:00
private final Display display;
2020-03-21 06:28:28 +01:00
private Shell parentShell;
2020-03-21 07:31:14 +01:00
private Shell shell;
2020-03-21 06:28:28 +01:00
private Widget tipWidget; // widget this tooltip is hovering over
private Point tipPosition; // the position being hovered over
private static final int HOVER_YOFFSET = 1;
2020-03-21 06:28:28 +01:00
/**
* Creates a new tooltip handler
*
* @param parent the parent Shell
*/
public ToolTipHandler(Shell parent) {
2020-03-21 07:31:14 +01:00
display = parent.getDisplay();
parentShell = parent;
2020-03-21 06:28:28 +01:00
}
/**
* Enables customized hover help for a specified control
*
* @control the control on which to enable hoverhelp
*/
public void activateHoverHelp(final Control control) {
Listener listener = new Listener () {
Shell shell = null;
2020-03-21 06:28:28 +01:00
@Override
public void handleEvent (Event event) {
switch (event.type) {
case SWT.KeyDown:
if (shell != null && shell.isVisible() && event.keyCode == SWT.F2)
shell.setFocus();
break;
case SWT.Dispose:
2020-03-21 11:30:30 +01:00
case SWT.MouseMove:
case SWT.MouseDown:
if (shell != null){
shell.dispose ();
shell = null;
2020-03-21 11:30:30 +01:00
tipWidget=null;
}
break;
case SWT.MouseHover:
createHoverWindow(control, event);
break;
default:
/* do nothing */
2020-03-21 06:28:28 +01:00
}
}
private void createHoverWindow(final Control control, Event event) {
Object o = control.getData(Constants.CONTENT_PROVIDER_TAG);
2021-01-09 23:25:31 +01:00
if(o instanceof IToolTipContentProvider) {
IToolTipContentProvider provider = ((IToolTipContentProvider)o);
Point pt = new Point (event.x, event.y);
tipPosition = control.toDisplay(pt);
if (shell != null && !shell.isDisposed ()) shell.dispose ();
shell = new Shell (parentShell, SWT.NO_FOCUS | SWT.TOOL);
shell.setBackground (display.getSystemColor (SWT.COLOR_INFO_BACKGROUND));
GridLayout layout = new GridLayout(1, true);
layout.verticalSpacing=0;
layout.horizontalSpacing=0;
layout.marginWidth = 0;
layout.marginHeight = 0;
shell.setLayout(layout);
boolean visible = provider.createContent(shell, pt);
shell.pack();
shell.setSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT));
setHoverLocation(shell, tipPosition);
shell.setVisible (visible);
if(visible)
tipWidget=event.widget;
2020-03-21 06:28:28 +01:00
}
}
};
control.addListener (SWT.Dispose, listener);
control.addListener (SWT.KeyDown, listener);
control.addListener (SWT.MouseHover, listener);
2020-03-21 11:30:30 +01:00
control.addListener (SWT.MouseDown, listener);
2020-03-21 06:28:28 +01:00
/*
* Trap F1 Help to pop up a custom help box
*/
control.addHelpListener(event -> {
if (tipWidget == null) return;
2021-01-09 23:25:31 +01:00
IToolTipHelpTextProvider handler = (IToolTipHelpTextProvider)tipWidget.getData(Constants.HELP_PROVIDER_TAG);
2020-03-21 06:28:28 +01:00
if (handler == null) return;
String text = handler.getHelpText(tipWidget);
if (text == null) return;
2020-03-21 07:31:14 +01:00
if (shell.isVisible()) {
shell.setVisible(false);
2020-03-21 06:28:28 +01:00
Shell helpShell = new Shell(parentShell, SWT.SHELL_TRIM);
helpShell.setLayout(new FillLayout());
Label label = new Label(helpShell, SWT.NONE);
label.setText(text);
helpShell.pack();
setHoverLocation(helpShell, tipPosition);
helpShell.open();
}
});
}
/**
* Sets the location for a hovering shell
* @param shell the object that is to hover
* @param position the position of a widget to hover over
* @return the top-left location for a hovering box
*/
private void setHoverLocation(Shell shell, Point position) {
Rectangle displayBounds = shell.getDisplay().getBounds();
Rectangle shellBounds = shell.getBounds();
shellBounds.x = Math.max(Math.min(position.x, displayBounds.width - shellBounds.width), 0);
shellBounds.y = Math.max(Math.min(position.y + HOVER_YOFFSET, displayBounds.height - shellBounds.height), 0);
2020-03-21 06:28:28 +01:00
shell.setBounds(shellBounds);
}
}