JavaMEComponent ExtendingVDComponent

NOTE: Before start reading this document make sure you completed part one: Part Two: Java ME Visual Designer component

Contents


Introduction

This document is a part of Java ME Custom Component Creation tutorial.

We have created Java ME Visual Designer presentation for Java ME component SplashScreenStatus.

But if you look at the source code of SplashScreenStatus class in SplashScreenExtLibrary project, you can see that it has a method setStatusPrefix(String) which allows to set status string prefix, which will be displayed at the left of completed percents number.

In this tutorial we will extend Visual Designer component to add

  • ability to set statusPrefix value using component property sheet.
  • preview component in Screen view of Visual Designer

You may download zipped project created in this tutorial: VMDSplashScreenExt_part3_JavaMEComponent_ExtendingVDComponent.zip

Property Descriptors

To control and store value of "statusPrefix" parameter of setStatusPrefix(String) method we have to declare property inside of the SplashScreenStatusCD using method getDeclaredPropertyDescriptors().

  1. Add constant to store property name String
   public static final String PROP_STATUS_PREFIX = "statusPrefix";
  1. Override getDeclaredPropertyDescriptors() method
   @Override
   public List<PropertyDescriptor> getDeclaredPropertyDescriptors() {
       return Arrays.asList(
               new PropertyDescriptor(PROP_STATUS_PREFIX, // Name of the property
                                      MidpTypes.TYPEID_JAVA_LANG_STRING,   // Type of the property
                                      PropertyValue.createNull(),          // Default value of the property
                                      true,                                // Is property allows null
                                      true,                                // Is property allows user code insted of String value
                                      MidpVersionable.MIDP)                // For what version of MIDP this porperty is valid
               );
   }

Presenters

After Visual Designer component is created, any it's presentation (into source code, in screen view, in flow view etc) is performed through Presenter classes.

List of presenters for this component should be created in protected List<? extends Presenter> createPresenters() method. Returned Presenters will e added or override Presenters derived from Super Component Descriptor (SplashScreenCD in our example).

We will add

  • PropertiesPresenter to have ability to add statusPrefix value using component property sheet.
  • CodeSetterPresenter to generate initialization source code for component
  • DisplayableDisplayPresenter to preview status string in Screen view.

PropertiesPresenter

Add the following method that creates PropertiesPresenter for statusPrefix property.

    private static DefaultPropertiesPresenter createPropertiesPresenter () {
        return new DefaultPropertiesPresenter(DesignEventFilterResolver.THIS_COMPONENT)
            .addPropertiesCategory(MidpPropertiesCategories.CATEGORY_PROPERTIES)
                .addProperty(NbBundle.getMessage(SplashScreenStatusCD.class, "DISP_SplashScreenStatus_status"), // NOI18N
                    PropertyEditorString.createInstance(NbBundle.getMessage(SplashScreenStatusCD.class,
                        "LBL_SplashScreenStatus_status")), PROP_STATUS_PREFIX); // NOI18N
    }    

and add it's invocation into createPresenters() method

    @Override
    protected List<? extends Presenter> createPresenters() {
        return Arrays.asList (
            MidpCodePresenterSupport.createAddImportPresenter(),
            createPropertiesPresenter()
        );
    }

You can see that localozed string values are loaded from localization properies file. Create Bundle.properties file in the same package as SplashScreenCD. And add the following lines to it:

DISP_SplashScreenStatus_status=Status Prefix
LBL_SplashScreenStatus_status=&Status Prefix:

CodeSetterPresenter

In our example we need to care about generation of source code for

  • component object creation by invoking constructor
  • setter for statusPrefix property.
  • new parameters for setters

Source code for properties defined in super Component Descriptors will be generated by derived CodeSetterPresenters.

Source code to support listed code generation cases is the following:

    private Presenter createSetterPresenter () {
        return new CodeSetterPresenter ()
            .addParameters (MidpParameter.create (PROP_STATUS_PREFIX))
            .addSetters (MidpSetter.createConstructor (TYPEID, MidpVersionable.MIDP_2)
                    .addParameters (MidpCustomCodePresenterSupport.PARAM_DISPLAY))
            .addSetters (MidpSetter.createSetter ("setStatusPrefix", MidpVersionable.MIDP_2)
                    .addParameters (PROP_STATUS_PREFIX));
    }

And add createSetterPresenter () invocation to createPresenters() method.

DisplayableDisplayPresenter

We will extend DisplayableDisplayPresenter from super component descriptor to add one more line with status string preview.

First we have to add VMD Screen Designer lirary to module dependencies.
Image:ext_dependency_JavaMEComponent_ExtendingVDComponent.jpg

In our example SplashScreenStatusCD derived from SplashScreenCD which in it's turn derived from AbstractInfoScreenCD.
AbstractInfoScreenCD provides AbstractInfoDisplayPresenter which we should inherit.

In our VMDSplashScreenExt project create new package org.netbeans.custom.screen.display.

Create new Class SplashScreenStatusDisplayPresenter that extends AbstractInfoDisplayPresenter.

In order to add status text preview to the panel we will add the following code:

In constructor we will create new label and add it to the panel

    public SplashScreenStatusDisplayPresenter() {
        statusLabel = new JLabel();
        statusLabel.setHorizontalAlignment(JLabel.CENTER);
        JPanel contentPanel = getPanel().getContentPanel();

        GridBagConstraints constraints = new GridBagConstraints();
        constraints.weightx = 1.0;
        constraints.weighty = 0;
        constraints.insets = new Insets(2, 2, 2, 2);
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.gridx = GridBagConstraints.REMAINDER;
        constraints.gridy = 1;
        constraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        constraints.anchor = GridBagConstraints.SOUTH;

        contentPanel.add(statusLabel, constraints);
    }


Then we will override reload(ScreenDeviceInfo) method to update Status String preview value.

    @Override
    public void reload(ScreenDeviceInfo deviceInfo) {
        super.reload(deviceInfo);

        PropertyValue value = getComponent().readProperty(SplashScreenStatusCD.PROP_STATUS_PREFIX);
        if (!PropertyValue.Kind.USERCODE.equals(value.getKind())) {
            String text = MidpTypes.getString(getComponent().readProperty(SplashScreenStatusCD.PROP_STATUS_PREFIX));
            if (text == null) {
                setStatusText(NbBundle.getMessage(SplashScreenStatusDisplayPresenter.class, "DISP_status_not_specified")); // NOI18N
            } else if (text.length() == 0) {
                setStatusText(NbBundle.getMessage(SplashScreenStatusDisplayPresenter.class, "DISP_status_is_empty")); // NOI18N
            } else {
                setStatusText(text);
                value = getComponent().readProperty(AbstractInfoScreenCD.PROP_TEXT_FONT);
                if (!PropertyValue.Kind.USERCODE.equals(value.getKind())) {
                    DesignComponent font = value.getComponent();
                    statusLabel.setFont(ScreenSupport.getFont(deviceInfo, font));
                }
            }
        } else {
            setStatusText(NbBundle.getMessage(SplashScreenStatusDisplayPresenter.class, "DISP_status_is_usercode")); // NOI18N
        }
    }

    /** sets status string preview text. Default percents are set to 100 */
    private void setStatusText(String text){
        statusLabel.setText(text + "100"); // NOI18N
    }

And then we will override getPropertyDescriptors() to add new ScreenPropertyDescriptor for PROP_STATUS_PREFIX.

    @Override
    public Collection<ScreenPropertyDescriptor> getPropertyDescriptors() {
        ArrayList<ScreenPropertyDescriptor> descriptors = new ArrayList<ScreenPropertyDescriptor>(super.getPropertyDescriptors());
        descriptors.add(new ScreenPropertyDescriptor(getComponent(), statusLabel, 
                new ScreenStringPropertyEditor(SplashScreenStatusCD.PROP_STATUS_PREFIX)));
        return descriptors;
    }

And the last action is to add SplashScreenStatusDisplayPresenter to SplashScreenStatusCD#createPresenters()

    @Override
    protected List<? extends Presenter> createPresenters() {
        return Arrays.asList (
            MidpCodePresenterSupport.createAddImportPresenter(),
            createSetterPresenter(),
            createPropertiesPresenter(),
            new SplashScreenStatusDisplayPresenter()
        );
    }

To make new ScreenDisplayPresenter availale, we should remove implementations inherited from super component descriptors.

    @Override
    protected void gatherPresenters (ArrayList<Presenter> presenters) {
        DocumentSupport.removePresentersOfClass (presenters, ScreenDisplayPresenter.class);
        super.gatherPresenters (presenters);
    }

You can see that localozed string values are loaded from localization properies file. Create Bundle.properties file in the same package as SplashScreenStatusDisplayPresenter. And add the following lines to it:

DISP_status_not_specified=<Prefix not specified>
DISP_status_is_empty=<Prefix is empty>
DISP_status_is_usercode=<Prefix is usercode>

After module reinstall you can see similar picture in the screen view:
Image:ext_screen_JavaMEComponent_ExtendingVDComponent.jpg



Back to Java ME Custom Component Creation tutorial

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo