Create Plugin

From JWic

This document describes how to create a new Plugin that provides a new Perspective which displays a View in the content area. The guide is based upon a development environment with Eclipse and Tomcat.

Contents

Create a Project

Create a new java project for your plugin. Modify the build path and add the following projects:

  • jWic
  • jwic.ecolib
  • jwic.wap

Modify the build path of the jwic.wap.dev project and add your new project. If you are using the sysdeo tomcat plugin, you must also change the DevLoader classpath in the Tomcat settings of the jwic.wap.dev project to include the output folder of your new project.

Create the Plugin

Create your top-level package, i.e. wap.test.demo and create a new java class in that package with the name MyDemoPlugin. This class should extend the class de.jwic.wap.platform.Plugin.

Each plugin must have a unique id. Since those IDs are often used to reference extensions/extension points, it is a good practice to create a constant field that stored the id. In our case, the field name is ID_PLUGIN and the value is our package name (wap.test.demo).

A plugin class is handled as a singleton and is shared among all sessions. To provide an easy way for our extensions (i.e. View) to access the plugin, it is a good practice to create a static method where you can access the plugin instance. The static accessor method is named getDefault().

Your class should now look like this:

package wap.test.demo;
import de.jwic.wap.platform.Plugin;
/**
 * ...
 */
public class MyDemoPlugin extends Plugin {

  public final static String ID_PLUGIN = "wap.test.demo";
	
  private static MyDemoPlugin instance = null;
	
  /**
   * Constructor.
   */
  public MyDemoPlugin() {
    setId(ID_PLUGIN);
    instance = this;
  }

  /**
   * Returns the default instance.
   * @return
   */
  public static MyDemoPlugin getDefault() {
    return instance;
  }
}

Register the Plugin

We must now register our plugin. This is done by adding it as a bean to the applicationContext configured in the jwic.wap.dev project (in web/WEB-INF). Spring is configured to add all files that end with -plugin.wap.xml to the applicationContext. So all we need to do is to create such a file for our plugin and specify our plugin as a bean there.

File waptest-plugin.wap.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="wap.test.demo" class="wap.test.demo.MyDemoPlugin" />
</beans>

Now our plugin will be registered by the platform during startup.

Create a View

Create a new java class called DemoView in your package. This class must extend de.jwic.wap.core.View. A view is also identified by a unique id, so it is a good practice to create a public constant with the id of the view. Since the classname is unique, it is common to use the full classname as the view ID:

public final static String ID_VIEW = DemoView.class.getName();

As next step you must implement the createControls(..) method, where you create your "content". In our demo, we just add a label with a Hello World message:

public void createControls(IControlContainer container) {
  LabelControl label = new LabelControl(container);
  label.setText("Hello World");
}

Create a Perspective

Because there is no generic "Open View" feature yet, we must provide our own perspective to actually open our view. A perspective specifies the visible areas on the page and defines the initial content, which will be our view.

So go on and create a new class called DemoPerspective that extends de.jwic.wap.core.Perspective. Just like the View and the Plugin, we create a constant for the id of the perspective:

public final static String ID_PERSPECTIVE = DemoPerspective.class.getName();

Then you override the initialize(Site) method, to specify the content of the perspective. For our demo, the only visible area should be the content area. Since this area is always visible, we just have to "hide" the other areas. After that, we must specify the views that will be initialiy visible. Here is the code for these actions:

public void initialize(Site site) {
  super.initialize(site);
  setShowLeftArea(false); // the other areas are invisible by default
  addInitialView(Area.POS_CONTENT, DemoView.ID_VIEW);
}

Register the Extensions

Before our View and Perspective can be used, we must 'register' them as extension to their extension point. This is done by the Plugin class when the plugin is instanciated. So open the MyDemoPlugin and modify the constructor as follows:

  public MyDemoPlugin() {
    setId(ID_PLUGIN);
    instance = this;
    // register the view
    addExtension(
      CorePlugin.ID_PLUGIN, 
      CorePlugin.ID_EXP_VIEW, 
      DemoView.ID_VIEW, 
      DemoView.class.getName()
    );	
    // register the perspective
    addExtension(
      CorePlugin.ID_PLUGIN,
      CorePlugin.ID_EXP_PERSPECTIVE,
      DemoPerspective.ID_PERSPECTIVE,
      DemoPerspective.class.getName()
    );
    
  }

Run & Test

Now you can launch the server and test your plugin. Once the application started, you should find your perspective in the list of perspectives.

If you used this guide, it would be nice from you to give some feedback in the discussion to this article.

SourceForge.net Logo