Skip to content

Commit

Permalink
Merge pull request #307 from imagej/macro-recorder-postprocessor
Browse files Browse the repository at this point in the history
Closes #305
  • Loading branch information
hinerm authored Jul 22, 2024
2 parents 915bec8 + ba93c4e commit 786a58b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<groupId>net.imagej</groupId>
<artifactId>imagej-legacy</artifactId>
<version>1.2.3-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>

<name>ImageJ Legacy Bridge</name>
<description>The legacy component enables backward compatibility with the original version of ImageJ (1.x). It contains the code necessary to translate between ImageJ and ImageJ2 images, so that ImageJ plugins can be executed faithfully.</description>
Expand Down
27 changes: 17 additions & 10 deletions src/main/java/net/imagej/legacy/DefaultLegacyHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@
public class DefaultLegacyHooks extends LegacyHooks {

private final LegacyService legacyService;
private final IJ1Helper helper;

private LogService log;
private LegacyEditor editor;
Expand All @@ -97,14 +96,7 @@ public class DefaultLegacyHooks extends LegacyHooks {
private BufferedWriter logFileWriter;

public DefaultLegacyHooks(final LegacyService legacyService) {
this(legacyService, legacyService.getIJ1Helper());
}

public DefaultLegacyHooks(final LegacyService legacyService,
final IJ1Helper helper)
{
this.legacyService = legacyService;
this.helper = helper;
}

@Override
Expand Down Expand Up @@ -147,6 +139,8 @@ public Object interceptRunPlugIn(final String className, final String arg) {
return legacyService == null ? null : legacyService.getContext();
}

IJ1Helper helper = helper();

// Intercept IJ1 commands
if (helper != null) {
// intercept ij.plugins.Commands
Expand Down Expand Up @@ -341,6 +335,7 @@ public void addMenuItem(final String menuPath, final String command) {
final Pattern pattern = Pattern.compile(
"^\\s*([^,]*),\\s*\"([^\"]*)\",\\s*([^\\s]*(\\(.*\\))?)\\s*");
final ClassLoader cl = Context.getClassLoader();
final IJ1Helper helper = helper();
try {
final Enumeration<URL> pluginsConfigs = cl.getResources("plugins.config");
while (pluginsConfigs.hasMoreElements()) {
Expand Down Expand Up @@ -501,7 +496,7 @@ public boolean interceptCloseAllWindows() {
final Window win = windows[w];

// Skip the ImageJ 1.x main window
if (win == null || win == helper.getIJ()) {
if (win == null || win == helper().getIJ()) {
continue;
}

Expand Down Expand Up @@ -550,6 +545,7 @@ public boolean interceptCloseAllWindows() {
@Override
public void interceptImageWindowClose(final Object window) {
final Frame w = (Frame)window;
final IJ1Helper helper = helper();
// When quitting, IJ1 doesn't dispose closing ImageWindows.
// If the quit is later canceled this would leave orphaned windows.
// Thus we queue any closed windows for disposal.
Expand All @@ -569,7 +565,7 @@ public boolean disposing() {
// within its ij.ImageJ#run() method, which is typically, but not always,
// called on a separate thread by ij.ImageJ#quit(). The question is: did
// the shutdown originate from an IJ1 code path, or a SciJava one?
if (helper.isDisposing()) {
if (helper().isDisposing()) {
// NB: ImageJ1 is in the process of a hard shutdown via an API call on
// the SciJava level. It was probably either LegacyService#dispose() or
// LegacyUI#dispose(), either of which triggers IJ1Helper#dispose().
Expand All @@ -586,6 +582,17 @@ public boolean disposing() {

// -- Helper methods --

/**
* Convenience method for accessing the attached {@link LegacyService}'s
* {@link IJ1Helper}.
*/
private IJ1Helper helper() {
// NB: although there is a setter for the IJ1Helper, it is documented as
// "non-API" and thus the helper should never be null. If this changes at
// some point we should do null checking here.
return legacyService.getIJ1Helper();
}

/**
* Determines whether a file is binary or text.
* <p>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/imagej/legacy/LegacyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ public void initialize() {
final ClassLoader loader = Context.getClassLoader();
ij1Helper = new IJ1Helper(this);
LegacyInjector.installHooks(loader, //
new DefaultLegacyHooks(this, ij1Helper));
new DefaultLegacyHooks(this));
instance = this;

// Initialize ImageJ 1.x, if needed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.scijava.Context;
import org.scijava.module.Module;
import org.scijava.module.process.AbstractPreprocessorPlugin;
import org.scijava.module.process.PostprocessorPlugin;
import org.scijava.module.process.PreprocessorPlugin;
import org.scijava.plugin.PluginInfo;
import org.scijava.plugin.PluginService;
Expand Down Expand Up @@ -86,6 +87,9 @@ public void testParametersRecorded() throws InterruptedException,
context.service(PluginService.class).addPlugin(new PluginInfo<>(
MockInputHarvester.class, PreprocessorPlugin.class));

context.service(PluginService.class).addPlugin(new PluginInfo<>(
MacroRecorderPostprocessor.class, PostprocessorPlugin.class));

// NB: Override the IJ1Helper to remember which parameters get recorded.
final EideticIJ1Helper ij1Helper = new EideticIJ1Helper();
legacyService.setIJ1Helper(ij1Helper);
Expand Down

0 comments on commit 786a58b

Please sign in to comment.