How to register actions programmatically with NetBeans Platform

"winder" <[email protected]>
Newsgroups gmane.comp.java.netbeans.modules.openide.devel
Message-ID <[email protected]>
Thanks Mark, I think I'm on the right track.

I'm now trying to create the instance as an action (So that it shows up in the Keymap), then create a Menu shadow entry, finally the shortcut shadow entry with a default value.

The action seems to be created now but the shadow entries are not working (Including the menu which was working earlier).

This is what I have so far:

Code:

    private void registerAction(String name, String category, String shortcut, String menuPath, Action a) throws IOException {
        ///////////////////////
        // Add/Update Action //
        ///////////////////////
        String originalFile = "Actions/" + category + "/" + name + ".instance";
        FileObject in = getFolderAt("Actions/" + category);
        FileObject obj = in.getFileObject(name, "instance");
        if (obj == null) {
            obj = in.createData(name, "instance");
        }
        a.putValue(Action.NAME, name);
        obj.setAttribute("instanceCreate", a);
        obj.setAttribute("instanceClass", a.getClass().getName());

        /////////////////////
        // Add/Update Menu //
        /////////////////////
        in = getFolderAt(menuPath);
        obj = in.getFileObject(name, "shadow");
        // Create if missing.
        if (obj == null) {
            obj = in.createData(name, "shadow");
            obj.setAttribute("originalFile", originalFile);
        }

        /////////////////////////
        // Add/Update Shortcut //
        /////////////////////////
        in = getFolderAt("Shortcuts");
        obj = in.getFileObject(shortcut, "shadow");
        if (obj == null) {
            obj = in.createData(shortcut, "shadow");
            obj.setAttribute("originalFile", originalFile);
        }
    }

    // This helper seems to work fine
    private static FileObject getFolderAt(String inputPath) throws IOException {
        String parts[] = inputPath.split("/");
        FileObject existing = FileUtil.getConfigFile(inputPath);
        if (existing != null)
            return existing;

        FileObject base = FileUtil.getConfigFile(parts[0]);
        if (base == null) return null;

        for (int i = 1; i < parts.length; i++) {
            String path = Joiner.on('/').join(Arrays.copyOfRange(parts,0,i+1));
            FileObject next = FileUtil.getConfigFile(path);
            if (next == null) {
                next = base.createFolder(parts[i]);
            }
            base = next;
        }

        return FileUtil.getConfigFile(inputPath);
    }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.