Mouse and Keyboard support for Actions in Tree

Answered
I'm learning about how best to organize actions for Tree that I have in a ToolWindow.

I would like to trigger an existing Action that I have when the user double-clicks a node in the Tree that I have, AND when the user hits the key VK_ENTER

I have already added a bunch of actions in an ActionGroup to a popup menu on my custom Tree using
PopupHandler.installPopupMenu()
that contains all the possible actions for any selected node (when the menu pops up).

Most of these actions are already configured with keyboard shortcuts (using registerCustomShortcutSet).
 
This all works just great when the right-click on a node in the Tree, then the popup menu shows what Actions are available, and the user can invoke the appropriate action from the menu.

So, now I want to fire one of those actions when the user double-clicks the node in the Tree
(preferable selecting the action that has the VK_ENETER registered, but I'll settle for something else)

How should I be arranging all this to support both keys and mouse clicks? (edited) 
0
3 comments

double-click: com.intellij.util.EditSourceOnDoubleClickHandler#install(javax.swing.JTree, java.lang.Runnable)

0

Thanks Yann Cebron,
I looked at that API, and decided it is not what I need.
My custom `Tree` is not file based.

Instead, I want to learn about the best practices for defining and managing a set of `AnAction` that are used by the `Tree` in different ways based upon its selected `DataContext`.

I understand how to assign them to a context menu, but I am missing the link to other mouse actions, like double-click.

I recognize that `PopupHandler.installPopupMenu` simply registers a `MouseListener` and that displays a menu with an `ActionGroup`.

I want to learn how to reuse that `ActionGroup` to display one of those actions when double-clicking, in another `MouseListener`.

Are there any examples of a `Tree` that is not file based?
I'd like to better understand how the `PopupHandler` processes each Action in the group to decide what is enabled or not at any time, the popup menu is shown.
and do the same thing on double-click.
Then be able to select one of the `AnAction` that is enabled, and if possible, also assigned to the `VK_ENTER` key.

 

My other option is to simply hardcode which `AnAction` I want in the double-click `MouseListener`, new that up and call `update` and `actionPerformed` on it manually. But not sure how to do that myself?

Can you help me with that?





 

0

Yann Cebron

A little further investigation (into PopupMenuHandler) yielded this approach.

Does it look like a reasonable approach?

    myTree.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {

var button = e.getButton();
if (button != MouseEvent.BUTTON1
|| e.getClickCount() != 2) {
return;
}

var actionEvent = AnActionEvent.createFromInputEvent(e, place, null, DataManager.getInstance().getDataContext(component));
var actions = ActionGroupUtil.getActiveActions(actionGroup, actionEvent);
actions.filter(action -> Arrays.stream(action.getShortcutSet().getShortcuts())
.anyMatch(shortcut -> shortcut.isKeyboard()
&& ((KeyboardShortcut) shortcut).getFirstKeyStroke().getKeyCode() == KeyEvent.VK_ENTER))
.collect();
if (actions.isNotEmpty()) {
var action = actions.first();
if (action != null) {
action.actionPerformed(actionEvent);
}
}
}
0

Please sign in to leave a comment.