Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3f6aa3a51e8761bfa68210943b5686696a15ec39 (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
/*******************************************************************************
 * Copyright (c) 2015, 2018 Obeo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors: Obeo - initial API and implementation
 *******************************************************************************/
package org.eclipse.eef.ide.api.extensions.impl;

import java.text.MessageFormat;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.eef.ide.api.extensions.AbstractRegistryEventListener;
import org.eclipse.eef.ide.api.extensions.IItemDescriptor;
import org.eclipse.eef.ide.api.extensions.IItemRegistry;
import org.eclipse.eef.ide.internal.EEFIdePlugin;
import org.eclipse.eef.ide.internal.Messages;

/**
 * Utility class used to retrieved the descriptors of extensions.
 *
 * @author adaussy
 * @author sbegaudeau
 *
 * @param <T>
 *            The type of the Object described
 */
@Deprecated
public class DescriptorRegistryEventListener<T> extends AbstractRegistryEventListener {
	/** Id attribute of the extension point. */
	public static final String ID_DESCRIPTOR_ATTR = "id"; //$NON-NLS-1$

	/** Label attribute of extension point. */
	public static final String LABEL_DESCRIPTOR_ATTR = "label"; //$NON-NLS-1$

	/** Description attribute of extension point. */
	public static final String DESCRIPTION_DESCRIPTOR_ATTR = "description"; //$NON-NLS-1$

	/** Implementation class attribute of extension point. */
	public static final String IMPL_CLASS_DESCRIPTOR_ATTR = "class"; //$NON-NLS-1$

	/** Descriptor tag of extension point. */
	public static final String TAG_DESCRIPTOR = "descriptor"; //$NON-NLS-1$

	/**
	 * The item registry.
	 */
	private IItemRegistry<T> itemRegistry;

	/**
	 * The constructor.
	 *
	 * @param namespace
	 *            The namespace of the extension point
	 * @param extensionPointID
	 *            The identifier of the extension point
	 * @param itemRegistry
	 *            The {@link IItemRegistry}
	 */
	public DescriptorRegistryEventListener(String namespace, String extensionPointID, IItemRegistry<T> itemRegistry) {
		super(namespace, extensionPointID);
		this.itemRegistry = itemRegistry;
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see org.eclipse.eef.ide.api.extensions.AbstractRegistryEventListener#validateConfigurationElement(org.eclipse.core.runtime.IConfigurationElement)
	 */
	@Override
	protected boolean validateConfigurationElement(IConfigurationElement configurationElement) {
		boolean isValid = false;

		if (TAG_DESCRIPTOR.equals(configurationElement.getName())) {
			if (!this.isValidAttribute(configurationElement, ID_DESCRIPTOR_ATTR)) {
				String message = MessageFormat.format(Messages.DescriptorRegistryEventListener_missingAttribute,
						configurationElement.getNamespaceIdentifier(), ID_DESCRIPTOR_ATTR);
				EEFIdePlugin.getPlugin().error(message);
			} else if (!this.isValidAttribute(configurationElement, LABEL_DESCRIPTOR_ATTR)) {
				String message = MessageFormat.format(Messages.DescriptorRegistryEventListener_missingAttribute,
						configurationElement.getNamespaceIdentifier(), LABEL_DESCRIPTOR_ATTR);
				EEFIdePlugin.getPlugin().error(message);
			} else if (!this.isValidAttribute(configurationElement, DESCRIPTION_DESCRIPTOR_ATTR)) {
				String message = MessageFormat.format(Messages.DescriptorRegistryEventListener_missingAttribute,
						configurationElement.getNamespaceIdentifier(), DESCRIPTION_DESCRIPTOR_ATTR);
				EEFIdePlugin.getPlugin().error(message);
			} else if (!this.isValidAttribute(configurationElement, IMPL_CLASS_DESCRIPTOR_ATTR)) {
				String message = MessageFormat.format(Messages.DescriptorRegistryEventListener_missingAttribute,
						configurationElement.getNamespaceIdentifier(), IMPL_CLASS_DESCRIPTOR_ATTR);
				EEFIdePlugin.getPlugin().error(message);
			} else {
				isValid = true;
			}
		}

		return isValid;
	}

	/**
	 * Indicates if an attribute of the configuration element is valid.
	 *
	 * @param configurationElement
	 *            The configuration element
	 * @param attributeName
	 *            The attribute name
	 * @return <code>true</code> if the attribute is valid, <code>false</code> otherwise
	 */
	private boolean isValidAttribute(IConfigurationElement configurationElement, String attributeName) {
		return configurationElement.getAttribute(attributeName) != null && !"".equals(configurationElement.getAttribute(attributeName)); //$NON-NLS-1$
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see org.eclipse.eef.ide.api.extensions.AbstractRegistryEventListener#processAddition(org.eclipse.core.runtime.IConfigurationElement)
	 */
	@SuppressWarnings("unchecked")
	@Override
	protected boolean processAddition(IConfigurationElement configurationElement) {
		String id = configurationElement.getAttribute(ID_DESCRIPTOR_ATTR);
		String label = configurationElement.getAttribute(LABEL_DESCRIPTOR_ATTR);
		String description = configurationElement.getAttribute(DESCRIPTION_DESCRIPTOR_ATTR);

		try {
			T instance = (T) configurationElement.createExecutableExtension(IMPL_CLASS_DESCRIPTOR_ATTR);

			ItemDescriptor<T> descriptor = new ItemDescriptor<T>(id, label, description, instance);
			IItemDescriptor<T> previous = this.itemRegistry.add(descriptor);
			if (previous != null) {
				String message = MessageFormat.format(Messages.DescriptorRegistryEventListener_extensionAlreadyContributed,
						configurationElement.getAttribute(ID_DESCRIPTOR_ATTR));
				EEFIdePlugin.getPlugin().warning(message);
			}
		} catch (CoreException e) {
			String message = MessageFormat.format(Messages.DescriptorRegistryEventListener_cannotInstantiateExtension,
					configurationElement.getAttribute(IMPL_CLASS_DESCRIPTOR_ATTR));
			EEFIdePlugin.getPlugin().error(message, e);

			return false;
		}

		return true;
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see org.eclipse.eef.ide.api.extensions.AbstractRegistryEventListener#processRemoval(org.eclipse.core.runtime.IConfigurationElement)
	 */
	@Override
	protected boolean processRemoval(IConfigurationElement configurationElement) {
		return this.itemRegistry.remove(configurationElement.getAttribute(ID_DESCRIPTOR_ATTR)) != null;
	}

}

Back to the top