Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 4346256
Votes 3
Synopsis Provide an AbstractTreeModel for the TreeModel hierarchy
Category java:classes_swing
Reported Against 1.2.2
Release Fixed
State 6-Fix Understood, request for enhancement
Priority: 3-Medium
Related Bugs
Submit Date 16-JUN-2000
Description



java version "1.2.2"
Classic VM (build JDK-1.2.2-001, native threads, symcjit)


Unlike the rest of the model class hierarchy, the TreeModel hierarchy does not
provide an AbstractTreeModel that takes care of registering listeners but
leaves the implementation up to the user. DefaultTreeModel is the only provided
implementation, and it requires the use of TreeNodes or DefaultMutableTreeNodes.
(Review ID: 102855) 
======================================================================
Posted Date : 2005-07-27 06:12:48.0

Refer to this forum thread for two suggested implementations:
  https://jdk-collaboration.dev.java.net/servlets/ProjectForumMessageView?forumID=1463&messageID=10881
Posted Date : 2006-01-13 22:55:35.0
Work Around


Use DefaultTreeModel or write your own and copy the listener support code from
DefaultTreeModel.
======================================================================
Evaluation
This would be extremely useful to a number of people, as well as reinforcing that people don't need to use TreeNode and the like to use JTree. Since we have run out of time for merlin, I'm commiting this to tiger.
  xxxxx@xxxxx   2001-03-14
Contribution-forum:https://jdk-collaboration.dev.java.net/servlets/ProjectForumMessageView?forumID=1463&messageID=10881

(Two different suggested fixes from java.net members...)
Posted Date : 2006-01-13 22:55:35.0
Comments
  
  Include a link with my name & email   

Submitted On 28-AUG-2000
heuermh
It might just be easier to cut and paste yourself, but here 
it is:

---

//:  AbstractTreeModel.java

package com.insert.your.package.here;

import java.util.*;
import javax.swing.tree.*;
import javax.swing.event.*;

/**
 * This abstract class provides default implementations for 
most of the methods in
 * the TreeModel interface. It takes care of the management 
of listeners and
 * provides some conveniences for generating 
TreeModelEvents and dispatching them to
 * the listeners.
 */
public abstract class AbstractTreeModel implements 
TreeModel {
    protected EventListenerList listenerList = new 
EventListenerList();

    abstract public Object getRoot();
	
    abstract public Object getChild(Object parent, int 
index);
	
    abstract public int getChildCount(Object parent);

    abstract public int getIndexOfChild(Object parent, 
Object child);
	
    abstract public boolean isLeaf(Object node);

    abstract public void valueForPathChanged(TreePath path, 
Object newValue);

    //
    //  Events
    //

    /**
     * Adds a listener for the TreeModelEvent posted after 
the tree changes.
     *
     * @see     #removeTreeModelListener
     * @param   l       the listener to add
     */
    public void addTreeModelListener(TreeModelListener l) {
        listenerList.add(TreeModelListener.class, l);
    }

    /**
     * Removes a listener previously added with 
<B>addTreeModelListener()</B>.
     *
     * @see     #addTreeModelListener
     * @param   l       the listener to remove
     */  
    public void removeTreeModelListener(TreeModelListener 
l) {
        listenerList.remove(TreeModelListener.class, l);
    }

	/**
	 * Wholesale update of the entire tree structure.
	 */
	public void fireTreeChanged(Object source, Object 
root) {
       
       	TreePath path = new TreePath(root);
       	
        // Guaranteed to return a non-null array
        Object[] listeners = listenerList.getListenerList();
        TreeModelEvent e = null;
        // Process the listeners last to first, notifying
        // those that are interested in this event
        for (int i = listeners.length-2; i>=0; i-=2) {
            if (listeners[i]==TreeModelListener.class) {
                // Lazily create the event:
                if (e == null)
                    e = new TreeModelEvent(source, path);
                ((TreeModelListener)listeners
[i+1]).treeNodesChanged(e);
            }          
        }
	}

    /*
     * Notify all listeners that have registered interest 
for
     * notification on this event type.  The event instance 
     * is lazily created using the parameters passed into 
     * the fire method.
     * @see EventListenerList
     */
    protected void fireTreeNodesChanged(Object source, 
Object[] path, 
                                        int[] childIndices, 
                                        Object[] children) {
        // Guaranteed to return a non-null array
        Object[] listeners = listenerList.getListenerList();
        TreeModelEvent e = null;
        // Process the listeners last to first, notifying
        // those that are interested in this event
        for (int i = listeners.length-2; i>=0; i-=2) {
            if (listeners[i]==TreeModelListener.class) {
                // Lazily create the event:
                if (e == null)
                    e = new TreeModelEvent(source, path, 
                                           childIndices, 
children);
                ((TreeModelListener)listeners
[i+1]).treeNodesChanged(e);
            }          
        }
    }

    /*
     * Notify all listeners that have registered interest 
for
     * notification on this event type.  The event instance 
     * is lazily created using the parameters passed into 
     * the fire method.
     * @see EventListenerList
     */
    protected void fireTreeNodesInserted(Object source, 
Object[] path, 
                                        int[] childIndices, 
                            


Submitted On 11-APR-2007
ian-desouza
I presume this never got into 6.0? Any plans for 7.0?



PLEASE NOTE: JDK6 is formerly known as Project Mustang