Skip to content

Commit

Permalink
Fixes issues with nested menus in BdvScijavaHelper
Browse files Browse the repository at this point in the history
Adds the possibility to add separator into Bdv Menu
  • Loading branch information
NicoKiaru committed Aug 16, 2024
1 parent e34a613 commit 5b4cb7b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 21 deletions.
68 changes: 48 additions & 20 deletions src/main/java/sc/fiji/bdvpg/scijava/BdvScijavaHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;
import java.awt.Component;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -75,7 +77,37 @@ static public void addCommandToBdvHandleMenu(BdvHandle bdvh, Context ctx,
Plugin plugin = commandClass.getDeclaredAnnotation(Plugin.class);
addActionToBdvHandleMenu(bdvh, plugin.menuPath(), skipTopLevels, () -> ctx
.getService(CommandService.class).run(commandClass, true, args));
}

static public void addSeparator(BdvHandle bdvh, String pathHierarchy) {
if (bdvh instanceof BdvHandleFrame) {
final JMenuBar bdvMenuBar = ((BdvHandleFrame) bdvh).getBigDataViewer()
.getViewerFrame().getJMenuBar();
List<String> path = Arrays.stream(pathHierarchy.split(">")).map(
String::trim).collect(Collectors.toList());
path.add("Separator"); // Simplify my life : direct reuse of the method below
JMenuItem jMenuItemRoot = findOrCreateJMenu(bdvMenuBar, path);
if (jMenuItemRoot != null) {
final JSeparator jSeparator = new JSeparator();
jMenuItemRoot.add(jSeparator);
bdvMenuBar.updateUI();
}
else {
logger.error("Could not find or create jmenu (" + bdvMenuBar + ", " +
path + ")");
}
}
else {
logger.error(
"Cannot put command on menu : the bdvhandle is not a frame.");
}
}

static public void addCommandToBdvHandleMenu(BdvHandle bdvh, Context ctx, String path,
Class<? extends Command> commandClass, Object... args)
{
addActionToBdvHandleMenu(bdvh, path, 0, () -> ctx
.getService(CommandService.class).run(commandClass, true, args));
}

static public void addActionToBdvHandleMenu(BdvHandle bdvh,
Expand Down Expand Up @@ -112,7 +144,7 @@ static public void addActionToBdvHandleMenu(BdvHandle bdvh,
private static JMenu findOrCreateJMenu(JMenuBar bdvMenuBar,
List<String> path)
{
if (path.size() == 0) {
if (path.isEmpty()) {
logger.error("No Path specified in find or create JMenu!");
}
if (path.size() == 1) {
Expand All @@ -136,39 +168,35 @@ private static JMenu findOrCreateJMenu(JMenuBar bdvMenuBar,
}
else {
jmenu = new JMenu(path.get(0));
bdvMenuBar.add(jmenu);
}
bdvMenuBar.add(jmenu);
path.remove(0);
return findOrCreateJMenu(jmenu, path);
}

private static JMenu findOrCreateJMenu(JMenu jMenu, List<String> path) {
if (path.size() == 0) {
if (path.isEmpty()) {
logger.error(" Reached unreachable statement !");
}
if (path.size() == 1) {
return jMenu;
}
boolean found = false;
int idx = 0;
while ((idx < jMenu.getMenuComponentCount()) && (!found)) {
JMenu jmenuTest = (JMenu) jMenu.getMenuComponent(idx);
if (jmenuTest.getText().equals(path.get(0))) {
found = true;
}
else {
idx++;
while (idx < jMenu.getMenuComponentCount()) {
Component component = jMenu.getMenuComponent(idx);
if (component instanceof JMenu) {
JMenu jmi = (JMenu) component;
if (path.get(0).equals(jmi.getText())) {
path.remove(0);
return findOrCreateJMenu(jmi, path);
}
}
idx++;
}
JMenu jmenuFound;
if (found) {
jmenuFound = (JMenu) jMenu.getMenuComponent(idx);
}
else {
jmenuFound = new JMenu(path.get(0));
}
jMenu.add(jmenuFound);

JMenu jm = new JMenu(path.get(0));
jMenu.add(jm);
path.remove(0);
return findOrCreateJMenu(jmenuFound, path);
return findOrCreateJMenu(jm, path);
}
}
12 changes: 11 additions & 1 deletion src/test/src/sc/fiji/bdvpg/demos/bdv/BdvCreatorDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
*/
package sc.fiji.bdvpg.demos.bdv;

import bdv.util.BdvHandle;
import org.scijava.Context;
import org.scijava.ui.UIService;
import org.scijava.ui.swing.SwingUI;
import sc.fiji.bdvpg.scijava.BdvScijavaHelper;
import sc.fiji.bdvpg.scijava.services.SourceAndConverterBdvDisplayService;

/**
Expand All @@ -51,7 +53,15 @@ public static void main( String[] args ) {
ctx.getService(SourceAndConverterBdvDisplayService.class).getNewBdv();

// Returns the previous BDV or the one "on top", meaning the one clicked by the user
ctx.getService(SourceAndConverterBdvDisplayService.class).getActiveBdv();
BdvHandle bdvh = ctx.getService(SourceAndConverterBdvDisplayService.class).getActiveBdv();

BdvScijavaHelper.addActionToBdvHandleMenu(bdvh,"File>SubMenu1>SubMenu2>Say Hello 1!",0, () -> bdvh.getViewerPanel().showMessage("Hello!"));
BdvScijavaHelper.addSeparator(bdvh,"File>SubMenu1");

BdvScijavaHelper.addActionToBdvHandleMenu(bdvh,"File>SubMenu1>SubMenu3>Say Hello 2!",0, () -> bdvh.getViewerPanel().showMessage("Hello!"));
BdvScijavaHelper.addActionToBdvHandleMenu(bdvh,"Greetings>Menu1>Menu2>Say Hello 3!",0, () -> bdvh.getViewerPanel().showMessage("Hello!"));
BdvScijavaHelper.addActionToBdvHandleMenu(bdvh,"Greetings>Menu1>Menu2>Say Hello 4!",0, () -> bdvh.getViewerPanel().showMessage("Hello!"));

}

}

0 comments on commit 5b4cb7b

Please sign in to comment.