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

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

There is a newer version: 3.0-alpha-6
Show newest version
/*
 * *##% 
 * JAXX Runtime
 * Copyright (C) 2008 - 2009 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
 * .
 * ##%*
 */
package jaxx.runtime.swing.wizard;

import java.awt.Window;
import javax.swing.ImageIcon;
import jaxx.runtime.JAXXContext;
import jaxx.runtime.context.JAXXInitialContext;
import jaxx.runtime.JAXXObject;
import org.apache.commons.beanutils.ConstructorUtils;

/**
 *
 * 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
 * @since 1.3
 */
public abstract class WizardUILancher, UI extends WizardUI> {

    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) {
    }

    @SuppressWarnings("unchecked")
    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("incoming", model);
        }
        // apply action
        uiContext.add("apply", new Runnable() {

            @Override
            public void run() {
                try {
                    doAction(ui);
                } finally {
                    doClose(ui, false);
                }
            }
        });
        // cancel action
        uiContext.add("cancel", new Runnable() {

            @Override
            public void run() {
                try {
                    doCancel(ui);
                } finally {
                    doClose(ui, true);
                }
            }
        });

        // instanciate ui

        UI newUI = (UI) ConstructorUtils.invokeConstructor(uiClass, new Object[]{mainUI, uiContext}, new Class[]{Window.class, JAXXContext.class});
        return newUI;
    }

    @SuppressWarnings("unchecked")
    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("incoming", model);
        }
        // apply action
        uiContext.add("apply", new Runnable() {

            @Override
            public void run() {
                try {
                    doAction(ui);
                } finally {
                    doClose(ui, false);
                }
            }
        });
        // cancel action
        uiContext.add("cancel", new Runnable() {

            @Override
            public void run() {
                try {
                    doCancel(ui);
                } finally {
                    doClose(ui, true);
                }
            }
        });

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy