mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
add scheduling rule to WorkbenchRunnableAdapter
This commit is contained in:
parent
d38225d581
commit
3da6b13c88
1 changed files with 63 additions and 11 deletions
|
@ -1,38 +1,58 @@
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
* Copyright (c) 2001 Rational Software Corp. and others.
|
* Copyright (c) 2000, 2004 IBM Corporation and others.
|
||||||
* All rights reserved. This program and the accompanying materials
|
* All rights reserved. This program and the accompanying materials
|
||||||
* are made available under the terms of the Common Public License v0.5
|
* are made available under the terms of the Common Public License v1.0
|
||||||
* which accompanies this distribution, and is available at
|
* which accompanies this distribution, and is available at
|
||||||
* http://www.eclipse.org/legal/cpl-v05.html
|
* http://www.eclipse.org/legal/cpl-v10.html
|
||||||
*
|
*
|
||||||
* Contributors:
|
* Contributors:
|
||||||
* Rational Software - initial implementation
|
* IBM Corporation - initial API and implementation
|
||||||
******************************************************************************/
|
*******************************************************************************/
|
||||||
package org.eclipse.cdt.internal.ui.actions;
|
package org.eclipse.cdt.internal.ui.actions;
|
||||||
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
|
||||||
|
import org.eclipse.cdt.core.model.CoreModel;
|
||||||
|
import org.eclipse.cdt.internal.ui.CUIStatus;
|
||||||
import org.eclipse.core.resources.IWorkspaceRunnable;
|
import org.eclipse.core.resources.IWorkspaceRunnable;
|
||||||
|
import org.eclipse.core.resources.ResourcesPlugin;
|
||||||
import org.eclipse.core.runtime.CoreException;
|
import org.eclipse.core.runtime.CoreException;
|
||||||
import org.eclipse.core.runtime.IProgressMonitor;
|
import org.eclipse.core.runtime.IProgressMonitor;
|
||||||
|
import org.eclipse.core.runtime.IStatus;
|
||||||
import org.eclipse.core.runtime.OperationCanceledException;
|
import org.eclipse.core.runtime.OperationCanceledException;
|
||||||
|
import org.eclipse.core.runtime.Status;
|
||||||
|
import org.eclipse.core.runtime.jobs.ISchedulingRule;
|
||||||
|
import org.eclipse.core.runtime.jobs.Job;
|
||||||
|
|
||||||
import org.eclipse.jface.operation.IRunnableWithProgress;
|
import org.eclipse.jface.operation.IRunnableWithProgress;
|
||||||
|
|
||||||
import org.eclipse.cdt.core.model.CoreModel;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An <code>IRunnableWithProgress</code> that adapts and <code>IWorkspaceRunnable</code>
|
* An <code>IRunnableWithProgress</code> that adapts and <code>IWorkspaceRunnable</code>
|
||||||
* so that is can be executed inside <code>IRunnableContext</code>. <code>OperationCanceledException</code>
|
* so that is can be executed inside <code>IRunnableContext</code>. <code>OperationCanceledException</code>
|
||||||
* thrown by the apapted runnabled are cought and rethrown as a <code>InterruptedException</code>.
|
* thrown by the adapted runnable are caught and re-thrown as a <code>InterruptedException</code>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class WorkbenchRunnableAdapter implements IRunnableWithProgress {
|
public class WorkbenchRunnableAdapter implements IRunnableWithProgress {
|
||||||
|
|
||||||
private IWorkspaceRunnable fWorkspaceRunnable;
|
private IWorkspaceRunnable fWorkspaceRunnable;
|
||||||
|
private ISchedulingRule fRule;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Runs a workspace runnable with the workspace lock.
|
||||||
|
*/
|
||||||
public WorkbenchRunnableAdapter(IWorkspaceRunnable runnable) {
|
public WorkbenchRunnableAdapter(IWorkspaceRunnable runnable) {
|
||||||
|
this(runnable, ResourcesPlugin.getWorkspace().getRoot());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Runs a workspace runnable with the given lock or <code>null</code> to run with no lock at all.
|
||||||
|
*/
|
||||||
|
public WorkbenchRunnableAdapter(IWorkspaceRunnable runnable, ISchedulingRule rule) {
|
||||||
fWorkspaceRunnable= runnable;
|
fWorkspaceRunnable= runnable;
|
||||||
|
fRule= rule;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ISchedulingRule getSchedulingRule() {
|
||||||
|
return fRule;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -40,7 +60,7 @@ public class WorkbenchRunnableAdapter implements IRunnableWithProgress {
|
||||||
*/
|
*/
|
||||||
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
|
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
|
||||||
try {
|
try {
|
||||||
CoreModel.run(fWorkspaceRunnable, monitor);
|
CoreModel.run(fWorkspaceRunnable, fRule, monitor);
|
||||||
} catch (OperationCanceledException e) {
|
} catch (OperationCanceledException e) {
|
||||||
throw new InterruptedException(e.getMessage());
|
throw new InterruptedException(e.getMessage());
|
||||||
} catch (CoreException e) {
|
} catch (CoreException e) {
|
||||||
|
@ -48,5 +68,37 @@ public class WorkbenchRunnableAdapter implements IRunnableWithProgress {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void runAsUserJob(String name, final Object jobFamiliy) {
|
||||||
|
Job buildJob = new Job(name){
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
|
||||||
|
*/
|
||||||
|
protected IStatus run(IProgressMonitor monitor) {
|
||||||
|
try {
|
||||||
|
WorkbenchRunnableAdapter.this.run(monitor);
|
||||||
|
} catch (InvocationTargetException e) {
|
||||||
|
Throwable cause= e.getCause();
|
||||||
|
if (cause instanceof CoreException) {
|
||||||
|
return ((CoreException) cause).getStatus();
|
||||||
|
} else {
|
||||||
|
return CUIStatus.createError(IStatus.ERROR, cause);
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
return Status.CANCEL_STATUS;
|
||||||
|
} finally {
|
||||||
|
monitor.done();
|
||||||
|
}
|
||||||
|
return Status.OK_STATUS;
|
||||||
|
}
|
||||||
|
public boolean belongsTo(Object family) {
|
||||||
|
return jobFamiliy == family;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
buildJob.setRule(fRule);
|
||||||
|
buildJob.setUser(true);
|
||||||
|
buildJob.schedule();
|
||||||
|
|
||||||
|
// TODO: should block until user pressed 'to background'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue