Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f06dc0a43abf4c2d79c7ce723a8481f5e4fba4b7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*******************************************************************************
 * Copyright (C) 2010, Jens Baumgart <jens.baumgart@sap.com>
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/
package org.eclipse.egit.core.internal.job;

import org.eclipse.core.resources.WorkspaceJob;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.IJobChangeListener;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.egit.core.op.IEGitOperation;

/**
 * Utility class for scheduling jobs
 */
public class JobUtil {

	/**
	 * Schedule a user job that executes an EGit operation
	 *
	 * @param op
	 *            EGit operation to run
	 * @param jobName
	 * @param jobFamily
	 */
	public static void scheduleUserJob(final IEGitOperation op, String jobName,
			final Object jobFamily) {
		scheduleUserJob(op, jobName, jobFamily, null);
	}

	/**
	 * Schedule a user job that executes an EGit operation
	 *
	 * @param op
	 *            EGit operation to run
	 * @param jobName
	 * @param jobFamily
	 * @param jobChangeListener
	 */
	public static void scheduleUserJob(final IEGitOperation op, String jobName,
			final Object jobFamily, IJobChangeListener jobChangeListener) {
		Job job = new Job(jobName) {
			@Override
			protected IStatus run(IProgressMonitor monitor) {
				try {
					op.execute(monitor);
				} catch (CoreException e) {
					return e.getStatus();
				}
				return Status.OK_STATUS;
			}

			@Override
			public boolean belongsTo(Object family) {
				if (jobFamily != null && family.equals(jobFamily))
					return true;
				return super.belongsTo(family);
			}
		};
		job.setRule(op.getSchedulingRule());
		job.setUser(true);
		if (jobChangeListener != null)
			job.addJobChangeListener(jobChangeListener);
		job.schedule();
	}

	/**
	 * Schedule a user workspace job that executes an EGit operation which
	 * ensures that resource change events are batched until the job is finished
	 *
	 * @param op
	 *            EGit operation to run
	 * @param jobName
	 * @param jobFamily
	 */
	public static void scheduleUserWorkspaceJob(final IEGitOperation op,
			String jobName, final Object jobFamily) {
		scheduleUserWorkspaceJob(op, jobName, jobFamily, null);
	}

	/**
	 * Schedule a user workspace job that executes an EGit operation which
	 * ensures that resource change events are batched until the job is finished
	 *
	 * @param op
	 *            EGit operation to run
	 * @param jobName
	 * @param jobFamily
	 * @param jobChangeListener
	 */
	public static void scheduleUserWorkspaceJob(final IEGitOperation op,
			String jobName, final Object jobFamily,
			IJobChangeListener jobChangeListener) {
		Job job = new WorkspaceJob(jobName) {
			@Override
			public IStatus runInWorkspace(IProgressMonitor monitor) {
				try {
					op.execute(monitor);
				} catch (CoreException e) {
					return e.getStatus();
				}
				return Status.OK_STATUS;
			}

			@Override
			public boolean belongsTo(Object family) {
				if (jobFamily != null && family.equals(jobFamily))
					return true;
				return super.belongsTo(family);
			}
		};
		job.setRule(op.getSchedulingRule());
		job.setUser(true);
		if (jobChangeListener != null)
			job.addJobChangeListener(jobChangeListener);
		job.schedule();
	}
}

Back to the top