Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 37d93fb6889780840bc5bbd4afb621aa4f40d139 (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
/*******************************************************************************
 * Copyright (c) 2012, 2016 Oracle. 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:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.common.core.internal.utility.command;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jpt.common.core.utility.command.JobCommand;
import org.eclipse.jpt.common.core.utility.command.JobCommandContext;
import org.eclipse.jpt.common.core.utility.command.NotifyingRepeatingJobCommand;
import org.eclipse.jpt.common.utility.exception.ExceptionHandler;
import org.eclipse.jpt.common.utility.internal.ListenerList;
import org.eclipse.jpt.common.utility.internal.model.ModelTools;

/**
 * @see org.eclipse.jpt.common.utility.internal.command.NotifyingRepeatingCommandWrapper
 */
public class NotifyingRepeatingJobCommandWrapper
	extends RepeatingJobCommandWrapper
	implements NotifyingRepeatingJobCommand
{
	private final ListenerList<Listener> listenerList = ModelTools.listenerList();


	// ********** construction **********

	/**
	 * Construct a notifying repeating command wrapper that executes the
	 * specified command. Any exceptions thrown by the command or listener
	 * will be handled by the specified exception handler.
	 */
	public NotifyingRepeatingJobCommandWrapper(JobCommand command, ExceptionHandler exceptionHandler) {
		super(command, exceptionHandler);
	}

	/**
	 * Construct a notifying repeating command wrapper that executes the
	 * specified command and uses the specified command context to execute the
	 * wrapped command whenever it is not already executing.
	 * Any exceptions thrown by the command or listener will be handled by the
	 * specified exception handler.
	 */
	public NotifyingRepeatingJobCommandWrapper(JobCommand command, JobCommandContext startCommandContext, ExceptionHandler exceptionHandler) {
		super(command, startCommandContext, exceptionHandler);
	}


	// ********** listeners **********

	public void addListener(Listener listener) {
		this.listenerList.add(listener);
	}

	public void removeListener(Listener listener) {
		this.listenerList.remove(listener);
	}


	// ********** override **********

	@Override
	/* private protected */ IStatus executeCommand(IProgressMonitor monitor) {
		IStatus status = super.executeCommand(monitor);
		if (status.getSeverity() == IStatus.CANCEL) {
			this.executionCanceled();
		}
		else if (this.state.isQuiesced()) {
			this.executionQuiesced();
		}
		return status;
	}

	/**
	 * Notify our listeners. All listeners are notified. There is no way to
	 * cancel the notifications (e.g. via a monitor or exception).
	 */
	private void executionQuiesced() {
		for (Listener listener : this.listenerList) {
			this.notifyListenerQuiesced(listener);
		}
	}

	private void notifyListenerQuiesced(Listener listener) {
		try {
			listener.executionQuiesced(this);
		} catch (Throwable ex) {
			this.exceptionHandler.handleException(ex);
		}
	}

	/**
	 * Notify our listeners. All listeners are notified. There is no way to
	 * cancel the notifications (e.g. via a monitor or exception).
	 */
	private void executionCanceled() {
		for (Listener listener : this.listenerList) {
			this.notifyListenerCanceled(listener);
		}
	}

	private void notifyListenerCanceled(Listener listener) {
		try {
			listener.executionCanceled(this);
		} catch (Throwable ex) {
			this.exceptionHandler.handleException(ex);
		}
	}
}

Back to the top