001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.actions;
011:
012: import java.lang.reflect.InvocationTargetException;
013:
014: import org.eclipse.core.runtime.CoreException;
015: import org.eclipse.core.runtime.IProgressMonitor;
016: import org.eclipse.core.runtime.IStatus;
017: import org.eclipse.core.runtime.OperationCanceledException;
018: import org.eclipse.core.runtime.Status;
019: import org.eclipse.core.runtime.jobs.ISchedulingRule;
020: import org.eclipse.core.runtime.jobs.Job;
021:
022: import org.eclipse.core.resources.IWorkspaceRunnable;
023: import org.eclipse.core.resources.ResourcesPlugin;
024:
025: import org.eclipse.jface.operation.IRunnableWithProgress;
026: import org.eclipse.jface.operation.IThreadListener;
027:
028: import org.eclipse.jdt.core.JavaCore;
029:
030: import org.eclipse.jdt.internal.ui.JavaUIStatus;
031:
032: /**
033: * An <code>IRunnableWithProgress</code> that adapts and <code>IWorkspaceRunnable</code>
034: * so that is can be executed inside <code>IRunnableContext</code>. <code>OperationCanceledException</code>
035: * thrown by the adapted runnable are caught and re-thrown as a <code>InterruptedException</code>.
036: */
037: public class WorkbenchRunnableAdapter implements IRunnableWithProgress,
038: IThreadListener {
039:
040: private boolean fTransfer = false;
041: private IWorkspaceRunnable fWorkspaceRunnable;
042: private ISchedulingRule fRule;
043:
044: /**
045: * Runs a workspace runnable with the workspace lock.
046: */
047: public WorkbenchRunnableAdapter(IWorkspaceRunnable runnable) {
048: this (runnable, ResourcesPlugin.getWorkspace().getRoot());
049: }
050:
051: /**
052: * Runs a workspace runnable with the given lock or <code>null</code> to run with no lock at all.
053: */
054: public WorkbenchRunnableAdapter(IWorkspaceRunnable runnable,
055: ISchedulingRule rule) {
056: fWorkspaceRunnable = runnable;
057: fRule = rule;
058: }
059:
060: /**
061: * Runs a workspace runnable with the given lock or <code>null</code> to run with no lock at all.
062: * @param transfer <code>true</code> if the rule is to be transfered
063: * to the model context thread. Otherwise <code>false</code>
064: */
065: public WorkbenchRunnableAdapter(IWorkspaceRunnable runnable,
066: ISchedulingRule rule, boolean transfer) {
067: fWorkspaceRunnable = runnable;
068: fRule = rule;
069: fTransfer = transfer;
070: }
071:
072: public ISchedulingRule getSchedulingRule() {
073: return fRule;
074: }
075:
076: /**
077: * {@inheritDoc}
078: */
079: public void threadChange(Thread thread) {
080: if (fTransfer)
081: Job.getJobManager().transferRule(fRule, thread);
082: }
083:
084: /*
085: * @see IRunnableWithProgress#run(IProgressMonitor)
086: */
087: public void run(IProgressMonitor monitor)
088: throws InvocationTargetException, InterruptedException {
089: try {
090: JavaCore.run(fWorkspaceRunnable, fRule, monitor);
091: } catch (OperationCanceledException e) {
092: throw new InterruptedException(e.getMessage());
093: } catch (CoreException e) {
094: throw new InvocationTargetException(e);
095: }
096: }
097:
098: public void runAsUserJob(String name, final Object jobFamiliy) {
099: Job buildJob = new Job(name) {
100: /* (non-Javadoc)
101: * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
102: */
103: protected IStatus run(IProgressMonitor monitor) {
104: try {
105: WorkbenchRunnableAdapter.this .run(monitor);
106: } catch (InvocationTargetException e) {
107: Throwable cause = e.getCause();
108: if (cause instanceof CoreException) {
109: return ((CoreException) cause).getStatus();
110: } else {
111: return JavaUIStatus.createError(IStatus.ERROR,
112: cause);
113: }
114: } catch (InterruptedException e) {
115: return Status.CANCEL_STATUS;
116: } finally {
117: monitor.done();
118: }
119: return Status.OK_STATUS;
120: }
121:
122: public boolean belongsTo(Object family) {
123: return jobFamiliy == family;
124: }
125: };
126: buildJob.setRule(fRule);
127: buildJob.setUser(true);
128: buildJob.schedule();
129:
130: // TODO: should block until user pressed 'to background'
131: }
132: }
|