asakovets opened a new issue, #4024:
URL: https://github.com/apache/netbeans/issues/4024

   ### Apache NetBeans version
   
   Apache NetBeans 13
   
   ### What happened
   
   Actions "Find Usages" and/or "Refactor" are occasionally disabled in popup 
menu.
   ![java navigator 
bug](https://user-images.githubusercontent.com/60353629/164908957-2a1b3585-e089-44c4-9878-4f93e066b5bd.png)
   .
   
   ### How to reproduce
   
   Invoke popup menu on any **unselected** node. Repeat until "Find Usages" 
and/or "Refactor" are disabled. It is important to press right button on an 
**unselected** node. First selecting node and then invoking popup menu works 
fine.
   
   ### Did this work correctly in an earlier version?
   
   No
   
   ### Operating System
   
   Linux openSUSE Tumbleweed
   
   ### JDK
   
   openJDK 11
   
   ### Apache NetBeans packaging
   
   Apache NetBeans binary zip
   
   ### Anything else
   
   Looks like there is a race condition happening.
   
   See class `org.netbeans.modules.java.navigation.ClassMemberPanelUI` and its 
method `propertyChange`.
   `ClassMemberPanelUI`'s lookup is an `AbstractLookup` instance with 
`selectedNodes` as its content.
   `selectedNodes` is  updated in a separate thread every time node selection 
changes:
   ```java
               RP.execute(new Runnable() {
                   @Override
                   public void run() {
                       final Node[] oldNodes = (Node[]) evt.getOldValue();
                       final Node[] newNodes = (Node[]) evt.getNewValue();
                       for (Node n : oldNodes) {
                           selectedNodes.remove(n);
                       }
                       for (Node n : newNodes) {
                           selectedNodes.add(n);
                       }
                       if (newNodes.length > 0 && !javadocDone && 
JavadocTopComponent.shouldUpdate()) {
                           scheduleJavadocRefresh(JDOC_TIME);
                       }
                   }
               });
   ```
   
   The same `lookup` object is used in `canFindUsages` of 
`org.netbeans.modules.refactoring.java.ui.RefactoringActionsProvider`:
   ```java
       @Override
       public boolean canFindUsages(Lookup lookup) {
           Collection<? extends Node> nodes = new 
HashSet<Node>(lookup.lookupAll(Node.class));
           if (nodes.size() != 1) {
               return false;
           }
           ...
   ```
   It is possible that `lookup.lookupAll(Node.class)` will return an empty 
collection if `canFindUsages` is called before a new selection is added to 
`selectedNodes` and when it happens -- "Find Usages" action is disabled.
   
   Replacing
   ```java
       @Override
       public org.openide.util.Lookup getLookup() {
           // XXX Check for chenge of FileObject
           return lookup;
       }
   ```
   in `ClassMemberPanelUI` with
   ```java
       @Override
       public org.openide.util.Lookup getLookup() {
           // XXX Check for chenge of FileObject
           return Lookups.fixed( getExplorerManager().getSelectedNodes() );
       }
   ```
   seems to solve the issue, but I am not sure it is a good fix.
   
   ### Are you willing to submit a pull request?
   
   Yes
   
   ### Code of Conduct
   
   Yes


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to