This tutorial is a quick introduction for developers who would like to create a plug-in for Designer 2. This document describes how to create a plug-in that adds a single custom component. Actually all the work can be done automatically using the "Add from Project" wizard in the "Palette Manager" in the Designer palette. The overall purpose is to show how plug-in development for NetBeans Mobility can be done. The plug-in also allows more customizations to be added later than the "Add from Project" wizard.
The documentation is intended as a starting point for exploring the nuts and bolts of the Visual Designer API and its MIDP model implementation.
In this document, we assume that you have a JavaME class/library which contains a component that you want to add to Designer 2. If not you have to create it first.
We will use "mylibrary.MyForm" class like this:
package mylibrary;
public class MyForm extends Form {
private int myValue;
public MyForm () {
super ("Hi");
myValue = 5;
}
public int getMyValue () {
return myValue;
}
public void setMyValue (int myValue) {
this.myValue = myValue;
}
}
The document will not describe how to add the library/class into your MobileApplication project. Let us know, if you would like to have it described in details.
cd /trunk hg clone http://hg.netbeans.org/main ant
Also assure that you are passing "-Xmx256m" into "ANT_OPTS" environment property e.g.: add following line to ".antrc" script into your home directory:
export ANT_OPTS="-Xmx512m"Otherwise the compilation runs out of memory.
Since the modele is created in the trunk directory it automatically integrates with NetBeans. All you have to do, it to set up your module dependencies.
Now you have everything set up.
Designer project is meta-modelling. This means:
For more details, see MobilityDesigner2QuickGuide
First of all we have to create a component descriptor for "mylibrary.MyForm" component.
package org.netbeans.vmd.mycustomcomponents;
import java.util.List;
import org.netbeans.modules.vmd.api.model.ComponentDescriptor;
import org.netbeans.modules.vmd.api.model.PropertyDescriptor;
import org.netbeans.modules.vmd.api.model.TypeDescriptor;
import org.netbeans.modules.vmd.api.model.TypeID;
import org.netbeans.modules.vmd.api.model.VersionDescriptor;
import org.netbeans.modules.vmd.midp.components.displayables.FormCD;
public class MyFormCD extends ComponentDescriptor {
// we use convention to typeid that reprensents a class that the second parameter is fully-qualified class-name
public static final TypeID TYPEID = new TypeID (TypeID.Kind.COMPONENT, "mylibrary.MyForm"); // NOI18N
public MyFormCD() {
}
public TypeDescriptor getTypeDescriptor() {
// we derive our component from "Form" component
return new TypeDescriptor (FormCD.TYPEID, TYPEID, true, true);
}
public VersionDescriptor getVersionDescriptor() {
return null; // not used for now
}
public List<PropertyDescriptor> getDeclaredPropertyDescriptors() {
return null;
}
protected List createPresenters() {
return null;
}
}
Now you have to register this component descriptor. This is done in layers.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE filesystem PUBLIC "-//NetBeans//DTD Filesystem 1.1//EN" "http://www.netbeans.org/dtds/filesystem-1_1.dtd">
<filesystem>
<folder name="vmd-midp">
<folder name="components">
<file name="org-netbeans-vmd-mycustomcomponents-MyFormCD.instance"/>
</folder>
</folder>
</filesystem>
The component descriptor is not enough for describing how to create a component. Therefore there are ComponentProducers. They are describing how the component is created. It allows component templates ... For our demo, it is enough to understand that it is the thing that is listed in the palette on the right side of the IDE.
package org.netbeans.vmd.mycustomcomponents;
import org.netbeans.modules.vmd.api.model.ComponentProducer;
import org.netbeans.modules.vmd.api.model.DesignComponent;
import org.netbeans.modules.vmd.api.model.DesignDocument;
import org.netbeans.modules.vmd.api.model.PaletteDescriptor;
import org.netbeans.modules.vmd.midp.components.displayables.FormCD;
import org.netbeans.modules.vmd.midp.palette.MidpPaletteProvider;
public class MyFormProducer extends ComponentProducer {
public MyFormProducer() {
// producer unique identification, typeid of a component created by this producer and palette descriptor
super(MyFormCD.TYPEID.toString(), MyFormCD.TYPEID, new PaletteDescriptor (MidpPaletteProvider.CATEGORY_DISPLAYABLES, "My Form", "This is my first component", FormCD.ICON_PATH, FormCD.ICON_LARGE_PATH)); // NOI18N
}
public Result createComponent(DesignDocument document) {
// this is called to create a component that this producer represents
DesignComponent createComponent = document.createComponent(MyFormCD.TYPEID);
return new Result (createComponent);
}
public boolean checkValidity(DesignDocument document) {
return true;
// later, you can use:
// return MidpJavaSupport.checkValidity(document, "mylibrary.MyForm"); // NOI18N
// check whether your class is on the classpath of your project
}
}
Now you have to register the producer in Designer. It is done in layer again. Therefore:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE filesystem PUBLIC "-//NetBeans//DTD Filesystem 1.1//EN" "http://www.netbeans.org/dtds/filesystem-1_1.dtd">
<filesystem>
<folder name="vmd-midp">
<folder name="components">
<file name="org-netbeans-vmd-mycustomcomponents-MyFormCD.instance"/>
</folder>
<folder name="producers">
<file name="org-netbeans-vmd-mycustomcomponents-MyFormProducer.instance"/>
</folder>
</folder>
</filesystem>
Now we are going to run it for the first time.
Since we had inherit all properties and presenters (explained later) from FormCD, then it behaves as the same way as a "Form" (except for code generation since it does not have specification of a constructor of "mylibrary.MyForm" class.
For having properties, you have to do two major things. Register a property for in the model and register a view of that property in the "Properties" window. For that you have to:
public List<PropertyDescriptor> getDeclaredPropertyDescriptors() {
return Arrays.asList (
// create a property description
// it is a property with name "myValue" which is of-type primitive int (see: MidpTypes class for more)
// and it default value is "5"
new PropertyDescriptor ("myValue", MidpTypes.TYPEID_INT, MidpTypes.createIntegerValue(5), false, true, MidpVersionable.MIDP)
);
}
protected List createPresenters() {
return Arrays.asList (
// create a properties presenter
new DefaultPropertiesPresenter()
// in the general properties category
.addPropertiesCategory(PropertiesCategories.CATEGORY_PROPERTIES)
// add property myValue
.addProperty("My Value", PropertyEditorNumber.createIntegerInstance(), PROP_MY_VALUE)
);
}
new CodeSetterPresenter()
// we will use "myValue" property as a parameter
.addParameters(MidpParameter.create(PROP_MY_VALUE))
// define a default contructor of the "MyForm" class
.addSetters(MidpSetter.createConstructor(TYPEID, MidpVersionable.MIDP).addParameters())
// define "setMyValue" setter
.addSetters (MidpSetter.createSetter("setMyValue", MidpVersionable.MIDP_2).addParameters(PROP_MY_VALUE))
You can download mycustomcomponents.zip
which is the source code of the module. Unzip it into "/trunk" directory. Then you can build it and run the IDE.
We have not talk about many things like programatically create/modifying/delete components in a document and how to plug a presenter for screen, flow, navigator, ... views. That will come in next tutorials.
contact: Karol Harezlak
| mycustomcomponents.zip | ![]() |
6901 bytes |