clean up of ToolTip handler
This commit is contained in:
parent
210d925987
commit
cea11743eb
|
@ -3,9 +3,9 @@ package com.minres.scviewer.database.swt.internal;
|
||||||
import org.eclipse.swt.SWT;
|
import org.eclipse.swt.SWT;
|
||||||
import org.eclipse.swt.events.MouseEvent;
|
import org.eclipse.swt.events.MouseEvent;
|
||||||
import org.eclipse.swt.events.MouseListener;
|
import org.eclipse.swt.events.MouseListener;
|
||||||
import org.eclipse.swt.events.MouseMoveListener;
|
|
||||||
import org.eclipse.swt.events.MouseTrackAdapter;
|
import org.eclipse.swt.events.MouseTrackAdapter;
|
||||||
import org.eclipse.swt.graphics.Color;
|
import org.eclipse.swt.events.PaintEvent;
|
||||||
|
import org.eclipse.swt.events.PaintListener;
|
||||||
import org.eclipse.swt.graphics.Font;
|
import org.eclipse.swt.graphics.Font;
|
||||||
import org.eclipse.swt.graphics.Point;
|
import org.eclipse.swt.graphics.Point;
|
||||||
import org.eclipse.swt.graphics.Rectangle;
|
import org.eclipse.swt.graphics.Rectangle;
|
||||||
|
@ -29,87 +29,63 @@ import com.minres.scviewer.database.swt.ToolTipTableContentProvider;
|
||||||
|
|
||||||
class ToolTipHandler {
|
class ToolTipHandler {
|
||||||
|
|
||||||
|
private final Display display;
|
||||||
private Shell parentShell;
|
private Shell parentShell;
|
||||||
private Shell tipShell;
|
private Shell shell;
|
||||||
private Label tipLabelText;
|
private Label label;
|
||||||
private Table tipTable;
|
private Table table;
|
||||||
|
private TableColumn nameCol;
|
||||||
|
private TableColumn valueCol;
|
||||||
|
|
||||||
private Widget tipWidget; // widget this tooltip is hovering over
|
private Widget tipWidget; // widget this tooltip is hovering over
|
||||||
private Point tipPosition; // the position being hovered over
|
private Point tipPosition; // the position being hovered over
|
||||||
|
|
||||||
private final TableColumn[] columns;
|
|
||||||
|
|
||||||
private static final int hoverYOffset = 1;
|
private static final int hoverYOffset = 1;
|
||||||
|
|
||||||
private static final String[] COLUMN_NAMES = { "Name", "Value"};
|
|
||||||
|
|
||||||
|
|
||||||
private static final int MAX_CHARS = 48;
|
|
||||||
// The names of the first 32 characters
|
|
||||||
private Color[] colors = new Color[MAX_CHARS];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new tooltip handler
|
* Creates a new tooltip handler
|
||||||
*
|
*
|
||||||
* @param parent the parent Shell
|
* @param parent the parent Shell
|
||||||
*/
|
*/
|
||||||
public ToolTipHandler(Shell parent) {
|
public ToolTipHandler(Shell parent) {
|
||||||
final Display display = parent.getDisplay();
|
display = parent.getDisplay();
|
||||||
this.parentShell = parent;
|
this.parentShell = parent;
|
||||||
|
|
||||||
tipShell = new Shell(parent, SWT.ON_TOP | SWT.TOOL);
|
shell = new Shell(parent, SWT.ON_TOP | SWT.NO_FOCUS | SWT.TOOL);
|
||||||
GridLayout gridLayout = new GridLayout();
|
GridLayout gridLayout = new GridLayout();
|
||||||
gridLayout.numColumns = 1;
|
gridLayout.numColumns = 1;
|
||||||
gridLayout.marginWidth = 2;
|
gridLayout.marginWidth = 2;
|
||||||
gridLayout.marginHeight = 2;
|
gridLayout.marginHeight = 2;
|
||||||
tipShell.setLayout(gridLayout);
|
shell.setLayout(gridLayout);
|
||||||
|
|
||||||
tipShell.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
|
shell.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
|
||||||
|
|
||||||
tipLabelText = new Label(tipShell, SWT.NONE);
|
label = new Label(shell, SWT.NONE);
|
||||||
tipLabelText.setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND));
|
label.setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND));
|
||||||
tipLabelText.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
|
label.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
|
||||||
tipLabelText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL |GridData.VERTICAL_ALIGN_CENTER));
|
label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL |GridData.VERTICAL_ALIGN_CENTER));
|
||||||
|
|
||||||
final Font font = new Font(Display.getCurrent(), "Terminal", 10, SWT.NORMAL);
|
final Font font = new Font(Display.getCurrent(), "Terminal", 10, SWT.NORMAL);
|
||||||
tipTable = new Table(tipShell, SWT.NONE);
|
table = new Table(shell, SWT.NONE);
|
||||||
tipTable.setHeaderVisible(true);
|
table.setHeaderVisible(false);
|
||||||
tipTable.setLinesVisible(true);
|
table.setLinesVisible(true);
|
||||||
tipTable.setFont(font);
|
table.setFont(font);
|
||||||
|
table.setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND));
|
||||||
|
table.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
|
||||||
|
table.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
|
||||||
|
|
||||||
|
nameCol = new TableColumn(table, SWT.LEFT);
|
||||||
|
nameCol.setText("Name");
|
||||||
|
valueCol = new TableColumn(table, SWT.LEFT);
|
||||||
|
nameCol.setText("Value");
|
||||||
|
|
||||||
columns = createColumns(tipTable);
|
shell.addPaintListener(new PaintListener() {
|
||||||
|
@Override
|
||||||
tipTable.setRedraw(false);
|
public void paintControl(PaintEvent e) {
|
||||||
for (int i = 0; i < MAX_CHARS; i++) {
|
Rectangle area = shell.getClientArea();
|
||||||
// Create a background color for this row
|
valueCol.setWidth(area.width - nameCol.getWidth());
|
||||||
colors[i] = new Color(tipTable.getDisplay(), 255 - i, 127 + i, i);
|
}
|
||||||
|
});
|
||||||
// Create the row in the table by creating
|
|
||||||
// a TableItem and setting text for each
|
|
||||||
// column
|
|
||||||
int c = 0;
|
|
||||||
TableItem item = new TableItem(tipTable, SWT.NONE);
|
|
||||||
item.setText(c++, String.valueOf((char) i));
|
|
||||||
item.setText(c++, String.valueOf(i));
|
|
||||||
item.setBackground(colors[i]);
|
|
||||||
}
|
|
||||||
// Now that we've set the text into the columns,
|
|
||||||
// we call pack() on each one to size it to the
|
|
||||||
// contents.
|
|
||||||
for (int i = 0, n = columns.length; i < n; i++)
|
|
||||||
columns[i].pack();
|
|
||||||
// Set redraw back to true so that the table
|
|
||||||
// will paint appropriately
|
|
||||||
tipTable.setRedraw(true);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private TableColumn[] createColumns(Table table) {
|
|
||||||
TableColumn[] columns = new TableColumn[COLUMN_NAMES.length];
|
|
||||||
for (int i = 0, n = columns.length; i < n; i++) {
|
|
||||||
columns[i] = new TableColumn(table, SWT.LEFT);
|
|
||||||
columns[i].setText(COLUMN_NAMES[i]);
|
|
||||||
}
|
|
||||||
return columns;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -122,27 +98,16 @@ class ToolTipHandler {
|
||||||
* Get out of the way if we attempt to activate the control underneath the tooltip
|
* Get out of the way if we attempt to activate the control underneath the tooltip
|
||||||
*/
|
*/
|
||||||
control.addMouseListener(MouseListener.mouseDownAdapter(e -> {
|
control.addMouseListener(MouseListener.mouseDownAdapter(e -> {
|
||||||
if (tipShell.isVisible())
|
if (shell.isVisible())
|
||||||
tipShell.setVisible(false);
|
shell.setVisible(false);
|
||||||
}));
|
}));
|
||||||
/*
|
|
||||||
* get out of the way if we move the mouse
|
|
||||||
*/
|
|
||||||
control.addMouseMoveListener(new MouseMoveListener() {
|
|
||||||
@Override
|
|
||||||
public void mouseMove(MouseEvent e) {
|
|
||||||
if (tipShell.isVisible())
|
|
||||||
tipShell.setVisible(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
/*
|
/*
|
||||||
* Trap hover events to pop-up tooltip
|
* Trap hover events to pop-up tooltip
|
||||||
*/
|
*/
|
||||||
control.addMouseTrackListener(new MouseTrackAdapter () {
|
control.addMouseTrackListener(new MouseTrackAdapter () {
|
||||||
@Override
|
@Override
|
||||||
public void mouseExit(MouseEvent e) {
|
public void mouseExit(MouseEvent e) {
|
||||||
if (tipShell.isVisible()) tipShell.setVisible(false);
|
if (shell.isVisible()) shell.setVisible(false);
|
||||||
tipWidget = null;
|
tipWidget = null;
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
|
@ -162,45 +127,47 @@ class ToolTipHandler {
|
||||||
widget = w.getItem (pt);
|
widget = w.getItem (pt);
|
||||||
}
|
}
|
||||||
if (widget == null) {
|
if (widget == null) {
|
||||||
tipShell.setVisible(false);
|
shell.setVisible(false);
|
||||||
tipWidget = null;
|
tipWidget = null;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (widget == tipWidget && tipShell.isVisible()) return;
|
Point newPos = control.toDisplay(pt);
|
||||||
|
if(shell.isFocusControl()) return;
|
||||||
|
if (widget == tipWidget && tipPosition.equals(newPos) && shell.isVisible()) return;
|
||||||
tipWidget = widget;
|
tipWidget = widget;
|
||||||
tipPosition = control.toDisplay(pt);
|
tipPosition = newPos;
|
||||||
boolean showDialog = false;
|
boolean showDialog = false;
|
||||||
Object o = widget.getData(Constants.CONTENT_PROVIDER_TAG);
|
Object o = widget.getData(Constants.CONTENT_PROVIDER_TAG);
|
||||||
if(o != null) {
|
if(o != null) {
|
||||||
ToolTipTableContentProvider provider = ((ToolTipTableContentProvider)o).initialize(widget, pt);
|
ToolTipTableContentProvider provider = ((ToolTipTableContentProvider)o).initialize(widget, pt);
|
||||||
tipLabelText.setText(provider.getTableTitle());
|
label.setText(provider.getTableTitle());
|
||||||
tipTable.setRedraw(false);
|
table.setRedraw(false);
|
||||||
tipTable.removeAll();
|
table.removeAll();
|
||||||
for (String[] strings : provider.getTableContent()) {
|
for (String[] strings : provider.getTableContent()) {
|
||||||
if(strings.length>0) {
|
if(strings.length>0) {
|
||||||
showDialog=true;
|
showDialog=true;
|
||||||
TableItem item = new TableItem(tipTable, SWT.NONE);
|
TableItem item = new TableItem(table, SWT.NONE);
|
||||||
item.setText(0, strings[0]);
|
item.setText(0, strings[0]);
|
||||||
if(strings.length>1)
|
if(strings.length>1)
|
||||||
item.setText(1, strings[1]);
|
item.setText(1, strings[1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (int i = 0, n = columns.length; i < n; i++)
|
nameCol.pack();
|
||||||
columns[i].pack();
|
valueCol.pack();
|
||||||
tipTable.setRedraw(true);
|
table.setRedraw(true);
|
||||||
tipTable.setVisible(true);
|
table.setVisible(true);
|
||||||
} else {
|
} else {
|
||||||
tipTable.setVisible(false);
|
table.setVisible(false);
|
||||||
}
|
}
|
||||||
String text = (String) widget.getData(Constants.TEXT_PROVIDER_TAG);
|
String text = (String) widget.getData(Constants.TEXT_PROVIDER_TAG);
|
||||||
if(text != null) {
|
if(text != null) {
|
||||||
tipLabelText.setText(text != null ? text : "Hover test should go here");
|
label.setText(text != null ? text : "Hover test should go here");
|
||||||
showDialog=true;
|
showDialog=true;
|
||||||
}
|
}
|
||||||
if(showDialog) {
|
if(showDialog) {
|
||||||
tipShell.pack();
|
shell.pack();
|
||||||
setHoverLocation(tipShell, tipPosition);
|
setHoverLocation(shell, tipPosition);
|
||||||
tipShell.setVisible(true);
|
shell.setVisible(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -215,8 +182,8 @@ class ToolTipHandler {
|
||||||
String text = handler.getHelpText(tipWidget);
|
String text = handler.getHelpText(tipWidget);
|
||||||
if (text == null) return;
|
if (text == null) return;
|
||||||
|
|
||||||
if (tipShell.isVisible()) {
|
if (shell.isVisible()) {
|
||||||
tipShell.setVisible(false);
|
shell.setVisible(false);
|
||||||
Shell helpShell = new Shell(parentShell, SWT.SHELL_TRIM);
|
Shell helpShell = new Shell(parentShell, SWT.SHELL_TRIM);
|
||||||
helpShell.setLayout(new FillLayout());
|
helpShell.setLayout(new FillLayout());
|
||||||
Label label = new Label(helpShell, SWT.NONE);
|
Label label = new Label(helpShell, SWT.NONE);
|
||||||
|
@ -226,6 +193,11 @@ class ToolTipHandler {
|
||||||
helpShell.open();
|
helpShell.open();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
// control.addKeyListener(KeyListener.keyPressedAdapter( e-> {
|
||||||
|
// if (e.keyCode == SWT.F2 && shell.isVisible()) {
|
||||||
|
// shell.setFocus();
|
||||||
|
// }
|
||||||
|
// }));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue