Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 43e3214e2ac7518111efb9738a83944fd0cfab7d (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*******************************************************************************
 * Copyright (c) 2000, 2017 IBM Corporation and others.
 *
 * 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
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.ui;

import java.lang.reflect.InvocationTargetException;
import java.net.URL;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.IJobManager;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.operation.IRunnableContext;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.team.internal.ui.TeamUIPlugin;
import org.eclipse.team.internal.ui.Utils;
import org.eclipse.team.internal.ui.actions.ITeamRunnableContext;
import org.eclipse.team.internal.ui.actions.JobRunnableContext;
import org.eclipse.team.internal.ui.actions.ProgressDialogRunnableContext;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchSite;
import org.eclipse.ui.progress.IProgressConstants;

/**
 * An operation that can be configured to run in the foreground using
 * the {@link org.eclipse.ui.progress.IProgressService} or the background
 * as a {@link org.eclipse.core.runtime.jobs.Job}. The execution context is determined
 * by what is returned by the {@link #canRunAsJob()} hint which may be overridden by subclasses.
 * Subclass must override the <code>run(IProgressMonitor)</code> method to perform
 * the behavior of the operation in the desired execution context.
 * <p>
 * If this operation is run as a job, it is registered with the job as a
 * {@link org.eclipse.core.runtime.jobs.IJobChangeListener} and is scheduled with
 * the part of this operation if it is not <code>null</code>.
 * Subclasses can override the methods of this interface to receive job change notifications.
 * </p>
 * @see org.eclipse.ui.progress.IProgressService
 * @see org.eclipse.core.runtime.jobs.Job
 * @see org.eclipse.core.runtime.jobs.ISchedulingRule
 * @see org.eclipse.core.runtime.jobs.IJobChangeListener
 * @since 3.0
 */
public abstract class TeamOperation extends JobChangeAdapter implements IRunnableWithProgress {

	private IWorkbenchPart part;
	private IRunnableContext context;

	/*
	 * Job context that configures how the team operation will
	 * interact with the progress service
	 */
	private static class TeamOperationJobContext extends JobRunnableContext {

	    private final TeamOperation operation;
        private IAction gotoAction;

	    public TeamOperationJobContext(TeamOperation operation) {
	        super(operation.getJobName(), operation, operation.getSite());
	        this.operation = operation;
	    }

		@Override
		protected void configureJob(Job job) {
		    super.configureJob(job);
		    if (operation.isKeepOneProgressServiceEntry())
		        job.setProperty(IProgressConstants.KEEPONE_PROPERTY, Boolean.TRUE);
		    else if(operation.getKeepOperation())
				job.setProperty(IProgressConstants.KEEP_PROPERTY, Boolean.TRUE);
			gotoAction = operation.getGotoAction();
			if(gotoAction != null)
				job.setProperty(IProgressConstants.ACTION_PROPERTY, gotoAction);
			URL icon = operation.getOperationIcon();
			if(icon != null)
				job.setProperty(IProgressConstants.ICON_PROPERTY, icon);
		}

        @Override
		protected boolean belongsTo(IContextJob job, Object family) {
            if (family instanceof IContextJob) {
                IContextJob otherJob = (IContextJob)family;
                IRunnableWithProgress runnable = otherJob.getRunnable();
                if (runnable instanceof TeamOperation) {
                    return operation.isSameFamilyAs((TeamOperation)runnable);
                }
            }
            return operation.belongsTo(family);
        }

        @Override
		protected IStatus getCompletionStatus() {
            if (gotoAction != null) {
                return new Status(IStatus.OK, TeamUIPlugin.ID, IStatus.OK, gotoAction.getText(), null);
            }
            return super.getCompletionStatus();
        }

        @Override
		protected boolean isUser() {
            return operation.isUserInitiated();
        }
	}

	/**
	 * Create an team operation associated with the given part.
	 * @param part the part the operation is associated with or <code>null</code> if the
	 * operation is to be run without a part.
	 */
	protected TeamOperation(IWorkbenchPart part) {
		this(part, null);
	}

    /**
	 * Create an team operation that will run in the given context.
	 * @param context a runnable context
	 */
	protected TeamOperation(IRunnableContext context) {
		this(null, context);
	}

	/**
	 * Create an team operation associated with the given part
	 * that will run in the given context.
	 * @param part the part the operation is associated with or <code>null</code>
	 * @param context a runnable context
	 */
	protected TeamOperation(IWorkbenchPart part, IRunnableContext context) {
		this.part = part;
		this.context = context;
	}

	/**
	 * Return the part that is associated with this operation.
	 *
	 * @return Returns the part or <code>null</code>
	 */
	public IWorkbenchPart getPart() {
		return part;
	}

	/**
	 * Run the operation in a context that is determined by the {@link #canRunAsJob()}
	 * hint. If this operation can run as a job then it will be run in a background thread.
	 * Otherwise it will run in the foreground and block the caller.
	 */
	public final void run() throws InvocationTargetException, InterruptedException {
		if (shouldRun()) {
			getRunnableContext().run(this);
		}
	}

	/**
	 * This method is invoked from the <code>run()</code> method before
	 * the operation is run in the operation's context. Subclasses may
	 * override in order to perform pre-checks to determine if the operation
	 * should run. This may include prompting the user for information, etc.
	 *
	 * @return whether the operation should be run.
	 */
	protected boolean shouldRun() {
		return true;
	}

	/**
	 * Returns the scheduling rule that is to be obtained before this
	 * operation is executed by its context or <code>null</code> if
	 * no scheduling rule is to be obtained. If the operation is run
	 * as a job, the scheduling rule is used as the scheduling rule of the
	 * job. Otherwise, it is obtained before execution of the operation
	 * occurs.
	 * <p>
	 * By default, no scheduling
	 * rule is obtained. Subclasses can override in order to obtain a
	 * scheduling rule or can obtain scheduling rules within their operation
	 * if finer grained scheduling is desired.
	 *
	 * @return the scheduling rule to be obtained by this operation
	 * or <code>null</code>.
	 */
	protected ISchedulingRule getSchedulingRule() {
		return null;
	}

	/**
	 * Return whether the auto-build should be postponed until after
	 * the operation is complete. The default is to postpone the auto-build.
	 * subclasses can override.
	 *
	 * @return whether to postpone the auto-build while the operation is executing.
	 */
	protected boolean isPostponeAutobuild() {
		return true;
	}

	/**
	 * If this operation can safely be run in the background, then subclasses can
	 * override this method and return <code>true</code>. This will make their
	 * action run in a {@link  org.eclipse.core.runtime.jobs.Job}.
	 * Subclass that override this method should
	 * also override the <code>getJobName()</code> method.
	 *
	 * @return <code>true</code> if this action can be run in the background and
	 * <code>false</code> otherwise.
	 */
	protected boolean canRunAsJob() {
		return false;
	}

	/**
	 * Return the job name to be used if the action can run as a job. (i.e.
	 * if <code>canRunAsJob()</code> returns <code>true</code>).
	 *
	 * @return the string to be used as the job name
	 */
	protected String getJobName() {
		return ""; //$NON-NLS-1$
	}

	/**
	 * This method is called to allow subclasses to configure an action that could be run to show
	 * the results of the action to the user. Default is to return null.
	 *
	 * @return an action that could be run to see the results of this operation
	 */
	protected IAction getGotoAction() {
		return null;
	}

	/**
	 * This method is called to allow subclasses to configure an icon to show when running this
	 * operation.
	 *
	 * @return an URL to an icon
	 */
	protected URL getOperationIcon() {
		return null;
	}

	/**
     * This method is called to allow subclasses to have the results of the
     * operation remain available to the user in the progress service even after
     * the job is done. This method is only relevant if the operation is run as
     * a job (i.e., <code>canRunAsJob</code> returns <code>true</code>).
     *
     * @return <code>true</code> to keep the operation and <code>false</code>
     *         otherwise.
     */
	protected boolean getKeepOperation() {
		return false;
	}

	/**
     * This method is similar to <code>getKeepOperation</code> but will
     * only keep one entry of a particular type available.
     * This method is only relevant if the operation is run as
     * a job (i.e., <code>canRunAsJob</code> returns <code>true</code>).
     * Subclasses that override this method should also override
     * <code>isSameFamilyAs</code> in order to match operations of the same type.
     *
     * @return <code>true</code> to keep the operation and <code>false</code>
     *         otherwise.
     * @since 3.1
     */
    public boolean isKeepOneProgressServiceEntry() {
        return false;
    }

    /**
     * Return whether this Team operation belongs to the same family
     * as the given operation for the purpose of showing only one
     * operation of the same type in the progress service when
     * <code>isKeepOneProgressServiceEntry</code> is overridden to
     * return <code>true</code>. By default,
     * <code>false</code> is returned. Subclasses may override.
     * @param operation a team operation
     * @since 3.1
     */
    protected boolean isSameFamilyAs(TeamOperation operation) {
        return false;
    }

    /**
     * Return whether the job that is running this operation should be considered
     * a member member of the given family. Subclasses can override this method in
     * order to support the family based functionality provided by the {@link IJobManager}.
     * By default, <code>false</code> is always returned. Subclasses that override the
     * <code>isKeepOneProgressServiceEntry</code> method do not need to override
     * this method, but instead should override <code>isSameFamilyAs</code>.
     *
     * @param family the family being tested.
     * @return whether the job that is running this operation should be considered
     * a member member of the given family.
     * @since 3.1
     */
    public boolean belongsTo(Object family) {
        return false;
    }

    /**
     * Indicates whether the operation was user initiated. The
     * progress for user initiated jobs may be presented differently
     * than non-user initiated operations if they are run as jobs.
     * @return whether the operation is user initiated
     * @since 3.1
     */
    public boolean isUserInitiated() {
        return true;
    }

	/**
	 * Return a shell that can be used by the operation to display dialogs, etc.
	 *
	 * @return a shell
	 */
	protected Shell getShell() {
		final Shell[] shell = new Shell[] { null };
		if (Display.getCurrent() == null) {
			Display.getDefault().syncExec(() -> shell[0] = Utils.getShell(getSite()));
		} else {
			shell[0] = Utils.getShell(getSite());
		}
		return shell[0];
	}

	/*
	 * Uses the {@link #canRunAsJob()} hint to return a {@link ITeamRunnableContext}
	 * that is used to execute the <code>run(SyncInfoSet, IProgressMonitor)</code>
	 * method of this action.
	 *
	 * @param syncSet the sync info set containing the selected elements for which this
	 * action is enabled.
	 * @return the runnable context in which to run this action.
	 */
	private ITeamRunnableContext getRunnableContext() {
		if (context == null && canRunAsJob()) {
			JobRunnableContext context = new TeamOperationJobContext(this);
			context.setPostponeBuild(isPostponeAutobuild());
			context.setSchedulingRule(getSchedulingRule());
			return context;
		} else {
			ProgressDialogRunnableContext context = new ProgressDialogRunnableContext();
			context.setPostponeBuild(isPostponeAutobuild());
			context.setSchedulingRule(getSchedulingRule());
			if (this.context != null) {
				context.setRunnableContext(this.context);
			}
			return context;
		}
	}

	private IWorkbenchSite getSite() {
		IWorkbenchSite site = null;
		if(part != null) {
			site = part.getSite();
		}
		return site;
	}
}

Back to the top