diff --git a/editor.lib2/nbproject/project.properties b/editor.lib2/nbproject/project.properties --- a/editor.lib2/nbproject/project.properties +++ b/editor.lib2/nbproject/project.properties @@ -40,7 +40,7 @@ is.autoload=true javac.source=1.5 javac.compilerargs=-Xlint:unchecked -spec.version.base=1.15.0 +spec.version.base=1.16.0 cp.extra=${nb_all}/apisupport.harness/external/openjdk-javac-6-b12.jar javadoc.arch=${basedir}/arch.xml diff --git a/editor.lib2/nbproject/project.xml b/editor.lib2/nbproject/project.xml --- a/editor.lib2/nbproject/project.xml +++ b/editor.lib2/nbproject/project.xml @@ -87,7 +87,7 @@ - 7.15 + 7.17 diff --git a/editor.lib2/src/org/netbeans/modules/editor/lib2/EditorActionRegistrationProcessor.java b/editor.lib2/src/org/netbeans/modules/editor/lib2/EditorActionRegistrationProcessor.java --- a/editor.lib2/src/org/netbeans/modules/editor/lib2/EditorActionRegistrationProcessor.java +++ b/editor.lib2/src/org/netbeans/modules/editor/lib2/EditorActionRegistrationProcessor.java @@ -292,8 +292,8 @@ } if (preferencesKey.length() > 0) { - file.stringvalue("PreferencesKey", preferencesKey); - file.methodvalue("PreferencesNode", EditorActionUtilities.class.getName(), "getGlobalPreferences"); + file.stringvalue("preferencesKey", preferencesKey); + file.methodvalue("preferencesNode", EditorActionUtilities.class.getName(), "getGlobalPreferences"); } // Deafult helpID is action's name @@ -314,7 +314,8 @@ } } else { // Create always enabled action - file.methodvalue("instanceCreate", "org.openide.awt.Actions", "alwaysEnabled"); + file.methodvalue("instanceCreate", "org.openide.awt.Actions", + checkBoxPresenter ? "checkbox" : "alwaysEnabled"); file.stringvalue("displayName", actionName); if (methodName != null) { diff --git a/openide.awt/apichanges.xml b/openide.awt/apichanges.xml --- a/openide.awt/apichanges.xml +++ b/openide.awt/apichanges.xml @@ -47,6 +47,21 @@ AWT API + + + Actions.checkbox() can represent a boolean key in Preferences. + + + + + + + checkbox + added to create an action that represents a key in Preferences. + + + + AwlaysEnabledAction can represent a boolean key in Preferences. diff --git a/openide.awt/nbproject/project.properties b/openide.awt/nbproject/project.properties --- a/openide.awt/nbproject/project.properties +++ b/openide.awt/nbproject/project.properties @@ -44,4 +44,4 @@ javadoc.arch=${basedir}/arch.xml javadoc.apichanges=${basedir}/apichanges.xml -spec.version.base=7.16.0 +spec.version.base=7.17.0 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 @@ -356,6 +356,55 @@ return AlwaysEnabledAction.create(map); } + /** Creates action which represents a boolean value in {@link java.util.prefs.Preferences}. + * When added to a menu the action is presented as a JCheckBox. + * This method can also be used from + * XML Layer + * directly by following XML definition: + *
+     * <file name="your-pkg-action-id.instance">
+     *   <attr name="preferencesNode" methodvalue="method-returning-Preferences-instance" or
+     *                                   methodvalue="method-returning-Lookup-that-contains-Preferences-instance" or
+     *                                   stringvalue="see below for the preferencesNode parameter description"
+     * />
+     *   <attr name="preferencesKey" stringvalue="preferences-key-name"/>
+     *   <attr name="instanceCreate" methodvalue="org.openide.awt.Actions.checkbox"/>
+     *   <attr name="displayName" bundlevalue="your.pkg.Bundle#key"/>
+     *   <attr name="iconBase" stringvalue="your/pkg/YourImage.png"/>
+     *   <!-- if desired: <attr name="noIconInMenu" boolvalue="false"/> -->
+     *   <!-- if desired: <attr name="asynchronous" boolvalue="true"/> -->
+     * </file>
+     * 
+ * + * @param preferencesNode It's one of: + *
    + *
  • Absolute path to preferences node under NbPreferences.root().
  • + *
  • "system:" followed by absolute path to preferences node under Preferences.systemRoot().
  • + *
  • "user:" followed by absolute path to preferences node under Preferences.userRoot().
  • + *
