Created
May 3, 2017 19:48
-
-
Save ctrueden/37d4163a52b2480a699eb9dc19164e08 to your computer and use it in GitHub Desktop.
Wrapping Runnable objects as SciJava Commands
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.JOptionPane; | |
import org.scijava.MenuPath; | |
import org.scijava.SciJava; | |
import org.scijava.command.Command; | |
import org.scijava.command.CommandInfo; | |
import org.scijava.menu.ShadowMenu; | |
/** | |
* Adds some commands to the SciJava menu which are backed by {@link Runnable} | |
* objects. | |
*/ | |
public class RunnableCommands { | |
public static void main(final String... args) throws Exception { | |
// Create a SciJava application context. | |
final SciJava sj = new SciJava(); | |
// Create some simple Runnable objects, as a demo. | |
final Runnable hello = new Runnable() { | |
@Override | |
public void run() { | |
JOptionPane.showMessageDialog(null, "Hello world"); | |
} | |
}; | |
final Runnable aloha = new Runnable() { | |
@Override | |
public void run() { | |
JOptionPane.showMessageDialog(null, "Aloha"); | |
} | |
}; | |
// Create Command wrappers for the Runnable objects, and register them. | |
final CommandInfo helloCommand = new RunnableCommandInfo(hello); | |
helloCommand.setMenuPath(new MenuPath("Greetings>Hello World")); | |
sj.module().addModule(helloCommand); | |
final CommandInfo alohaCommand = new RunnableCommandInfo(aloha); | |
alohaCommand.setMenuPath(new MenuPath("Greetings>Aloha")); | |
sj.module().addModule(alohaCommand); | |
// Verify that the SciJava menu has been updated accordingly. | |
final ShadowMenu greetingsMenu = sj.menu().getMenu().getMenu("Greetings"); | |
sj.log().info("greetings menu:\n" + greetingsMenu); | |
// Display the UI, to test out the commands! | |
sj.ui().showUI(); | |
} | |
public static class RunnableCommandInfo extends CommandInfo { | |
private Runnable r; | |
public RunnableCommandInfo(final Runnable r) { | |
super(RunnableCommand.class); | |
this.r = r; | |
} | |
@Override | |
public RunnableCommand createInstance() { | |
return new RunnableCommand(r); | |
} | |
} | |
public static class RunnableCommand implements Command { | |
private final Runnable r; | |
public RunnableCommand(Runnable r) { | |
this.r = r; | |
} | |
@Override | |
public void run() { | |
r.run(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that as of this writing, it doesn't quite work: the commands do not appear in the menu of the UI. Did not have time to troubleshoot why not. They are present in the
MenuService
's applicationShadowMenu
, though.