# Creating a Java ME MIDlet Template

In this section, you will be learning how to create a MIDlet Template to be integrated to the MTJ MIDlet Templates Wizard.

## Summary:

### The template creation process is composed of the following tasks:

1. Extend the org.eclipse.mtj.ui.midlettemplate extension point;
2. Implement an instance of a org.eclipse.mtj.ui.templates.AbstractTemplateWizardPage class;
3. Implement the MIDlet template code;
4. Create a folder structure for the template code and resources;

### Extending the org.eclipse.mtj.ui.midlettemplate

- Add an extension of the org.eclipse.mtj.ui.midlettemplate extension point as show bellow:
- Fill in the extension fields, all extension fields have hints to help users to understand the fields usages and if the developer wants more detailed description go to the extension point description.

### Implementing org.eclipse.mtj.ui.templates.AbstractTemplateWizardPage

The AbstractTemplateWizardPage abstract class has several methods for Wizard UI customization and template code generation. The interface is described bellow:

    /**
     * Gets the template dictionary of tags
     * and it's corresponding values according
     * to the page's custom fields.
     *
     * @return template's dictionary.
     */
    public abstract Map<String , String> getDictionary();

    /**
     * Creates the Wizard Template custom page under the given
     * parent composite.
     *
     * @param parent the parent composite.
     */
    public abstract void createControl(Composite parent);

    /**
     * Returns whether this page is complete or not.
     *
     * This information is typically used by the wizard to decide
     * when it is okay to finish.
     *
     *
     * @return true if this page is complete, and
     *  	   false otherwise.
     */
    public boolean isPageComplete();

### Implement the MIDlet template code

Implement the MIDlet code as an usual application and replace the custom fields on the code for tags defined by you. The example bellow uses the $xxxxx$ tag format as an example:

    import javax.microedition.lcdui.Canvas;
    import javax.microedition.lcdui.Font;
    import javax.microedition.lcdui.Graphics;

    public class ExampleCanvas extends Canvas {

    	/* (non-Javadoc)
    	 * @see javax.microedition.lcdui.Canvas#paint(javax.microedition.lcdui.Graphics)
    	 */
    	protected void paint(Graphics g) {
    		g.setColor($bg$);
    		g.fillRect(0x00, 0x00, getWidth(), getHeight());

    		String message = "$message$";
    		Font font = Font.getFont($font-face$, $font-style$, $font-size$);
    		g.setFont(font);

    		g.setColor($fg$);
    		g.drawString(message, getWidth()/2, getHeight()/2, Graphics.HCENTER | Graphics.TOP);
    	}

    }

NOTE: The template tags format is free for the developer to define since the tags will be replaced by the dictionary provided by the ITemplateProvider instance implemented by the template's developer as shown bellow:

    public Map<String , String> getDictionary() {
        Map<String , String> dict = new HashMap<String , String>();

        dict.put("$message$", this.msgText.getText());
        dict.put("$bg$", this.bgColorText.getText());
        dict.put("$fg$", this.fgColorText.getText());

        dict.put("$font-face$", getFontFace());
        dict.put("$font-style$", getFontStyle());
        dict.put("$font-size$", getFontSize());

        return dict;
    }

Exception: The MIDlet class of the template MUST be named $class_name$.template

    // File $class_name$.template
    public class $class_name$ extends MIDlet {

    	public $class_name$() {

    	}
    }

### Creating template folder structure

The templates folder structure MUST follow the format bellow:

- There must be a "templates" folder inside the plugin's project root;
- There must be a folder named after the template's id set on the extension editor;
- There must be a "java" folder inside the folder described above to place all template's source code files;

  ### NOTE: All source files must be renamed to "ClassName".template

- There should be a "resources" folder in case there is any non Java resources, all resources content must be inside the "resources" folder.

  ### NOTE: All folder content will be copied to the MIDlet Project's root folder.

### See example bellow: