Strizzi
Strizzi

Reputation: 59

Java SWT get bounds of custom element in TreeView

I´ve created a TreeView with a ContentProvider and custom tree elements. I also have a ISelectionChangedListener added to the TreeView.

I want to add an MouseListener, do detect if an element of the tree is right-clicked and show a popup menu. If the white area around the tree is clicked, i don´t want to display the popup menu. The menu is added via Extensions in the plugin.xml.

How can I now evaluate if a tree element is right-clicked, so I can show the popup menu (maybe with visibleWhen in the plugin.xml) ? I also want to clear the selection, if the right-click is detected in the white area of the TreeView.

Upvotes: 0

Views: 145

Answers (1)

Strizzi
Strizzi

Reputation: 59

Ok, i did not realized that i can still use tree.getItem(...). So here is my full MouseListener:

treeOPCUA.addMouseListener(new MouseListener()
{
    @Override
    public void mouseUp(MouseEvent e)
    {
        if(e.button == 3 && rightMouseClicked == true)
            rightMouseClicked = false;
    }

    @Override
    public void mouseDown(MouseEvent e)
    {
        if(e.button == 3 && rightMouseClicked == false)
            rightMouseClicked = true;
        if(treeOPCUA.getItem(new Point(e.x, e.y)) == null)
            viewer.setSelection(null);
    }

    @Override
    public void mouseDoubleClick(MouseEvent e)
    {
        viewer.setExpandedState(e.getSource(), true);
    }
});

With the boolean variable "rightMouseClicked" I detect in my ISelectionChangedListener if the right mouse is clicked:

if(event.getSelection() instanceof IStructuredSelection && !rightMouseClicked)

I hope this answer helps anyone.

Upvotes: 0

Related Questions