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

org.nuiton.jaxx.runtime.swing.nav.treetable.NavTreeTableHelper Maven / Gradle / Ivy

There is a newer version: 3.0.3
Show newest version
/*
 * #%L
 * JAXX :: Runtime Swing Nav
 * %%
 * Copyright (C) 2008 - 2020 Code Lutin, Ultreia.io
 * %%
 * 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.nav.treetable;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.jdesktop.swingx.JXTreeTable;
import org.nuiton.jaxx.runtime.swing.nav.NavHelper;
import org.nuiton.jaxx.runtime.swing.nav.tree.AbstractNavTreeCellRenderer;

import javax.swing.event.TreeSelectionListener;
import javax.swing.event.TreeWillExpandListener;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import java.util.ArrayList;
import java.util.List;

/**
 * The implementation of {@link NavHelper} based on a {@link JXTreeTable} component.
 *
 * @author Tony Chemit - [email protected]
 * @since 2.1
 */
public class NavTreeTableHelper> extends NavHelper, N> {

    /** Logger */
    static private final Logger log = LogManager.getLogger(NavTreeTableHelper.class);

    public NavTreeTableHelper() {
        super(new NavTreeTableBridge<>());
    }

    @Override
    public void scrollPathToVisible(TreePath path) {
        getUI().scrollPathToVisible(path);
    }

    @Override
    public void setSelectionPath(TreePath path) {
        getUI().getTreeSelectionModel().setSelectionPath(path);
    }

    @Override
    public void addSelectionPath(TreePath path) {
        getUI().getTreeSelectionModel().addSelectionPath(path);
    }

    @Override
    public void addSelectionPaths(TreePath[] paths) {
        getUI().getTreeSelectionModel().addSelectionPaths(paths);
    }

    @Override
    public void removeSelectionPath(TreePath path) {
        getUI().getTreeSelectionModel().removeSelectionPath(path);
    }

    @Override
    public void removeSelectionPaths(TreePath[] paths) {
        getUI().getTreeSelectionModel().removeSelectionPaths(paths);
    }

    @Override
    public TreeSelectionModel getSelectionModel() {
        return getUI().getTreeSelectionModel();
    }

    @Override
    public boolean isExpanded(TreePath pathToExpand) {
        return getUI().isExpanded(pathToExpand);
    }

    @Override
    public void expandPath(TreePath pathToExpand) {
        getUI().expandPath(pathToExpand);
    }

    @Override
    public AbstractNavTreeCellRenderer getTreeCellRenderer() {
        //FIXME Implements it if possible
        return null;
    }

    @SuppressWarnings({"unchecked"})
    @Override
    public N getSelectedNode() {
        TreePath path = getSelectionModel().getSelectionPath();
        N node = null;
        if (path != null) {
            node = (N) path.getLastPathComponent();
        }
        return node;
    }

    @SuppressWarnings({"unchecked"})
    @Override
    public List getSelectedNodes() {
        JXTreeTable tree = getUI();
        if (tree == null) {
            return null;
        }
        TreePath[] paths = tree.getTreeSelectionModel().getSelectionPaths();
        List nodes = new ArrayList<>();
        if (paths != null) {
            for (TreePath path : paths) {
                if (path != null) {
                    nodes.add((N) path.getLastPathComponent());
                }
            }
        }
        return nodes;
    }

    @Override
    public void setUI(JXTreeTable tree,
                      boolean addExpandTreeListener,
                      boolean addOneClickSelectionListener,
                      TreeSelectionListener listener,
                      TreeWillExpandListener willExpandListener) {
        setUI(tree);
        if (willExpandListener != null) {
            tree.addTreeWillExpandListener(willExpandListener);
        }
        if (addExpandTreeListener) {
            tree.addTreeWillExpandListener(expandListener);
        }
        if (listener != null) {
            tree.addTreeSelectionListener(listener);
        }
        if (addOneClickSelectionListener) {
            tree.addTreeSelectionListener(selectionListener);
        }
    }

    @Override
    protected NavTreeTableModel createModel(N node, Object... extraArgs) {

        // must have a single extra params with delegate model
        if (extraArgs.length != 1) {
            throw new IllegalArgumentException("Should have exactly one extra parameter (delegate model)");
        }
        if (!(extraArgs[0] instanceof NavTreeTableModel.MyDefaultTreeTableModel)) {
            throw new IllegalArgumentException("extra parameter is not instance of " + NavTreeTableModel.MyDefaultTreeTableModel.class.getName());
        }
        NavTreeTableModel.MyDefaultTreeTableModel delegate = (NavTreeTableModel.MyDefaultTreeTableModel) extraArgs[0];
        NavTreeTableBridge bridge = getBridge();
        NavTreeTableModel model = bridge.getModel();
        if (model == null) {
            model = new NavTreeTableModel(delegate);
            bridge.setModel(model);
//            model = new NavTreeTableBridge(tableModel);
            bridge.addTreeModelListener(treeModelListener);
//            ((NavTreeTableModel) model).addTreeModelListener(treeModelListener);
        }
        bridge.setRoot(node);

        // notify structure has changed
        bridge.nodeStructureChanged(getRootNode());
        return model;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy