KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > monitors > MonitorsView


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

11 package org.eclipse.jdt.internal.debug.ui.monitors;
12
13
14 import java.util.HashSet JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.core.runtime.jobs.Job;
22 import org.eclipse.debug.internal.ui.DebugUIPlugin;
23 import org.eclipse.debug.internal.ui.preferences.IDebugPreferenceConstants;
24 import org.eclipse.debug.internal.ui.views.AbstractDebugEventHandlerView;
25 import org.eclipse.debug.ui.IDebugUIConstants;
26 import org.eclipse.jdt.debug.core.IJavaDebugTarget;
27 import org.eclipse.jdt.internal.debug.core.model.JDIDebugElement;
28 import org.eclipse.jdt.internal.debug.ui.IJavaDebugHelpContextIds;
29 import org.eclipse.jface.action.IMenuManager;
30 import org.eclipse.jface.action.IToolBarManager;
31 import org.eclipse.jface.action.Separator;
32 import org.eclipse.jface.viewers.ISelection;
33 import org.eclipse.jface.viewers.IStructuredSelection;
34 import org.eclipse.jface.viewers.StructuredViewer;
35 import org.eclipse.jface.viewers.TreeViewer;
36 import org.eclipse.jface.viewers.Viewer;
37 import org.eclipse.swt.SWT;
38 import org.eclipse.swt.graphics.Color;
39 import org.eclipse.swt.widgets.Composite;
40 import org.eclipse.swt.widgets.Control;
41 import org.eclipse.swt.widgets.Display;
42 import org.eclipse.swt.widgets.Item;
43 import org.eclipse.swt.widgets.TreeItem;
44 import org.eclipse.ui.ISelectionListener;
45 import org.eclipse.ui.IWorkbenchActionConstants;
46 import org.eclipse.ui.IWorkbenchPart;
47 import org.eclipse.ui.part.Page;
48 import org.eclipse.ui.progress.IWorkbenchSiteProgressService;
49
50 /**
51  * Handles the different viewers: Thread, Monitor and Deadlock
52  */

