# HG changeset patch # Parent 8806d22a885d457211176c9b34fcdd968c62b9fc # User Jesse Glick #205798: API to retrieve a specific action by ID. XXX TODO: review codebase for other usages of `"Actions/' or similar patterns. diff --git a/bugtracking/src/org/netbeans/modules/bugtracking/util/FindTypesSupport.java b/bugtracking/src/org/netbeans/modules/bugtracking/util/FindTypesSupport.java --- a/bugtracking/src/org/netbeans/modules/bugtracking/util/FindTypesSupport.java +++ b/bugtracking/src/org/netbeans/modules/bugtracking/util/FindTypesSupport.java @@ -339,11 +339,11 @@ // add(new JMenuItem(new AbstractAction("Find in projects") { // @Override // public void actionPerformed(ActionEvent e) { -// FindInFilesAction a = (FindInFilesAction) FileUtil.getConfigObject("Actions/Edit/org-netbeans-modules-search-FindInFilesAction.instance", Action.class); +// Action a = Actions.forID("Edit", "org.netbeans.modules.search.FindInFilesAction"); // if(a instanceof ContextAwareAction) { //// a = ((ContextAwareAction)a).createContextAwareInstance(Lookups.singleton(ctx)); // } -// a.performAction(); +// a.actionPerformed(null); // } // })); } catch(Exception ex) { diff --git a/git/src/org/netbeans/modules/git/Annotator.java b/git/src/org/netbeans/modules/git/Annotator.java --- a/git/src/org/netbeans/modules/git/Annotator.java +++ b/git/src/org/netbeans/modules/git/Annotator.java @@ -248,6 +248,7 @@ if(accelerate) { action = Utils.getAcceleratedAction(ACTIONS_PATH_PREFIX + name + ".instance"); } else { + // or use Actions.forID action = (Action) FileUtil.getConfigObject(ACTIONS_PATH_PREFIX + name + ".instance", Action.class); } if(action instanceof ContextAwareAction) { diff --git a/maven/nbproject/project.xml b/maven/nbproject/project.xml --- a/maven/nbproject/project.xml +++ b/maven/nbproject/project.xml @@ -285,7 +285,7 @@ - 7.22 + 7.42 diff --git a/maven/src/org/netbeans/modules/maven/newproject/ExistingWizardIterator.java b/maven/src/org/netbeans/modules/maven/newproject/ExistingWizardIterator.java --- a/maven/src/org/netbeans/modules/maven/newproject/ExistingWizardIterator.java +++ b/maven/src/org/netbeans/modules/maven/newproject/ExistingWizardIterator.java @@ -55,7 +55,7 @@ import org.netbeans.modules.maven.api.archetype.ArchetypeWizards; import static org.netbeans.modules.maven.newproject.Bundle.*; import org.openide.WizardDescriptor; -import org.openide.filesystems.FileUtil; +import org.openide.awt.Actions; import org.openide.util.NbBundle.Messages; import org.openide.util.RequestProcessor; @@ -107,7 +107,7 @@ } private void tryOpenProject() { - final Action act = FileUtil.getConfigObject("Actions/Project/org-netbeans-modules-project-ui-OpenProject.instance", Action.class); //NOI18N + final Action act = Actions.forID("Project", "org.netbeans.modules.project.ui.OpenProject"); //NOI18N if (act != null) { SwingUtilities.invokeLater(new Runnable() { public void run() { diff --git a/mercurial/nbproject/project.xml b/mercurial/nbproject/project.xml --- a/mercurial/nbproject/project.xml +++ b/mercurial/nbproject/project.xml @@ -153,7 +153,7 @@ - 6.8 + 7.42 diff --git a/mercurial/src/org/netbeans/modules/mercurial/MercurialAnnotator.java b/mercurial/src/org/netbeans/modules/mercurial/MercurialAnnotator.java --- a/mercurial/src/org/netbeans/modules/mercurial/MercurialAnnotator.java +++ b/mercurial/src/org/netbeans/modules/mercurial/MercurialAnnotator.java @@ -88,7 +88,7 @@ import org.netbeans.modules.mercurial.ui.update.UpdateAction; import org.netbeans.modules.mercurial.util.HgUtils; import org.netbeans.modules.versioning.util.SystemActionBridge; -import org.openide.filesystems.FileUtil; +import org.openide.awt.Actions; import org.openide.util.ContextAwareAction; import org.openide.util.ImageUtilities; import org.openide.util.Lookup; @@ -370,7 +370,7 @@ } else { Lookup context = ctx.getElements(); if (noneVersioned){ - Action a = (Action) FileUtil.getConfigObject("Actions/Mercurial/org-netbeans-modules-mercurial-ui-create-CreateAction.instance", Action.class); + Action a = Actions.forID("Mercurial", "org.netbeans.modules.mercurial.ui.create.CreateAction"); if(a instanceof ContextAwareAction) { a = ((ContextAwareAction)a).createContextAwareInstance(Lookups.singleton(ctx)); } diff --git a/openide.awt/apichanges.xml b/openide.awt/apichanges.xml --- a/openide.awt/apichanges.xml +++ b/openide.awt/apichanges.xml @@ -50,6 +50,32 @@ AWT API + + + Added Actions.forID + + + + +

