All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nuiton.jaxx.runtime.swing.wizard.WizardUILancher Maven / Gradle / Ivy

There is a newer version: 3.0-alpha-6
Show newest version
/*
 * #%L
 * JAXX :: Runtime Swing Wizard
 * %%
 * Copyright (C) 2008 - 2017 CodeLutin
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * .
 * #L%
 */
package org.nuiton.jaxx.runtime.swing.wizard;

import org.nuiton.jaxx.runtime.JAXXContext;
import org.nuiton.jaxx.runtime.JAXXObject;
import org.nuiton.jaxx.runtime.JAXXUtil;
import org.nuiton.jaxx.runtime.context.JAXXContextEntryDef;
import org.nuiton.jaxx.runtime.context.JAXXInitialContext;

import javax.swing.ImageIcon;
import java.awt.Window;

/**
 * Une classe pour lancer une ui de wizard.
 *
 * @param   le type des etapes
 * @param   le type de modele
 * @param  le type d'ui
 * @author Tony Chemit - [email protected]
 * @since 1.3
 */
public abstract class WizardUILancher, UI extends WizardUI> {

    /** the jaxx context entry to store the apply action */
    public static final JAXXContextEntryDef APPLY_DEF =
            JAXXUtil.newContextEntryDef("apply", Runnable.class);

    /** the jaxx context entry to store the cancel action */
    public static final JAXXContextEntryDef CANCEL_DEF =
            JAXXUtil.newContextEntryDef("cancel", Runnable.class);

    public static > JAXXContextEntryDef newModelEntry(Class modelType) {
        return JAXXUtil.newContextEntryDef("incoming", modelType);
    }

    protected UI ui;

    public WizardUILancher(JAXXContext context,
                           Class uiClass,
                           Class modelClass,
                           String title,
                           String tip,
                           ImageIcon icon) {
        this(context, uiClass, modelClass, null, title, tip, icon);
    }

    public WizardUILancher(JAXXContext context,
                           Class uiClass,
                           Class modelClass,
                           M model,
                           String title,
                           String tip,
                           ImageIcon icon) {
        try {
            ui = createUI(context,
                          uiClass,
                          modelClass,
                          model,
                          title,
                          tip,
                          icon
            );
        } catch (Exception ex) {
            throw new RuntimeException(
                    "could not instanciate launcher for reason " +
                            ex.getMessage(), ex);
        }
    }

    public WizardUILancher(JAXXContext context,
                           Window mainUI,
                           Class uiClass,
                           Class modelClass,
                           M model) {
        try {
            ui = createUI(context,
                          mainUI,
                          uiClass,
                          modelClass,
                          model
            );
        } catch (Exception ex) {
            throw new RuntimeException(
                    "could not instanciate launcher for reason " +
                            ex.getMessage(), ex);
        }
    }

    public WizardUILancher(JAXXContext context,
                           Window mainUI,
                           Class uiClass,
                           Class modelClass) {
        this(context, mainUI, uiClass, modelClass, null);
    }

    public void start() {
        init(ui);
        start(ui);
    }

    protected void start(UI ui) {
        ui.start();
    }

    public  T getContextValue(Class clazz, String name) {
        if (ui == null) {
            throw new NullPointerException("ui can not be null");
        }
        if (!(ui instanceof JAXXObject)) {
            throw new ClassCastException("ui can not be casted to JAXXObject ");
        }

        return ((JAXXObject) ui).getContextValue(clazz, name);
    }

    public  T getContextValue(Class clazz) {
        return getContextValue(clazz, null);
    }

    protected void init(UI ui) {
    }

    protected void doAction(UI ui) {
    }

    protected void doCancel(UI ui) {
    }

    protected void doClose(UI ui, boolean wasCanceld) {
    }

    protected UI createUI(JAXXContext context,
                          Window mainUI,
                          Class uiClass,
                          Class modelClass,
                          M model) throws Exception {
        JAXXInitialContext uiContext = new JAXXInitialContext();
        uiContext.add(mainUI == null ? context : mainUI);
        // parent context model
        uiContext.add(modelClass.newInstance());
        if (model != null) {
            uiContext.add(newModelEntry(modelClass), model);
        }
        // apply action
        uiContext.add(APPLY_DEF, () -> {
            try {
                doAction(ui);
            } finally {
                doClose(ui, false);
            }
        });
        // cancel action
        uiContext.add(CANCEL_DEF, () -> {
            try {
                doCancel(ui);
            } finally {
                doClose(ui, true);
            }
        });

        // instanciate ui

        return JAXXUtil.invokeConstructor(
                uiClass,
                new Class[]{Window.class, JAXXContext.class},
                mainUI, uiContext
        );
    }

    protected UI createUI(JAXXContext context,
                          Class uiClass,
                          Class modelClass,
                          M model,
                          String title,
                          String tip,
                          ImageIcon icon) throws Exception {
        JAXXInitialContext uiContext = new JAXXInitialContext();
        uiContext.add(context);
        // parent context model
        uiContext.add(modelClass.newInstance());
        if (model != null) {
            uiContext.add(newModelEntry(modelClass), model);
        }
        // apply action
        uiContext.add(APPLY_DEF, () -> {
            try {
                doAction(ui);
            } finally {
                doClose(ui, false);
            }
        });

        // cancel action
        uiContext.add(CANCEL_DEF, () -> {
            try {
                doCancel(ui);
            } finally {
                doClose(ui, true);
            }
        });

        // instanciate ui
        return JAXXUtil.invokeConstructor(
                uiClass,
                new Class[]{
                        JAXXContext.class,
                        String.class,
                        String.class,
                        ImageIcon.class
                },
                uiContext, title, tip, icon
        );
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy