Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c78f105052ef9eb5ecf1b0167bb6c9b6294bb2bb (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
/*
 * Copyright (c) 2014 CEA 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:
 *   Christian W. Damus (CEA) - Initial API and implementation
 *
 */
package org.eclipse.papyrus.infra.ui.editor.reload;

import java.util.Collection;
import java.util.Collections;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.papyrus.infra.core.utils.AdapterUtils;

import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;


/**
 * An {@linkplain EditorReloadEvent#putContext(Object) editor reload context} that composes other reload contexts.
 * This should be used whenever a {@linkplain IReloadContextProvider reload context provider} supplies multiple
 * reload contexts, to ensure that they are properly initialized by the reload system.
 * 
 * @since 1.2
 */
public class CompositeReloadContext implements IDisposableReloadContext, IAdaptable {

	private final Collection<Object> reloadContexts;

	public CompositeReloadContext() {
		this(Collections.EMPTY_LIST);
	}

	public CompositeReloadContext(Iterable<?> reloadContexts) {
		super();

		this.reloadContexts = Lists.newArrayList(reloadContexts);
	}

	public <T> T add(T reloadContext) {
		reloadContexts.add(reloadContext);
		return reloadContext;
	}

	public Iterable<?> getReloadContexts() {
		return Collections.unmodifiableCollection(reloadContexts);
	}

	public <T> Iterable<T> getReloadContexts(Class<T> type) {
		return Iterables.filter(getReloadContexts(), type);
	}

	@Override
	public void dispose() {
		for (Object next : reloadContexts) {
			if (next instanceof IDisposableReloadContext) {
				((IDisposableReloadContext) next).dispose();
			}
		}

		reloadContexts.clear();
	}

	@Override
	public Object getAdapter(@SuppressWarnings("rawtypes") Class adapter) {
		return (adapter == IInternalEMFSelectionContext.class) ? getEMFContext() : null;
	}

	private IInternalEMFSelectionContext getEMFContext() {
		IInternalEMFSelectionContext result = null;

		for (Object next : reloadContexts) {
			if (AdapterUtils.adapt(next, IInternalEMFSelectionContext.class, null) != null) {
				// We need the adapter
				result = new IInternalEMFSelectionContext.Composite(this);
				break;
			}
		}

		return result;
	}
}

Back to the top