Skip to content

Commit

Permalink
Fix open maven subproject context menu
Browse files Browse the repository at this point in the history
attempt to fix UI issue of not selectable sub context menu on some
configurations.

Lazy init logic will create the JPopupMenu only once now.
  • Loading branch information
mbien committed Sep 2, 2024
1 parent 33d1130 commit 0a2287e
Showing 1 changed file with 20 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@
import javax.swing.JPopupMenu;
import org.netbeans.api.annotations.common.StaticResource;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectInformation;
import org.netbeans.api.project.ProjectUtils;
import org.netbeans.modules.project.ui.OpenProjectList;

import static org.netbeans.modules.project.ui.actions.Bundle.*;

import org.netbeans.spi.project.ProjectContainerProvider;
import org.netbeans.spi.project.SubprojectProvider;
import org.openide.awt.ActionID;
Expand Down Expand Up @@ -137,8 +140,8 @@ private void openAllRequiredProjects(Node [] activatedNodes) {

private class LazyMenu extends JMenu {
private final Node[] activatedNodes;
boolean initialized; // create only once, prevents recreating items when user repeatedly expends and collapses the menu
private Set<? extends Project> subProjects;
private boolean menuCreated; // create only once, prevents recreating items when user repeatedly expends and collapses the menu
private volatile Set<? extends Project> subProjects;

@NbBundle.Messages("LBL_SubProjectPopupMenu_Initializing=Initializing...")
private LazyMenu(Node[] nodes) {
Expand All @@ -147,25 +150,20 @@ private LazyMenu(Node[] nodes) {
JMenuItem item = new JMenuItem(Bundle.LBL_SubProjectPopupMenu_Initializing());
item.setEnabled(false);
add(item);
RP.post(new Runnable() {
@Override
public void run() {
getSubProjects();
}
});
RP.post(this::asyncFindSubProjects);
}

@Override
public JPopupMenu getPopupMenu() {
if(initialized) {
if (!menuCreated && subProjects != null) {
createSubMenu();
}
return super.getPopupMenu();
}

private void createSubMenu() {
removeAll();
if(subProjects != null && !subProjects.isEmpty()) {
if (!subProjects.isEmpty()) {
super.getPopupMenu().setLayout(new VerticalGridLayout());
final JMenuItem openAllProjectsItem = new JMenuItem(new AbstractAction() {
@Override
Expand All @@ -181,7 +179,7 @@ public void actionPerformed(ActionEvent e) {
nothingItem.setEnabled(false);
add(nothingItem);
}
if(subProjects != null && !subProjects.isEmpty()) {
if (!subProjects.isEmpty()) {
for(final Project prjIter:subProjects) {
JMenuItem selectPrjAction = new JMenuItem(new AbstractAction() {
@Override
Expand All @@ -193,9 +191,10 @@ public void actionPerformed(ActionEvent e) {
add(selectPrjAction);
}
}
menuCreated = true;
}

private void getSubProjects() {
private void asyncFindSubProjects() {
try {
if(activatedNodes != null) {
for( int i = 0; i < activatedNodes.length; i++ ) {
Expand All @@ -214,7 +213,15 @@ private void getSubProjects() {
}
}
} finally {
initialized = true;
if (subProjects == null) {
subProjects = Set.of();
} else {
// trigger project name query tasks
// without this, the first call to getDisplayName() will likely return a project path fallback
for (Project project : subProjects) {
ProjectUtils.getInformation(project).getDisplayName();
}
}
}
}
}
Expand Down

0 comments on commit 0a2287e

Please sign in to comment.