KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > actions > ShowSystemThreadsAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.debug.ui.actions;
12
13 import org.eclipse.core.runtime.IAdaptable;
14 import org.eclipse.debug.core.DebugEvent;
15 import org.eclipse.debug.core.DebugException;
16 import org.eclipse.debug.core.DebugPlugin;
17 import org.eclipse.debug.core.IDebugEventSetListener;
18 import org.eclipse.jdt.debug.core.IJavaThread;
19 import org.eclipse.jdt.debug.ui.IJavaDebugUIConstants;
20 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
21 import org.eclipse.jface.viewers.StructuredSelection;
22 import org.eclipse.jface.viewers.Viewer;
23 import org.eclipse.ui.IViewPart;
24
25 /**
26  * An action delegate that toggles the state of its viewer to
27  * show/hide System Threads.
28  */

29 public class ShowSystemThreadsAction extends ViewFilterAction implements IDebugEventSetListener {
30
31     /* (non-Javadoc)
32      * @see org.eclipse.jdt.internal.debug.ui.actions.ThreadFilterAction#getPreferenceKey()
33      */

34     protected String JavaDoc getPreferenceKey() {
35         return IJavaDebugUIConstants.PREF_SHOW_SYSTEM_THREADS;
36     }
37
38     /* (non-Javadoc)
39      * @see org.eclipse.jface.viewers.ViewerFilter#select(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
40      */

41     public boolean select(Viewer viewer, Object JavaDoc parentElement, Object JavaDoc element) {
42         if (!getValue()) {
43             
44             IJavaThread thread = getJavaThread(element);
45
46             if (thread != null) {
47                 try {
48                     // Show only non-system threads and suspended threads.
49
return !thread.isSystemThread() || thread.isSuspended();
50                 } catch (DebugException e) {
51                 }
52             }
53         }
54         return true;
55     }
56     
57     private IJavaThread getJavaThread(Object JavaDoc element) {
58         IJavaThread thread = null;
59
60         if (element instanceof IJavaThread)
61             thread = (IJavaThread) element;
62         else if (element instanceof IAdaptable)
63             thread = (IJavaThread) ((IAdaptable)element).getAdapter(IJavaThread.class);
64
65         return thread;
66     }
67
68     /* (non-Javadoc)
69      * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
70      */

71     public void init(IViewPart view) {
72         super.init(view);
73         DebugPlugin.getDefault().addDebugEventListener(this);
74     }
75     /* (non-Javadoc)
76      * @see org.eclipse.ui.IActionDelegate2#dispose()
77      */

78     public void dispose() {
79         super.dispose();
80         DebugPlugin.getDefault().removeDebugEventListener(this);
81     }
82
83     /* (non-Javadoc)
84      * @see org.eclipse.debug.core.IDebugEventSetListener#handleDebugEvents(org.eclipse.debug.core.DebugEvent[])
85      */

86     public void handleDebugEvents(DebugEvent[] events) {
87         if (getValue()) {
88             // if showing system threads, no need to worry about displaying/hinding
89
return;
90         }
91         for (int i = 0; i < events.length; i++) {
92             DebugEvent event = events[i];
93             switch (event.getKind()) {
94                 case DebugEvent.SUSPEND:
95                     if (event.getDetail() == DebugEvent.BREAKPOINT) {
96                         refresh(event.getSource(), true);
97                     }
98                     break;
99                 case DebugEvent.RESUME:
100                     if (event.getDetail() == DebugEvent.CLIENT_REQUEST) {
101                         refresh(event.getSource(), false);
102                     }
103                     break;
104             }
105         }
106     }
107     
108     private void refresh(Object JavaDoc source, final boolean select) {
109         final IJavaThread thread = getJavaThread(source);
110         if (thread != null) {
111             try {
112                 if (thread.isSystemThread()) {
113                     Runnable JavaDoc r = new Runnable JavaDoc() {
114                         public void run() {
115                             getStructuredViewer().refresh();
116                             if (select) {
117                                 Object JavaDoc tos;
118                                 try {
119                                     tos = thread.getTopStackFrame();
120                                     if (tos != null) {
121                                         getStructuredViewer().setSelection(new StructuredSelection(tos));
122                                     }
123                                 } catch (DebugException e) {
124                                 }
125                             }
126                         }
127                     };
128                     JDIDebugUIPlugin.getStandardDisplay().asyncExec(r);
129                     return;
130                 }
131             } catch (DebugException e) {
132             }
133         }
134     }
135 }
136
Popular Tags