Difference: DpHipeTools (41 vs. 42)

Revision 422009-06-10 - JaimeSaiz

Line: 1 to 1
 
META TOPICPARENT name="DpHipe"

Adding Tools to HIPE

Line: 6 to 6
  Some short-cuts
Added:
>
>
  -->
Line: 328 to 329
 This follows in some way the MVC pattern: your target data is the Model, your associated viewer is the View, and your tool is the Controller.

Tool Implementation

Changed:
<
<
The Tool interface is simple:
public interface Tool {

    // Known categories to which a tool may belong to
    public enum Category { SPIRE, PACS, HIFI, GENERAL, IMAGE }

    // Whether the tool can cope with the given values; that is, if they can be associated to the input parameters
    boolean canHandle(List<?> values);

    // Whether the tool can cope with the given value; that is, if it can be associated to the prime input
    boolean canHandle(Object value);

>
>
In order to write a tool, Tool interface needs to be implemented. Instead of doing it directly, it is encouraged to extend AbstractTool.
 
Changed:
<
<
// Get an array of categories to which this tool belongs to Category[] getCategories();
>
>
The information to be provided is passed to one of its constructors in a super call from the derived class:
 
Changed:
<
<
// Return the set of input parameters List<Parameter> getInputs();

// Get the tool name String getName();

// Return the prime input parameter Parameter getPrimeInput();

// Return the source code of this tool SourceCode getSourceCoe();

>
>
    /** Constructor for a tool with a single parameter and general category. */
    protected AbstractTool(String name, Parameter primeInput)

 
Changed:
<
<
// Get the actual object that does the work Object getToolObject();
>
>
/** Constructor for a tool with a single input parameter. */ protected AbstractTool(String name, Parameter primeInput, Category... categories)
 
Changed:
<
<
// Set the actual object that does the work void setToolObject(Object o);
>
>
/** Constructor for a tool with multiple input parameters and general category. */ protected AbstractTool(String name, Parameter primeInput, List<? extends Parameter> inputs)
 
Changed:
<
<
// Returns whether this tool is able to be executed boolean isApplicable(); }
>
>
/** Constructor with all arguments. */ protected AbstractTool(String name, Parameter primeInput, List<? extends Parameter> inputs, Category... categories)
 
Changed:
<
<
You provide the variable types you are interested in within the prime input: just return a ToolParameter initiated with the proper class of data you want to handle.
>
>
You provide the variable types you are interested in within the prime input: just return a ToolParameter initiated with the proper class of data you want to handle:
 

Changed:
<
<
private ToolParameter _prime = new ToolParameter("data", MyTargetDataType.class); public Parameter getPrimeInput() { return _prime; }
>
>
new ToolParameter("data", MyTargetDataType.class)
 
Added:
>
>
More conditions for checking whether the tool can react on a particular variable can be added by providing a ParameterValidator to the prime input.
 The actual job to be done can be delegated to a third object (the "tool object"), or just be executed by the tool class itself.
Changed:
<
<
In this latter case, the method Object getToolObject() should return this.
>
>
This latter case is the default, otherwise, you need to call setToolObject(Object toolObject) in your constructor.

Moreover, you may return the categories you think the tool is meaningful for, by providing the proper ones in the super call.

Naming conventions

 
Changed:
<
<
Moreover, you may return the categories you think the tool is meaningful for, through the proper implementation of Category[] getCategories().
>
>
Conventions for names of a tool class and its variable in the Tasks view are similar than those for tasks. For example, a tool for spectrum filtering could be called:

     Name of the Class              : SpectrumFilterTool
     Name of the Tool (getName())   : spectrumFilter
     Name of the variable in Jython : spectrumFilter
 
 

Tool Viewer

Every tool has an associated viewer, which must implement EditorComponent (by extending AbstractEditorComponent or one of its subclasses).
Line: 388 to 382
 
# Associate the tool with the viewer
REGISTRY.register(COMPONENT,Extension(

Changed:
<
<
"My Tool", "herschel.path.to.MyToolComponent",
>
>
"Spectrum Filter Tool", "herschel.path.to.SpectrumFilterToolComponent",
  "factory.editor.tool",
Changed:
<
<
"herschel.path.to.MyTool"))
>
>
"herschel.path.to.SpectrumFilterTool"))
  # Register the tool so it is automatically available for the proper variables in HIPE from herschel.ia.gui.kernel import ToolRegistry
