jdk/src/share/classes/com/sun/tools/example/debug/gui/ThreadTreeTool.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 51 6fe31bc95bbc
child 5506 202f599c92aa
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 51
diff changeset
     2
 * Copyright 1998-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.example.debug.gui;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.List;  // Must import explicitly due to conflict with javax.awt.List
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.tree.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import com.sun.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import com.sun.tools.example.debug.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import com.sun.tools.example.debug.bdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
//### Bug: If the name of a thread is changed via Thread.setName(), the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
//### thread tree view does not reflect this.  The name of the thread at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
//### the time it is created is used throughout its lifetime.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public class ThreadTreeTool extends JPanel {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private Environment env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private ExecutionManager runtime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private SourceManager sourceManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private ClassManager classManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private JTree tree;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private DefaultTreeModel treeModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private ThreadTreeNode root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private SearchPath sourcePath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private CommandInterpreter interpreter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private static String HEADING = "THREADS";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public ThreadTreeTool(Environment env) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        super(new BorderLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        this.env = env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        this.runtime = env.getExecutionManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        this.sourceManager = env.getSourceManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        this.interpreter = new CommandInterpreter(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        root = createThreadTree(HEADING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        treeModel = new DefaultTreeModel(root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        // Create a tree that allows one selection at a time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        tree = new JTree(treeModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        tree.setSelectionModel(new SingleLeafTreeSelectionModel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        MouseListener ml = new MouseAdapter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            public void mouseClicked(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                int selRow = tree.getRowForLocation(e.getX(), e.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                if(selRow != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    if(e.getClickCount() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                        ThreadTreeNode node =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                            (ThreadTreeNode)selPath.getLastPathComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                        // If user clicks on leaf, select it, and issue 'thread' command.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                        if (node.isLeaf()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                            tree.setSelectionPath(selPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                            interpreter.executeCommand("thread " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                                                       node.getThreadId() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                                                       "  (\"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                                                       node.getName() + "\")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        tree.addMouseListener(ml);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        JScrollPane treeView = new JScrollPane(tree);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        add(treeView);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        // Create listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        ThreadTreeToolListener listener = new ThreadTreeToolListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        runtime.addJDIListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        runtime.addSessionListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        //### remove listeners on exit!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    HashMap<ThreadReference, List<String>> threadTable = new HashMap<ThreadReference, List<String>>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private List<String> threadPath(ThreadReference thread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        // May exit abnormally if VM disconnects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        List<String> l = new ArrayList<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        l.add(0, thread.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        ThreadGroupReference group = thread.threadGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        while (group != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            l.add(0, group.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            group = group.parent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    private class ThreadTreeToolListener extends JDIAdapter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                              implements JDIListener, SessionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        // SessionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        public void sessionStart(EventObject e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   136
                for (ThreadReference thread : runtime.allThreads()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    root.addThread(thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            } catch (VMDisconnectedException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                // VM went away unexpectedly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            } catch (NoSessionException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                // Ignore.  Should not happen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        public void sessionInterrupt(EventObject e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        public void sessionContinue(EventObject e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        // JDIListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        public void threadStart(ThreadStartEventSet e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            root.addThread(e.getThread());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        public void threadDeath(ThreadDeathEventSet e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            root.removeThread(e.getThread());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        public void vmDisconnect(VMDisconnectEventSet e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            // Clear the contents of this view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            root = createThreadTree(HEADING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            treeModel = new DefaultTreeModel(root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            tree.setModel(treeModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            threadTable = new HashMap<ThreadReference, List<String>>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    ThreadTreeNode createThreadTree(String label) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return new ThreadTreeNode(label, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    class ThreadTreeNode extends DefaultMutableTreeNode {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        ThreadReference thread; // null if thread group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        long uid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        String description;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        ThreadTreeNode(String name, ThreadReference thread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                name = "<unnamed>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            this.thread = thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            if (thread == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                this.uid = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                this.description = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                this.uid = thread.uniqueID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                this.description = name + " (t@" + Long.toHexString(uid) + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            return description;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        public ThreadReference getThread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            return thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        public String getThreadId() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            return "t@" + Long.toHexString(uid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        private boolean isThreadGroup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            return (thread == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        public boolean isLeaf() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            return !isThreadGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        public void addThread(ThreadReference thread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            // This can fail if the VM disconnects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            // It is important to do all necessary JDI calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            // before modifying the tree, so we don't abort
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            // midway through!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            if (threadTable.get(thread) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                // Add thread only if not already present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                    List<String> path = threadPath(thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                    // May not get here due to exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                    // If we get here, we are committed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    // We must not leave the tree partially updated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                        threadTable.put(thread, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                        addThread(path, thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                    } catch (Throwable tt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                        //### Assertion failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                        throw new RuntimeException("ThreadTree corrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                } catch (VMDisconnectedException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    // Ignore.  Thread will not be added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   245
        private void addThread(List<String> threadPath, ThreadReference thread) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            int size = threadPath.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            if (size == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            } else if (size == 1) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   250
                String name = threadPath.get(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                insertNode(name, thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            } else {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   253
                String head = threadPath.get(0);
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   254
                List<String> tail = threadPath.subList(1, size);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                ThreadTreeNode child = insertNode(head, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                child.addThread(tail, thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        private ThreadTreeNode insertNode(String name, ThreadReference thread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            for (int i = 0; i < getChildCount(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                ThreadTreeNode child = (ThreadTreeNode)getChildAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                int cmp = name.compareTo(child.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                if (cmp == 0 && thread == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    // A like-named interior node already exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    return child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                } else if (cmp < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    // Insert new node before the child.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    ThreadTreeNode newChild = new ThreadTreeNode(name, thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    treeModel.insertNodeInto(newChild, this, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    return newChild;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            // Insert new node after last child.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            ThreadTreeNode newChild = new ThreadTreeNode(name, thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            treeModel.insertNodeInto(newChild, this, getChildCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            return newChild;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        public void removeThread(ThreadReference thread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            List<String> threadPath = threadTable.get(thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            // Only remove thread if we recorded it in table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            // Original add may have failed due to VM disconnect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            if (threadPath != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                removeThread(threadPath, thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   289
        private void removeThread(List<String> threadPath, ThreadReference thread) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            int size = threadPath.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            if (size == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            } else if (size == 1) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   294
                String name = threadPath.get(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                ThreadTreeNode child = findLeafNode(thread, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                treeModel.removeNodeFromParent(child);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            } else {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   298
                String head = threadPath.get(0);
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   299
                List<String> tail = threadPath.subList(1, size);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                ThreadTreeNode child = findInternalNode(head);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                child.removeThread(tail, thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                if (child.isThreadGroup() && child.getChildCount() < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    // Prune non-leaf nodes with no children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    treeModel.removeNodeFromParent(child);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        private ThreadTreeNode findLeafNode(ThreadReference thread, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            for (int i = 0; i < getChildCount(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                ThreadTreeNode child = (ThreadTreeNode)getChildAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                if (child.getThread() == thread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                    if (!name.equals(child.getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                        //### Assertion failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                        throw new RuntimeException("name mismatch");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                    return child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            //### Assertion failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            throw new RuntimeException("not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        private ThreadTreeNode findInternalNode(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            for (int i = 0; i < getChildCount(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                ThreadTreeNode child = (ThreadTreeNode)getChildAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                if (name.equals(child.getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                    return child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            //### Assertion failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            throw new RuntimeException("not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
}