How to create Justinmind plugins

Launch the Eclipse app inside your JustinmindSDK folder. Then select a folder to save your workspace and click “Use this as the default and do not ask again”.

First setup

Next, select “Create a new Java project…”

welcome-justinmindsdk

Choose a name for your plugin and make sure the execution environment JRE selected is ‘JavaSE-11’. Then click on Next.
newplugin-justinmindsdk

In Java Settings click on the Libraries tab. Select “ModulePath” and add all the JavaFX modules that you’ll find in ‘javafx’ folder inside the ‘JustinmindPluginSDK’ folder using the option Add External JARS.

For Windows : JustinmindPluginSDK/javafx/lib

For MacOS: Applications/JustinmindPluginSDK/javafx/lib

modules-justinmindsdk

Select “Classpath” and add all the jars contained in the folder ‘api_plugins’ that you’ll find in the JustinmindSDK folder.

For Windows : JustinmindPluginSDK/api_plugins/

For MacOS: /Applications/JustinmindPluginSDK/api_plugins/

classpath-justinmindsdk

Click on Finish.

Select Don’t create module-info.java when prompted.

Now we have created the main Java project that will contain our plugin’s code.

Write your plugin

Structure

The structure of a Justinmind plugin is very simple. You just need a main class file which will contain the run code of your plugin’s action. This class must implement the Justinmind API interface ‘IPlugin’.

IPlugin defines two methods:

getName() – this method will define the name that will be shown in Justinmind UI once your plugin is installed.

run() – what your plugin does.

Create the class file. Right click on your Plugin ‘src’ folder and select New>Class.

Give your main class a name and select the IPlugin interface from com.justinmind.prototyper.api.plugins package. Click on Finish.

createplugin-justinmindsdk

Now you can write your code.

Example: export all texts to a CSV file

import java.awt.Desktop;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.List;
import com.justinmind.pcontrols.ControlUtils;
import com.justinmind.prototyper.api.API;
import com.justinmind.prototyper.api.IPrototype;
import com.justinmind.prototyper.api.plugins.IPlugin;
import com.justinmind.prototyper.api.ui.canvas.ICanvas;
import com.justinmind.prototyper.api.ui.component.ICanvasComponent;
import com.justinmind.prototyper.api.ui.component.IImage;
import com.justinmind.prototyper.api.ui.component.IInput;
import com.justinmind.prototyper.api.ui.component.IValuedComponent;
import javafx.stage.FileChooser;

public class TestPlugin implements IPlugin { 

  @Override
  public void run() {
    try {
      IPrototype prototype = API.getPrototypeLoader().loadPrototype();
      List canvas = prototype.getApiCanvases();
      String content = "Word in prototype\n\n";
      for (ICanvas current : canvas) {
        content += "Screen: " + current.getApiName() + "\n";
        List components = current.getApiRoot().getApiChildren();
        for (ICanvasComponent iComponent : components) {
          content += addTextsToContent(iComponent);
        }
      }
 
      FileChooser dialog = new FileChooser();
      FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("CSV files (*.csv)", "*.csv");
      dialog.getExtensionFilters().add(extFilter);
      File file = dialog.showSaveDialog(ControlUtils.getMainWindow());
      if (file != null) {
        Writer output = null;
        FileWriter fw = null;
        try {
          fw = new FileWriter(file);
          output = new BufferedWriter(fw);
          output.write(content);
          output.close();
          Desktop.getDesktop().open(file);
        } catch (IOException e) {
          Alert a = new Alert(AlertType.ERROR);
          a.setContentText(e.getLocalizedMessage());
          a.show();
        }
      } else {
        // show error
      }
    } catch (Exception e) {
      Alert a = new Alert(AlertType.ERROR);
      a.setContentText(e.getLocalizedMessage());
      a.show();
    }
  } 

  private String addTextsToContent(ICanvasComponent iComponent) {
    String content = "";
    if (iComponent instanceof IValuedComponent && !(iComponent instanceof IImage) && !(iComponent instanceof IInput)) {
      content += ((IValuedComponent) iComponent).getApiValue() + "\n";
    }
    List components = iComponent.getApiChildren();
    for (ICanvasComponent current_child : components) {
      content += addTextsToContent(current_child);
    }
    return content;
  }

  @Override
  public String getName() {
    return "Test Plugin";
  }
}

Deployment

Once your code is finished, you need to export it as a JAR file in order to create a valid installable plugin in Justinmind.

Right click on you plugin project and select Export.

Select JAR file under Java and click on Next.

selectjar-justinmindsdk

Select an export destination leaving all the other options as default and click on Finish.

export-justinmindsdk

Install and run

Once we have our plugin packed in a JAR file, we need to install it in our Justinmind app in order to run it.

Open Justinmind and open the menu Plug-ins.

Select the option Install a Plug-in…

Select the JAR file exported containing your plugin and click Open.

After this point your plug-in will be installed and a new option will appear under the menu Plug-ins with the name you chose when implementing your plugin. To Run it just click on it.