Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 48c297d8a54307b8da4cdbd64281160579b5e869 (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
/*****************************************************************************
 * Copyright (c) 2013, 2014 CEA LIST 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:
 *   CEA LIST - Initial API and implementation
 *   Christian W. Damus (CEA) - bug 422257
 *
 *****************************************************************************/
package org.eclipse.papyrus.customization.properties.storage.actions;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;
import org.eclipse.papyrus.customization.properties.Activator;
import org.eclipse.papyrus.infra.core.utils.OneTimeRegistryReader;
import org.eclipse.papyrus.infra.properties.contexts.Context;

/**
 * This is the ContextStorageActionRegistry type. Enjoy.
 */
public class ContextStorageActionRegistry {

	private static final String EXT_POINT = "contextStorage"; //$NON-NLS-1$

	private final List<IContextStorageActionProvider> providers = new java.util.ArrayList<IContextStorageActionProvider>();

	public ContextStorageActionRegistry() {
		super();

		new MyRegistryReader().readRegistry();
	}

	public List<IContextStorageActionProvider> getStorageActionProviders() {
		return Collections.unmodifiableList(providers);
	}

	public IContextStorageActionProvider getStorageActionProvider(Context context) {
		IContextStorageActionProvider result = IContextStorageActionProvider.READ_ONLY;

		for (IContextStorageActionProvider next : providers) {
			if (next.providesFor(context)) {
				result = next;
				break;
			}
		}

		return result;
	}

	public IContextCopyAction getContextCopyAction(Context context) {
		return getStorageActionProvider(context).getContextCopyAction();
	}

	public IContextEditAction getContextEditAction(Context context) {
		return getStorageActionProvider(context).getContextEditAction();
	}

	public IContextDeleteAction getContextDeleteAction(Context context) {
		return getStorageActionProvider(context).getContextDeleteAction();
	}

	//
	// Nested types
	//

	private class MyRegistryReader extends OneTimeRegistryReader {

		private static final String A_CLASS = "class"; //$NON-NLS-1$

		private static final String E_ACTION_PROVIDER = "actionProvider"; //$NON-NLS-1$

		private Map<IContextStorageActionProvider, IConfigurationElement> providerElements = new java.util.HashMap<IContextStorageActionProvider, IConfigurationElement>();

		MyRegistryReader() {
			super(Platform.getExtensionRegistry(), Activator.PLUGIN_ID, EXT_POINT);
		}

		@Override
		public void readRegistry() {
			super.readRegistry();

			// sort any providers contributed by this plug-in to the front
			Collections.sort(providers, new Comparator<IContextStorageActionProvider>() {

				@Override
				public int compare(IContextStorageActionProvider o1, IContextStorageActionProvider o2) {
					int result = 0;

					if (isOurs(o1)) {
						result = isOurs(o2) ? 0 : -1;
					} else if (isOurs(o2)) {
						result = +1;
					}

					return result;
				}
			});
		}

		private boolean isOurs(IContextStorageActionProvider provider) {
			boolean result = false;

			IConfigurationElement config = providerElements.get(provider);
			if (config != null) {
				Activator.PLUGIN_ID.equals(config.getContributor().getName());
			}

			return result;
		}

		@Override
		protected boolean readElement(IConfigurationElement element) {
			boolean result = true;

			if (E_ACTION_PROVIDER.equals(element.getName())) {
				try {
					providers.add((IContextStorageActionProvider) element.createExecutableExtension(A_CLASS));
				} catch (Exception e) {
					result = false;
					Activator.log.error("Failed to instantiate context storage action provider extension.", e); //$NON-NLS-1$
				}
			}

			return result;
		}
	}
}

Back to the top