|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2007 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.evaluation; |
| 12 |
|
| 13 |
import java.util.HashMap; |
| 14 |
import java.util.Map; |
| 15 |
|
| 16 |
import org.eclipse.core.runtime.IAdaptable; |
| 17 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 18 |
import org.eclipse.core.runtime.IStatus; |
| 19 |
import org.eclipse.core.runtime.Platform; |
| 20 |
import org.eclipse.core.runtime.Status; |
| 21 |
import org.eclipse.core.runtime.jobs.Job; |
| 22 |
import org.eclipse.debug.ui.DebugUITools; |
| 23 |
import org.eclipse.debug.ui.contexts.DebugContextEvent; |
| 24 |
import org.eclipse.debug.ui.contexts.IDebugContextListener; |
| 25 |
import org.eclipse.debug.ui.contexts.IDebugContextProvider; |
| 26 |
import org.eclipse.jdt.debug.core.IJavaStackFrame; |
| 27 |
import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin; |
| 28 |
import org.eclipse.jface.viewers.ISelection; |
| 29 |
import org.eclipse.jface.viewers.IStructuredSelection; |
| 30 |
import org.eclipse.ui.IWorkbenchPart; |
| 31 |
import org.eclipse.ui.IWorkbenchPartSite; |
| 32 |
import org.eclipse.ui.IWorkbenchWindow; |
| 33 |
import org.eclipse.ui.contexts.IContextActivation; |
| 34 |
import org.eclipse.ui.contexts.IContextService; |
| 35 |
import org.eclipse.ui.progress.UIJob; |
| 36 |
|
| 37 |
/** |
| 38 |
* @since 3.3 |
| 39 |
*/ |
| 40 |
public class EvaluationContextManager2 implements IDebugContextListener { |
| 41 |
|
| 42 |
/** |
| 43 |
* Context that is enabled when there is a valid context for performing |
| 44 |
* an expression evaluation. |
| 45 |
*/ |
| 46 |
private static final String EVALUATION_CONTEXT_ID = "org.eclipse.jdt.debug.ui.evaluation"; //$NON-NLS-1$ |
| 47 |
|
| 48 |
/** |
| 49 |
* Debug flag for tracing information |
| 50 |
*/ |
| 51 |
private static boolean DEBUG_EVALUATION_CONTEXT_MANAGER = false; |
| 52 |
|
| 53 |
static { |
| 54 |
DEBUG_EVALUATION_CONTEXT_MANAGER = JDIDebugUIPlugin.DEBUG && "true".equals( //$NON-NLS-1$ |
| 55 |
Platform.getDebugOption("org.eclipse.jdt.debug.ui/debug/evaluation/contextManager")); //$NON-NLS-1$ |
| 56 |
} |
| 57 |
|
| 58 |
class UpdateJobFamily { |
| 59 |
public UpdateJobFamily(IWorkbenchWindow window) { |
| 60 |
fWindow = window; |
| 61 |
} |
| 62 |
public IWorkbenchWindow fWindow; |
| 63 |
} |
| 64 |
|
| 65 |
class UpdateContextJob extends Job { |
| 66 |
|
| 67 |
private DebugContextEvent fEvent; |
| 68 |
private IWorkbenchWindow fWindow; |
| 69 |
|
| 70 |
/** |
| 71 |
* @param name |
| 72 |
*/ |
| 73 |
public UpdateContextJob(DebugContextEvent event, IWorkbenchWindow window) { |
| 74 |
super("Update expression evaluation context"); //$NON-NLS-1$ |
| 75 |
fEvent = event; |
| 76 |
fWindow = window; |
| 77 |
setSystem(true); |
| 78 |
} |
| 79 |
|
| 80 |
/* (non-Javadoc) |
| 81 |
* @see org.eclipse.core.runtime.jobs.Job#belongsTo(java.lang.Object) |
| 82 |
*/ |
| 83 |
public boolean belongsTo(Object family) { |
| 84 |
if (family instanceof UpdateJobFamily) { |
| 85 |
UpdateJobFamily ujf = (UpdateJobFamily) family; |
| 86 |
return ujf.fWindow.equals(fWindow); |
| 87 |
} |
| 88 |
return false; |
| 89 |
} |
| 90 |
|
| 91 |
/* (non-Javadoc) |
| 92 |
* @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor) |
| 93 |
*/ |
| 94 |
protected IStatus run(IProgressMonitor monitor) { |
| 95 |
if (DEBUG_EVALUATION_CONTEXT_MANAGER) { |
| 96 |
System.out.println("[Evaluation] Updating enablement"); //$NON-NLS-1$ |
| 97 |
} |
| 98 |
boolean activate = false; |
| 99 |
if (!monitor.isCanceled()) { |
| 100 |
ISelection context = fEvent.getContext(); |
| 101 |
if (context instanceof IStructuredSelection) { |
| 102 |
IStructuredSelection ss = (IStructuredSelection) context; |
| 103 |
Object firstElement = ss.getFirstElement(); |
| 104 |
if (firstElement instanceof IAdaptable) { |
| 105 |
IAdaptable adaptable = (IAdaptable) firstElement; |
| 106 |
Object adapter = adaptable.getAdapter(IJavaStackFrame.class); |
| 107 |
if (adapter != null) { |
| 108 |
// TODO: scrap book |
| 109 |
activate = true; |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
IContextService service = (IContextService) fWindow.getService(IContextService.class); |
| 115 |
if (service != null) { |
| 116 |
if (activate) { |
| 117 |
if (!service.getActiveContextIds().contains(EVALUATION_CONTEXT_ID)) { |
| 118 |
if (DEBUG_EVALUATION_CONTEXT_MANAGER) { |
| 119 |
System.out.println("[Evaluation]\tSchedule activation"); //$NON-NLS-1$ |
| 120 |
} |
| 121 |
Job job = new PerformActivation(service); |
| 122 |
job.schedule(); |
| 123 |
} else { |
| 124 |
if (DEBUG_EVALUATION_CONTEXT_MANAGER) { |
| 125 |
System.out.println("[Evaluation]\tStill enabled"); //$NON-NLS-1$ |
| 126 |
} |
| 127 |
} |
| 128 |
} else { |
| 129 |
IContextActivation activation = (IContextActivation) fActivations.remove(service); |
| 130 |
if (activation != null) { |
| 131 |
if (DEBUG_EVALUATION_CONTEXT_MANAGER) { |
| 132 |
System.out.println("[Evaluation]\tSchedule deactivation"); //$NON-NLS-1$ |
| 133 |
} |
| 134 |
Job job = new PerformDeactivation(activation); |
| 135 |
job.schedule(); |
| 136 |
} else { |
| 137 |
if (DEBUG_EVALUATION_CONTEXT_MANAGER) { |
| 138 |
System.out.println("[Evaluation]\tStill deactivated"); //$NON-NLS-1$ |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
return Status.OK_STATUS; |
| 144 |
} |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Map of context activations per service |
| 150 |
*/ |
| 151 |
private Map fActivations = new HashMap(); |
| 152 |
|
| 153 |
class PerformActivation extends UIJob { |
| 154 |
|
| 155 |
private IContextService fService; |
| 156 |
|
| 157 |
/** |
| 158 |
* @param name |
| 159 |
*/ |
| 160 |
public PerformActivation(IContextService service) { |
| 161 |
super("Activate evaluation context"); //$NON-NLS-1$ |
| 162 |
fService = service; |
| 163 |
setSystem(true); |
| 164 |
} |
| 165 |
|
| 166 |
/* (non-Javadoc) |
| 167 |
* @see org.eclipse.ui.progress.UIJob#runInUIThread(org.eclipse.core.runtime.IProgressMonitor) |
| 168 |
*/ |
| 169 |
public IStatus runInUIThread(IProgressMonitor monitor) { |
| 170 |
if (DEBUG_EVALUATION_CONTEXT_MANAGER) { |
| 171 |
System.out.println("[Evaluation] Activate " + EVALUATION_CONTEXT_ID); //$NON-NLS-1$ |
| 172 |
} |
| 173 |
IContextActivation activation = fService.activateContext(EVALUATION_CONTEXT_ID); |
| 174 |
fActivations.put(fService, activation); |
| 175 |
return Status.OK_STATUS; |
| 176 |
} |
| 177 |
|
| 178 |
} |
| 179 |
|
| 180 |
class PerformDeactivation extends UIJob { |
| 181 |
|
| 182 |
private IContextActivation fActivation; |
| 183 |
|
| 184 |
/** |
| 185 |
* @param name |
| 186 |
*/ |
| 187 |
public PerformDeactivation(IContextActivation activation) { |
| 188 |
super("Deactivate evaluation context"); //$NON-NLS-1$ |
| 189 |
fActivation = activation; |
| 190 |
setSystem(true); |
| 191 |
} |
| 192 |
|
| 193 |
/* (non-Javadoc) |
| 194 |
* @see org.eclipse.ui.progress.UIJob#runInUIThread(org.eclipse.core.runtime.IProgressMonitor) |
| 195 |
*/ |
| 196 |
public IStatus runInUIThread(IProgressMonitor monitor) { |
| 197 |
if (DEBUG_EVALUATION_CONTEXT_MANAGER) { |
| 198 |
System.out.println("Evaluation] Deactivate " + EVALUATION_CONTEXT_ID); //$NON-NLS-1$ |
| 199 |
} |
| 200 |
fActivation.getContextService().deactivateContext(fActivation); |
| 201 |
return Status.OK_STATUS; |
| 202 |
} |
| 203 |
|
| 204 |
} |
| 205 |
|
| 206 |
/* (non-Javadoc) |
| 207 |
* @see org.eclipse.debug.ui.contexts.IDebugContextListener#debugContextChanged(org.eclipse.debug.ui.contexts.DebugContextEvent) |
| 208 |
*/ |
| 209 |
public void debugContextChanged(DebugContextEvent event) { |
| 210 |
if ((event.getFlags() & DebugContextEvent.ACTIVATED) > 0) { |
| 211 |
IDebugContextProvider provider = (IDebugContextProvider) event.getSource(); |
| 212 |
IWorkbenchPart part = provider.getPart(); |
| 213 |
if (part != null) { |
| 214 |
IWorkbenchPartSite site = part.getSite(); |
| 215 |
if (site != null) { |
| 216 |
IWorkbenchWindow window = site.getWorkbenchWindow(); |
| 217 |
if (window != null) { |
| 218 |
Job.getJobManager().cancel(new UpdateJobFamily(window)); |
| 219 |
Job job = new UpdateContextJob(event, window); |
| 220 |
job.schedule(100); |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
public static void startup() { |
| 228 |
EvaluationContextManager2 manager = new EvaluationContextManager2(); |
| 229 |
DebugUITools.getDebugContextManager().addDebugContextListener(manager); |
| 230 |
} |
| 231 |
|
| 232 |
} |