Search in sources :

Example 1 with AbstractEntityView

use of com.codename1.rad.ui.AbstractEntityView in project CodenameOne by codenameone.

the class RADStatusViewSample method start.

public void start() {
    if (current != null) {
        current.show();
        return;
    }
    Form hi = new Form("Hi World", BoxLayout.y());
    Tag TAG_ONLINE = new Tag("online");
    class User extends Entity {
    }
    entityTypeBuilder(User.class).Boolean(TAG_ONLINE).string(Thing.name).factory(cls -> {
        return new User();
    }).build();
    /**
     * A view that displays the status (online/offline) of an entity using
     * the TAG_ONLINE tag.
     */
    class StatusView extends AbstractEntityView {

        // ViewNode.  Not used;
        ViewNode node;

        // Flag to indicate the current state online/offline of the view
        private boolean online;

        // Label used
        private Label label = new Label();

        /**
         * Creates a new StatusView for user model.
         */
        StatusView(User user) {
            super(user);
            this.node = new ViewNode(new Attribute[] {});
            setLayout(new BorderLayout());
            $(this).setMargin(0).setPadding(0);
            this.add(CENTER, label);
            update();
        }

        @Override
        public void update() {
            // Check to see if the model is online.
            boolean modelOnline = !getEntity().isFalsey(TAG_ONLINE);
            if (modelOnline != online) {
                // Model state has changed since last update
                online = modelOnline;
                if (online) {
                    label.setText("Online");
                    label.setUIID("OnlineLabel");
                    FontImage.setMaterialIcon(label, FontImage.MATERIAL_CONNECTED_TV);
                } else {
                    label.setText("Offline");
                    label.setUIID("OfflineLabel");
                    FontImage.setMaterialIcon(label, FontImage.MATERIAL_OFFLINE_BOLT);
                }
                Form f = getComponentForm();
                if (f != null) {
                    // Changing the text in this case may change the layout size
                    // of the status view here, so it is best to just issue a revalidate
                    // for the whole form.  If the status change didn't change
                    // the layout size, then we could have just skipped this step.
                    f.revalidateWithAnimationSafety();
                }
            }
        }

        @Override
        public void commit() {
        // Don't need to implement commit() because this view doesn't
        // "update" the model - it only shows model stats.
        }

        @Override
        public Node getViewNode() {
            return node;
        }
    }
    // Create a new user
    User user = new User();
    // Create a status view for the user
    StatusView statusView = new StatusView(user);
    // Add a UI switch to toggle user state between online and offline
    Switch onlineSwitch = new Switch("Online");
    onlineSwitch.addChangeListener(e -> {
        // Set the user TAG_ONLINE status.  This will trigger a property
        // change in the model and update the view.
        user.set(TAG_ONLINE, onlineSwitch.isOn());
    });
    hi.add(onlineSwitch);
    hi.add(statusView);
    hi.show();
}
Also used : Switch(com.codename1.components.Switch) ViewNode(com.codename1.rad.nodes.ViewNode) Toolbar(com.codename1.ui.Toolbar) BoxLayout(com.codename1.ui.layouts.BoxLayout) Resources(com.codename1.ui.util.Resources) AbstractEntityView(com.codename1.rad.ui.AbstractEntityView) EntityTypeBuilder.entityTypeBuilder(com.codename1.rad.models.EntityTypeBuilder.entityTypeBuilder) Form(com.codename1.ui.Form) Log(com.codename1.io.Log) BorderLayout(com.codename1.ui.layouts.BorderLayout) UIManager(com.codename1.ui.plaf.UIManager) ComponentSelector.$(com.codename1.ui.ComponentSelector.$) Dialog(com.codename1.ui.Dialog) FontImage(com.codename1.ui.FontImage) Label(com.codename1.ui.Label) Node(com.codename1.rad.nodes.Node) CN(com.codename1.ui.CN) Thing(com.codename1.rad.schemas.Thing) Attribute(com.codename1.rad.models.Attribute) Entity(com.codename1.rad.models.Entity) Tag(com.codename1.rad.models.Tag) Entity(com.codename1.rad.models.Entity) Form(com.codename1.ui.Form) Attribute(com.codename1.rad.models.Attribute) AbstractEntityView(com.codename1.rad.ui.AbstractEntityView) Label(com.codename1.ui.Label) BorderLayout(com.codename1.ui.layouts.BorderLayout) Switch(com.codename1.components.Switch) ViewNode(com.codename1.rad.nodes.ViewNode) Tag(com.codename1.rad.models.Tag)

Aggregations

Switch (com.codename1.components.Switch)1 Log (com.codename1.io.Log)1 Attribute (com.codename1.rad.models.Attribute)1 Entity (com.codename1.rad.models.Entity)1 EntityTypeBuilder.entityTypeBuilder (com.codename1.rad.models.EntityTypeBuilder.entityTypeBuilder)1 Tag (com.codename1.rad.models.Tag)1 Node (com.codename1.rad.nodes.Node)1 ViewNode (com.codename1.rad.nodes.ViewNode)1 Thing (com.codename1.rad.schemas.Thing)1 AbstractEntityView (com.codename1.rad.ui.AbstractEntityView)1 CN (com.codename1.ui.CN)1 ComponentSelector.$ (com.codename1.ui.ComponentSelector.$)1 Dialog (com.codename1.ui.Dialog)1 FontImage (com.codename1.ui.FontImage)1 Form (com.codename1.ui.Form)1 Label (com.codename1.ui.Label)1 Toolbar (com.codename1.ui.Toolbar)1 BorderLayout (com.codename1.ui.layouts.BorderLayout)1 BoxLayout (com.codename1.ui.layouts.BoxLayout)1 UIManager (com.codename1.ui.plaf.UIManager)1