package com.macmillan.nmeyers; import java.awt.event.*; import javax.swing.*; import javax.swing.tree.*; import java.io.*; import java.util.*; /* * PerfTree.java: Part of the PerfAnal tool * * This class defines the base class for the various trees used to * view the analysis. * * Author: Nathan Meyers, nmeyers@javalinux.net * $Id: PerfTree.java,v 1.8 1999/11/10 03:36:14 nathanm Exp $ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * with this program. If not, the license is available from the * GNU project, at http://www.gnu.org. */ class PerfTree extends JScrollPane { JTree tree = null; JPopupMenu popupMenu = null; FindMethod findMethod; SelectThreads.ChooseThread chooseThread; PerfTreeNode root; MouseListener mouseListener = null; PerfTree(PerfTreeNode n, FindMethod fm, SelectThreads.ChooseThread ct) { setBorder(BorderFactory.createTitledBorder(n.toString())); root = n; findMethod = fm; chooseThread = ct; initializeRoot(n); } void initializeRoot(PerfTreeNode n) { if (tree != null) { tree.removeMouseListener(mouseListener); } tree = new JTree(root = n); tree.setLargeModel(true); tree.setRootVisible(false); if (popupMenu == null) { popupMenu = new JPopupMenu(); popupMenu.add( new AbstractAction("Goto this Method") { public void actionPerformed(ActionEvent e) { PerfTreeNode node = (PerfTreeNode)tree.getSelectionPath(). getLastPathComponent(); findMethod.findMethod(node.getProcName()); } }); popupMenu.add( new AbstractAction("Select a Method to Analyze") { public void actionPerformed(ActionEvent e) { findMethod.findMethod(null); } }); popupMenu.add( new AbstractAction("Select Thread(s) to Analyze") { public void actionPerformed(ActionEvent e) { chooseThread.chooseThread(); } }); } popupMenu.setInvoker(tree); if (mouseListener == null) mouseListener = new MouseAdapter() { public void mousePressed(MouseEvent e) { if (e.isPopupTrigger()) { int x = e.getX(); int y = e.getY(); int row = tree.getRowForLocation(x, y); if (row != -1) { tree.setSelectionRow(row); popupMenu.show(tree, x, y); } } } public void mouseReleased(MouseEvent e) { if (e.isPopupTrigger()) { int x = e.getX(); int y = e.getY(); int row = tree.getRowForLocation(x, y); if (row != -1) { tree.setSelectionRow(row); popupMenu.show(tree, x, y); } } } }; tree.addMouseListener(mouseListener); setViewportView(tree); } void selectThisProc(String procName) { if (procName == null) return; TreePath path = root.findThisProc(procName); if (path != null) { tree.setSelectionPath(path); tree.scrollPathToVisible(path); } } void saveData(PrintWriter writer) { root.saveData(writer); } abstract static class PerfTreeNode extends DefaultMutableTreeNode { PerfTreeNode(Object o) { super(o); } abstract String getProcName(); abstract TreePath findThisProc(String procName); void saveData(PrintWriter writer) { int level = getLevel(); for (int i = 0; i < level; i++) writer.print(" "); if (level > 0) writer.print("" + level + ": "); writer.println(toString()); if (children != null) { Iterator iterator = children.iterator(); while (iterator.hasNext()) ((PerfTreeNode)iterator.next()).saveData(writer); } } } interface FindMethod { void findMethod(String method); } }