+ * @param preferencesKey name of the preferences key. + * @param displayName the name of the action + * @param iconBase the location to the actions icon + * @param noIconInMenu true if this icon shall not have an item in menu + * @since 7.17 + */ + public static Action checkbox( + String preferencesNode, String preferencesKey, + String displayName, String iconBase, boolean noIconInMenu + ) { + HashMap map = new HashMap(); + map.put("preferencesNode", preferencesNode); // NOI18N + map.put("preferencesKey", preferencesKey); // NOI18N + map.put("displayName", displayName); // NOI18N + map.put("iconBase", iconBase); // NOI18N + map.put("noIconInMenu", noIconInMenu); // NOI18N + return checkbox(map); + } + // for use from layers + static Action checkbox(Map map) { + return AlwaysEnabledAction.create(map); + } + /** Creates new "callback" action. Such action has an assigned key * which is used to find proper delegate in {@link ActionMap} of currently * active component. diff --git a/openide.awt/src/org/openide/awt/AlwaysEnabledAction.java b/openide.awt/src/org/openide/awt/AlwaysEnabledAction.java --- a/openide.awt/src/org/openide/awt/AlwaysEnabledAction.java +++ b/openide.awt/src/org/openide/awt/AlwaysEnabledAction.java @@ -21,8 +21,6 @@ import javax.swing.Icon; import javax.swing.JCheckBoxMenuItem; import javax.swing.JMenuItem; -import javax.swing.KeyStroke; -import javax.swing.text.Keymap; import org.netbeans.modules.openide.util.ActionsBridge; import org.netbeans.modules.openide.util.ActionsBridge.ActionRunnable; import org.openide.util.ContextAwareAction; @@ -43,30 +41,9 @@ // -J-Dorg.openide.awt.AlwaysEnabledAction.level=FINE private static final Logger LOG = Logger.getLogger(AlwaysEnabledAction.class.getName()); - /** - * Action property for key in {@link java.util.prefs.Preferences}. - * - * String key = (String) action.getValue("PreferencesKey"); - * boolean selected = preferencesNode.getBoolean(key, false); - * - */ - private static final String PREFERENCES_KEY = "PreferencesKey"; // NOI18N + private static final String PREFERENCES_NODE = "preferencesNode"; // NOI18N - /** - * Action property for retrieving {@link java.util.prefs.Preferences} node. - * Its value can be a String: - *
    - *
  • "system:/path" for {@link java.util.prefs.Preferences#systemRoot() }.
  • - *
  • "user:/key" for {@link java.util.prefs.Preferences#userRoot() }.
  • - *
  • "nb:/key" for {@link org.openide.util.NbPreferences#root() }.
  • - *
- * or a method value returning one of the following: - *
    - *
  • {@link java.util.prefs.Preferences } instance.
  • - *
  • {@link org.openide.util.Lookup }.
  • - *
- */ - private static final String PREFERENCES_NODE = "PreferencesNode"; // NOI18N + private static final String PREFERENCES_KEY = "preferencesKey"; // NOI18N static AlwaysEnabledAction create(Map m) { return (m.containsKey(PREFERENCES_KEY)) ? new CheckBox(m) : new AlwaysEnabledAction(m); @@ -101,7 +78,7 @@ return a; } - private ActionListener getDelegate() { + protected ActionListener getDelegate() { if (delegate == null) { Object listener = map.get("delegate"); // NOI18N if (!(listener instanceof ActionListener)) { @@ -283,6 +260,12 @@ private static final long serialVersionUID = 1L; + private static final ActionListener EMPTY = new ActionListener() { + public void actionPerformed(ActionEvent ae) { + // Do nothing + } + }; + private JCheckBoxMenuItem menuItem; private JCheckBoxMenuItem popupItem; @@ -332,6 +315,11 @@ } @Override + protected ActionListener getDelegate() { + return EMPTY; + } + + @Override public Action createContextAwareInstance(Lookup actionContext) { return new CheckBox(map, delegate, actionContext, equals); } @@ -389,18 +377,15 @@ preferencesNode = null; } } - } else if (nodeName.startsWith("nb:")) { + } else { preferencesNode = NbPreferences.root(); if (preferencesNode != null) { - nodeName = nodeName.substring("nb:".length());; try { preferencesNode = preferencesNode.nodeExists(nodeName) ? preferencesNode.node(nodeName) : null; } catch (BackingStoreException ex) { preferencesNode = null; } } - } else { - preferencesNode = null; } } else if (prefsNodeOrLookup instanceof Preferences) { diff --git a/openide.awt/test/unit/src/org/openide/awt/AlwaysEnabledActionTest.java b/openide.awt/test/unit/src/org/openide/awt/AlwaysEnabledActionTest.java --- a/openide.awt/test/unit/src/org/openide/awt/AlwaysEnabledActionTest.java +++ b/openide.awt/test/unit/src/org/openide/awt/AlwaysEnabledActionTest.java @@ -343,17 +343,22 @@ @RandomlyFails public void testPreferencesAction() throws Exception { // checkPreferencesAction("testSystemPreferences.instance", Preferences.systemRoot()); - checkPreferencesAction("testUserPreferences.instance", Preferences.userRoot()); - checkPreferencesAction("testNbPreferences.instance", NbPreferences.root()); - checkPreferencesAction("testCustomPreferences.instance", Preferences.userRoot()); // customPreferences() uses "myNode" subnode + checkPreferencesAction("testUserPreferences.instance", "user:", Preferences.userRoot()); + checkPreferencesAction("testNbPreferences.instance", "", NbPreferences.root()); + checkPreferencesAction("testCustomPreferences.instance", "user:", Preferences.userRoot()); // customPreferences() uses "myNode" subnode } - private void checkPreferencesAction(String actionFileName, Preferences prefsRoot) throws Exception { - final int[] changeCount = new int[] { 0 }; + private void checkPreferencesAction(String actionFileName, String preferencesNodePrefix, Preferences prefsRoot) throws Exception { Action a = readAction(actionFileName); Preferences prefsNode = prefsRoot.node("myNode"); + checkPreferencesAction(a, prefsNode); + a = Actions.checkbox(preferencesNodePrefix + "/myNode", "myKey", null, null, false); + checkPreferencesAction(a, prefsNode); + } + + private void checkPreferencesAction(Action a, Preferences prefsNode) throws Exception { prefsNode.putBoolean("myKey", true); - prefsRoot.sync(); + prefsNode.sync(); int delay = 1; Thread.sleep(delay); // Verify value @@ -368,7 +373,7 @@ JMenuItem item = ((Presenter.Menu) a).getMenuPresenter(); TestCase.assertTrue("Expected to be selected", item.isSelected()); prefsNode.putBoolean("myKey", false); - prefsRoot.sync(); + prefsNode.sync(); Thread.sleep(delay); TestCase.assertFalse("Expected to not be selected", item.isSelected()); a.actionPerformed(null); // new ActionEvent(null, 0, "")); diff --git a/openide.awt/test/unit/src/org/openide/awt/test-layer.xml b/openide.awt/test/unit/src/org/openide/awt/test-layer.xml --- a/openide.awt/test/unit/src/org/openide/awt/test-layer.xml +++ b/openide.awt/test/unit/src/org/openide/awt/test-layer.xml @@ -99,34 +99,28 @@ - + - - - - - - - - + + - + - - + + - + - - + + - + - - + +