alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.tasklist.compiler.nodestreetable;

import java.beans.PropertyChangeEvent;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.netbeans.modules.tasklist.compiler.treetable.AbstractTreeTableModel;
import org.netbeans.modules.tasklist.core.ColumnProperty;
import org.netbeans.modules.tasklist.core.TLUtils;
import org.netbeans.modules.tasklist.compiler.treetable.TreeTableModel;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.nodes.NodeListener;

/**
 * TreeTableModel based on org.openide.nodes.Node
 *
 * @author Tim Lebedkov
 */
public class NodesTreeTableModel extends AbstractTreeTableModel implements
NodeListener {
    private static final Logger LOGGER = TLUtils.getLogger(
        NodesTreeTableModel.class); 
    
    static {
        LOGGER.setLevel(Level.OFF);
    }
    
    private ColumnProperty[] properties;
    
    /** 
     * Creates a new instance of NodesTreeTableModel 
     *
     * @param properties column descriptions
     * @param root root node
     */
    public NodesTreeTableModel(ColumnProperty[] properties, Node root) {
        super(root);
        root.addNodeListener(this);
        this.properties = properties;
    }

    public void setRoot(Object root) {
        LOGGER.log(Level.FINE, root == null ? "null" : root.toString(),
            new Exception());
        super.setRoot(root);
    }
    
    /**
     * Changes the columns
     *
     * @param properties column descriptions
     */
    public void setProperties(ColumnProperty[] properties) {
        this.properties = properties;
        fireTreeStructureChanged(this, new Object[] {root});
    }
    
    /**
     * Returns column descriptions
     *
     * @return column descriptions
     */
    public ColumnProperty[] getProperties() {
        return properties;
    }
    
    public Object getChild(Object node, int index) {
        return ((Node) node).getChildren().getNodes(true)[index];
    }
    
    public int getChildCount(Object node) {
        ((Node) node).removeNodeListener(this);
        ((Node) node).addNodeListener(this);
        return ((Node) node).getChildren().getNodes(true).length;
    }
    
    public void setValueAt(Object aValue, Object node, int column) {
        // read-only model
    }
    
    public Object getValueAt(Object node, int column) {
        Node.PropertySet[] sets = ((Node) node).getPropertySets();
        for (int i = 0; i < sets.length; i++) {
            Node.Property[] props = sets[i].getProperties();
            for (int j = 0; j < props.length; j++) {
                if (props[j].getName().equals(properties[column].getName())) {
                    try {
                        return props[j].getValue();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return null;
    }
    
    public String getColumnName(int column) {
        return properties[column].getDisplayName();
    }
    
    public Class getColumnClass(int column) {
        if (column == 0)
            return TreeTableModel.class;
        else
            return properties[column].getValueType();
    }
    
    public int getColumnCount() {
        return properties.length;
    }
    
    public void childrenRemoved(org.openide.nodes.NodeMemberEvent ev) {
        LOGGER.fine("childrenRemoved");
        fireTreeNodesRemoved(this, getPathToNode(ev.getNode()), 
            ev.getDeltaIndices(), ev.getDelta());
    }
    
    public void childrenAdded(org.openide.nodes.NodeMemberEvent ev) {
        LOGGER.fine(ev.getNode() + " " + ev.getDelta().length);
        LOGGER.fine(ev.getDelta()[0].toString());
        LOGGER.fine(String.valueOf(ev.getDeltaIndices()[0]));
        fireTreeNodesInserted(this, getPathToNode(ev.getNode()),
            ev.getDeltaIndices(), ev.getDelta());
    }
    
    public void nodeDestroyed(org.openide.nodes.NodeEvent ev) {
    }
    
    public void childrenReordered(org.openide.nodes.NodeReorderEvent ev) {
        fireTreeStructureChanged(this, getPathToNode(ev.getNode()));
    }
    
    /**
     * Finds the path to the specified node
     *
     * @param n a node under the root
     * @return path to the specified node. Includes the root and the node itself.
     */
    private Node[] getPathToNode(Node node) {
        List l = new ArrayList();
        Node n = node;
        while (n != root && n != null) {
            l.add(0, n);
            n = n.getParentNode();
        }
        l.add(0, root);
        return (Node[]) l.toArray(new Node[l.size()]);
    }
    
    /**
     * Finds the index of a node in it's parent
     *
     * @param n a node
     * @return index in it's parent or -1
     */
    private int getIndexOf(Node n) {
        if (n.getParentNode() == null)
            return -1;
        
        Node[] c = n.getParentNode().getChildren().getNodes();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == n)
                return i;
        }
        
        return -1;
    }
    
    public void propertyChange(PropertyChangeEvent e) {
        LOGGER.fine("propertyChange");
        
        if (e.getPropertyName() == Node.PROP_PARENT_NODE ||
            e.getPropertyName() == Node.PROP_PROPERTY_SETS)
            return;
        
        Node n = (Node) e.getSource();
        Node parent = n.getParentNode();
        if (parent != null) {
            fireTreeNodesChanged(this, getPathToNode(parent),
                new int[] {getIndexOf(n)}, new Object[] {n});
        }
    }

    /** 
     * By default, make the column with the Tree in it the only editable one.
     * Making this column editable causes the JTable to forward mouse
     * and keyboard events in the Tree column to the underlying JTree.
     */
    public boolean isCellEditable(Object node, int column) {
        return getColumnClass(column) == TreeTableModel.class;
    }

    /**
     * Find the index of the property with the specified name in this model.
     *
     * @return found index or -1
     * @param property a property name
     */
    public int modelIndexForProperty(String property) {
        for (int i = 0; i < properties.length; i++) {
            if (properties[i].getName().equals(property))
                return i;
        }
        return -1;
    }
    
    public boolean isLeaf(Object node) {
        return ((Node) node).getChildren() == Children.LEAF;
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.