Changed:
<
<
from herschel.path.to import MyTool ToolRegistry.getInstance().register(MyTool())
>
>
from herschel.path.to import SpectrumFilterTool spectrumFilter = SpectrumFilterTool() ToolRegistry.getInstance().register(spectrumFilter)

all = [ "spectrumFilter", "SpectrumFilterTool", ... ]

 
Line: 427 to 424
     1. The tool class

Changed:
<
<
public class ButtonTool implements Tool {
>
>
public class SimpleButtonTool extends AbstractTool {
 
Deleted:
<
<
private Category[] _categories = { Category.GENERAL }; private ToolParameter _prime = new ToolParameter("data", ArrayData.class);
  private ArrayData _data; private boolean _flag = true;
Changed:
<
<
public Category[] getCategories() { return _categories; }

public String getName() { return "Button Tool"; }

public Parameter getPrimeInput() { return _prime; }

public Object getToolObject() { return this;

>
>
public SimpleButtonTool() { super("simpleButton", new ToolParameter("data", ArrayData.class));
  }
Changed:
<
<
public void setToolObject(Object o) { // do nothing }

public void setData(ArrayData data) {

>
>
void setData(ArrayData data) {
  _data = data; }
Line: 471 to 450
     2. The viewer class

Changed:
<
<
public class ButtonToolComponent extends AbstractEditorComponent<ToolSelection> {
>
>
public class SimpleButtonToolComponent extends AbstractEditorComponent<ToolSelection> {
  private static final long serialVersionUID = 1L; private static int _counter = 1;
Changed:
<
<
private ButtonTool _tool;
>
>
private SimpleButtonTool _tool;
 
Changed:
<
<
public ButtonToolComponent() {
>
>
public SimpleButtonToolComponent() {
  super(new BorderLayout()); }
Changed:
<
<
protected Class<ToolSelection> getSelectionType() {
>
>
protected Class getSelectionType() {
  return ToolSelection.class; }

protected boolean makeEditorContent() { final JButton button = new JButton(); setName("Button Tool " + _counter++);

Changed:
<
<
_tool = (ButtonTool)getSelection().getTool();
>
>
_tool = (SimpleButtonTool)getSelection().getTool();
  _tool.setData((ArrayData)getSelection().getSelection().getValue()); _tool.updateLabel(button); button.addActionListener(new ActionListener() {
Line: 510 to 489
     3. The registration

Added:
>
>
COMPONENT = ExtensionRegistry.COMPONENT REGISTRY = ExtensionRegistry.getInstance()

REGISTRY.register(COMPONENT,Extension( "Tool", "herschel.ia.task.views.TaskToolComponent", "factory.editor.new", "herschel.ia.gui.kernel.ToolSelection"));

 REGISTRY.register(COMPONENT,Extension( "Button Tool",
Changed:
<
<
"herschel.your.package.ButtonToolComponent",
>
>
"herschel.ia.task.views.SimpleButtonToolComponent",
  "factory.editor.tool",
Changed:
<
<
"herschel.your.package.ButtonTool"))
>
>
"herschel.ia.task.views.SimpleButtonTool"))
  from herschel.ia.gui.kernel import ToolRegistry
Changed:
<
<
from herschel.your.package import ButtonTool ToolRegistry().getInstance().register(ButtonTool())
>
>
from herschel.ia.task.views import SimpleButtonTool simpleButton = SimpleButtonTool() ToolRegistry.getInstance().register(simpleButton)

# cleanup del(ExtensionRegistry, Extension, REGISTRY, COMPONENT)

all = [ "simpleButton", "SimpleButtonTool" ]

 
 
This site is powered by the TWiki collaboration platform Powered by Perl