Package com.nexirius.jnex.example

Source Code of com.nexirius.jnex.example.JnexExampleHtmlApplication

package com.nexirius.jnex.example;

import com.nexirius.framework.datamodel.DataModel;
import com.nexirius.framework.htmlview.*;
import com.nexirius.framework.htmlview.application.HTMLApplication;
import com.nexirius.framework.htmlview.function.HTMLFunction;
import com.nexirius.framework.htmlview.function.HTMLTransition;
import com.nexirius.jnex.example.command.*;
import com.nexirius.jnex.example.datamodel.MainModel;
import com.nexirius.jnex.example.datamodel.PersonModel;
import com.nexirius.util.XFile;

import java.io.File;

public class JnexExampleHtmlApplication extends HTMLApplication {
    public void preInit() {
    }

    public String getApplicationName() {
        return "JnexExampleHtmlApplication";
    }

    /**
     * Method that returns the main model in its initial state
     */
    public DataModel createHomeModel() {
        MainModel mainModel;

        mainModel = new MainModel();

        XFile textFile = getTextFile();

        if (textFile.exists()) {
            try {
                mainModel.dropData(new String(textFile.getBytes()));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return mainModel;
    }

    public static XFile getTextFile() {
        File dir = new File(File.separator, "tmp");
        dir.mkdirs();
        XFile file = new XFile(dir, "jnexHtmlExample.txt");
        return file;
    }

    //Method that defines an array of state-transitions
    public HTMLTransition[] getHTMLTransistions(DataModel homeModel) {

        //Here we define our possible states where the first argument is the name
        //of this state, the second is the corresponding model to render this state,
        //the third is true if the model should be editable in this state , and the fourth
        //argument is the name of the HTML-Template in the Stream-Map to render the model.
        HTMLState overviewState = new HTMLState("overview", homeModel, true, "overview");
        HTMLState newPersonState = new HTMLState("newPerson", new PersonModel(), true, "newPerson");
        HTMLState editPersonState = new HTMLState("editPerson", null, true, "editPerson");

        //Here we return an array of possible transitions between the HTML-States
        //where the first argument is the state we are at, the third argument is the state we
        //go to and the second argument is the name of the event that takes us there.

        //Optionally you can insert an HTMLCommand as third argument, so that the transition is only performed
        //if the execute-method of the command returns true.

        //Events are defined as buttons in the templates : $!button("eventname")
        return new HTMLTransition[]{
            new HTMLTransition(overviewState, "newPerson", newPersonState),
            new HTMLTransition(overviewState, "editPerson", editPersonState),
            new HTMLTransition(overviewState, "deletePerson", new DeletePersonCommand(), overviewState),
            new HTMLTransition(overviewState, "saveAll", new SaveAllCommand(), overviewState),
            new HTMLTransition(newPersonState, "OK", new NewPersonCommand(), overviewState),
            new HTMLTransition(newPersonState, "CANCEL", overviewState),
            new HTMLTransition(editPersonState, "OK", new EditPersonCommand(), overviewState),
            new HTMLTransition(editPersonState, "CANCEL", overviewState),
            new HTMLTransition("toggleLanguage", new ToggleLanguageCommand()),
            new HTMLTransition(overviewState, "sort", new SortPersonArrayCommand(), overviewState),
        };

       //As you could see, an HTML-State, a template and an event can have the same name.

    }
    //Method that maps Template-Names to Template-Files.
    public HTMLStreamMapEntry[] getStreamMapperEntries() {
        return new HTMLStreamMapEntry[]{
            new HTMLStreamMapEntry("header", new XFile("html/header.html")),
            new HTMLStreamMapEntry("footer", new XFile("html/footer.html")),
            new HTMLStreamMapEntry("overview", new XFile("html/overview.html")),
            new HTMLStreamMapEntry("newPerson", new XFile("html/newPerson.html")),
            new HTMLStreamMapEntry("editPerson", new XFile("html/editPerson.html")),
            new HTMLStreamMapEntry("person", new XFile("html/model/person.html")),
            new HTMLStreamMapEntry("personTableRow", new XFile("html/model/personTableRow.html")),
            new HTMLStreamMapEntry("mandatory", new XFile("html/mandatory.html")),
        };
    }

   
    /**
     Method that returns a mapping from datamodel types to templates. This mapping is used when templates call
     translate function to render child models with default templates. There is a default template for all
     default datamodels, this method overrides or extends the default mapping.
     */
    public HTMLTemplateMapEntry[] getTemplateMapEntries() {
        return new HTMLTemplateMapEntry[]{
            new HTMLTemplateMapEntry(PersonModel.class, "person", true),
        };
    }
    /**
     Method to define your own HTML-Template functions ($!function(args))
     */
    public HTMLFunction[] getHTMLFunctions() {
        return new HTMLFunction[]{};
    }
    //Method this is called after the init method
    public void postInit(HTMLSessionVariable sessionVariable) {

    }
    //Method is called when exception occures when rendering or mapping
    public void handleException(HTMLSessionVariable sessionVariable, Exception e) {
        e.printStackTrace();
    }

    protected HTMLVariable[] getHTMLVariables() {
        return new HTMLVariable[] {
          new HTMLVariable("company", "Nexirius GmbH"),
          new HTMLVariable("TH", "TH BGCOLOR='$!color(tableHeader)'"),
        };
    }
}
TOP

Related Classes of com.nexirius.jnex.example.JnexExampleHtmlApplication

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.