KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > propertypages > ThreadFilterEditor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.propertypages;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.debug.core.DebugException;
18 import org.eclipse.debug.core.DebugPlugin;
19 import org.eclipse.debug.core.ILaunch;
20 import org.eclipse.debug.core.ILaunchManager;
21 import org.eclipse.debug.core.model.IDebugElement;
22 import org.eclipse.debug.core.model.IDebugTarget;
23 import org.eclipse.debug.core.model.IStackFrame;
24 import org.eclipse.debug.core.model.IThread;
25 import org.eclipse.debug.ui.DebugUITools;
26 import org.eclipse.jdt.debug.core.IJavaDebugTarget;
27 import org.eclipse.jdt.debug.core.IJavaThread;
28 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
29 import org.eclipse.jface.viewers.AbstractTreeViewer;
30 import org.eclipse.jface.viewers.CheckStateChangedEvent;
31 import org.eclipse.jface.viewers.CheckboxTreeViewer;
32 import org.eclipse.jface.viewers.ICheckStateListener;
33 import org.eclipse.jface.viewers.ITreeContentProvider;
34 import org.eclipse.jface.viewers.Viewer;
35 import org.eclipse.swt.SWT;
36 import org.eclipse.swt.layout.GridData;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.Label;
39
40 /**
41  *
42  */

