wiki:MolgenisCustomCommand

Adding Custom buttons to the generated code

Each button in the auto-generated MOLGENIS user interface is actually an implementation of the  ScreenCommand. Examples of these commands can be found here. Note how each command defines its behavior.

MOLGENIS users can customize the generated user interface by adding more commands or even replacing the standard commands. Below is described how.

1. Define the custom commands in your *ui.xml

To add custom buttons one needs to define references to the ScreenCommand subclasses you implemented as button. That is done as follows:

<form name="Experiments" entity="Experiment" commands="commands.MyCommand">

Multiple commands can be added using ',' seperation, e.g.

<form name="Experiments" entity="Experiment" commands="commands.MyCommand,commands.MyCommand2">

2. Implement the button

Secondly the referenced classes need to be implemented. This can be done by subclassing an existing command and overriding its main methods. Users interested to create a command from scratch can extends from the generic SimpleCommand.

Example of extending the 'AddCommand':

package commands;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.util.List;

import org.molgenis.framework.db.Database;
import org.molgenis.framework.db.DatabaseException;
import org.molgenis.framework.ui.FormModel;
import org.molgenis.framework.ui.ScreenModel;
import org.molgenis.framework.ui.ScreenModel.Show;
import org.molgenis.framework.ui.commands.AddCommand;
import org.molgenis.framework.ui.html.ActionInput;
import org.molgenis.framework.ui.html.HtmlInput;
import org.molgenis.util.Tuple;

import app.ui.ExperimentsForm;

/**
 * Customized version of the 'Add' command.
 */
public class MyCommand extends AddCommand
{
        public MyCommand(FormModel s)
        {
                //override the standard add function
                super("edit_new", s);
        }

        @Override
        public List<HtmlInput> getInputs() throws DatabaseException
        {
                //reuse the inputs from Experiment
                List<HtmlInput> inputs = new ExperimentsForm().getInputs();
                
                //you could now replace some of these inputs with your own
                //e.g. create a custom 'PubmedInput'
                inputs.add(new PubmedInput("pubmed"));
                
                return inputs;
        }
        
        @Override
        public List<HtmlInput> getActions()
        {
                //get the standard inputs
                List<HtmlInput> actions = super.getActions();
                
                //add an extra button
//              ActionInput close = new ActionInput("close");
//              close.setIcon("path/to/image");
//              close.setLabel("My Command Example");
//              actions.add(close);
                
                return actions;
        }
        
        @Override
        public ScreenModel.Show handleRequest(Database db, Tuple request, PrintWriter downloadStream) throws ParseException, DatabaseException, IOException
        {
                Show result = super.handleRequest(db, request, downloadStream);
                
                //return SHOW_MAIN if this request should be handled in the main screen
                //return SHOW_DIALOG if you want to keep the dialog open
                return result;
        }
}