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-2000 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.debugger.jpda.models;

import com.sun.jdi.ObjectCollectedException;
import com.sun.jdi.ThreadGroupReference;
import com.sun.jdi.ThreadReference;
import com.sun.jdi.VMDisconnectedException;
import com.sun.jdi.VirtualMachine;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import java.util.WeakHashMap;

import org.netbeans.api.debugger.DebuggerEngine;
import org.netbeans.spi.debugger.ContextProvider;
import org.netbeans.api.debugger.jpda.JPDADebugger;
import org.netbeans.spi.viewmodel.ComputingException;
import org.netbeans.spi.viewmodel.NoInformationException;
import org.netbeans.spi.viewmodel.TreeModel;
import org.netbeans.spi.viewmodel.TreeModelListener;
import org.netbeans.spi.viewmodel.UnknownTypeException;

import org.netbeans.modules.debugger.jpda.JPDADebuggerImpl;

import org.openide.util.RequestProcessor;


/**
 * Basic simple model for ThreadGroupReferences and ThreadReferences.
 * Listens on JPDADebugger state and updates all hierarchy.
 *
 * @author   Jan Jancura
 */
public class BasicThreadsTreeModel implements TreeModel {

    
    private static boolean verbose = 
        (System.getProperty ("netbeans.debugger.viewrefresh") != null) &&
        (System.getProperty ("netbeans.debugger.viewrefresh").indexOf ('t') >= 0);
    
    private JPDADebuggerImpl debugger;
    private Listener listener;
    private Vector listeners = new Vector ();
    
    
    public BasicThreadsTreeModel (ContextProvider lookupProvider) {
        debugger = (JPDADebuggerImpl) lookupProvider.
            lookupFirst (null, JPDADebugger.class);
    }
    
    public Object getRoot () {
        return ROOT;
    }
    
    public Object[] getChildren (Object o, int from, int to)
    throws UnknownTypeException {
        try {
            if (o.equals (ROOT)) {
                VirtualMachine vm = debugger.getVirtualMachine ();
                List ts = new ArrayList ();
                if (vm != null)
                    ts = vm.topLevelThreadGroups ();
                ts = ts.subList (from, to);
                return ts.toArray (new Object [ts.size ()]);
            } else
            if (o instanceof ThreadGroupReference) {
                ThreadGroupReference tgr = (ThreadGroupReference) o;
                List l = new ArrayList (tgr.threadGroups ());
                l.addAll (tgr.threads ());
                l = l.subList (from, to);
                Object[] os = new Object [l.size ()];
                return l.toArray (os);
            } else
            throw new UnknownTypeException (o);
        } catch (VMDisconnectedException ex) {
            return new Object [0];
        }
    }
    
    /**
     * Returns number of children for given node.
     * 
     * @param   node the parent node
     * @throws  UnknownTypeException if this TreeModel implementation is not
     *          able to resolve children for given node type
     *
     * @return  true if node is leaf
     */
    public int getChildrenCount (Object node) throws UnknownTypeException {
        try {
            if (node.equals (ROOT)) {
                VirtualMachine vm = debugger.getVirtualMachine ();
                if (vm != null)
                    return vm.topLevelThreadGroups ().size ();
                return 0;
            } else
            if (node instanceof ThreadGroupReference) {
                ThreadGroupReference tgr = (ThreadGroupReference) node;
                return tgr.threadGroups ().size () + tgr.threads ().size ();
            } else
            throw new UnknownTypeException (node);
        } catch (VMDisconnectedException ex) {
            return 0;
        }
    }
    
    public boolean isLeaf (Object o) throws UnknownTypeException {
        if (o instanceof ThreadReference) return true;
        if (o instanceof ThreadGroupReference) return false;
        if (o == ROOT) return false;
        throw new UnknownTypeException (o);
    }

    public void addTreeModelListener (TreeModelListener l) {
        listeners.add (l);
        if (listener == null) {
            listener = new Listener (this, debugger);
        }
    }

    public void removeTreeModelListener (TreeModelListener l) {
        listeners.remove (l);
        if (listeners.size () == 0) {
            listener.destroy ();
            listener = null;
        }
    }
    
    public void fireTreeChanged () {
        Vector v = (Vector) listeners.clone ();
        int i, k = v.size ();
        for (i = 0; i < k; i++)
            ((TreeModelListener) v.get (i)).treeChanged ();
    }

    /**
     * Listens on JPDADebugger state property and updates all threads hierarchy.
     */
    private static class Listener implements PropertyChangeListener {
        
        private JPDADebugger debugger;
        private WeakReference model;
        
        private Listener (
            BasicThreadsTreeModel tm,
            JPDADebugger debugger
        ) {
            this.debugger = debugger;
            model = new WeakReference (tm);
            debugger.addPropertyChangeListener (this);
        }
        
        private BasicThreadsTreeModel getModel () {
            BasicThreadsTreeModel tm = (BasicThreadsTreeModel) model.get ();
            if (tm == null) {
                destroy ();
            }
            return tm;
        }
        
        void destroy () {
            debugger.removePropertyChangeListener (this);
            if (task != null) {
                // cancel old task
                task.cancel ();
                if (verbose)
                    System.out.println("TTM cancel old task " + task);
                task = null;
            }
        }
        
        // currently waiting / running refresh task
        // there is at most one
        private RequestProcessor.Task task;
        
        public void propertyChange (PropertyChangeEvent e) {
            if ( (e.getPropertyName () == debugger.PROP_STATE) &&
                 (debugger.getState () == debugger.STATE_STOPPED)
            ) {
                final BasicThreadsTreeModel tm = getModel ();
                if (tm == null) return;
                if (task != null) {
                    // cancel old task
                    task.cancel ();
                    if (verbose)
                        System.out.println("TTM cancel old task " + task);
                    task = null;
                }
                task = RequestProcessor.getDefault ().post (new Runnable () {
                    public void run () {
                        if (verbose)
                            System.out.println("TTM do task " + task);
                        tm.fireTreeChanged ();
                    }
                }, 500);
                if (verbose)
                    System.out.println("TTM  create task " + task);
            } else 
            if ( (e.getPropertyName () == debugger.PROP_STATE) &&
                 (debugger.getState () == debugger.STATE_RUNNING)
            ) {
                final BasicThreadsTreeModel tm = getModel ();
                if (tm == null) return;
                if (task != null) {
                    // cancel old task
                    task.cancel ();
                if (verbose)
                    if (verbose)
                        System.out.println("TTM cancel old R task " + task);
                    task = null;
                }
                task = RequestProcessor.getDefault ().post (new Runnable () {
                    public void run () {
                        if (verbose)
                            System.out.println("TTM do R task " + task);
                        tm.fireTreeChanged ();
                        if ( (task != null) &&
                             (debugger.getState () == debugger.STATE_RUNNING)
                        )
                            task.schedule (2000);
                    }
                }, 2000);
                if (verbose)
                    System.out.println("TTM  create R task " + task);
            }
        }
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 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.