在资源管理器视图中而不是在 TopComponent 中删除节点

Deleting nodes in a explorer view not in a TopComponent

我在从 DialogDescriptor 创建的对话框中使用资源管理器视图 (OutlineView)。以下是我的代码的精简版:

@ActionID(category = "Example", id = "org.example.Test")
@ActionRegistration(displayName = "Test")
@ActionReference(path = "Menu/File", position = 0)
public class SomeAction implements ActionListener {

  @Override
  public void actionPerformed(ActionEvent e) {
    DialogDescriptor dd = new DialogDescriptor(new MyPanel(), "Titel", true, null);
    DialogDisplayer.getDefault().notify(dd);
  }
}

class MyPanel extends JPanel implements ExplorerManager.Provider {
  private final ExplorerManager em;

  public MyPanel() {
    em = new ExplorerManager();
    em.setRootContext(new MyNode());
    add(new OutlineView());
  }

  @Override
  public ExplorerManager getExplorerManager() {
    return em;
  }
}

class MyNode extends AbstractNode {
  public MyNode() { super(Children.LEAF); }

  @Override
  public Action[] getActions(boolean context) {
    return new Action[] { SystemAction.get(DeleteAction.class) };
  }

  @Override
  public boolean canDestroy() {
    return true;
  }

  @Override
  public void destroy() {
    // Never called
  }
}

调用动作时,会显示对话框,大纲视图会显示根节点。但是,从节点的上下文菜单中选择 Delete 会打开一个确认对话框,以删除在活动 TopComponent 后面选择的任何内容模态对话框。如何让 Delete 系统操作考虑对话框中的选择?我想我需要类似于此的东西

ActionMap map = getActionMap();
map.put("delete", ExplorerUtils.actionDelete(em, true));
associateLookup(ExplorerUptils.createLookup(em, map));

取自 TopComponent,但无法完全弄清楚哪里出了问题。因此,非常感谢任何指点。

在 MyPanel 构造函数中,您必须将删除操作添加到大纲视图的操作映射中:

OutlineView outlineView = new OutlineView();
DeleteAction delAction = SystemAction.get(DeleteAction.class);

outlineView.getOutline().getActionMap().put(delAction.getActionMapKey(), ExplorerUtils.actionDelete(em,true));

如果您还想启用删除键,则必须将相同的操作键添加到大纲视图的输入图中:

KeyStroke delKey = KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0);
outlineView.getOutline().getInputMap().put(delKey, delAction.getActionMapKey());