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.IncompatibleThreadStateException;
import com.sun.jdi.InvalidStackFrameException;
import com.sun.jdi.Location;
import com.sun.jdi.StackFrame;
import com.sun.jdi.ThreadReference;
import com.sun.jdi.VMDisconnectedException;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.lang.Integer;
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.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;


/**
 * @author   Jan Jancura
 */
public class BasicCallStackTreeModel implements TreeModel {

    
    private static boolean verbose = 
        (System.getProperty ("netbeans.debugger.viewrefresh") != null) &&
        (System.getProperty ("netbeans.debugger.viewrefresh").indexOf ('c') >= 0);
    
    
    private JPDADebuggerImpl debugger;
    private Listener listener;
    private Vector listeners = new Vector ();
    
    
    public BasicCallStackTreeModel (ContextProvider lookupProvider) {
        debugger = (JPDADebuggerImpl) lookupProvider.
            lookupFirst (null, JPDADebugger.class);
    }
    
    public Object getRoot () {
        return ROOT;
    }
    
    public Object[] getChildren (Object o, int from, int to) 
    throws NoInformationException, UnknownTypeException {
        if ( o.equals (ROOT) ||
             (o instanceof ThreadReference) 
        ) {
            // 1) get ThreadReference
            ThreadReference threadRef = null;
            if (o.equals (ROOT)) {
                JPDAThreadImpl ti = (JPDAThreadImpl) debugger.
                    getCurrentThread ();
                if (ti != null)
                    threadRef = ti.getThreadReference ();
            } else
                threadRef = (ThreadReference) o;
            if (threadRef == null) throw new NoInformationException ("No current thread");
            
            // 2) get StackFrames for this ThreadReference
            try {
                List l = threadRef.frames (from, to - from);
                StackFrame[] callStack = new StackFrame [l.size ()];
                return (StackFrame []) l.toArray (callStack);
            } catch (IncompatibleThreadStateException ex) {
                throw new NoInformationException ("Thread is running");
            } catch (VMDisconnectedException ex) {
                return new StackFrame [0];
            }
        } else
        throw new UnknownTypeException (o);
    }
    
    /**
     * 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,
    NoInformationException {
        if ( node.equals (ROOT) ||
             (node instanceof ThreadReference) 
        ) {
            // 1) get ThreadReference
            ThreadReference threadRef = null;
            if (node.equals (ROOT)) {
                JPDAThreadImpl ti = (JPDAThreadImpl) debugger.
                    getCurrentThread ();
                if (ti != null)
                    threadRef = ti.getThreadReference ();
            } else
                threadRef = (ThreadReference) node;
            if (threadRef == null) throw new NoInformationException ("No current thread");
            
            // 2) get StackFrames count for this ThreadReference
            try {
                return threadRef.frameCount ();
            } catch (IncompatibleThreadStateException ex) {
                throw new NoInformationException ("Thread is running");
            } catch (VMDisconnectedException ex) {
                return 0;
            }
        } else
        throw new UnknownTypeException (node);
    }
    
    public boolean isLeaf (Object o) throws UnknownTypeException {
        if (o.equals (ROOT)) return false;
        if (o instanceof StackFrame) return true;
        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 on PROP_STATE
     */
    private static class Listener implements PropertyChangeListener {
        
        private JPDADebugger debugger;
        private WeakReference model;
        
        private Listener (
            BasicCallStackTreeModel tm,
            JPDADebugger debugger
        ) {
            this.debugger = debugger;
            model = new WeakReference (tm);
            debugger.addPropertyChangeListener (this);
        }
        
        private BasicCallStackTreeModel getModel () {
            BasicCallStackTreeModel tm = (BasicCallStackTreeModel) 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("CSTM 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_CURRENT_THREAD) ||
                   (e.getPropertyName () == debugger.PROP_STATE)
                 ) && (debugger.getState () == debugger.STATE_STOPPED)
            ) {
                final BasicCallStackTreeModel tm = getModel ();
                if (tm == null) return;
                if (task != null) {
                    // cancel old task
                    task.cancel ();
                    if (verbose)
                        System.out.println("CSTM cancel old task " + task);
                    task = null;
                }
                task = RequestProcessor.getDefault ().post (new Runnable () {
                    public void run () {
                        if (debugger.getState () != debugger.STATE_STOPPED) {
                            if (verbose)
                                System.out.println("CSTM cancel started task " + task);
                            return;
                        }
                        if (verbose)
                            System.out.println("CSTM do task " + task);
                        tm.fireTreeChanged ();
                    }
                }, 500);
                if (verbose)
                    System.out.println("CSTM  create task " + task);
            } else
            if ( (e.getPropertyName () == debugger.PROP_STATE) &&
                 (debugger.getState () != debugger.STATE_STOPPED) &&
                 (task != null)
            ) {
                // debugger has been resumed
                // =>> cancel task
                task.cancel ();
                if (verbose)
                    System.out.println("CSTM cancel task " + task);
                task = null;
            }
        }
    }
}
... 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.