KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > resources > InternalWorkspaceJob


1 /*******************************************************************************
2  * Copyright (c) 2003, 2005 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 - Initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.resources;
12
13 import org.eclipse.core.internal.utils.Policy;
14 import org.eclipse.core.resources.ResourcesPlugin;
15 import org.eclipse.core.runtime.*;
16 import org.eclipse.core.runtime.jobs.Job;
17
18 /**
19  * Batches the activity of a job as a single operation, without obtaining the workspace
20  * lock.
21  */

22 public abstract class InternalWorkspaceJob extends Job {
23     private Workspace workspace;
24
25     public InternalWorkspaceJob(String JavaDoc name) {
26         super(name);
27         this.workspace = (Workspace) ResourcesPlugin.getWorkspace();
28     }
29
30     public final IStatus run(IProgressMonitor monitor) {
31         monitor = Policy.monitorFor(monitor);
32         try {
33             int depth = -1;
34             try {
35                 workspace.prepareOperation(null, monitor);
36                 workspace.beginOperation(true);
37                 depth = workspace.getWorkManager().beginUnprotected();
38                 return runInWorkspace(monitor);
39             } catch (OperationCanceledException e) {
40                 workspace.getWorkManager().operationCanceled();
41                 return Status.CANCEL_STATUS;
42             } finally {
43                 if (depth >= 0)
44                     workspace.getWorkManager().endUnprotected(depth);
45                 workspace.endOperation(null, false, monitor);
46             }
47         } catch (CoreException e) {
48             return e.getStatus();
49         }
50     }
51
52     protected abstract IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException;
53 }
54
Popular Tags