Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 03bb6ebb61726f58a43c8d07f10c414a016f27fc (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
/*******************************************************************************
 * Copyright (c) 2012 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.utility.internal.command;

import org.eclipse.jpt.common.utility.ExceptionHandler;
import org.eclipse.jpt.common.utility.command.NotifyingRepeatingCommand;
import org.eclipse.jpt.common.utility.command.Command;
import org.eclipse.jpt.common.utility.command.CommandExecutor;
import org.eclipse.jpt.common.utility.internal.ListenerList;

/**
 * <strong>NB:</strong> If another execution cycle is initiated while we are
 * notifying the command's listeners (i.e. the {@link #state} is set
 * to "repeat"), the new
 * execution will not start until all the listeners are notified.
 * <p>
 * <strong>NB2:</strong> The command's listeners can, themselves,
 * trigger another execution (by directly or indirectly calling
 * {@link #execute()});
 * but, again, this execution will not occur until <em>after</em> all the
 * listeners have been notified.
 * <p>
 * <strong>NB3:</strong> Along with any exceptions thrown by the wrapped
 * command, any exceptions thrown by the command's listeners will
 * be passed to the {@link #exceptionHandler exception handler}.
 */
public class NotifyingRepeatingCommandWrapper
	extends RepeatingCommandWrapper
	implements NotifyingRepeatingCommand
{
	private final ListenerList<Listener> listenerList = new ListenerList<Listener>(Listener.class);


	// ********** 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 NotifyingRepeatingCommandWrapper(Command command, ExceptionHandler exceptionHandler) {
		super(command, exceptionHandler);
	}

	/**
	 * Construct a notifying repeating command wrapper that executes the specified
	 * command and uses the specified command executor 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 NotifyingRepeatingCommandWrapper(Command command, CommandExecutor startCommandExecutor, ExceptionHandler exceptionHandler) {
		super(command, startCommandExecutor, exceptionHandler);
	}


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

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

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


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

	@Override
	/* private protected */ void executeCommand() {
		super.executeCommand();
		if (this.state.isQuiesced()) {
			// hmmm - we will notify listeners even when we are "stopped"; that seems OK...
			this.executionQuiesced();
		}
	}

	/**
	 * Notify our listeners.
	 */
	private void executionQuiesced() {
		for (Listener listener : this.listenerList.getListeners()) {
			try {
				listener.executionQuiesced(this);
			} catch (Throwable ex) {
				this.exceptionHandler.handleException(ex);
			}
		}
	}
}

Back to the top