wiki-convert/wiki-asciidoc/DevFaqActionsAddAtRuntime.asciidoc (126 lines of code) (raw):

// // Licensed to the Apache Software Foundation (ASF) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information // regarding copyright ownership. The ASF licenses this file // to you under the Apache License, Version 2.0 (the // "License"); you may not use this file except in compliance // with the License. You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, // software distributed under the License is distributed on an // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, either express or implied. See the License for the // specific language governing permissions and limitations // under the License. // = DevFaqActionsAddAtRuntime :jbake-type: wiki :jbake-tags: wiki, devfaq, needsreview :jbake-status: published == How do I add an action (incl. shortcut) at runtime? Create system file system entries in the `Actions`/`Menu`/`Shortcuts` folders! See the following example Usage: [source,java] ---- 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)); } catch (IOException ex) { Exceptions.printStackTrace(ex); } } ---- Helper-Service [source,java] ---- 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); } } ---- Taken from mailing list link:http://forums.netbeans.org/topic65421.html[http://forums.netbeans.org/topic65421.html] Based on link:https://blogs.oracle.com/geertjan/entry/dynamically_creating_menu_items_part[https://blogs.oracle.com/geertjan/entry/dynamically_creating_menu_items_part] === Apache Migration Information The content in this page was kindly donated by Oracle Corp. to the Apache Software Foundation. This page was exported from link:http://wiki.netbeans.org/DevFaqActionsAddAtRuntime[http://wiki.netbeans.org/DevFaqActionsAddAtRuntime] , that was last modified by NetBeans user Markiewb on 2016-03-13T14:03:58Z. *NOTE:* This document was automatically converted to the AsciiDoc format on 2018-01-10, and needs to be reviewed.