Skip to content

Instantly share code, notes, and snippets.

@tferr
Created March 31, 2021 22:38
Show Gist options
  • Save tferr/a92e269ec16876d5ba9e2309c86bf5c8 to your computer and use it in GitHub Desktop.
Save tferr/a92e269ec16876d5ba9e2309c86bf5c8 to your computer and use it in GitHub Desktop.
Example to test Macro recordability of Scijava components
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import org.scijava.ItemVisibility;
import org.scijava.command.Command;
import org.scijava.command.DynamicCommand;
import org.scijava.module.MutableModuleItem;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import org.scijava.widget.Button;
import net.imagej.ImageJ;
import net.imagej.lut.LUTService;
import net.imglib2.display.ColorTable;
@Plugin(type = Command.class, menuPath = "Plugins>Failed Recorded Cmd", initializer = "init")
public class Test extends DynamicCommand {
@Parameter
private LUTService lutService;
/* Prompt */
@Parameter(required = false, visibility = ItemVisibility.MESSAGE, label = "A message")
private String aMsg;
@Parameter(label = "A double", required = false)
private double aDouble;
@Parameter(label = "A boolean", required = false)
private boolean aBoolean;
@Parameter(label = "A button", required = false)
private Button aButton;
@Parameter(label = "A string choice", required = false, callback = "lutChoiceChanged")
private String lutChoice;
@Parameter(label = " A Colortable", required = false)
private ColorTable aTable;
@Parameter(label = "A file", required = false)
private File aFile;
private Map<String, URL> luts;
@SuppressWarnings("unused")
private void init() {
// see net.imagej.lut.LUTSelector
luts = lutService.findLUTs();
final ArrayList<String> choices = new ArrayList<>();
for (final Map.Entry<String, URL> entry : luts.entrySet()) {
choices.add(entry.getKey());
}
Collections.sort(choices);
final MutableModuleItem<String> input = getInfo().getMutableInput("lutChoice", String.class);
input.setChoices(choices);
input.setValue(this, lutChoice);
if (lutChoice == null) lutChoice = "Fire.lut";
if (aFile == null) aFile = new File("/some/file/path");
lutChoiceChanged();
}
private void lutChoiceChanged() {
try {
aTable = lutService.loadLUT(luts.get(lutChoice));
} catch (final NullPointerException | IOException ignored) {
// do nothing
}
}
public static void main(final String[] args) {
final ImageJ ij = new ImageJ();
ij.ui().showUI();
ij.command().run(Test.class, true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment