Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » generalization of Snippet 125 "Tooltip on TableItem" (for Tree or Table)
generalization of Snippet 125 "Tooltip on TableItem" (for Tree or Table) [message #463236] Wed, 02 November 2005 08:32
Paul E. Keyser is currently offline Paul E. KeyserFriend
Messages: 878
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------000609090801050301090403
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit

The attached plaintext file, TooltipListener.java, contains an abstract class based on the code in
Snippet 125, which can be used to add tooltips to either Table-items or to Tree-items. I post it
here, thinking it could be useful to others.

Paul


--------------000609090801050301090403
Content-Type: text/plain;
name="TooltipListener.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="TooltipListener.java"

// be sure to add requisite imports!

/**
* TooltipListener is a Listener that creates a tool-tip for an {@link Item}
* in a Table or a Tree. The code has been copied and modified from
* "Snippet 125" of Eclipse, i.e.: copied and modified from
* http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet125.java
*
* @author keyser
*/
protected static abstract class TooltipListener implements Listener {

/**
* @param comp Composite, i.e., a Table or a Tree
*/
protected TooltipListener(final Composite comp) {
this.shell = comp.getShell();
this.display = this.shell.getDisplay();
this.comp = comp;
}

/**
* @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt .widgets.Event)
*/
public void handleEvent(Event event) {
switch (event.type) {
case SWT.Dispose:
case SWT.KeyDown:
case SWT.MouseMove: {
disposeTip();
break;
}
case SWT.MouseHover: {
final Item item = retrieveItem(event);
if (item != null) {
disposeTip(); // clear out old tip, if any
createTipFor(item);
}
}
}
}

/**
* Create the tooltip for the given Item
* @param item Item
*/
protected void createTipFor(final Item item) {
tip = new Shell(shell, SWT.ON_TOP | SWT.TOOL);
tip.setLayout(new FillLayout());
label = new Label(tip, SWT.NONE);
label.setForeground(display.getSystemColor(SWT.COLOR_INFO_FO REGROUND));
label.setBackground(display.getSystemColor(SWT.COLOR_INFO_BA CKGROUND));
label.setText(computeTextFor(item));

Point size = tip.computeSize(SWT.DEFAULT, SWT.DEFAULT);
Point locn = getItemLocation(item);
tip.setBounds(locn.x, locn.y, size.x, size.y);

// klugey way to pass the Item to the label-listener
label.setData("_TABLEITEM", item);
label.addListener(SWT.MouseExit, labelListener);
label.addListener(SWT.MouseDown, labelListener);

tip.setVisible(true);
}

/**
* Compute the text to be displayed for the given Item
* @param item Item
* @return String
*/
protected abstract String computeTextFor(final Item item);

/**
* Compute the location of the given Item in coordinates relative to display
* @param item Item
* @return Point
*/
protected abstract Point getItemLocation(final Item item);
// Rectangle rect = ((TreeItem) item).getBounds(0);
// return tree.toDisplay(rect.x, rect.y);


/**
* Retrieve the Item at the location of the Event
* @param event Event
* @return Item
*/
protected abstract Item retrieveItem(final Event event);
// return tree.getItem(new Point(event.x, event.y));


/**
* Select the given Item as if the underlying Composite received the mouse down event
* @param item Item
*/
protected abstract void selectItem(final Item item);
// tree.setSelection(new TreeItem[] { (TreeItem) item });


/**
* Dispose the tooltip
*/
protected void disposeTip() {
if (tip != null && !tip.isDisposed()) {
tip.dispose();
tip = null;
label = null;
}
}

protected final Listener labelListener = new Listener() {
public void handleEvent(Event event) {
switch (event.type) {
case SWT.MouseDown:
final Item item = (Item) label.getData("_TABLEITEM");
selectItem(item);

Event e = new Event();
e.item = item;
comp.notifyListeners(SWT.Selection, e);
; // fall through, so as to dispose the tip's Shell
case SWT.MouseExit:
disposeTip();
break;
}
}
};

protected final Composite comp;
protected final Display display;
protected final Shell shell;

protected Shell tip = null;
protected Label label = null;
}


--------------000609090801050301090403--
Previous Topic:Getting the content of the Browser
Next Topic:Is there a good data binding framework for SWT?
Goto Forum:
  


Current Time: Mon Dec 22 12:12:28 GMT 2014

Powered by FUDForum. Page generated in 0.11486 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software