KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > viewers > update > DefaultWatchExpressionModelProxy


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.debug.internal.ui.viewers.update;
12
13 import org.eclipse.core.runtime.IProgressMonitor;
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.core.runtime.Status;
16 import org.eclipse.debug.core.ILaunch;
17 import org.eclipse.debug.core.model.IDebugElement;
18 import org.eclipse.debug.core.model.IWatchExpression;
19 import org.eclipse.debug.internal.ui.DebugUIPlugin;
20 import org.eclipse.debug.ui.DebugUITools;
21 import org.eclipse.debug.ui.contexts.DebugContextEvent;
22 import org.eclipse.debug.ui.contexts.IDebugContextListener;
23 import org.eclipse.debug.ui.contexts.IDebugContextService;
24 import org.eclipse.jface.viewers.ISelection;
25 import org.eclipse.jface.viewers.IStructuredSelection;
26 import org.eclipse.jface.viewers.Viewer;
27 import org.eclipse.ui.IWorkbenchWindow;
28 import org.eclipse.ui.PlatformUI;
29 import org.eclipse.ui.progress.UIJob;
30
31 /**
32  * @since 3.2
33  *
34  */

35 public class DefaultWatchExpressionModelProxy extends DefaultExpressionModelProxy implements IDebugContextListener {
36     
37     private IWorkbenchWindow fWindow;
38     
39     public DefaultWatchExpressionModelProxy(IWatchExpression expression) {
40         super(expression);
41     }
42     
43     /* (non-Javadoc)
44      * @see org.eclipse.debug.internal.ui.viewers.provisional.AbstractModelProxy#installed(org.eclipse.jface.viewers.Viewer)
45      */

46     public void installed(final Viewer viewer) {
47         super.installed(viewer);
48         UIJob job = new UIJob("install watch expression model proxy") { //$NON-NLS-1$
49
public IStatus runInUIThread(IProgressMonitor monitor) {
50                 IWorkbenchWindow[] workbenchWindows = PlatformUI.getWorkbench().getWorkbenchWindows();
51                 for (int i = 0; i < workbenchWindows.length; i++) {
52                     IWorkbenchWindow window = workbenchWindows[i];
53                     if (viewer.getControl().getShell().equals(window.getShell())) {
54                         fWindow = window;
55                         break;
56                     }
57                 }
58                 if (fWindow == null) {
59                     fWindow = DebugUIPlugin.getActiveWorkbenchWindow();
60                 }
61                 IDebugContextService contextService = DebugUITools.getDebugContextManager().getContextService(fWindow);
62                 contextService.addDebugContextListener(DefaultWatchExpressionModelProxy.this);
63                 ISelection activeContext = contextService.getActiveContext();
64                 if (activeContext != null) {
65                     contextActivated(activeContext);
66                 }
67                 return Status.OK_STATUS;
68             }
69         
70         };
71         job.setSystem(true);
72         job.schedule();
73     }
74
75     /* (non-Javadoc)
76      * @see org.eclipse.debug.internal.ui.viewers.update.DefaultExpressionModelProxy#dispose()
77      */

78     public synchronized void dispose() {
79         super.dispose();
80         DebugUITools.getDebugContextManager().getContextService(fWindow).removeDebugContextListener(this);
81         fWindow = null;
82     }
83
84     /* (non-Javadoc)
85      * @see org.eclipse.debug.internal.ui.viewers.update.EventHandlerModelProxy#createEventHandlers()
86      */

87     protected DebugEventHandler[] createEventHandlers() {
88         return new DebugEventHandler[]{new ExpressionEventHandler(this)};
89     }
90
91     protected void contextActivated(ISelection selection) {
92         if (fWindow != null) {
93             if (selection instanceof IStructuredSelection) {
94                 IDebugElement context = null;
95                 IStructuredSelection ss = (IStructuredSelection)selection;
96                 if (ss.size() < 2) {
97                     Object JavaDoc object = ss.getFirstElement();
98                     if (object instanceof IDebugElement) {
99                         context= (IDebugElement) object;
100                     } else if (object instanceof ILaunch) {
101                         context= ((ILaunch) object).getDebugTarget();
102                     }
103                 }
104                 ((IWatchExpression)getExpression()).setExpressionContext(context);
105             }
106         }
107     }
108
109     public void debugContextChanged(DebugContextEvent event) {
110         if ((event.getFlags() & DebugContextEvent.ACTIVATED) > 0) {
111             contextActivated(event.getContext());
112         }
113     }
114     
115 }
116
Popular Tags