Re: How to register actions programmatically with NetBeans Platform
Johannes Wahle <[email protected]>
| Newsgroups | gmane.comp.java.netbeans.modules.openide.devel |
|---|---|
| Message-ID | <[email protected]> |
You can remove all the null checks and getFolderAt() by:
String path = ...
FileObject root= FileUtil.getConfigRoot();
FileObject f = FileUtil.createData(root, path);
This creates the file including nonexistent parent folders if necessary
(FileUtil.createFolder() does the equivalent for folders).
Generally the FileUtil class contains a lot of useful stuff.
Regards,
Johannes
On 12.03.2016 04:32, winder wrote:
> Thanks again for the tips, everything is working great now. I wrapped it up in a service that can go on the wiki.
>
>
>
> Code:
>
> import com.google.common.base.Joiner;
> import java.io.IOException;
> import java.util.Arrays;
> import javax.swing.Action;
> import org.openide.filesystems.FileObject;
> import org.openide.filesystems.FileUtil;
> import org.openide.util.lookup.ServiceProvider;
>
> /**
> *
> * @author wwinder
> */
> @ServiceProvider(service=ActionRegistrationService.class)
> public class ActionRegistrationService {
> /**
> * Registers an action with the platform along with optional shortcuts and
> * menu items.
> * @param name Display name of the action.
> * @param category Category in the Keymap tool.
> * @param shortcut Default shortcut, use an empty string or null for none.
> * @param menuPath Menu location starting with "Menu", like "Menu/File"
> * @param action an action object to attach to the action entry.
> * @throws IOException
> */
> public void registerAction(String name, String category, String shortcut, String menuPath, Action action) 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");
> }
> action.putValue(Action.NAME, name);
> obj.setAttribute("instanceCreate", action);
> obj.setAttribute("instanceClass", action.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);
> }
> }
>
> private 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);
> }
> }
>
>
>
>
> Now to register actions you just call the service:
>
> Code:
>
> private void initActions() {
> ActionRegistrationService ars = Lookup.getDefault().lookup(ActionRegistrationService.class);
>
> try {
> String menuPath = "Menu/Machine/Jog";
>
> ars.registerAction(getMessage(this.getClass(), "JogService.xPlus") ,
> "Machine", "M-RIGHT" , menuPath, new JogAction(this, 1, 0, 0));
> ars.registerAction(getMessage(this.getClass(), "JogService.xMinus"),
> "Machine", "M-LEFT" , menuPath, new JogAction(this,-1, 0, 0));
> ars.registerAction(getMessage(this.getClass(), "JogService.yPlus") ,
> "Machine", "M-UP" , menuPath, new JogAction(this, 0, 1, 0));
> ars.registerAction(getMessage(this.getClass(), "JogService.yMinus"),
> "Machine", "M-DOWN" , menuPath, new JogAction(this, 0,-1, 0));
> ars.registerAction(getMessage(this.getClass(), "JogService.zPlus") ,
> "Machine", "SM-UP" , menuPath, new JogAction(this, 0, 0, 1));
> ars.registerAction(getMessage(this.getClass(), "JogService.zMinus"),
> "Machine", "SM-DOWN" , menuPath, new JogAction(this, 0, 0,-1));
> } catch (IOException ex) {
> Exceptions.printStackTrace(ex);
> }
> }
>
>
>
>
>
>
>