Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7747817ab3f421e9165fd6b1f91a18a0de5aea39 (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
/*******************************************************************************
 * Copyright (c) 2014 Obeo.
 * 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:
 *     Obeo - initial API and implementation
 *******************************************************************************/
package org.eclipse.emf.compare.rcp.ui.internal.configuration.ui;

import java.util.Map;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.emf.compare.rcp.extension.AbstractRegistryEventListener;
import org.eclipse.emf.compare.rcp.internal.extension.IItemRegistry;
import org.eclipse.emf.compare.rcp.ui.internal.EMFCompareRCPUIMessages;

/**
 * Registry event listener used for {@link IConfiguratorUIRegistry}
 * 
 * @author <a href="mailto:arthur.daussy@obeo.fr">Arthur Daussy</a>
 */
public class ConfigurationUIRegistryEventListener extends AbstractRegistryEventListener {

	private static final String ELEMENT_TAG = "configurator"; //$NON-NLS-1$

	private static final String ITEM_TO_CONFIGURE_ATTR = "itemToConfigure"; //$NON-NLS-1$

	private static final String UI_PROVIDER_ATTR = "uiProvider"; //$NON-NLS-1$

	/** Storage for {@link IConfiguratorUIRegistry}. */
	private final Map<String, IConfigurationUIFactory> registry;

	/** Mirror registry holding item to configure. */
	private final IItemRegistry<?> registryOfConfiguredItem;

	/**
	 * Constructor.
	 * 
	 * @param namespace
	 *            The namespace of the extension point to be monitored.
	 * @param extensionPointID
	 *            The extension point ID to be monitored
	 * @param log
	 *            The log object to be used to log error and/or warning.
	 * @param registry
	 *            that will be filled
	 * @param registryOfConfiguredItem
	 *            Mirror registry holding item to configure.
	 */
	public ConfigurationUIRegistryEventListener(String namespace, String extensionPointID, ILog log,
			Map<String, IConfigurationUIFactory> registry, IItemRegistry<?> registryOfConfiguredItem) {
		super(namespace, extensionPointID, log);
		this.registry = registry;
		this.registryOfConfiguredItem = registryOfConfiguredItem;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.compare.rcp.extension.AbstractRegistryEventListener#validateExtensionElement(org.eclipse.core.runtime.IConfigurationElement)
	 */
	@Override
	protected boolean validateExtensionElement(IConfigurationElement element) {
		final boolean ret;
		if (ELEMENT_TAG.equals(element.getName())) {
			if (element.getAttribute(ITEM_TO_CONFIGURE_ATTR) == null) {
				logMissingAttribute(element, ITEM_TO_CONFIGURE_ATTR);
				ret = false;
			} else if (element.getAttribute(ITEM_TO_CONFIGURE_ATTR) != null) {
				Object itemToConfigure = registryOfConfiguredItem.getItemDescriptor(element
						.getAttribute(ITEM_TO_CONFIGURE_ATTR));
				if (itemToConfigure == null) {
					log(IStatus.WARNING, element, EMFCompareRCPUIMessages
							.getString("ConfigurationUIRegistryEventListener.INCORRECT_ID_PARAMETER")); //$NON-NLS-1$
					ret = false;
				} else {
					ret = true;
				}
			} else if (element.getAttribute(UI_PROVIDER_ATTR) == null) {
				logMissingAttribute(element, UI_PROVIDER_ATTR);
				ret = false;
			} else {
				ret = true;
			}
		} else {
			ret = false;
		}
		return ret;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.compare.rcp.extension.AbstractRegistryEventListener#addedValid(org.eclipse.core.runtime.IConfigurationElement)
	 */
	@Override
	protected boolean addedValid(IConfigurationElement element) {
		try {
			Object configurator = element.createExecutableExtension(UI_PROVIDER_ATTR);
			if (configurator instanceof IConfigurationUIFactory) {
				IConfigurationUIFactory configurationFactory = (IConfigurationUIFactory)configurator;
				String itemToConfigureID = element.getAttribute(ITEM_TO_CONFIGURE_ATTR);
				IConfigurationUIFactory previous = registry.put(itemToConfigureID, configurationFactory);
				if (previous != null) {
					log(IStatus.WARNING, element, EMFCompareRCPUIMessages.getString(
							"duplicate.extension", registry.getClass().getName())); //$NON-NLS-1$
				}
			} else {
				log(IStatus.WARNING, element, EMFCompareRCPUIMessages
						.getString("ConfigurationUIRegistryEventListener.INCORRECT_CONFIGURATOR_PARAMETER")); //$NON-NLS-1$
			}
		} catch (CoreException e) {
			e.printStackTrace();
			log(element, e);
		}
		return true;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.compare.rcp.extension.AbstractRegistryEventListener#removedValid(org.eclipse.core.runtime.IConfigurationElement)
	 */
	@Override
	protected boolean removedValid(IConfigurationElement element) {
		return registry.remove(element.getAttribute(ITEM_TO_CONFIGURE_ATTR)) != null;
	}

}

Back to the top