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

fr.ird.observe.client.tree.navigation.nodes.NavigationTreeNodeSupport Maven / Gradle / Ivy

package fr.ird.observe.client.tree.navigation.nodes;

/*-
 * #%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%
 */

import fr.ird.observe.client.ObserveKeyStrokes;
import fr.ird.observe.client.action.UIActionSupport;
import fr.ird.observe.client.form.FormUI;
import fr.ird.observe.client.tree.ObserveTreeNodeSupport;
import fr.ird.observe.dto.data.DataDto;
import fr.ird.observe.dto.reference.DataDtoReference;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.JMenuItem;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuiton.jaxx.runtime.JAXXObject;

/**
 * Created on 14/11/16.
 *
 * @author Tony Chemit - [email protected]
 * @since 6.0
 */
public abstract class NavigationTreeNodeSupport extends ObserveTreeNodeSupport {

    /** Logger */
    private static final Log log = LogFactory.getLog(NavigationTreeNodeSupport.class);
    private static final long serialVersionUID = 1L;

    private boolean loaded;

    public abstract Class getDataType();

    public abstract void reload();

    public abstract Class getContentClass();

    public void updateNode() {
        loaded = false;
        reload();
        if (withChildrenToLoad()) {
            populateChilds();
        }
        loaded = true;
    }

    private boolean withChildrenToLoad() {
        return WithChildsToReload.class.isAssignableFrom(getClass());
    }

    public boolean isPersisted() {
        return getId() != null;
    }

    @Override
    public RootNavigationTreeNode getRoot() {
        return (RootNavigationTreeNode) super.getRoot();
    }

    @Override
    public NavigationTreeNodeSupport getParent() {
        return (NavigationTreeNodeSupport) super.getParent();
    }

    protected NavigationTreeNodeSupport(O data, boolean allowChildren) {
        super(data, allowChildren);
    }

    //FIXME Should reload then node ?
    public void setData(O data) {
        setUserObject(data);
    }

    public final boolean populateChilds() {
        if (isPersisted() && getAllowsChildren() && !isLoaded()) {
            if (isPersisted() && withChildrenToLoad()) {
                removeAllChildren();
                ((WithChildsToReload) this).reloadChilds();
            }
            loaded = true;
            return true;
        }
        return false;
    }

    public boolean isLoaded() {
        return loaded;
    }

    public void setLoaded(boolean loaded) {
        this.loaded = loaded;
    }

    public NavigationTreeNodeSupport findChildById(String id) {
        if (id == null) {

            // id null ? donc rien a faire
            return null;
        }
        if (id.equals(getId())) {

            // on a trouve le bon noeud
            return this;
        }

        if (!isLoaded()) {

            // il faut charger les fils du noeud pour effectuer la recherche
            populateChilds();
        }

        if (isLeaf()) {

            // au final le noeud est une feuille, donc ne convient pas
            return null;
        }
        Enumeration children = children();
        while (children.hasMoreElements()) {
            NavigationTreeNodeSupport node = children.nextElement();
            if (id.equals(node.getId()) || (node.getUserObject() instanceof String && id.equals(node.getUserObject()))) {
                return node;
            }
        }

        return null;
    }

    public NavigationTreeNodeSupport findChildByType(Class childType) {

        if (childType.isAssignableFrom(getClass())) {

            // on a trouve le bon noeud
            return this;
        }

        if (!isLoaded()) {

            // il faut charger les fils du noeud pour effectuer la recherche
            populateChilds();
        }

        if (isLeaf()) {

            // au final le noeud est une feuille, donc ne convient pas
            return null;
        }
        Enumeration children = children();
        while (children.hasMoreElements()) {
            NavigationTreeNodeSupport node = children.nextElement();
            if (childType.isAssignableFrom(node.getClass())) {
                return node;
            }
        }

        return null;
    }

    public NavigationTreeNodeSupport findChildByClass(Class childType) {

        if (!isLoaded()) {

            // il faut charger les fils du noeud pour effectuer la recherche
            populateChilds();
        }

        if (isLeaf()) {

            // au final le noeud est une feuille, donc ne convient pas
            return null;
        }
        Enumeration children = children();
        while (children.hasMoreElements()) {
            NavigationTreeNodeSupport node = children.nextElement();
            if (childType.isAssignableFrom((Class) node.getUserObject())) {
                return node;
            }
        }

        return null;
    }

    @Override
    public boolean isLeaf() {
        return isLoaded() && super.isLeaf();
    }

    public boolean isOpen() {
        return false;
    }

    @Override
    public Color getColor() {
        return isOpen() ? Color.BLACK : Color.GRAY;
    }

    public JMenuItem toMenuItem(UIActionSupport action, JAXXObject ui) {

        JMenuItem mi = new JMenuItem();
        AbstractAction a = new AbstractAction() {

            private static final long serialVersionUID = 5992047515938551634L;

            @Override
            public void actionPerformed(ActionEvent e) {
                if (log.isInfoEnabled()) {
                    log.info("Select action from menuItem: " + e.getSource());
                }
                //FIXME!!! Voir si ça fonctionne tjs
//                action.actionPerformed(mi);
                action.actionPerformed(e);
            }
        };
        mi.setAction(a);
        mi.setText(getText().trim());
        ObserveKeyStrokes.addKeyStroke(mi, ObserveKeyStrokes.KEY_STROKE_PRESSED_ENTER);
        mi.setIcon(getIcon("-16"));
        mi.setForeground(getColor());
        mi.setBackground(Color.WHITE);
        mi.putClientProperty("node", this);
        mi.putClientProperty("ui", ui);
        return mi;
    }

    public > List toChildList() {
        List data = new LinkedList<>();
        Enumeration children = children();
        while (children.hasMoreElements()) {
            NavigationTreeNodeSupport o = (NavigationTreeNodeSupport) children.nextElement();
            data.add((R) o.getData());
        }
        return data;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy