This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 177262
Collapse All | Expand All

(-)a/projectui/src/org/netbeans/modules/project/ui/actions/FileCommandAction.java (-10 / +17 lines)
Lines 52-64 Link Here
52
import org.openide.loaders.DataObject;
52
import org.openide.loaders.DataObject;
53
import org.openide.util.ImageUtilities;
53
import org.openide.util.ImageUtilities;
54
import org.openide.util.Lookup;
54
import org.openide.util.Lookup;
55
import org.openide.util.Mutex;
55
56
56
/** An action sensitive to selected node. Used for 1-off actions
57
/** An action sensitive to selected node. Used for 1-off actions
57
 */
58
 */
58
public final class FileCommandAction extends ProjectAction {
59
public final class FileCommandAction extends ProjectAction {
59
60
60
    private String presenterName;
61
        
62
    public FileCommandAction( String command, String namePattern, String iconResource, Lookup lookup ) {
61
    public FileCommandAction( String command, String namePattern, String iconResource, Lookup lookup ) {
63
        this( command, namePattern, ImageUtilities.loadImageIcon(iconResource, false), lookup );
62
        this( command, namePattern, ImageUtilities.loadImageIcon(iconResource, false), lookup );
64
    }
63
    }
Lines 66-99 Link Here
66
    public FileCommandAction( String command, String namePattern, Icon icon, Lookup lookup ) {
65
    public FileCommandAction( String command, String namePattern, Icon icon, Lookup lookup ) {
67
        super( command, namePattern, icon, lookup );
66
        super( command, namePattern, icon, lookup );
68
        assert namePattern != null : "Name patern must not be null";
67
        assert namePattern != null : "Name patern must not be null";
69
        presenterName = ActionsUtil.formatName( getNamePattern(), 0, "" );
68
        String presenterName = ActionsUtil.formatName( getNamePattern(), 0, "" );
70
        setDisplayName( presenterName );
69
        setDisplayName( presenterName );
71
        putValue(SHORT_DESCRIPTION, Actions.cutAmpersand(presenterName));
70
        putValue(SHORT_DESCRIPTION, Actions.cutAmpersand(presenterName));
72
    }
71
    }
73
    
72
    
74
    @Override
73
    protected @Override void refresh(final Lookup context) {
75
    protected void refresh( Lookup context ) {
74
        RP.post(new Runnable() {
75
            public @Override void run() {
76
        Project[] projects = ActionsUtil.getProjectsFromLookup( context, getCommand() );
76
        Project[] projects = ActionsUtil.getProjectsFromLookup( context, getCommand() );
77
77
        final boolean enable;
78
        final String presenterName;
78
        if ( projects.length != 1 ) {
79
        if ( projects.length != 1 ) {
79
            if (projects.length == 0 && globalProvider(context) != null) {
80
            if (projects.length == 0 && globalProvider(context) != null) {
80
                setEnabled(true);
81
                enable = true;
81
                Collection<? extends DataObject> files = context.lookupAll(DataObject.class);
82
                Collection<? extends DataObject> files = context.lookupAll(DataObject.class);
82
                presenterName = ActionsUtil.formatName(getNamePattern(), files.size(),
83
                presenterName = ActionsUtil.formatName(getNamePattern(), files.size(),
83
                        files.isEmpty() ? "" : files.iterator().next().getPrimaryFile().getNameExt()); // NOI18N
84
                        files.isEmpty() ? "" : files.iterator().next().getPrimaryFile().getNameExt()); // NOI18N
84
            } else {
85
            } else {
85
                setEnabled(false); // Zero or more than one projects found or command not supported
86
                enable = false; // Zero or more than one projects found or command not supported
86
                presenterName = ActionsUtil.formatName(getNamePattern(), 0, "");
87
                presenterName = ActionsUtil.formatName(getNamePattern(), 0, "");
87
            }
88
            }
88
        }
89
        }
89
        else {
90
        else {
90
            FileObject[] files = ActionsUtil.getFilesFromLookup( context, projects[0] );
91
            FileObject[] files = ActionsUtil.getFilesFromLookup( context, projects[0] );
91
            setEnabled( true );
92
            enable = true;
92
            presenterName = ActionsUtil.formatName( getNamePattern(), files.length, files.length > 0 ? files[0].getNameExt() : "" ); // NOI18N
93
            presenterName = ActionsUtil.formatName( getNamePattern(), files.length, files.length > 0 ? files[0].getNameExt() : "" ); // NOI18N
93
        }
94
        }
94
        
95
        Mutex.EVENT.writeAccess(new Runnable() {
96
            public @Override void run() {
95
        putValue("menuText", presenterName);
97
        putValue("menuText", presenterName);
96
        putValue(SHORT_DESCRIPTION, Actions.cutAmpersand(presenterName));
98
        putValue(SHORT_DESCRIPTION, Actions.cutAmpersand(presenterName));
99
        setEnabled(enable);
100
            }
101
        });
102
            }
103
        });
97
    }
104
    }
98
    
105
    
99
    @Override
106
    @Override
(-)a/projectui/src/org/netbeans/modules/project/ui/actions/LookupSensitiveAction.java (-2 / +2 lines)
Lines 41-47 Link Here
41
41
42
package org.netbeans.modules.project.ui.actions;
42
package org.netbeans.modules.project.ui.actions;
43
43
44
import java.awt.EventQueue;
45
import java.awt.event.ActionEvent;
44
import java.awt.event.ActionEvent;
46
import java.beans.PropertyChangeEvent;
45
import java.beans.PropertyChangeEvent;
47
import java.beans.PropertyChangeListener;
46
import java.beans.PropertyChangeListener;
Lines 60-65 Link Here
60
import org.openide.util.LookupListener;
59
import org.openide.util.LookupListener;
61
import org.openide.util.Mutex;
60
import org.openide.util.Mutex;
62
import org.openide.util.NbBundle;
61
import org.openide.util.NbBundle;
62
import org.openide.util.RequestProcessor;
63
import org.openide.util.WeakListeners;
63
import org.openide.util.WeakListeners;
64
import org.openide.util.actions.Presenter;
64
import org.openide.util.actions.Presenter;
65
import org.openide.util.lookup.ProxyLookup;
65
import org.openide.util.lookup.ProxyLookup;
Lines 71-76 Link Here
71
abstract class LookupSensitiveAction extends BasicAction implements Runnable, LookupListener, Presenter.Popup, Presenter.Menu {
71
abstract class LookupSensitiveAction extends BasicAction implements Runnable, LookupListener, Presenter.Popup, Presenter.Menu {
72
    static Logger UILOG = Logger.getLogger("org.netbeans.ui.actions"); // NOI18N
72
    static Logger UILOG = Logger.getLogger("org.netbeans.ui.actions"); // NOI18N
73
    private static Logger LOG = Logger.getLogger(LookupSensitiveAction.class.getName());
73
    private static Logger LOG = Logger.getLogger(LookupSensitiveAction.class.getName());
74
    protected static final RequestProcessor RP = new RequestProcessor(MainProjectAction.class);
74
75
75
    private Lookup lookup;
76
    private Lookup lookup;
76
    private Class<?>[] watch;
77
    private Class<?>[] watch;
Lines 101-107 Link Here
101
        if (initialized) {
102
        if (initialized) {
102
            return false;
103
            return false;
103
        }
104
        }
104
        assert EventQueue.isDispatchThread () : "Cannot be called outside EQ!";
105
        this.results = new Lookup.Result[watch.length];
105
        this.results = new Lookup.Result[watch.length];
106
        // Needs to listen on changes in results
106
        // Needs to listen on changes in results
107
        for ( int i = 0; i < watch.length; i++ ) {
107
        for ( int i = 0; i < watch.length; i++ ) {
(-)a/projectui/src/org/netbeans/modules/project/ui/actions/MainProjectAction.java (-2 lines)
Lines 67-73 Link Here
67
import org.openide.util.Lookup;
67
import org.openide.util.Lookup;
68
import org.openide.util.Mutex;
68
import org.openide.util.Mutex;
69
import org.openide.util.NbBundle;
69
import org.openide.util.NbBundle;
70
import org.openide.util.RequestProcessor;
71
import org.openide.util.WeakListeners;
70
import org.openide.util.WeakListeners;
72
71
73
/** Invokes command on the main project.
72
/** Invokes command on the main project.
Lines 79-85 Link Here
79
    private String command;
78
    private String command;
80
    private ProjectActionPerformer performer;
79
    private ProjectActionPerformer performer;
81
    private String name;
80
    private String name;
82
    private static final RequestProcessor RP = new RequestProcessor(MainProjectAction.class);
83
81
84
    public MainProjectAction(ProjectActionPerformer performer, String name, Icon icon) {
82
    public MainProjectAction(ProjectActionPerformer performer, String name, Icon icon) {
85
        this( null, performer, name, icon );
83
        this( null, performer, name, icon );
(-)a/projectui/src/org/netbeans/modules/project/ui/actions/ProjectAction.java (-34 / +29 lines)
Lines 41-47 Link Here
41
41
42
package org.netbeans.modules.project.ui.actions;
42
package org.netbeans.modules.project.ui.actions;
43
43
44
import java.awt.EventQueue;
45
import java.util.logging.Level;
44
import java.util.logging.Level;
46
import java.util.logging.LogRecord;
45
import java.util.logging.LogRecord;
47
import javax.swing.Action;
46
import javax.swing.Action;
Lines 54-59 Link Here
54
import org.openide.loaders.DataObject;
53
import org.openide.loaders.DataObject;
55
import org.openide.util.ContextAwareAction;
54
import org.openide.util.ContextAwareAction;
56
import org.openide.util.Lookup;
55
import org.openide.util.Lookup;
56
import org.openide.util.Mutex;
57
import org.openide.util.NbBundle;
57
import org.openide.util.NbBundle;
58
58
59
/** Action sensitive to current project
59
/** Action sensitive to current project
Lines 137-176 Link Here
137
        
137
        
138
    }
138
    }
139
    
139
    
140
    @Override
140
    protected @Override void refresh(final Lookup context) {
141
    protected void refresh( Lookup context ) {
141
        RP.post(new Runnable() {
142
        Project[] projects = ActionsUtil.getProjectsFromLookup( context, command );
142
            public @Override void run() {
143
        
143
                Project[] projects = ActionsUtil.getProjectsFromLookup(context, command);
144
        if ( command != null ) {
144
                final boolean enable;
145
            enable( projects.length == 1 );
145
                if (command != null) {
146
        } else if ( performer != null && projects.length == 1 ) {
146
                    enable = projects.length == 1;
147
            enable( performer.enable( projects[0] ) );
147
                } else if (performer != null && projects.length == 1) {
148
        } else {
148
                    enable = performer.enable(projects[0]);
149
            enable( false );
149
                } else {
150
        }
150
                    enable = false;
151
        
152
        String presenterName = ActionsUtil.formatProjectSensitiveName( namePattern, projects );
153
        putValue("menuText", presenterName); // NOI18N
154
        if (popupPattern != null) {
155
            String popupName = ActionsUtil.formatProjectSensitiveName(popupPattern, projects);
156
            putValue("popupText", popupName); // NOI18N
157
        }
158
                        
159
        putValue(SHORT_DESCRIPTION, Actions.cutAmpersand(presenterName));
160
    }
161
    
162
    // #131674
163
    private void enable(final boolean enable) {
164
        if (!EventQueue.isDispatchThread()) {
165
            EventQueue.invokeLater(new Runnable() {
166
                @Override
167
                public void run() {
168
                    setEnabled(enable);
169
                }
151
                }
170
            });
152
                final String presenterName = ActionsUtil.formatProjectSensitiveName(namePattern, projects);
171
        } else {
153
                final String popupName;
172
            setEnabled(enable);
154
                if (popupPattern != null) {
173
        }
155
                    popupName = ActionsUtil.formatProjectSensitiveName(popupPattern, projects);
156
                } else {
157
                    popupName = null;
158
                }
159
                Mutex.EVENT.writeAccess(new Runnable() {
160
                    public @Override void run() {
161
                        putValue("menuText", presenterName); // NOI18N
162
                        putValue("popupText", popupName); // NOI18N
163
                        putValue(SHORT_DESCRIPTION, Actions.cutAmpersand(presenterName));
164
                        setEnabled(enable);
165
                    }
166
                });
167
            }
168
        });
174
    }
169
    }
175
    
170
    
176
    protected final String getCommand() {
171
    protected final String getCommand() {
(-)a/projectui/test/unit/src/org/netbeans/modules/project/ui/actions/ProjectActionTest.java (-14 / +15 lines)
Lines 41-46 Link Here
41
41
42
package org.netbeans.modules.project.ui.actions;
42
package org.netbeans.modules.project.ui.actions;
43
43
44
import java.awt.EventQueue;
44
import java.util.ArrayList;
45
import java.util.ArrayList;
45
import java.util.List;
46
import java.util.List;
46
import javax.swing.Action;
47
import javax.swing.Action;
Lines 101-127 Link Here
101
               
102
               
102
        project2 = (TestSupport.TestProject)ProjectManager.getDefault().findProject( p2 );                
103
        project2 = (TestSupport.TestProject)ProjectManager.getDefault().findProject( p2 );                
103
    }
104
    }
104
    
105
105
    @Override
106
    static void assertEnablement(final Action action, final boolean enabled) throws Exception {
106
    public boolean runInEQ () {
107
        LookupSensitiveAction.RP.post(new Runnable() {public @Override void run() {}}).waitFinished();
107
        return true;
108
        EventQueue.invokeAndWait(new Runnable() {
109
            public @Override void run() {
110
                assertTrue(action.isEnabled() ^ !enabled);
111
            }
112
        });
108
    }
113
    }
109
    
114
    
110
    public void testCommandEnablement() throws Exception {
115
    public void testCommandEnablement() throws Exception {
111
        TestSupport.ChangeableLookup lookup = new TestSupport.ChangeableLookup();
116
        TestSupport.ChangeableLookup lookup = new TestSupport.ChangeableLookup();
112
        ProjectAction action = new ProjectAction( "COMMAND", "TestProjectAction", null, lookup );
117
        Action action = new ProjectAction("COMMAND", "TestProjectAction", null, lookup);
113
        
118
        action.isEnabled(); // priming check
114
        assertFalse( "Action should NOT be enabled", action.isEnabled() );        
119
        assertEnablement(action, false);
115
        
116
        lookup.change(d1_1);
120
        lookup.change(d1_1);
117
        assertTrue( "Action should be enabled", action.isEnabled() );        
121
        assertEnablement(action, true);
118
        
119
        lookup.change(d1_1, d1_2);
122
        lookup.change(d1_1, d1_2);
120
        assertFalse( "Action should NOT be enabled", action.isEnabled() );        
123
        assertEnablement(action, false);
121
        
122
        lookup.change(d1_1, d2_1);
124
        lookup.change(d1_1, d2_1);
123
        assertFalse( "Action should NOT be enabled", action.isEnabled() );        
125
        assertEnablement(action, false);
124
        
125
    }
126
    }
126
    
127
    
127
    public void testProviderEnablement() throws Exception {
128
    public void testProviderEnablement() throws Exception {
(-)a/projectui/test/unit/src/org/netbeans/modules/project/ui/actions/SetMainProjectTest.java (-4 lines)
Lines 68-77 Link Here
68
        clearWorkDir ();
68
        clearWorkDir ();
69
    }
69
    }
70
    
70
    
71
    public boolean runInEQ () {
72
        return true;
73
    }
74
    
75
    public void testAcceleratorsPropagated() {
71
    public void testAcceleratorsPropagated() {
76
        ProjectActionTest.doTestAcceleratorsPropagated(new ActionCreator() {
72
        ProjectActionTest.doTestAcceleratorsPropagated(new ActionCreator() {
77
            public ProjectAction create(Lookup l) {
73
            public ProjectAction create(Lookup l) {

Return to bug 177262