Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6f1d901a6d422dd1e7de9fb6af2f1f0f194d5a9a (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
/*******************************************************************************
 * Copyright (c) 2011 Wind River Systems, Inc. and others. All rights reserved.
 * This program and the accompanying materials are made available under the terms
 * of the Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.core.async;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.tcf.te.core.async.interfaces.IAsyncExecutable;
import org.eclipse.tcf.te.runtime.callback.Callback;
import org.eclipse.tcf.te.runtime.interfaces.callback.ICallback;
import org.eclipse.tcf.te.runtime.utils.ProgressHelper;

/**
 * Abstract asynchronous executable stepper job.
 * <p>
 * The job executes a list of actions. If any action returns with an error, the whole job will be
 * aborted.
 */
public abstract class AbstractAsyncExecutableStepperJob extends AbstractAsyncExecutableJob {
	private final IAsyncExecutable[] actions;

	/**
	 * Constructor.
	 *
	 * @param name The job name. Must not be <code>null</code>
	 * @param actions The actions to execute. Must not be <code>null</code>.
	 */
	public AbstractAsyncExecutableStepperJob(String name, IAsyncExecutable[] actions) {
		super(name);

		Assert.isNotNull(actions);
		this.actions = actions;
	}

	/**
	 * Returns the actions to execute.
	 *
	 * @return The actions to execute.
	 */
	protected IAsyncExecutable[] getActions() {
		return actions;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.core.async.AbstractAsyncExecutableJob#getJobTicks()
	 */
	@Override
	protected int getJobTicks() {
		return getActions().length * 100;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.core.async.AbstractAsyncExecutableJob#internalExecute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.tcf.te.runtime.interfaces.callback.ICallback)
	 */
	@Override
	protected final void internalExecute(final IProgressMonitor monitor, final ICallback callback) {
		if (getActions() == null || getActions().length == 0) {
			callback.done(this, Status.OK_STATUS);
		}
		else {
			ICallback stepCallback = new Callback() {
				private int index = 0;

				/* (non-Javadoc)
				 * @see org.eclipse.tcf.te.runtime.callback.Callback#internalDone(java.lang.Object, org.eclipse.core.runtime.IStatus)
				 */
				@Override
				protected void internalDone(Object caller, IStatus status) {
					if (!ProgressHelper.isCancelOrError(caller, status, getProgressMonitor(), null) && index < getActions().length) {
						setProperty(PROPERTY_IS_DONE, false);
						getActions()[index++].execute(monitor, 100, this);
					}
					else {
						callback.done(caller, status);
					}
				}
			};
			stepCallback.done(this, Status.OK_STATUS);
		}
	}
}

Back to the top