43 public class ThreadFilterEditor {
44
45     private JavaBreakpointAdvancedPage fPage;
46     private CheckboxTreeViewer fThreadViewer;
47     private ThreadFilterContentProvider fContentProvider;
48     private CheckHandler fCheckHandler;
49     private static String JavaDoc MAIN= "main"; //$NON-NLS-1$
50

51     public ThreadFilterEditor(Composite parent, JavaBreakpointAdvancedPage page) {
52         fPage= page;
53         fContentProvider= new ThreadFilterContentProvider();
54         fCheckHandler= new CheckHandler();
55         createThreadViewer(parent);
56     }
57     
58     private void createThreadViewer(Composite parent) {
59         Label label= new Label(parent, SWT.NONE);
60         label.setText(PropertyPageMessages.ThreadFilterEditor_1);
61         label.setFont(parent.getFont());
62         label.setLayoutData(new GridData());
63         
64         GridData data= new GridData(GridData.FILL_BOTH);
65         data.heightHint= 100;
66         fThreadViewer= new CheckboxTreeViewer(parent, SWT.BORDER);
67         fThreadViewer.addCheckStateListener(fCheckHandler);
68         fThreadViewer.getTree().setLayoutData(data);
69         fThreadViewer.getTree().setFont(parent.getFont());
70         fThreadViewer.setContentProvider(fContentProvider);
71         fThreadViewer.setLabelProvider(DebugUITools.newDebugModelPresentation());
72         fThreadViewer.setInput(DebugPlugin.getDefault().getLaunchManager());
73         setInitialCheckedState();
74     }
75     
76     protected void doStore() {
77         IDebugTarget[] targets= getDebugTargets();
78         IJavaDebugTarget target;
79         IThread[] threads;
80         IJavaThread thread;
81         for (int i= 0, numTargets= targets.length; i < numTargets; i++) {
82             target = (IJavaDebugTarget)targets[i].getAdapter(IJavaDebugTarget.class);
83             if (target != null) {
84                 try {
85                     if (fThreadViewer.getChecked(target)) {
86                         threads= target.getThreads();
87                         for (int j=0, numThreads= threads.length; j < numThreads; j++) {
88                             thread= (IJavaThread)threads[j];
89                             if (fThreadViewer.getChecked(thread)) {
90                                 // thread selected for filtering
91
fPage.getBreakpoint().setThreadFilter(thread);
92                                 break; // Can only set one filtered thread.
93
}
94                         }
95                     } else {
96                         fPage.getBreakpoint().removeThreadFilter(target);
97                     }
98                 } catch (CoreException e) {
99                     JDIDebugUIPlugin.log(e);
100                 }
101             }
102         }
103     }
104
105     /**
106      * Sets the initial checked state of the tree viewer.
107      * The initial state should reflect the current state
108      * of the breakpoint. If the breakpoint has a thread
109      * filter in a given thread, that thread should be
110      * checked.
111      */

112     protected void setInitialCheckedState() {
113         try {
114             IDebugTarget[] targets= getDebugTargets();
115             for (int i= 0, numTargets= targets.length; i < numTargets; i++) {
116                 IJavaDebugTarget target = (IJavaDebugTarget)targets[i].getAdapter(IJavaDebugTarget.class);
117                 if (target != null) {
118                     IJavaThread filteredThread= fPage.getBreakpoint().getThreadFilter(target);
119                     if (filteredThread != null) {
120                         fCheckHandler.checkThread(filteredThread, true);
121                     }
122                 }
123             }
124         } catch (CoreException e) {
125             JDIDebugUIPlugin.log(e);
126         }
127     }
128     
129     /**
130      * Returns the debug targets that appear in the tree
131      */

132     protected IDebugTarget[] getDebugTargets() {
133         Object JavaDoc input= fThreadViewer.getInput();
134         if (!(input instanceof ILaunchManager)) {
135             return new IJavaDebugTarget[0];
136         }
137         ILaunchManager launchManager= (ILaunchManager)input;
138         return launchManager.getDebugTargets();
139     }
140     
141     class CheckHandler implements ICheckStateListener {
142         public void checkStateChanged(CheckStateChangedEvent event) {
143             Object JavaDoc element= event.getElement();
144             if (element instanceof IDebugTarget) {
145                 checkTarget((IDebugTarget)element, event.getChecked());
146             } else if (element instanceof IThread) {
147                 checkThread((IThread)element, event.getChecked());
148             }
149             verifyCheckedState();
150         }
151         
152         /**
153          * Check or uncheck a debug target in the tree viewer.
154          * When a debug target is checked, attempt to
155          * check one of the target's threads by default.
156          * When a debug target is unchecked, uncheck all
157          * its threads.
158          */

159         protected void checkTarget(IDebugTarget target, boolean checked) {
160             fThreadViewer.setChecked(target, checked);
161             if (checked) {
162                 fThreadViewer.expandToLevel(target, AbstractTreeViewer.ALL_LEVELS);
163                 IThread[] threads;
164                 try {
165                     threads= target.getThreads();
166                 } catch (DebugException exception) {
167                     JDIDebugUIPlugin.log(exception);
168                     return;
169                 }
170                 IThread thread;
171                 boolean checkedThread= false;
172                 // Try to check the "main" thread by default
173
for (int i= 0, numThreads= threads.length; i < numThreads; i++) {
174                     thread= threads[i];
175                     String JavaDoc name= null;
176                     try {
177                         name= thread.getName();
178                     } catch (DebugException exception) {
179                         JDIDebugUIPlugin.log(exception);
180                     }
181                     if (MAIN.equals(name)) {
182                         checkedThread= fThreadViewer.setChecked(thread, true);
183                     }
184                 }
185                 // If the main thread couldn't be checked, check the first
186
// available thread
187
if (!checkedThread) {
188                     for (int i= 0, numThreads= threads.length; i < numThreads; i++) {
189                         if (fThreadViewer.setChecked(threads[i], true)) {
190                             break;
191                         }
192                     }
193                 }
194             } else { // Unchecked
195
IThread[] threads;
196                 try {
197                     threads= target.getThreads();
198                 } catch (DebugException exception) {
199                     JDIDebugUIPlugin.log(exception);
200                     return;
201                 }
202                 for (int i= 0, numThreads= threads.length; i < numThreads; i++) {
203                     fThreadViewer.setChecked(threads[i], false);
204                 }
205             }
206         }
207     
208         /**
209          * Check or uncheck a thread.
210          * When a thread is checked, make sure its debug
211          * target is also checked.
212          * When a thread is unchecked, uncheck its debug
213          * target.
214          */

215         protected void checkThread(IThread thread, boolean checked) {
216             fThreadViewer.setChecked(thread, checked);
217             IDebugTarget target= (thread).getDebugTarget();
218             if (checked) {
219                 // When a thread is checked, make sure the target
220
// is checked and all other threads are
221
// unchecked (simulate radio button behavior)
222
if (!fThreadViewer.getChecked(target)) {
223                     fThreadViewer.setChecked(target, true);
224                 }
225                 IThread[] threads;
226                 try {
227                     threads= target.getThreads();
228                 } catch (DebugException exception) {
229                     JDIDebugUIPlugin.log(exception);
230                     return;
231                 }
232                 for (int i= 0, numThreads= threads.length; i < numThreads; i++) {
233                     if (threads[i] != thread) {
234                         // Uncheck all threads other than the selected thread
235
fThreadViewer.setChecked(threads[i], false);
236                     }
237                 }
238             } else {
239                 // When a thread is unchecked, uncheck the target
240
fThreadViewer.setChecked(target, false);
241             }
242         }
243     
244         /**
245          * Verify the state of the tree viewer.
246          * If the user selects a debug target, they must select
247          * a thread.
248          */

249         protected void verifyCheckedState() {
250             IDebugTarget[] targets= getDebugTargets();
251             IDebugTarget target;
252             IThread[] threads;
253             boolean checkedThread;
254             for (int i= 0, numTargets= targets.length; i < numTargets; i++) {
255                 target= targets[i];
256                 if (!fThreadViewer.getChecked(target)) {
257                     continue;
258                 }
259                 try {
260                     threads= target.getThreads();
261                 } catch (DebugException exception) {
262                     JDIDebugUIPlugin.log(exception);
263                     continue;
264                 }
265                 checkedThread= false;
266                 for (int j= 0, numThreads= threads.length; j < numThreads; j++) {
267                     if (fThreadViewer.getChecked(threads[j])) {
268                         checkedThread= true;
269                         break;
270                     }
271                 }
272                 if (checkedThread) {
273                     fPage.setErrorMessage(null);
274                 } else {
275                     fPage.setErrorMessage(PropertyPageMessages.ThreadFilterEditor_2);
276                 }
277             }
278         }
279         
280     }
281     
282     class ThreadFilterContentProvider implements ITreeContentProvider {
283         /**
284          * @see ITreeContentProvider#getChildren(Object)
285          */

286         public Object JavaDoc[] getChildren(Object JavaDoc parent) {
287             if (parent instanceof IDebugTarget) {
288                 IJavaDebugTarget target = (IJavaDebugTarget)((IDebugTarget)parent).getAdapter(IJavaDebugTarget.class);
289                 if (target != null) {
290                     try {
291                         return ((IJavaDebugTarget)parent).getThreads();
292                     } catch (DebugException e) {
293                         JDIDebugUIPlugin.log(e);
294                     }
295                 }
296             }
297             if (parent instanceof ILaunchManager) {
298                 List JavaDoc children= new ArrayList JavaDoc();
299                 ILaunch[] launches= ((ILaunchManager) parent).getLaunches();
300                 IDebugTarget[] targets;
301                 IJavaDebugTarget target;
302                 for (int i= 0, numLaunches= launches.length; i < numLaunches; i++) {
303                     targets= launches[i].getDebugTargets();
304                     for (int j= 0, numTargets= targets.length; j < numTargets; j++) {
305                         target= (IJavaDebugTarget)targets[j].getAdapter(IJavaDebugTarget.class);
306                         if (target != null && !target.isDisconnected() && !target.isTerminated()) {
307                             children.add(target);
308                         }
309                     }
310                 }
311                 return children.toArray();
312             }
313             return new Object JavaDoc[0];
314         }
315
316         /**
317          * @see ITreeContentProvider#getParent(Object)
318          */

319         public Object JavaDoc getParent(Object JavaDoc element) {
320             if (element instanceof IThread) {
321                 return ((IThread)element).getDebugTarget();
322             }
323             if (element instanceof IDebugTarget) {
324                 return ((IDebugElement)element).getLaunch();
325             }
326             if (element instanceof ILaunch) {
327                 return DebugPlugin.getDefault().getLaunchManager();
328             }
329             return null;
330         }
331
332         /**
333          * @see ITreeContentProvider#hasChildren(Object)
334          */

335         public boolean hasChildren(Object JavaDoc element) {
336             if (element instanceof IStackFrame) {
337                 return false;
338             }
339             if (element instanceof IDebugElement) {
340                 return getChildren(element).length > 0;
341             }
342             if (element instanceof ILaunch) {
343                 return true;
344             }
345             if (element instanceof ILaunchManager) {
346                 return ((ILaunchManager) element).getLaunches().length > 0;
347             }
348             return false;
349         }
350
351         /**
352          * @see IStructuredContentProvider#getElements(Object)
353          */

354         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
355             return getChildren(inputElement);
356         }
357
358         /**
359          * @see IContentProvider#dispose()
360          */

361         public void dispose() {
362         }
363
364         /**
365          * @see IContentProvider#inputChanged(Viewer, Object, Object)
366          */

367         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
368         }
369     }
370 }
371
Popular Tags