RunTimeCustomComponent
Mobility Visual Designer - Part Two: Custom Components Tutorial - "Run Time"
NOTE: Before start reading this document make sure you completed part one: Mobility Visual Designer - Part One: Custom Components Tutorial - "Design Time"
Contents |
Abstract
This documents shows how to create Netbeans Module Project for Mobility Custom Components. Describes basics of Visual Designer Components architecture. It also contains useful references to the documents about Visual Designer architecture. Result of this tutorial is fully functional Visual Designer and Java ME component based on the Java Me Form.java class.
Introduction to both parts of this tutorial: Custom Component Tutorial
You may want to look at the final result of this tutorial which is available here:
CustomComponentTutorial_RunTimeCustomComponent.zip
NOTE: To run project from zip file you have to change Netbeans Platform to one registered in your Netbeans IDE.
Netbeans Java Me Mobility Project.
If you not familiar with Netbeans Mobility look at this Document before you start: NetBeans 6.0 CLDC/MIDP Development Quick Start Guide
First step is to create Netbeans Mobility Project and name it MyCustomComponentJavaME (do not create HelloMidlet). Now we'll create package org.netbeans.mycustomcomponentjavame and new Java class MyCustomComponent.java.
Mobility Java ME project
As we know form previous tutorial our component is based on javax.microedition.lcdui.Form so we have to extend our MyCustomComponent.java with Form class and add one public method setWarning(String warning) as is shown below:
package org.netbeans.mycustomcomponentjavame;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
public class MyCustomComponent extends Form {
private StringItem stringItem;
public MyCustomComponent() {
super("MyCustomComponent");
stringItem = new StringItem("", "");
append(stringItem);
}
public void setWarning(String warning) {
stringItem.setText(warning);
}
}
As you can see we add additional functionality to our component which shows "warning" on the form using setWarning(String warning). This component functionality will be represents as Warning property in the PropertyWindow. Functionality is very simple and our component in action should look like this:
MyCustomComponent in the WTK 2.5.2
Now we have to build our project by using Clean and Build Action form pop up menu.
Creating Netbeans libraries
If you not familiar with Netbeans Module Development look at this Document before you start: Introduction to NetBeans Module Development
Now we have to open MyCustomComponentSuite crated in the previous tutorial and add new Netbeans Module to the Suite. Let's name it MyCustomComponentLibrary and change Code Name Base to the org.netbeans.mycustomcomponentlibrary. Next step we going to take is to create Netbeans library based on MyCustomComponentJavaME. To do so right click newly crated project MyCustomComponentLibrary and choose New then Other in the pop up menu. In the Module Development categorie choose Java SE Library Descriptor. Now lets create new Library which will be available under list of global Netbeans libraries. On the displayed Java SE Library Descriptor wizard click on the Manage Libraries button then on the New Library button. In the New Library dialog provide name of the Library: MyCustomComponentLibrary, and click OK.
Library dialog
On the right side of the available libraries new MyCostomCOmponentLibrary should appeared. Now we have to assign JAR file from MyCustomComponentJavaME project to our new library. To do so click on the Add JAR/Folder button, find MyCustomComponentJavaME folder and choose JAR from the dist folder of this project like is shown below:
Click Library manager by clicking OK. In next step choose MyCustomComponentLibrary from the list of the available libraries and click Next in the Java SE Library Descriptor wizard. On the next screen you should see predefined parameters of our library like Library Name, Display Name and Project as is shown:
Library dialog
Click Finish to close Java SE Library Descriptor and wait until all new files are created.
Now our Project Window should look like this:
Now we are ready to run MyCustomComponentSuite and check how our Custom Component behave in the Visual Designer. To do so create new Mobility Project add Java ME platform if it's necessary and in the HelloMidlet switch to the Flow view. You should see MyCustomComponent in the Mobility Palette.
//TODO Flow Designer
Bofre we run our project let's look at the class MyCustomComponentProducer in the MyCustomComponent project. In the method createMainComponent(DesignDocument document) we can find line which requaries additional explanation.
MidpProjectSupport.addLibraryToProject(document, "MyCustomComponentLibrary");
Static method MidpProjectSupport.addLibraryToProject is used to add available libraries to the Mobility project, search for the library is done based on the name of the library. In this case MyCustomComponentLibrary is added to the Mobility project when MyCustomComponent component is create it and add it to the document for the first time.
Whole listing of the createMainComponent(DesignDocument document) method from MyCustomCoponentProducer class:
@Override
protected DesignComponent createMainComponent(DesignDocument document) {
super.createMainComponent(document);
MidpProjectSupport.addLibraryToProject(document, "MyCustomComponentLibrary"); // Library is added to the Mobility Project Resources when component is created
return super.createMainComponent(document);
}
Right now reach state when MyCustomComponent is visible in the Mobility Visual Designer Palette, it is possible to edit it using Screen Editor and D&D component form Mobility Palette. At this point project is not compilable because source code missing imports with MyCustomComponent class. To fix our example project we can go to the Source view and choose Fix Imports form Netbeans Source menu then try to run it.
In the next document we'll learn how add PropertiesPresenter and how to use, maintain and create properties in Property Window also we'll talk about Presenters responsible for code generation.
contact: Karol Harezlak
Back to MobilityDesignerHome
