如何在IconView中显示节点操作

时间:2015-02-03 16:04:59

标签: netbeans nodes netbeans-platform

在我的Netbeans平台项目中,我有TopComponentIconView元素,但当我向节点添加NodeAction时,它不会显示。右键单击节点不会弹出上下文菜单。如何显示动作?这就是我所拥有的:

public class ItemDeleteAction extends NodeAction
{
    ⋮
    @Override
    protected boolean enable(Node[] nodes)
    {
        for (int i = 0; i < nodes.length; i++) {
            Node node = nodes[i];
            if (node instanceof ViewerItemNode) return true;
        }
        return false;
    }
    ⋮
}

public final class JFlavourViewerTopComponent extends TopComponent implements ExplorerManager.Provider
{
    public JFlavourViewerTopComponent()
    {
        ⋮
        setLayout(new BorderLayout());
        add(new IconView(), BorderLayout.CENTER);
        ActionMap actionMap = getActionMap();
        actionMap.put("delete", new ItemDeleteAction());
        associateLookup(ExplorerUtils.createLookup(explorerManager, actionMap));
    }
}

public class ViewerItemNode extends AbstractNode
{
    private ViewerItem item;

    public ViewerItemNode(ViewerItem item, JFlavourProjectBean activeProject)
    {
        super (Children.LEAF, Lookups.singleton(item));
        this.item = item;
        setDisplayName (item.item.getLabel());
    }

    @Override
    public Action[] getActions(boolean context)
    {
        if (!context) {
            return new Action[]
            {
                SystemAction.get( ItemDeleteAction.class )
            };
        } else {
            return super.getActions(context);
        }
    }

    @Override
    public Action getPreferredAction()
    {
        return SystemAction.get(PlayAudioAction.class);
    }
}

我认为这是所有相关的代码。

我在另一个模块中的另一个TopComponent做同样的事情并且它工作正常,但在该模块中,节点显示为BeanTreeView而不是IconView

1 个答案:

答案 0 :(得分:0)

尝试删除context条件:

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