PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/macros/Misc/Show_Threads.bsh

#
Unknown | 150 lines | 117 code | 33 blank | 0 comment | 0 complexity | 94d2e1fb3f4cca3ac9d0bb52f6cbe633 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /* Show_Threads.bsh
  2. *
  3. * A BeanShell macro script for jEdit - displays all threads in a tree.
  4. *
  5. * Copyright (c) 2001 Dirk Moebius (dmoebius@gmx.net)
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. *
  21. *
  22. * Version 1.0
  23. * requires JDK 1.1, jEdit3.1
  24. *
  25. * $Id: Show_Threads.bsh 4001 2002-01-31 05:35:49Z spestov $
  26. */
  27. import java.awt.event.*;
  28. import javax.swing.*;
  29. import javax.swing.tree.*;
  30. JFrame createThreadsFrame() {
  31. void windowClosing(WindowEvent e) {
  32. GUIUtilities.saveGeometry(frame, "macros-show-threads");
  33. }
  34. void keyPressed(KeyEvent e) {
  35. if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
  36. GUIUtilities.saveGeometry(frame, "macros-show-threads");
  37. frame.setVisible(false);
  38. frame.dispose();
  39. }
  40. else if (e.getKeyCode() == KeyEvent.VK_U) {
  41. tree.setModel(new DefaultTreeModel(createModel()));
  42. tree.revalidate();
  43. }
  44. }
  45. // Having these members of KeyListener and WindowListener implemented as
  46. // no-ops will speedup execution. Otherwise BeanShell throws an
  47. // exception each time one of these events occur.
  48. void keyReleased(KeyEvent e) { }
  49. void keyTyped(KeyEvent e) { }
  50. void windowActivated(WindowEvent e) { }
  51. void windowClosed(WindowEvent e) { }
  52. void windowDeactivated(WindowEvent e) { }
  53. void windowDeiconified(WindowEvent e) { }
  54. void windowIconified(WindowEvent e) { }
  55. void windowOpened(WindowEvent e) { }
  56. ThreadGroup getMainThreadGroup() {
  57. t = Thread.currentThread();
  58. g = t.getThreadGroup();
  59. while (g.getParent() != null)
  60. g = g.getParent();
  61. return g;
  62. }
  63. DefaultMutableTreeNode createModel() {
  64. g = getMainThreadGroup();
  65. return createGroupNode(g);
  66. }
  67. DefaultMutableTreeNode createGroupNode(ThreadGroup g) {
  68. node = new DefaultMutableTreeNode(g.getName() + (g.isDaemon() ? " (daemon)" : ""), true);
  69. groups = new ThreadGroup[g.activeGroupCount()];
  70. count = g.enumerate(groups, false);
  71. for (int i = 0; i < count; ++i)
  72. node.add(createGroupNode(groups[i]));
  73. threads = new Thread[g.activeCount()];
  74. count = g.enumerate(threads, false);
  75. for (int i = 0; i < count; ++i)
  76. node.add(new DefaultMutableTreeNode(getDescription(threads[i]), false));
  77. return node;
  78. }
  79. String getDescription(Thread t) {
  80. StringBuffer buf = new StringBuffer(t.getName());
  81. buf.append(" (prio ");
  82. buf.append(t.getPriority());
  83. if (!t.isAlive())
  84. buf.append(", not started");
  85. if (t.isDaemon())
  86. buf.append(", daemon");
  87. if (t.isInterrupted())
  88. buf.append(", interrupted");
  89. buf.append(")");
  90. return buf.toString();
  91. }
  92. tree = new JTree(createModel());
  93. tree.putClientProperty("JTree.lineStyle", "Angled");
  94. tree.addKeyListener(this);
  95. stage = new JScrollPane(tree);
  96. stage.setColumnHeaderView(new JLabel("[Esc] Close [U] Update"));
  97. frame = new JFrame("Current Threads");
  98. frame.setContentPane(stage);
  99. frame.addWindowListener(this);
  100. // set default size and position:
  101. frame.setSize(new Dimension(400, 400)); // faster than pack()
  102. // overwrite default size and position loading user properties:
  103. GUIUtilities.loadGeometry(frame, "macros-show-threads");
  104. frame.setVisible(true);
  105. return frame;
  106. }
  107. createThreadsFrame();
  108. /*
  109. Macro index data (in DocBook format)
  110. <listitem>
  111. <para><filename>Show_Threads.bsh</filename></para>
  112. <abstract><para>
  113. Displays all running Java threads in a tree.
  114. </para></abstract>
  115. </listitem>
  116. */
  117. // end Show_Threads.bsh