NetBeans平台:禁用操作,但快捷方式处于活动状态

时间:2012-06-11 18:19:49

标签: java keyboard-shortcuts action toolbar netbeans-platform

在对我的申请进行了一个月的工作后,我发现了一些奇特的东西。我有一个查看器模块,其中包含所有TopComponents,以及一个MenuToolbar模块,这是我保留所有工具栏操作的地方。这是我的补充:

package com.demo.toolbar;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.openide.awt.ActionRegistration;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionID;
import org.openide.util.NbBundle.Messages;

@ActionID(category = "Edit",
id = "com.demo.toolbar.AddAction")
@ActionRegistration(iconBase = "com/demo/toolbar/icons/add.png",
displayName = "#CTL_AddAction")
@ActionReferences({
    @ActionReference(path = "Toolbars/AddEditDelete", position = 1),
    @ActionReference(path = "Shortcuts", name = "D-A")
})
@Messages("CTL_AddAction=Add")
public final class AddAction implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        //code here
    }
}

通过CTRL + A激活该快捷方式,并将TopComponent置于添加模式。我还有一个用CTRL + D命令激活的DeleteAction。当该人遇到CTRL + A时,会发生以下情况:

List<Component> c = new ArrayList<Component>();
        c.addAll(Arrays.asList(ToolbarPool.getDefault().findToolbar("AddEditDelete").getComponents()));
if (mode.equals("add")) {
    for (Component component : c) {
        component.setEnabled(false);
        }
        c.get(13).setEnabled(true);
        c.get(14).setEnabled(true);
}

因此,基本上每当用户点击工具栏上的“添加”按钮时,它都会禁用所有其他按钮(包括删除),以便用户在“添加”模式下无法执行这些操作。

然而,他们仍然可以按CTRL + D删除。这是一个很大的禁忌......

我该如何解决此问题?

1 个答案:

答案 0 :(得分:1)

您不应直接启用/禁用操作。看看the Actions APICookieAction可能就是你想要的。我们的想法是将Cookie(某些上下文)发布到全局查找。您可以自动启用/禁用Cookie感知操作,具体取决于Cookie的存在。

实际上这就是IDE中的保存按钮的工作原理。每当编辑器将SaveCookie放入全局上下文时,工具栏按钮和Ctrl + S都会启用;正如所描述的here

您可以考虑使用state machine以干净的方式控制Cookie的存在。

相关问题