Skip to content

Commit

Permalink
Merge 'ij1-macro-engine' into HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
dscho committed May 26, 2014
2 parents c0116b3 + fafa80a commit 9ad1002
Show file tree
Hide file tree
Showing 6 changed files with 247 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>net.imagej</groupId>
<artifactId>pom-imagej</artifactId>
<version>2.13</version>
<version>2.15</version>
<relativePath />
</parent>

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/net/imagej/legacy/IJ1Helper.java
Expand Up @@ -567,4 +567,9 @@ private Menu get(final String rawPath) {
}

}

public String runMacro(String macro) {
return IJ.runMacro(macro);
}

}
4 changes: 2 additions & 2 deletions src/main/java/net/imagej/legacy/LegacyJavaRunner.java
@@ -1,8 +1,8 @@
/*
* #%L
* JSR-223-compliant Java scripting language plugin.
* ImageJ software for multidimensional image processing and analysis.
* %%
* Copyright (C) 2008 - 2014 Board of Regents of the University of
* Copyright (C) 2009 - 2014 Board of Regents of the University of
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
* Institute of Molecular Cell Biology and Genetics.
* %%
Expand Down
110 changes: 110 additions & 0 deletions src/main/java/net/imagej/legacy/plugin/IJ1MacroEngine.java
@@ -0,0 +1,110 @@
/*
* #%L
* ImageJ software for multidimensional image processing and analysis.
* %%
* Copyright (C) 2009 - 2014 Board of Regents of the University of
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
* Institute of Molecular Cell Biology and Genetics.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/

package net.imagej.legacy.plugin;

import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

import javax.script.ScriptException;

import net.imagej.legacy.IJ1Helper;

import org.scijava.script.AbstractScriptEngine;

/**
* An almost JSR-223-compliant script engine for the ImageJ 1.x macro language.
* <p>
* As far as possible, this script engine conforms to JSR-223. It lets the user
* evaluate ImageJ 1.x macros. Due to the ImageJ 1.x macro interpreter's
* limitations, functionality such as the {@link #put(String, Object)} is not
* supported, however.
* </p>
*
* @author Johannes Schindelin
*/
public class IJ1MacroEngine extends AbstractScriptEngine {

private final IJ1Helper ij1Helper;

/**
* Constructs an ImageJ 1.x macro engine.
*
* @param ij1Helper
* the helper to evaluate the macros
*/
public IJ1MacroEngine(final IJ1Helper ij1Helper) {
this.ij1Helper = ij1Helper;
}

@Override
public Object eval(final String macro) throws ScriptException {
return ij1Helper.runMacro(macro);
}

@Override
public Object eval(final Reader reader) throws ScriptException {
final StringBuilder builder = new StringBuilder();
final char[] buffer = new char[16384];
try {
for (;;) {
final int count = reader.read(buffer);
if (count < 0) {
break;
}
builder.append(buffer, 0, count);
}
reader.close();
} catch (final IOException e) {
throw new ScriptException(e);
}
return eval(builder.toString());
}

@Override
public void put(final String key, final Object value) {
if (FILENAME.equals(key)) {
return; // ignore
}
final String message = "Unhandled key: " + key + ": " + value;
final Writer writer = getContext().getErrorWriter();
if (writer != null) {
try {
writer.write(message + "\n");
return;
} catch (final IOException e) {
e.printStackTrace();
}
}
System.err.println(message);
}
}
64 changes: 64 additions & 0 deletions src/main/java/net/imagej/legacy/plugin/IJ1MacroLanguage.java
@@ -0,0 +1,64 @@
/*
* #%L
* ImageJ software for multidimensional image processing and analysis.
* %%
* Copyright (C) 2009 - 2014 Board of Regents of the University of
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
* Institute of Molecular Cell Biology and Genetics.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/

package net.imagej.legacy.plugin;

import javax.script.ScriptEngine;

import net.imagej.legacy.DefaultLegacyService;

import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import org.scijava.script.AbstractScriptLanguage;
import org.scijava.script.ScriptLanguage;

/**
* Implements a factory for the ImageJ 1.x Macro language engine.
*
* @author Johannes Schindelin
*/
@Plugin(type = ScriptLanguage.class)
public class IJ1MacroLanguage extends AbstractScriptLanguage {

@Parameter
private DefaultLegacyService legacyService;

@Override
public String getLanguageName() {
return "IJ1 Macro";
}

@Override
public ScriptEngine getScriptEngine() {
return new IJ1MacroEngine(legacyService.getIJ1Helper());
}

}
65 changes: 65 additions & 0 deletions src/test/java/net/imagej/legacy/LegacyEditorTestDrive.java
@@ -0,0 +1,65 @@
/*
* #%L
* ImageJ software for multidimensional image processing and analysis.
* %%
* Copyright (C) 2009 - 2014 Board of Regents of the University of
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
* Institute of Molecular Cell Biology and Genetics.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/

package net.imagej.legacy;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.SwingUtilities;

import net.imagej.ui.swing.script.TextEditor;

import org.scijava.Context;

/**
* Interactive test for the script editor in legacy mode.
*
* @author Johannes Schindelin
*/
public class LegacyEditorTestDrive {
public static void main(String[] args) throws Exception {
final Context context = new Context();
final TextEditor editor = new TextEditor(context);
editor.addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(final WindowEvent e) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
context.dispose();
}
});
}
});
editor.setVisible(true);
}
}

0 comments on commit 9ad1002

Please sign in to comment.