Highlight TreeItems on mouse over [message #326066] |
Thu, 06 March 2008 13:28 |
Eclipse User |
|
|
|
Hi,
I have a TreeViewer and Tree (generated from emf model) and I wanted to mimic highlighting TreeItems
when the mouse passes over them - (similar to a mouseover on a webpage) i.e. changing the cursor and
colour of each item as the mouse passes over it and setting it back to the normal once the mouse has
passed it.
I attached a MouseMoveListener to the tree, which worked ok, but I was wondering if there is any
other mechanism by which I could perform this type of highlighting?
public void mouseMove(MouseEvent e)
{
// get current TreeItem that mouse is over
TreeItem item = treeViewer.getTree().getItem(new Point (e.x, e.y));
// turn off default too
treeViewer.getTree().setToolTipText("");
// test if we want to highlight it
if (item != null && item.getData() instanceof ComponentType)
{
// make sure it's not already highlighted
if (!item.equals(previousItem))
{
// unhighlight any previously highlighted item
if (previousItem != null)
{
previousItem.setBackground(previousBgColor);
previousItem.setForeground(previousFgColor);
}
// highlight the new item
previousBgColor = item.getBackground();
previousFgColor = item.getForeground();
previousItem = item;
item.setBackground(highlightBgColor);
item.setForeground(highlightFgColor);
// change the cursor to a hand
if (!isMouseCursorDisplayed)
{
System.out.println("setting cursor");
isMouseCursorDisplayed = true;
treeViewer.getTree().setCursor(cursor);
}
}
}
// item is null or we don't want to highlight this item
else
{
// remove highlighting
if (previousItem != null)
{
previousItem.setBackground(previousBgColor);
previousItem.setForeground(previousFgColor);
previousItem = null;
}
// set cursor to normal
if (isMouseCursorDisplayed)
{
isMouseCursorDisplayed = false;
treeViewer.getTree().setCursor(null);
}
}
}
Many thanks
Conor.
|
|
|
Powered by
FUDForum. Page generated in 0.03003 seconds