Skip to main content
summaryrefslogtreecommitdiffstats
blob: 85426dfb8770ad5f93409069e66792f4dae2f070 (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
/**
 * Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
 * All rights reserved. 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:
 *         Florian Pirchner - Initial implementation
 */
package org.eclipse.osbp.ecview.dsl.autowire.hook;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;
import org.eclipse.osbp.ecview.dsl.autowire.IAutowireDelegate;
import org.eclipse.osbp.ecview.dsl.derivedstate.UiModelDerivedStateComputerx;
import org.eclipse.osbp.ecview.semantic.uimodel.UiLayout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.Inject;
import com.google.inject.Injector;

/**
 * Delegates autowirings to registered extensions. See extensionpoint
 * "org.eclipse.osbp.ecview.dsl.autowireDelegate"
 */
public class ExtensionsAutowireDelegate implements IAutowireDelegate {

	private static final String DEFAULT_IMPL_ID = "org.eclipse.osbp.ecview.dsl.autowireDelegate";

	private static final Logger LOGGER = LoggerFactory
			.getLogger(ExtensionsAutowireDelegate.class);

	private static final String ATTR_ID = "id"; //$NON-NLS-1$
	private static final String ATTR_AUTOWIREHOOK = "autowireDelegateClass"; //$NON-NLS-1$
	private static final String AUTOWIRE_HOOK_EXTPT = "autowireDelegate"; //$NON-NLS-1$

	@Inject
	private Injector injector;

	private IAutowireDelegate delegate;

	@Override
	public void autowire(UiLayout uiLayout,
			UiModelDerivedStateComputerx computer, boolean mobile) {
		ensureExtention();
		if (delegate != null) {
			delegate.autowire(uiLayout, computer, mobile);
		}
	}

	private void ensureExtention() {
		if (delegate != null) {
			return;
		}

		IExtensionRegistry registry = Platform.getExtensionRegistry();
		IExtensionPoint point = registry.getExtensionPoint(
				"org.eclipse.osbp.ecview.dsl", AUTOWIRE_HOOK_EXTPT);
		if (point == null) {
			return;
		}

		IConfigurationElement bestMatch = null;
		String bestMatchId = null;
		IExtension[] extensions = point.getExtensions();
		for (int i = 0; i < extensions.length; i++) {
			IConfigurationElement[] elements = extensions[i]
					.getConfigurationElements();
			for (int j = 0; j < elements.length; j++) {
				String _id = elements[j].getAttribute(ATTR_ID);
				if (bestMatchId == null) {
					bestMatchId = _id;
					bestMatch = elements[j];
					if (!bestMatchId.equals(DEFAULT_IMPL_ID)) {
						// if not the default impl, it is a better match and use
						// it
						break;
					} else {
						// wait for better match
					}
				} else {
					if (!_id.equals(DEFAULT_IMPL_ID)) {
						// if not the default impl, it is a better match and use
						// it
						bestMatchId = _id;
						bestMatch = elements[j];
						break;
					}
				}
			}
		}

		if (bestMatch != null) {
			try {
				delegate = (IAutowireDelegate) bestMatch
						.createExecutableExtension(ATTR_AUTOWIREHOOK);
				injector.injectMembers(delegate);
			} catch (CoreException e) {
				LOGGER.error("{}", e);
			}
		}
	}
}

Back to the top