53 public class MonitorsView extends AbstractDebugEventHandlerView implements ISelectionListener {
54
55     public static final int VIEW_ID_THREAD = 1;
56     public static final int VIEW_ID_MONITOR = 2;
57     public static final int VIEW_ID_DEADLOCK = 3;
58     
59     private int fViewId = VIEW_ID_MONITOR;
60     
61     private Viewer fDeadLocksViewer;
62     private Viewer fMonitorsViewer;
63     
64     private boolean fMonitorInformationAvailable= true;
65     
66     private boolean fValidSelection= false;
67     
68     private IJavaDebugTarget fLastSelectedTarget;
69     
70     /**
71      * A page in this view's page book that contains this
72      * view's viewer.
73      */

74     class MonitorsViewerPage extends Page {
75         
76         /* (non-Javadoc)
77          * @see org.eclipse.ui.part.IPage#createControl(org.eclipse.swt.widgets.Composite)
78          */

79         public void createControl(Composite parent) {
80             Viewer viewer = createMonitorsViewer(parent);
81             setMonitorsViewer(viewer);
82         }
83
84         /* (non-Javadoc)
85          * @see org.eclipse.ui.part.IPage#getControl()
86          */

87         public Control getControl() {
88             return getMonitorsViewer().getControl();
89         }
90
91         /* (non-Javadoc)
92          * @see org.eclipse.ui.part.IPage#setFocus()
93          */

94         public void setFocus() {
95             Viewer viewer= getMonitorsViewer();
96             if (viewer != null) {
97                 Control c = viewer.getControl();
98                 if (!c.isFocusControl()) {
99                     c.setFocus();
100                 }
101             }
102         }
103     }
104     
105     /**
106      * A page in this view's page book that contains this
107      * view's viewer.
108      */

109     class DeadLocksViewerPage extends Page {
110         
111         /* (non-Javadoc)
112          * @see org.eclipse.ui.part.IPage#createControl(org.eclipse.swt.widgets.Composite)
113          */

114         public void createControl(Composite parent) {
115             Viewer viewer = createDeadLocksViewer(parent);
116             setDeadLocksViewer(viewer);
117         }
118
119         /* (non-Javadoc)
120          * @see org.eclipse.ui.part.IPage#getControl()
121          */

122         public Control getControl() {
123             return getDeadLocksViewer().getControl();
124         }
125
126         /* (non-Javadoc)
127          * @see org.eclipse.ui.part.IPage#setFocus()
128          */

129         public void setFocus() {
130             Viewer viewer= getDeadLocksViewer();
131             if (viewer != null) {
132                 Control c = viewer.getControl();
133                 if (!c.isFocusControl()) {
134                     c.setFocus();
135                 }
136             }
137         }
138     }
139     
140     public MonitorsView(){
141         setEventHandler(new MonitorsDebugEventHandler(this));
142     }
143
144     /**
145      * Sets the current view.
146      * Must be called after creation of the viewpart.
147      */

148     public void setViewId(int viewerIndex) {
149         fViewId = viewerIndex;
150         refreshCurrentViewer(fMonitorInformationAvailable, true);
151     }
152     
153     /**
154      * Returns the current view id.
155      */

156     public int getViewId() {
157         return fViewId;
158     }
159
160     /* (non-Javadoc)
161      * @see org.eclipse.debug.ui.AbstractDebugView#createViewer(org.eclipse.swt.widgets.Composite)
162      */

163     protected Viewer createViewer(Composite parent) {
164         StructuredViewer threadViewer = new TreeViewer(parent, SWT.MULTI);
165         threadViewer.setContentProvider(new ThreadsViewContentProvider());
166         threadViewer.setLabelProvider(new MonitorModelPresentation());
167         threadViewer.setInput(MonitorManager.getDefault());
168         return threadViewer;
169     }
170
171     protected Viewer createMonitorsViewer(Composite parent) {
172         StructuredViewer monitorsViewer = new TreeViewer(parent, SWT.MULTI);
173         monitorsViewer.setContentProvider(new MonitorsViewContentProvider());
174         monitorsViewer.setLabelProvider(new MonitorModelPresentation());
175         monitorsViewer.setInput(MonitorManager.getDefault());
176         return monitorsViewer;
177     }
178
179     protected Viewer createDeadLocksViewer(Composite parent) {
180         StructuredViewer deadLocksViewer = new TreeViewer(parent, SWT.MULTI) {
181             //when refreshing, sets the color of the threads caught in a deadlock to red
182
public void refresh() {
183                 getControl().setRedraw(false);
184                 super.refresh();
185                 
186                 Item[] children = getChildren(getControl());
187                 if (children != null) {
188                     //to be changed
189
Color c= DebugUIPlugin.getPreferenceColor(IDebugPreferenceConstants.CHANGED_VARIABLE_COLOR);
190                     for (int i = 0; i < children.length; i++) {
191                         updateColor((TreeItem)children[i], c, 0);
192                     }
193                 }
194                 getControl().setRedraw(true);
195             }
196             
197             //goes down the tree, but only changes the color of the items caught in a deadlock
198
public void updateColor(TreeItem item, Color c, int count) {
199                 Object JavaDoc data= item.getData();
200                 if (data instanceof DeadLocksViewContentProvider.ContentThreadWrapper) {
201                     if(((DeadLocksViewContentProvider.ContentThreadWrapper)data).caughtInADeadLock) {
202                         item.setForeground(c);
203                     }
204                 }
205                 
206                 TreeItem[] children = item.getItems();
207                 for (int i = 0; i < children.length; i++) {
208                     updateColor(children[i], c, (count+1));
209                 }
210             }
211         };
212         deadLocksViewer.setContentProvider(new DeadLocksViewContentProvider());
213         deadLocksViewer.setLabelProvider(new MonitorModelPresentation());
214         deadLocksViewer.setInput(MonitorManager.getDefault());
215         return deadLocksViewer;
216     }
217
218     /* (non-Javadoc)
219      * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
220      */

221     public void createPartControl(Composite parent) {
222         super.createPartControl(parent);
223         // create the message page
224

225         DeadLocksViewerPage deadLocksPage = new DeadLocksViewerPage();
226         deadLocksPage.createControl(getPageBook());
227         initPage(deadLocksPage);
228         
229         MonitorsViewerPage monitorsViewerPage = new MonitorsViewerPage();
230         monitorsViewerPage.createControl(getPageBook());
231         initPage(monitorsViewerPage);
232
233         createContextMenu(getDeadLocksViewer().getControl());
234         createContextMenu(getMonitorsViewer().getControl());
235         
236         setViewId(VIEW_ID_MONITOR);
237
238         // listen to selection in debug view
239
getSite().getPage().addSelectionListener(IDebugUIConstants.ID_DEBUG_VIEW, this);
240     }
241
242     /* (non-Javadoc)
243      * @see org.eclipse.debug.ui.AbstractDebugView#createActions()
244      */

245     protected void createActions() {
246         
247     }
248
249     /* (non-Javadoc)
250      * @see org.eclipse.debug.ui.AbstractDebugView#getHelpContextId()
251      */

252     protected String JavaDoc getHelpContextId() {
253         return IJavaDebugHelpContextIds.MONITORS_VIEW;
254     }
255
256     /* (non-Javadoc)
257      * @see org.eclipse.debug.ui.AbstractDebugView#fillContextMenu(org.eclipse.jface.action.IMenuManager)
258      */

259     protected void fillContextMenu(IMenuManager menu) {
260         menu.add(new Separator("vmGroup")); //$NON-NLS-1$
261
menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
262     }
263
264     /* (non-Javadoc)
265      * @see org.eclipse.debug.ui.AbstractDebugView#configureToolBar(org.eclipse.jface.action.IToolBarManager)
266      */

267     protected void configureToolBar(IToolBarManager tbm) {
268         tbm.add(new Separator("vmGroup")); //$NON-NLS-1$
269
updateObjects();
270     }
271
272     /**
273      * Returns the deadLocksViewer.
274      * @return DeadLocksViewer
275      */

276     public Viewer getDeadLocksViewer() {
277         return fDeadLocksViewer;
278     }
279
280     /**
281      * Returns the monitorsViewer.
282      * @return MonitorsViewer
283      */

284     public Viewer getMonitorsViewer() {
285         return fMonitorsViewer;
286     }
287
288     /**
289      * Sets the deadLocksViewer.
290      * @param deadLocksViewer The deadLocksViewer to set
291      */

292     public void setDeadLocksViewer(Viewer deadLocksViewer) {
293         fDeadLocksViewer = deadLocksViewer;
294     }
295
296     /**
297      * Sets the monitorsViewer.
298      * @param monitorsViewer The monitorsViewer to set
299      */

300     public void setMonitorsViewer(Viewer monitorsViewer) {
301         fMonitorsViewer = monitorsViewer;
302     }
303
304     protected void refreshCurrentViewer(boolean monitorInformationAvailable, boolean showPage) {
305         if (getPageBook().isDisposed()) {
306             return;
307         }
308         if (!fValidSelection) {
309             showMessage(MonitorMessages.getString("MonitorsView.2")); //$NON-NLS-1$
310
updateObjects();
311             return;
312         }
313         boolean changeFromShowMessagePage= monitorInformationAvailable && !fMonitorInformationAvailable;
314         fMonitorInformationAvailable= monitorInformationAvailable;
315         if (!monitorInformationAvailable) {
316             showMessage(MonitorMessages.getString("MonitorsView.1")); //$NON-NLS-1$
317
updateObjects();
318             return;
319         }
320         Control page= null;
321         switch (fViewId) {
322             case VIEW_ID_THREAD:
323                 page= getViewer().getControl();
324                 page.setRedraw(false);
325                 getViewer().refresh();
326                 ((TreeViewer)getViewer()).expandAll();
327                 page.setRedraw(true);
328                 break;
329             case VIEW_ID_DEADLOCK:
330                 if(MonitorManager.getDefault().getNumberOfDeadlocks() == 0 && MonitorManager.getDefault().getThreads().length > 0) {
331                     showMessage(MonitorMessages.getString("MonitorsView.3")); //$NON-NLS-1$
332
showPage= false;
333                     break;
334                 }
335                 changeFromShowMessagePage= true;
336                 page= getDeadLocksViewer().getControl();
337                 page.setRedraw(false);
338                 getDeadLocksViewer().refresh();
339                 ((TreeViewer)getDeadLocksViewer()).expandAll();
340                 page.setRedraw(true);
341                 break;
342             case VIEW_ID_MONITOR:
343                 page= getMonitorsViewer().getControl();
344                 page.setRedraw(false);
345                 getMonitorsViewer().refresh();
346                 ((TreeViewer)getMonitorsViewer()).expandAll();
347                 page.setRedraw(true);
348                 break;
349         }
350         if ((showPage | changeFromShowMessagePage) && page != null) {
351             getPageBook().showPage(page);
352         }
353         updateObjects();
354     }
355
356     /* (non-Javadoc)
357      * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
358      */

359     public void selectionChanged(IWorkbenchPart part, ISelection selection) {
360         if (selection instanceof IStructuredSelection) {
361             IStructuredSelection structuredSelection= (IStructuredSelection)selection;
362             Set JavaDoc targets= new HashSet JavaDoc();
363             for (Iterator JavaDoc iter= structuredSelection.iterator(); iter.hasNext();) {
364                 Object JavaDoc element= iter.next();
365                 if (element instanceof JDIDebugElement) {
366                     targets.add(((JDIDebugElement)element).getDebugTarget());
367                 }
368             }
369             if (targets.size() == 1) {
370                 final IJavaDebugTarget debugTarget= (IJavaDebugTarget)targets.toArray()[0];
371                 if (debugTarget != fLastSelectedTarget || !fValidSelection) {
372                     Job job = new Job(MonitorMessages.getString("MonitorsView.4")) { //$NON-NLS-1$
373
protected IStatus run(IProgressMonitor monitor) {
374                             final boolean monitorInformationAvailable= debugTarget.supportsMonitorInformation();
375                             if (monitorInformationAvailable) {
376                                 MonitorManager.getDefault().updatePartial(debugTarget);
377                             }
378                             fValidSelection= true;
379                             Display.getDefault().asyncExec(new Runnable JavaDoc() {
380                                 public void run() {
381                                     refreshCurrentViewer(monitorInformationAvailable, true);
382                                     fLastSelectedTarget= debugTarget;
383                                 }
384                             });
385                             
386                             return Status.OK_STATUS;
387                         }
388                     };
389                     IWorkbenchSiteProgressService progressService = (IWorkbenchSiteProgressService) getSite().getAdapter(IWorkbenchSiteProgressService.class);
390                     job.setSystem(true);
391                     if (progressService == null) {
392                         job.schedule();
393                     } else {
394                         progressService.schedule(job);
395                     }
396                 }
397                 return;
398             }
399         }
400         if (fValidSelection) {
401             Display.getDefault().asyncExec(new Runnable JavaDoc() {
402                 public void run() {
403                     fValidSelection= false;
404                     refreshCurrentViewer(false, true);
405                 }
406             });
407         }
408     }
409     
410     /* (non-Javadoc)
411      * @see org.eclipse.debug.internal.ui.views.AbstractDebugEventHandlerView#dispose()
412      */

413     public void dispose() {
414         getSite().getPage().removeSelectionListener(IDebugUIConstants.ID_DEBUG_VIEW, this);
415         super.dispose();
416     }
417
418 }
419
Popular Tags