package com.macmillan.nmeyers; import javax.swing.*; import java.util.*; import java.awt.*; import java.awt.event.*; /* * SelectThreads.java: Part of the PerfAnal tool * * This class implements a dialog for selecting one or more threads * to analyze. * * Author: Nathan Meyers, nmeyers@javalinux.net * $Id: SelectThreads.java,v 1.5 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 SelectThreads extends JDialog { Box box1; ChooseThread chooseThread; SelectThreads(JFrame owner, ChooseThread ct, HashMap threads) { super(owner, "Select Thread(s) to Analyze", true); chooseThread = ct; ArrayList list = new ArrayList(threads.values()); Collections.sort(list, new Comparator() { public int compare(Object o1, Object o2) { return ((ThreadInfo)o2).count - ((ThreadInfo)o1).count; } public boolean Equals(Object o) { return false; } }); box1 = Box.createVerticalBox(); getContentPane().add(new JScrollPane(box1), BorderLayout.CENTER); for (Iterator i = list.iterator(); i.hasNext();) { ThreadCheckBox checkBox = new ThreadCheckBox((ThreadInfo)i.next()); box1.add(checkBox); } Box box2 = Box.createHorizontalBox(); getContentPane().add(box2, BorderLayout.SOUTH); box2.add(Box.createGlue()); JButton button = new JButton("OK"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { setVisible(false); Object[] boxes = box1.getComponents(); for (int i = 0; i < boxes.length; i++) ((ThreadCheckBox)boxes[i]).thread.enabled = ((ThreadCheckBox)boxes[i]).isSelected(); chooseThread.recomputeTotals(); } }); box2.add(button); getRootPane().setDefaultButton(button); box2.add(Box.createGlue()); button = new JButton("Set All"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Object[] boxes = box1.getComponents(); for (int i = 0; i < boxes.length; i++) ((ThreadCheckBox)boxes[i]).setSelected(true); } }); box2.add(button); box2.add(Box.createGlue()); button = new JButton("Clear All"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Object[] boxes = box1.getComponents(); for (int i = 0; i < boxes.length; i++) ((ThreadCheckBox)boxes[i]).setSelected(false); } }); box2.add(button); box2.add(Box.createGlue()); button = new JButton("Cancel"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { setVisible(false); } }); box2.add(button); box2.add(Box.createGlue()); pack(); } static class ThreadCheckBox extends JCheckBox { ThreadInfo thread; ThreadCheckBox(ThreadInfo thr) { super(thr.toString()); thread = thr; } } public interface ChooseThread { public void chooseThread(); public void recomputeTotals(); } public void setVisible(boolean vis) { if (vis) { Object[] boxes = box1.getComponents(); for (int i = 0; i < boxes.length; i++) ((ThreadCheckBox)boxes[i]). setSelected(((ThreadCheckBox)boxes[i]).thread.enabled); } super.setVisible(vis); } }