+ Calls to FileUtil.getConfigObject or similar should be reviewed. +

+

+ Modules with a dependency only on org.openide.loaders + but not org.openide.awt must add the latter dep in + order to use AcceleratorBinding. +

+
+ +

+ Added a utility method Actions.forID. + Also moved AcceleratorBinding down into the org.openide.awt + module (keeping the same FQN and signature). +

+
+ + +
Added lazy attribute to ActionRegistration diff --git a/openide.awt/manifest.mf b/openide.awt/manifest.mf --- a/openide.awt/manifest.mf +++ b/openide.awt/manifest.mf @@ -2,5 +2,5 @@ OpenIDE-Module: org.openide.awt OpenIDE-Module-Localizing-Bundle: org/openide/awt/Bundle.properties AutoUpdate-Essential-Module: true -OpenIDE-Module-Specification-Version: 7.41 +OpenIDE-Module-Specification-Version: 7.42 diff --git a/openide.loaders/src/org/openide/awt/AcceleratorBinding.java b/openide.awt/src/org/openide/awt/AcceleratorBinding.java rename from openide.loaders/src/org/openide/awt/AcceleratorBinding.java rename to openide.awt/src/org/openide/awt/AcceleratorBinding.java --- a/openide.loaders/src/org/openide/awt/AcceleratorBinding.java +++ b/openide.awt/src/org/openide/awt/AcceleratorBinding.java @@ -56,14 +56,11 @@ * This class is only intended for use from NetBeans infrastructure code: * that which either reads shortcut definitions (i.e. the global {@link Keymap}); * or which loads actions into presenters (e.g. {@link Toolbar}). - * @since org.openide.loaders 7.13 + * @since org.openide.loaders 7.13 but moved down to org.openide.awt 7.42 */ public abstract class AcceleratorBinding { private static final Iter ALL = new Iter(); - static void init() { - } - /** * Subclass constructor. Only certain implementations are permitted. */ diff --git a/openide.awt/src/org/openide/awt/ActionID.java b/openide.awt/src/org/openide/awt/ActionID.java --- a/openide.awt/src/org/openide/awt/ActionID.java +++ b/openide.awt/src/org/openide/awt/ActionID.java @@ -56,6 +56,7 @@ * @author Jaroslav Tulach * @since 7.26 * @see ActionRegistration + * @see Actions#forID */ @Retention(RetentionPolicy.SOURCE) @Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD}) diff --git a/openide.awt/src/org/openide/awt/Actions.java b/openide.awt/src/org/openide/awt/Actions.java --- a/openide.awt/src/org/openide/awt/Actions.java +++ b/openide.awt/src/org/openide/awt/Actions.java @@ -81,9 +81,12 @@ import org.netbeans.api.actions.Openable; import org.netbeans.api.actions.Printable; import org.netbeans.api.actions.Viewable; +import org.openide.filesystems.FileObject; +import org.openide.filesystems.FileUtil; import org.openide.util.ContextAwareAction; import org.openide.util.ImageUtilities; import org.openide.util.LookupListener; +import org.openide.util.Utilities; import org.openide.util.WeakListeners; import org.openide.util.actions.BooleanStateAction; import org.openide.util.actions.SystemAction; @@ -716,6 +719,32 @@ } throw new IllegalStateException(type); } + + /** + * Locates a specific action programmatically. + * The action will typically have been registered using {@link ActionRegistration}. + *

Normally an {@link ActionReference} will suffice to insert the action + * into various UI elements (typically using {@link Utilities#actionsForPath}), + * but in special circumstances you may need to find a single known action. + * This method is just a shortcut for using {@link FileUtil#getConfigObject} + * with the correct arguments, plus using {@link AcceleratorBinding#setAccelerator}. + * @param category as in {@link ActionID#category} + * @param id as in {@link ActionID#id} + * @return the action registered under that ID, or null + * @since 7.42 + */ + public static Action forID(String category, String id) { + String path = "Actions/" + category + "/" + id.replace('.', '-') + ".instance"; + Action a = FileUtil.getConfigObject(path, Action.class); + if (a == null) { + return null; + } + FileObject def = FileUtil.getConfigFile(path); + if (def != null) { + AcceleratorBinding.setAccelerator(a, def); + } + return a; + } /** Extracts help from action. */ diff --git a/openide.loaders/manifest.mf b/openide.loaders/manifest.mf --- a/openide.loaders/manifest.mf +++ b/openide.loaders/manifest.mf @@ -1,6 +1,6 @@ Manifest-Version: 1.0 OpenIDE-Module: org.openide.loaders -OpenIDE-Module-Specification-Version: 7.33 +OpenIDE-Module-Specification-Version: 7.34 OpenIDE-Module-Localizing-Bundle: org/openide/loaders/Bundle.properties OpenIDE-Module-Provides: org.netbeans.modules.templates.v1_0 OpenIDE-Module-Layer: org/netbeans/modules/openide/loaders/layer.xml diff --git a/openide.loaders/nbproject/project.xml b/openide.loaders/nbproject/project.xml --- a/openide.loaders/nbproject/project.xml +++ b/openide.loaders/nbproject/project.xml @@ -98,7 +98,7 @@ - 7.33 + 7.42 diff --git a/openide.loaders/src/org/openide/awt/MenuBar.java b/openide.loaders/src/org/openide/awt/MenuBar.java --- a/openide.loaders/src/org/openide/awt/MenuBar.java +++ b/openide.loaders/src/org/openide/awt/MenuBar.java @@ -127,7 +127,11 @@ static final long serialVersionUID =-4721949937356581268L; static { - AcceleratorBinding.init(); + try { + Class.forName(AcceleratorBinding.class.getName()); + } catch (ClassNotFoundException x) { + throw new ExceptionInInitializerError(x); + } } /** Don't call this constructor or this class will not get diff --git a/openide.loaders/src/org/openide/awt/Toolbar.java b/openide.loaders/src/org/openide/awt/Toolbar.java --- a/openide.loaders/src/org/openide/awt/Toolbar.java +++ b/openide.loaders/src/org/openide/awt/Toolbar.java @@ -101,7 +101,11 @@ static final long serialVersionUID = 5011742660516204764L; static { - AcceleratorBinding.init(); + try { + Class.forName(AcceleratorBinding.class.getName()); + } catch (ClassNotFoundException x) { + throw new ExceptionInInitializerError(x); + } } /** Create a new Toolbar with empty name. */ diff --git a/openide.loaders/test/unit/src/org/openide/loaders/SavableDataObjectTest.java b/openide.loaders/test/unit/src/org/openide/loaders/SavableDataObjectTest.java --- a/openide.loaders/test/unit/src/org/openide/loaders/SavableDataObjectTest.java +++ b/openide.loaders/test/unit/src/org/openide/loaders/SavableDataObjectTest.java @@ -49,6 +49,7 @@ import org.netbeans.junit.MockServices; import org.netbeans.junit.NbTestCase; import org.netbeans.spi.actions.AbstractSavable; +import org.openide.awt.Actions; import org.openide.filesystems.FileObject; import org.openide.filesystems.FileUtil; import org.openide.util.ContextAwareAction; @@ -102,14 +103,12 @@ private static ContextAwareAction findSaveAction() { - FileObject fo = FileUtil.getConfigFile("Actions/System/org-openide-actions-SaveAction.instance"); - assertNotNull("File found", fo); - Object obj = fo.getAttribute("instanceCreate"); - assertTrue("action found: " + obj, obj instanceof ContextAwareAction); - return (ContextAwareAction)obj; + Action a = Actions.forID("System", "org.openide.actions.SaveAction"); + assertTrue(a instanceof ContextAwareAction); + return (ContextAwareAction) a; } private static Action findSaveAllAction() { - Action a = FileUtil.getConfigObject("Actions/System/org-openide-actions-SaveAllAction.instance", Action.class); + Action a = Actions.forID("System", "org.openide.actions.SaveAllAction"); assertNotNull("action found: " + a, a); return a; } diff --git a/projectui/nbproject/project.xml b/projectui/nbproject/project.xml --- a/projectui/nbproject/project.xml +++ b/projectui/nbproject/project.xml @@ -142,7 +142,7 @@ - 7.22 + 7.42 diff --git a/projectui/src/org/netbeans/modules/project/ui/actions/Actions.java b/projectui/src/org/netbeans/modules/project/ui/actions/Actions.java --- a/projectui/src/org/netbeans/modules/project/ui/actions/Actions.java +++ b/projectui/src/org/netbeans/modules/project/ui/actions/Actions.java @@ -51,7 +51,6 @@ import org.netbeans.modules.project.uiapi.ActionsFactory; import org.netbeans.spi.project.ActionProvider; import org.netbeans.spi.project.ui.support.ProjectActionPerformer; -import org.openide.filesystems.FileUtil; import org.openide.util.ContextAwareAction; import org.openide.util.Exceptions; import org.openide.util.ImageUtilities; @@ -236,7 +235,7 @@ null ); try { - final Action delete = FileUtil.getConfigObject("Actions/Edit/org-openide-actions-DeleteAction.instance", Action.class); + final Action delete = org.openide.awt.Actions.forID("Edit", "org.openide.actions.DeleteAction"); a.putValue(Action.ACCELERATOR_KEY, delete.getValue(Action.ACCELERATOR_KEY)); delete.addPropertyChangeListener(new PropertyChangeListener() { public @Override void propertyChange(PropertyChangeEvent evt) { diff --git a/subversion/nbproject/project.xml b/subversion/nbproject/project.xml --- a/subversion/nbproject/project.xml +++ b/subversion/nbproject/project.xml @@ -181,7 +181,7 @@ - 7.35 + 7.42 diff --git a/subversion/src/org/netbeans/modules/subversion/Annotator.java b/subversion/src/org/netbeans/modules/subversion/Annotator.java --- a/subversion/src/org/netbeans/modules/subversion/Annotator.java +++ b/subversion/src/org/netbeans/modules/subversion/Annotator.java @@ -83,9 +83,7 @@ import org.netbeans.modules.subversion.ui.lock.LockAction; import org.netbeans.modules.subversion.ui.lock.UnlockAction; import org.netbeans.modules.subversion.ui.properties.VersioningInfoAction; -import org.openide.awt.AcceleratorBinding; -import org.openide.filesystems.FileObject; -import org.openide.filesystems.FileUtil; +import org.openide.awt.Actions; import org.openide.util.ContextAwareAction; import org.openide.util.ImageUtilities; import org.openide.util.lookup.Lookups; @@ -480,7 +478,7 @@ ResourceBundle loc = NbBundle.getBundle(Annotator.class); Lookup context = ctx.getElements(); if (noneVersioned) { - Action a = FileUtil.getConfigObject("Actions/Subversion/org-netbeans-modules-subversion-ui-project-ImportAction.instance", Action.class); + Action a = Actions.forID("Subversion", "org.netbeans.modules.subversion.ui.project.ImportAction"); if(a instanceof ContextAwareAction) { a = ((ContextAwareAction)a).createContextAwareInstance(Lookups.singleton(ctx)); } diff --git a/versioning.util/src/org/netbeans/modules/versioning/util/Utils.java b/versioning.util/src/org/netbeans/modules/versioning/util/Utils.java --- a/versioning.util/src/org/netbeans/modules/versioning/util/Utils.java +++ b/versioning.util/src/org/netbeans/modules/versioning/util/Utils.java @@ -1538,6 +1538,7 @@ } public static Action getAcceleratedAction(String path) { + // or use Actions.forID Action a = FileUtil.getConfigObject(path, Action.class); FileObject fo = FileUtil.getConfigFile(path); if(fo != null) {