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

时间:2018-08-24 14:18:24

标签: netbeans-platform

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

@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
  }
}

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

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

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

1 个答案:

答案 0 :(得分:2)

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

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

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

如果您还想启用Delete键,则必须将相同的动作图键放置到轮廓视图的输入图中:

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