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

fr.ird.observe.client.form.FormUIManager Maven / Gradle / Ivy

/*
 * #%L
 * ObServe Toolkit :: Common Client
 * %%
 * Copyright (C) 2008 - 2017 IRD, Ultreia.io
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU 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 Public License for more details.
 * 
 * You should have received a copy of the GNU General Public 
 * License along with this program.  If not, see
 * .
 * #L%
 */
package fr.ird.observe.client.form;

import fr.ird.observe.client.MainUI;
import fr.ird.observe.client.util.SwingSessionHelper;
import io.ultreia.java4all.lang.SingletonSupplier;
import java.awt.Component;
import java.lang.reflect.Constructor;
import javax.swing.JPanel;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuiton.jaxx.runtime.JAXXContext;
import org.nuiton.jaxx.runtime.context.JAXXInitialContext;
import org.nuiton.jaxx.runtime.swing.CardLayout2;

/**
 * Manager des ecrans d'editions
 *
 * @author Tony Chemit - [email protected]
 * @since 1.4
 */
public class FormUIManager {

    /** Logger */
    private static final Log log = LogFactory.getLog(FormUIManager.class);

    private final SingletonSupplier swingSessionHelper;
    private final SingletonSupplier mainUI;

    public FormUIManager(SingletonSupplier swingSessionHelper, SingletonSupplier mainUI) {
        this.swingSessionHelper = swingSessionHelper;
        this.mainUI = mainUI;
    }

    @SuppressWarnings("unchecked")
    public  U getContent(Class uiClass) {

        CardLayout2 layout = getContentLayout();
        JPanel layoutContent = getContent();
        String constraints = uiClass.getName();

        if (!layout.contains(constraints)) {

            // pas trouvé
            return null;
        }

        U content = (U) layout.getComponent(layoutContent, constraints);

        if (log.isDebugEnabled()) {
            log.debug("Will use existing content [" + constraints + "] : " + content.getClass().getName());
        }
        return content;
    }

    public  U createContent(Class uiClass) {

        String constraints = uiClass.getName();

        U result;
        try {
            Constructor constructor = uiClass.getConstructor(JAXXContext.class);
            if (log.isDebugEnabled()) {
                log.debug("create new content : " + uiClass);
            }
            result = constructor.newInstance(new JAXXInitialContext().add(getMainUI()));
        } catch (Exception e) {
            throw new IllegalStateException("Could not create content ui " + uiClass, e);
        }

        try {

            // ajout du content dans son parent
            getContent().add(result, constraints);
            if (log.isDebugEnabled()) {
                log.debug("Add new content [" + constraints + "] : " + result.getClass().getName());
            }

            getMainUI().attachFocusOnBody(result);
            getMainUI().attachFocusOnBodyDeep(result);

            return result;
        } catch (Exception e) {
            throw new IllegalStateException("Could not init content ui " + uiClass, e);
        }
    }

    public void openContent(FormUI content) {

        String constraints = content.getClass().getName();

        if (log.isDebugEnabled()) {
            log.debug("Will open ui [" + constraints + "] : " + content.getClass());
        }

        try {

            content.open();

            SwingSessionHelper swingSessionHelper = this.swingSessionHelper.get();
            swingSessionHelper.addComponent(content, true);
            swingSessionHelper.save();

        } catch (Exception e) {
            getMainUI().handlingError(e);
        } finally {
            getContentLayout().show(getContent(), constraints);
        }

    }

    public void close() {
        getContentLayout().reset(getContent());
    }

//    public FormUI getSelectedContentUI() {
//        return getSelectedContentUI(getMainUI());
//    }

//    public boolean closeSelectedContentUI() {
//        return closeSelectedContentUI(getMainUI());
//    }

    /**
     * Essaye de fermer l'écran d'édition s'il existe.
     *
     * @return {@code true} si le contenu a bien été fermé, {@code false} si on
     * ne peut pas fermer l'écran
     * @since 1.5
     */
    public boolean closeSelectedContentUI() {
        FormUI ui = getSelectedContentUI();
        if (ui == null) {
            // no content ui
            return true;
        }
        boolean closed = false;
        try {
            closed = ui.close();
        } catch (Exception e) {
            getMainUI().handlingError(e);
        }
        return closed;
    }

    public void removeSelectedContentUI() {

        FormUI selectedContentUI = getSelectedContentUI();
        if (selectedContentUI != null) {
            getContentLayout().removeLayoutComponent(selectedContentUI, selectedContentUI.getClass().getName());
            getContent().remove(selectedContentUI);
            selectedContentUI.destroy();
        }
    }

//    public void restartEdit() {
//
//        FormUI selectedUI = getSelectedContentUI();
//        if (selectedUI == null) {
//
//            // pas d'écran selectionne
//            return;
//        }
//        FormUIModel model = selectedUI.getModel();
//        if (!model.isEditable()) {
//
//            // modele non editable
//            return;
//        }
//
//        FormUIMode contentMode = model.getMode();
//        if (FormUIMode.UPDATE != contentMode) {
//
//            // ecran non en mode mis a jour
//            return;
//        }
//
//        if (log.isDebugEnabled()) {
//            log.debug("Will restart edit for " + selectedUI.getName());
//        }
//        selectedUI.restartEdit();
//
//    }


    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        close();
    }

    private JPanel getContent() {
        return getMainUI().getContent();
    }

    private CardLayout2 getContentLayout() {
        return getMainUI().getContentLayout();
    }

    private MainUI getMainUI() {
        return mainUI.get();
    }

    public FormUI getSelectedContentUI() {
//
//        if (ui == null) {
//            // no ui, so no modification
//            return null;
//        }

        CardLayout2 layout = getContentLayout();
        JPanel container = getContent();
        Component currentContent = layout.getVisibleComponent(container);
        FormUI result = null;
        if (currentContent != null && currentContent instanceof FormUI) {
            result = (FormUI) currentContent;
        }
        return result;
    }

    public void restartEdit() {
        //FIXME!!!
    }
}