Skip to content

define category

Steve Hannah edited this page Jul 13, 2021 · 1 revision

<define-category>

Synopsis

Defines an action category that can be used in the tags like <bind-action> and <buttons> to render controller-supplied actions as buttons and menu items inside the view. Once defined, the action category can be referenced by controllers to define actions on it so that that view is able to look them up on demand.

Usage

Place zero or more <define-category> tags as child tags of the root view tag. Each tag will result in a public static final variable of type ActionNode.Category to be added to resulting the view class.

<define-category name="CATEGORY_NAME" value="CATEGORY_ALIAS"/>

Where CATEGORY_NAME is the name to assign to the ActionNode.Category static variable, and CATEGORY_ALIAS is an optional attribute specifying an exising ActionNode.Category that this category should be an alias of.

Attributes

name

The name of the action category. This will be the name of the public static final variable for this category in the generated view.

value

Optional. A java expression resolving to an ActionNode.Category. This is often used to make this new category an alias of an existing category.

Examples

The following view defines the SAMPLE_ACTION category using the <define-category> tag. It then binds a button to this category using the <bind-action> tag.

<?xml version="1.0"?>
<y view-controller="com.codename1.rad.sampler.controllers.SampleViewController"
        xsi:noNamespaceSchemaLocation="CustomViewControllerSample.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <define-category name="SAMPLE_ACTION"/>
    <define-tag name="name" value="Person.name"/>

    <label>Enter name</label>
    <radTextField tag="name"/>
    <button>
        <bind-action category="SAMPLE_ACTION"/>
    </button>
</y>

Notice that this view also defined custom view controller using the view-controller attribute. This view controller registers an action in the SAMPLE_ACTION category so that it will be notified when the user clicks the button.

The source of this controller is:

package com.codename1.rad.sampler.controllers;

import com.codename1.rad.controllers.Controller;
import com.codename1.rad.controllers.ViewController;
import com.codename1.rad.nodes.ActionNode;
import com.codename1.rad.sampler.CustomViewControllerSample;
import com.codename1.rad.schemas.Person;
import com.codename1.ui.Dialog;

public class SampleViewController extends ViewController {
    /**
     * Creates a new ViewController with the given parent controller.
     *
     * @param parent
     */
    public SampleViewController(Controller parent) {
        super(parent);
    }


    @Override
    protected void initControllerActions() {
        super.initControllerActions();
        ActionNode.builder()
                .label("Submit")
                .addToController(this, CustomViewControllerSample.SAMPLE_ACTION, evt -> {
                    evt.consume();
                    Dialog.show("Received in "+getClass().getName(), "Name is "+evt.getEntity().getText(Person.name), "OK", null);
                });
    }
}
Clone this wiki locally