# HG changeset patch # Parent 859c623e5f6b1ad54831907076f25ea662a6665e Issue #177262: refresh enablement of project-related actions off EQ. diff --git a/projectui/src/org/netbeans/modules/project/ui/actions/FileCommandAction.java b/projectui/src/org/netbeans/modules/project/ui/actions/FileCommandAction.java --- a/projectui/src/org/netbeans/modules/project/ui/actions/FileCommandAction.java +++ b/projectui/src/org/netbeans/modules/project/ui/actions/FileCommandAction.java @@ -52,13 +52,12 @@ import org.openide.loaders.DataObject; import org.openide.util.ImageUtilities; import org.openide.util.Lookup; +import org.openide.util.Mutex; /** An action sensitive to selected node. Used for 1-off actions */ public final class FileCommandAction extends ProjectAction { - private String presenterName; - public FileCommandAction( String command, String namePattern, String iconResource, Lookup lookup ) { this( command, namePattern, ImageUtilities.loadImageIcon(iconResource, false), lookup ); } @@ -66,34 +65,42 @@ public FileCommandAction( String command, String namePattern, Icon icon, Lookup lookup ) { super( command, namePattern, icon, lookup ); assert namePattern != null : "Name patern must not be null"; - presenterName = ActionsUtil.formatName( getNamePattern(), 0, "" ); + String presenterName = ActionsUtil.formatName( getNamePattern(), 0, "" ); setDisplayName( presenterName ); putValue(SHORT_DESCRIPTION, Actions.cutAmpersand(presenterName)); } - @Override - protected void refresh( Lookup context ) { + protected @Override void refresh(final Lookup context) { + RP.post(new Runnable() { + public @Override void run() { Project[] projects = ActionsUtil.getProjectsFromLookup( context, getCommand() ); - + final boolean enable; + final String presenterName; if ( projects.length != 1 ) { if (projects.length == 0 && globalProvider(context) != null) { - setEnabled(true); + enable = true; Collection files = context.lookupAll(DataObject.class); presenterName = ActionsUtil.formatName(getNamePattern(), files.size(), files.isEmpty() ? "" : files.iterator().next().getPrimaryFile().getNameExt()); // NOI18N } else { - setEnabled(false); // Zero or more than one projects found or command not supported + enable = false; // Zero or more than one projects found or command not supported presenterName = ActionsUtil.formatName(getNamePattern(), 0, ""); } } else { FileObject[] files = ActionsUtil.getFilesFromLookup( context, projects[0] ); - setEnabled( true ); + enable = true; presenterName = ActionsUtil.formatName( getNamePattern(), files.length, files.length > 0 ? files[0].getNameExt() : "" ); // NOI18N } - + Mutex.EVENT.writeAccess(new Runnable() { + public @Override void run() { putValue("menuText", presenterName); putValue(SHORT_DESCRIPTION, Actions.cutAmpersand(presenterName)); + setEnabled(enable); + } + }); + } + }); } @Override diff --git a/projectui/src/org/netbeans/modules/project/ui/actions/LookupSensitiveAction.java b/projectui/src/org/netbeans/modules/project/ui/actions/LookupSensitiveAction.java --- a/projectui/src/org/netbeans/modules/project/ui/actions/LookupSensitiveAction.java +++ b/projectui/src/org/netbeans/modules/project/ui/actions/LookupSensitiveAction.java @@ -41,7 +41,6 @@ package org.netbeans.modules.project.ui.actions; -import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; @@ -60,6 +59,7 @@ import org.openide.util.LookupListener; import org.openide.util.Mutex; import org.openide.util.NbBundle; +import org.openide.util.RequestProcessor; import org.openide.util.WeakListeners; import org.openide.util.actions.Presenter; import org.openide.util.lookup.ProxyLookup; @@ -71,6 +71,7 @@ abstract class LookupSensitiveAction extends BasicAction implements Runnable, LookupListener, Presenter.Popup, Presenter.Menu { static Logger UILOG = Logger.getLogger("org.netbeans.ui.actions"); // NOI18N private static Logger LOG = Logger.getLogger(LookupSensitiveAction.class.getName()); + protected static final RequestProcessor RP = new RequestProcessor(MainProjectAction.class); private Lookup lookup; private Class[] watch; @@ -101,7 +102,6 @@ if (initialized) { return false; } - assert EventQueue.isDispatchThread () : "Cannot be called outside EQ!"; this.results = new Lookup.Result[watch.length]; // Needs to listen on changes in results for ( int i = 0; i < watch.length; i++ ) { diff --git a/projectui/src/org/netbeans/modules/project/ui/actions/MainProjectAction.java b/projectui/src/org/netbeans/modules/project/ui/actions/MainProjectAction.java --- a/projectui/src/org/netbeans/modules/project/ui/actions/MainProjectAction.java +++ b/projectui/src/org/netbeans/modules/project/ui/actions/MainProjectAction.java @@ -67,7 +67,6 @@ import org.openide.util.Lookup; import org.openide.util.Mutex; import org.openide.util.NbBundle; -import org.openide.util.RequestProcessor; import org.openide.util.WeakListeners; /** Invokes command on the main project. @@ -79,7 +78,6 @@ private String command; private ProjectActionPerformer performer; private String name; - private static final RequestProcessor RP = new RequestProcessor(MainProjectAction.class); public MainProjectAction(ProjectActionPerformer performer, String name, Icon icon) { this( null, performer, name, icon ); diff --git a/projectui/src/org/netbeans/modules/project/ui/actions/ProjectAction.java b/projectui/src/org/netbeans/modules/project/ui/actions/ProjectAction.java --- a/projectui/src/org/netbeans/modules/project/ui/actions/ProjectAction.java +++ b/projectui/src/org/netbeans/modules/project/ui/actions/ProjectAction.java @@ -41,7 +41,6 @@ package org.netbeans.modules.project.ui.actions; -import java.awt.EventQueue; import java.util.logging.Level; import java.util.logging.LogRecord; import javax.swing.Action; @@ -54,6 +53,7 @@ import org.openide.loaders.DataObject; import org.openide.util.ContextAwareAction; import org.openide.util.Lookup; +import org.openide.util.Mutex; import org.openide.util.NbBundle; /** Action sensitive to current project @@ -137,40 +137,35 @@ } - @Override - protected void refresh( Lookup context ) { - Project[] projects = ActionsUtil.getProjectsFromLookup( context, command ); - - if ( command != null ) { - enable( projects.length == 1 ); - } else if ( performer != null && projects.length == 1 ) { - enable( performer.enable( projects[0] ) ); - } else { - enable( false ); - } - - String presenterName = ActionsUtil.formatProjectSensitiveName( namePattern, projects ); - putValue("menuText", presenterName); // NOI18N - if (popupPattern != null) { - String popupName = ActionsUtil.formatProjectSensitiveName(popupPattern, projects); - putValue("popupText", popupName); // NOI18N - } - - putValue(SHORT_DESCRIPTION, Actions.cutAmpersand(presenterName)); - } - - // #131674 - private void enable(final boolean enable) { - if (!EventQueue.isDispatchThread()) { - EventQueue.invokeLater(new Runnable() { - @Override - public void run() { - setEnabled(enable); + protected @Override void refresh(final Lookup context) { + RP.post(new Runnable() { + public @Override void run() { + Project[] projects = ActionsUtil.getProjectsFromLookup(context, command); + final boolean enable; + if (command != null) { + enable = projects.length == 1; + } else if (performer != null && projects.length == 1) { + enable = performer.enable(projects[0]); + } else { + enable = false; } - }); - } else { - setEnabled(enable); - } + final String presenterName = ActionsUtil.formatProjectSensitiveName(namePattern, projects); + final String popupName; + if (popupPattern != null) { + popupName = ActionsUtil.formatProjectSensitiveName(popupPattern, projects); + } else { + popupName = null; + } + Mutex.EVENT.writeAccess(new Runnable() { + public @Override void run() { + putValue("menuText", presenterName); // NOI18N + putValue("popupText", popupName); // NOI18N + putValue(SHORT_DESCRIPTION, Actions.cutAmpersand(presenterName)); + setEnabled(enable); + } + }); + } + }); } protected final String getCommand() { diff --git a/projectui/test/unit/src/org/netbeans/modules/project/ui/actions/ProjectActionTest.java b/projectui/test/unit/src/org/netbeans/modules/project/ui/actions/ProjectActionTest.java --- a/projectui/test/unit/src/org/netbeans/modules/project/ui/actions/ProjectActionTest.java +++ b/projectui/test/unit/src/org/netbeans/modules/project/ui/actions/ProjectActionTest.java @@ -41,6 +41,7 @@ package org.netbeans.modules.project.ui.actions; +import java.awt.EventQueue; import java.util.ArrayList; import java.util.List; import javax.swing.Action; @@ -101,27 +102,27 @@ project2 = (TestSupport.TestProject)ProjectManager.getDefault().findProject( p2 ); } - - @Override - public boolean runInEQ () { - return true; + + static void assertEnablement(final Action action, final boolean enabled) throws Exception { + LookupSensitiveAction.RP.post(new Runnable() {public @Override void run() {}}).waitFinished(); + EventQueue.invokeAndWait(new Runnable() { + public @Override void run() { + assertTrue(action.isEnabled() ^ !enabled); + } + }); } public void testCommandEnablement() throws Exception { TestSupport.ChangeableLookup lookup = new TestSupport.ChangeableLookup(); - ProjectAction action = new ProjectAction( "COMMAND", "TestProjectAction", null, lookup ); - - assertFalse( "Action should NOT be enabled", action.isEnabled() ); - + Action action = new ProjectAction("COMMAND", "TestProjectAction", null, lookup); + action.isEnabled(); // priming check + assertEnablement(action, false); lookup.change(d1_1); - assertTrue( "Action should be enabled", action.isEnabled() ); - + assertEnablement(action, true); lookup.change(d1_1, d1_2); - assertFalse( "Action should NOT be enabled", action.isEnabled() ); - + assertEnablement(action, false); lookup.change(d1_1, d2_1); - assertFalse( "Action should NOT be enabled", action.isEnabled() ); - + assertEnablement(action, false); } public void testProviderEnablement() throws Exception { diff --git a/projectui/test/unit/src/org/netbeans/modules/project/ui/actions/SetMainProjectTest.java b/projectui/test/unit/src/org/netbeans/modules/project/ui/actions/SetMainProjectTest.java --- a/projectui/test/unit/src/org/netbeans/modules/project/ui/actions/SetMainProjectTest.java +++ b/projectui/test/unit/src/org/netbeans/modules/project/ui/actions/SetMainProjectTest.java @@ -68,10 +68,6 @@ clearWorkDir (); } - public boolean runInEQ () { - return true; - } - public void testAcceleratorsPropagated() { ProjectActionTest.doTestAcceleratorsPropagated(new ActionCreator() { public ProjectAction create(Lookup l) {