view src/MPipeline/src/mpipeline/LTPDALibraryModel.java @ 27:29276498ebdb database-connection-manager

Remove LTPDARepositoryManager implementation * * * Remove GUI helper
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Mon, 05 Dec 2011 16:20:06 +0100
parents f0afece42f48
children
line wrap: on
line source

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mpipeline;

import java.io.Serializable;
import java.util.Vector;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

/**
 * Implements a tree-model view of an LTPDA library. 
 * 
 * @author hewitson
 */
public class LTPDALibraryModel implements TreeModel, Serializable {

    private LTPDAlibrary lib;
    private Vector<TreeModelListener> treeModelListeners =
            new Vector<TreeModelListener>();

    /**
     * Construct an LTPDALibraryModel of the given library.
     * @param l
     */
    public LTPDALibraryModel(LTPDAlibrary l) {
        lib = l;
    }

    /**
     * Set the library for this model.
     * @param l
     */
    public void setLibrary(LTPDAlibrary l) {
        lib = l;
    }

    /**
     * Get the root object in this model.
     * @return
     */
    public Object getRoot() {
        return lib;
    }

    /**
     * Get the child at the given index of the given parent node.
     * @param parent
     * @param index
     * @return
     */
    public Object getChild(Object parent, int index) {
        // we need to know what kind of object the parent is
        if (parent instanceof LTPDAlibrary) {
            // get the category at this index
            return lib.getCategory(index);
        } else if (parent instanceof LTPDAcategory) {
            LTPDAcategory cat = (LTPDAcategory) parent;
            if (cat.getSubCategoryCount() > 0) {
                return cat.getSubCategory(index);
            } else {
                return cat.getAlgorithm(index);
            }
        } else {
            System.err.println("Unknown parent object");
        }

        return null;
    }

    /**
     * Get a count of the child nodes of the given parent node.
     * @param parent
     * @return
     */
    public int getChildCount(Object parent) {
        // we need to know what kind of object the parent is
        if (parent instanceof LTPDAlibrary) {
            // get the category at this index
            return lib.getCategoryCount();
        } else if (parent instanceof LTPDAcategory) {
            LTPDAcategory cat = (LTPDAcategory) parent;
            if (cat.getSubCategoryCount() > 0) {
                return cat.getSubCategoryCount();
            } else {
                return cat.getAlgorithmCount();
            }
        } else {
            System.err.println("Unknown parent object");
        }
        return 0;
    }

    /**
     * Return true if this node is a leaf.
     * @param node
     * @return
     */
    public boolean isLeaf(Object node) {
        if (node instanceof LTPDAalgorithm) {
            return true;
        } else {
            return false;
        }
    }

    public void valueForPathChanged(TreePath path, Object newValue) {
    }

    /**
     * Return the index of the given child of the given parent.
     * @param parent
     * @param child
     * @return
     */
    public int getIndexOfChild(Object parent, Object child) {

        // we need to know what kind of object the parent is
        if (parent instanceof LTPDAlibrary) {
            // get the index of this category
            if (child instanceof LTPDAcategory) {
                return lib.getIndexOfCategory((LTPDAcategory) child);
            } else {
                
            }
        } else if (parent instanceof LTPDAcategory) {
            LTPDAcategory cat = (LTPDAcategory) parent;
            if (child instanceof LTPDAcategory) {
                return cat.getIndexOfCategory((LTPDAcategory) child);
            } else {
                return cat.getIndexOfAlgorithm((LTPDAalgorithm) child);
            }
        } else {
            System.err.println("Unknown parent object");
        }
        return 0;
    }

    public void addTreeModelListener(TreeModelListener l) {
        treeModelListeners.add(l);
    }

    public void removeTreeModelListener(TreeModelListener l) {
        treeModelListeners.removeElement(l);
    }

    /**
     * Get the LTPDA algorithm object at the given treepath.
     * @param tp
     * @return
     */
    public LTPDAalgorithm getAlgorithmAtPath(TreePath tp) {

        LTPDAalgorithm a = (LTPDAalgorithm) tp.getLastPathComponent();
        return a;
    }
}