Dimiter
unread,Oct 6, 2009, 7:10:33 PM10/6/09Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to ImageJX, Rasband Wayne
Hi All,
I have the following ideas about the upgrade of the current plugin
model.
The main weakness I think is that the current plugins do not define
formally inputs and outputs. In this way it is difficult to build
process chains for different analytical/processing tasks.
So here is the code, which I propose :
import java.util.*;
import java.util.concurrent.*;
import ij.ImagePlus;
import ij.plugin.PlugIn;
/**
* @author (C)Dimiter Prodanov
* IMEC
*
* @version 1.0 6 Oct 2009
*
* @contents This is a plugin plus framework.
*
* @license This library is free software; you can redistribute it and/
or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later
version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
Public
* License along with this library; if not, write to the Free
Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA
*/
public interface PlugInPlus extends PlugIn {
abstract void savePreferences(Properties prefs);
abstract void registerInput(String inname, Object invalue)
throws
NullPointerException;
abstract void registerOutput(String outname, Object outvalue)
throws NullPointerException;
@SuppressWarnings("unchecked")
abstract ConcurrentHashMap getOutputs();
@SuppressWarnings("unchecked")
abstract ConcurrentHashMap getInputs();
abstract String getOutputsAsString();
abstract String getInputsAsString();
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
import ij.ImagePlus;
import ij.process.ImageProcessor;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.*;
import java.util.concurrent.*;
/**
* @author (C)Dimiter Prodanov
* IMEC
*
* @version 1.0 6 Oct 2009
*
* @contents This is a plugin plus framework.
*
* @license This library is free software; you can redistribute it and/
or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later
version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
Public
* License along with this library; if not, write to the Free
Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA
*/
public abstract class TPlugin implements PlugInPlus {
private static ConcurrentHashMap<String, Object> in= new
ConcurrentHashMap<String, Object>();
private static ConcurrentHashMap<String, Object> out = new
ConcurrentHashMap<String, Object>();
//public ConcurrentHashMap log = new ConcurrentHashMap<String,
String>
();
/**
*
*/
public TPlugin() {
Class<? extends TPlugin> c=this.getClass();
registerFields(c);
}
private void registerFields(Class<? extends TPlugin> c) {
Field[] fields=c.getDeclaredFields();
for (int i=0; i<fields.length; i++) {
int mod=fields[i].getModifiers();
//System.out.println(f[i].toGenericString() +"
mod: " +
//mod);
try {
if (Modifier.isPublic(mod) &&
Modifier.isStatic(mod) && !(
Modifier.isFinal(mod)
||
Modifier.isTransient
(mod) ||
Modifier.isVolatile
(mod) )) {
System.out.println("accessible
field: "+ fields[i].getName()
+ " = "+ fields
[i] .get(this));
in.put(fields[i].getName(),
false);
out.put(fields[i].getName(),
false);
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} // end try
} // end for
}
public void trimInputs() {
trim(in);
}
public void trimOutputs() {
trim(out);
}
@SuppressWarnings("unchecked")
private void trim( ConcurrentHashMap<String, Object> map) {
for (Enumeration<String> keys=map.keys();
keys.hasMoreElements();) {
String s=keys.nextElement();
try {
if (! (Boolean)map.get(s))
map.remove(s);
} catch (ClassCastException e) {
// brute force approach
}
}
}
/* (non-Javadoc)
* @see PlugInPlus#getInputs()
*/
@Override
public ConcurrentHashMap<String, Object> getInputs() {
// TODO Auto-generated method stub
return in;
}
/* (non-Javadoc)
* @see PlugInPlus#getInputsAsString()
*/
@Override
public String getInputsAsString() {
return mapAsString(in);
}
/* (non-Javadoc)
* @see PlugInPlus#getOutputs()
*/
@Override
public ConcurrentHashMap<String, Object> getOutputs() {
// TODO Auto-generated method stub
return out;
}
/* (non-Javadoc)
* @see PlugInPlus#getOutputsAsString()
*/
@Override
public String getOutputsAsString() {
return mapAsString(out);
}
protected String mapAsString(ConcurrentHashMap map) {
StringBuffer sb=new StringBuffer();
for (Enumeration<String> keys=map.keys();
keys.hasMoreElements();) {
String s=keys.nextElement();
sb.append(s);
sb.append("=");
sb.append(in.get(s) );
sb.append(" ");
}
return sb.toString();
}
/* (non-Javadoc)
* @see PlugInPlus#registerInput(java.lang.Object)
*/
@Override
public void registerInput(String inname, Object invalue) throws
NullPointerException {
if (in.containsKey(inname)) {
in.put(inname, invalue);
out.put(inname, false);
}
}
/*
public void registerImageInput(ImagePlus imp) {
String name=imp.getTitle();
in.put(name, imp);
}
*/
/* (non-Javadoc)
* @see PlugInPlus#registerOutput(java.lang.Object)
*/
@Override
public void registerOutput(String outname, Object outvalue) {
if (out.containsKey(outname)) {
try {
out.put(outname, outvalue);
} catch (NullPointerException e) {
out.put(outname, true);
//e.printStackTrace();
}
in.put(outname, false);
}
}
/* (non-Javadoc)
* @see PlugInPlus#savePreferences(java.util.Properties)
*/
@Override
public void savePreferences(Properties prefs) {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see ij.plugin.PlugIn#run(java.lang.String)
*/
@Override
public void run(String arg) {
// TODO Auto-generated method stub
}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
There is also a test program
import ij.ImagePlus;
/**
* @author (C)Dimiter Prodanov
* IMEC
*
* @version 1.0 6 Oct 2009
*
* @contents This is an examples for the PluginPlus framework.
*
* @license This library is free software; you can redistribute it and/
or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later
version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
Public
* License along with this library; if not, write to the Free
Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA
*/
public class Ptest extends TPlugin implements PlugInPlus {
/**
*
* only static fields are registered correctly
*
*/
public static double d=-1.95;
public static int x=25;
public String s="test";
private String ps="test";
private static int ps2=-4;
public static ImagePlus imp=null;
public Ptest() {
super();
}
/**
* @param args
*/
public static void main(String[] args) {
Ptest pt=new Ptest();
pt.registerInput("x", x);
pt.registerInput("ps2", -4);
pt.registerInput("d", 2.34);
//System.out.println(pt.getInputsAsString());
pt.trimInputs();
System.out.println("inputs: " +pt.getInputsAsString());
pt.registerOutput("imp", imp);
pt.trimOutputs();
System.out.println("outputs: " +pt.getOutputsAsString
());
}
}