Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bf4e84167f21cf3d2ebee2c159990dbea0113d17 (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
/*******************************************************************************
 * Copyright (c) 2006, 2020 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.internal.ui.mapping;

import org.eclipse.compare.ITypedElement;
import org.eclipse.compare.structuremergeviewer.ICompareInput;
import org.eclipse.compare.structuremergeviewer.ICompareInputChangeListener;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.ListenerList;
import org.eclipse.core.runtime.SafeRunner;
import org.eclipse.swt.graphics.Image;

/**
 * An abstract compare input whose purpose is to support change notification
 * through a {@link CompareInputChangeNotifier}.
 */
public abstract class AbstractCompareInput implements ICompareInput {

	private ITypedElement ancestor;
	private ITypedElement left;
	private ITypedElement right;
	private int kind;
	private final ListenerList<ICompareInputChangeListener> listeners = new ListenerList<>(ListenerList.IDENTITY);

	public AbstractCompareInput(int kind,
			ITypedElement ancestor,
			ITypedElement left,
			ITypedElement right) {
				this.kind = kind;
				this.ancestor = ancestor;
				this.left = left;
				this.right = right;
	}

	@Override
	public void addCompareInputChangeListener(
			ICompareInputChangeListener listener) {
		if (!containsListener(listener)) {
			listeners.add(listener);
			getChangeNotifier().connect(this);
		}
	}

	@Override
	public void removeCompareInputChangeListener(
			ICompareInputChangeListener listener) {
		if (containsListener(listener)) {
			listeners.remove(listener);
			getChangeNotifier().disconnect(this);
		}
	}

	/**
	 * Fire a compare input change event.
	 * This method must be called from the UI thread.
	 */
	protected void fireChange() {
		if (!listeners.isEmpty()) {
			Object[] allListeners = listeners.getListeners();
			for (Object l : allListeners) {
				final ICompareInputChangeListener listener = (ICompareInputChangeListener) l;
				SafeRunner.run(new ISafeRunnable() {
					@Override
					public void run() throws Exception {
						listener.compareInputChanged(AbstractCompareInput.this);
					}
					@Override
					public void handleException(Throwable exception) {
						// Logged by the safe runner
					}
				});
			}
		}
	}

	private boolean containsListener(ICompareInputChangeListener listener) {
		if (listeners.isEmpty())
			return false;
		Object[] allListeners = listeners.getListeners();
		for (Object object : allListeners) {
			if (object == listener)
				return true;
		}
		return false;
	}

	@Override
	public void copy(boolean leftToRight) {
		Assert.isTrue(false, "Copy is not support by this type of compare input"); //$NON-NLS-1$
	}

	@Override
	public ITypedElement getAncestor() {
		return ancestor;
	}

	@Override
	public Image getImage() {
		return getMainElement().getImage();
	}

	/**
	 * Return the main non-null element that identifies
	 * this input. By default, the left is returned if non-null.
	 * If the left is null, the right is returned. If both the
	 * left and right are null the ancestor is returned.
	 * @return the main non-null element that identifies
	 * this input
	 */
	private ITypedElement getMainElement() {
		if (left != null)
			return left;
		if (right != null)
			return right;
		return ancestor;
	}

	@Override
	public int getKind() {
		return kind;
	}

	/**
	 * Set the kind of this compare input
	 * @param kind the new kind
	 */
	public void setKind(int kind) {
		this.kind = kind;
	}

	@Override
	public ITypedElement getLeft() {
		return left;
	}

	@Override
	public String getName() {
		return getMainElement().getName();
	}

	@Override
	public ITypedElement getRight() {
		return right;
	}

	/**
	 * Return the change notifier that will call {@link #fireChange()}
	 * when the state of the compare input changes.
	 * @return the change notifier
	 */
	protected abstract CompareInputChangeNotifier getChangeNotifier();

	/**
	 * Set the ancestor of this compare input.
	 * @param ancestor the ancestor
	 */
	public void setAncestor(ITypedElement ancestor) {
		this.ancestor = ancestor;
	}

	/**
	 * Set the left element of this compare input.
	 * @param left the left element
	 */
	public void setLeft(ITypedElement left) {
		this.left = left;
	}

	/**
	 * Set the right element of this compare input.
	 * @param right the right element
	 */
	public void setRight(ITypedElement right) {
		this.right = right;
	}

	/**
	 * Update the compare input and fire change notification.
	 */
	public abstract void update();

	/**
	 * Return whether this compare input needs to be updated.
	 * @return whether this compare input needs to be updated
	 */
	public abstract boolean needsUpdate();


}

Back to the top