AbstractActions 的枚举集包装器

发布于 2024-09-15 11:54:44 字数 395 浏览 5 评论 0原文

我想在运行时将一系列 Swing 操作加载到容器中,并通过常量名称访问它们,就像使用枚举一样。这样做的目的是限制可能的操作并提高可读性。

最初,我正在考虑一种动态枚举(请参阅 http ://blog.xebia.com/2009/04/02/dynamic-enums-in-java/),但正如 Java Drinker 下面指出的那样,这是不必要的,因为操作保持不变。

理想情况下,我想要做的是一种包含 AbstractAction 实例的包装器,这些实例可以启用/禁用,并且能够通过符号名称引用每个操作。

编辑:问题已重新表述。

I'd like to load a series of Swing Actions into a container at runtime and access them by a constant name as is possible with an Enum. The purpose of this would be to both restrict the Actions possible and also improve readability.

Initially, I was considering a sort of dynamic enum (see http://blog.xebia.com/2009/04/02/dynamic-enums-in-java/) but as Java Drinker points out below this is unnecessary since the actions remain unchanged.

Ideally, what I'd like to do is a sort of wrapper that contains AbstractAction instances which could be enabled/disabled and be able to refer to each action through a symbolic name.

Edit: Question has been reformulated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叹梦 2024-09-22 11:54:44

您可以这样做:

public enum Actions {
    COPY( new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            //do something
        }
    }),
    PASTE( new PasteAction() );

    public final AbstractAction action;
    private Actions(AbstractAction action) {
        this.action = action;
    }

}

//...
Actions.COPY.action...;

然后正如其他人所说,将枚举与 EnumSet 或 EnumMap 结合使用。就我个人而言,我认为它并没有比仅仅拥有一些没有枚举的命名字段有更大的价值。

我认为您真正想要的是某种像这样的操作注册表:

public class ActionsManager {
    private final Map<String, Action> actions;
    private final Map<User, Set<String>> enabledActions;
    public Action get(String id);
    public void register(String id, Action action);
    public void deregister(String id);
    public boolean isEnabled(User user, Action action);
}

实施留作练习。

You can do something like this:

public enum Actions {
    COPY( new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            //do something
        }
    }),
    PASTE( new PasteAction() );

    public final AbstractAction action;
    private Actions(AbstractAction action) {
        this.action = action;
    }

}

//...
Actions.COPY.action...;

And then as others have said, use the enum in conjunction with an EnumSet or EnumMap. Personally, I don't see a huge value in it over just having a few named fields without an enum.

I think what you really want is some kind of Action registry like this:

public class ActionsManager {
    private final Map<String, Action> actions;
    private final Map<User, Set<String>> enabledActions;
    public Action get(String id);
    public void register(String id, Action action);
    public void deregister(String id);
    public boolean isEnabled(User user, Action action);
}

Implementation left as an exercise.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文