Skip to main content
summaryrefslogtreecommitdiffstats
blob: fddc012d48486ac64731784a6c7b24eb7aa2dbd3 (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*******************************************************************************
 * Copyright (c) 2013, 2015 Tasktop Technologies 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:
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.internal.tasks.ui.util;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IAdaptable;
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.ISafeRunnable;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.SafeRunner;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.mylyn.commons.core.StatusHandler;
import org.eclipse.mylyn.commons.ui.CommonImages;
import org.eclipse.mylyn.internal.tasks.core.util.ContributorBlackList;
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin;
import org.eclipse.mylyn.tasks.core.AbstractRepositoryConnector;
import org.eclipse.mylyn.tasks.core.spi.RepositoryConnectorBranding;
import org.eclipse.mylyn.tasks.ui.AbstractRepositoryConnectorUi;
import org.eclipse.mylyn.tasks.ui.TasksUi;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.ui.plugin.AbstractUIPlugin;

public class RepositoryConnectorUiExtensionReader {

	private static final String EXTENSION_REPOSITORIES = "org.eclipse.mylyn.tasks.ui.repositories"; //$NON-NLS-1$

	public static final String ELMNT_REPOSITORY_UI = "connectorUi"; //$NON-NLS-1$

	private static final String ATTR_BRANDING_ICON = "brandingIcon"; //$NON-NLS-1$

	private static final String ATTR_OVERLAY_ICON = "overlayIcon"; //$NON-NLS-1$

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

	private final IExtensionRegistry registry;

	/**
	 * Plug-in ids of connector extensions that failed to load.
	 */
	private final ContributorBlackList blackList;

	public RepositoryConnectorUiExtensionReader(IExtensionRegistry registry, ContributorBlackList blackList) {
		Assert.isNotNull(registry);
		Assert.isNotNull(blackList);
		this.registry = registry;
		this.blackList = blackList;
	}

	public void registerConnectorUis() {
		registerFromExtensionPoint();
		registerFromAdaptable();
		registerConnetorToConnectorUi();
	}

	private void registerConnetorToConnectorUi() {
		for (AbstractRepositoryConnector connector : TasksUi.getRepositoryManager().getRepositoryConnectors()) {
			AbstractRepositoryConnectorUi connectorUi = TasksUiPlugin.getConnectorUi(connector.getConnectorKind());
			if (connectorUi != null) {
				setConnectorForConnectorUi(connector, connectorUi);
			}
		}
	}

	private void registerFromAdaptable() {
		for (AbstractRepositoryConnector connector : TasksUi.getRepositoryManager().getRepositoryConnectors()) {
			if (TasksUiPlugin.getConnectorUi(connector.getConnectorKind()) == null) {
				registerFromAdaptable(connector);
			}
		}
	}

	private void registerFromAdaptable(final AbstractRepositoryConnector connector) {
		SafeRunner.run(new ISafeRunnable() {
			@Override
			public void run() throws Exception {
				AbstractRepositoryConnectorUi connectorUi = loadAdapter(connector, AbstractRepositoryConnectorUi.class);
				if (connectorUi != null) {
					TasksUiPlugin.getDefault().addRepositoryConnectorUi(connectorUi);
				}

				RepositoryConnectorBranding branding = loadAdapter(connector, RepositoryConnectorBranding.class);
				if (branding != null) {
					InputStream brandingImageData = branding.getBrandingImageData();
					if (brandingImageData != null) {
						try {
							TasksUiPlugin.getDefault().addBrandingIcon(connector.getConnectorKind(),
									getImage(brandingImageData));
						} finally {
							closeQuietly(brandingImageData);
						}
					}
					InputStream overlayImageData = branding.getOverlayImageData();
					if (overlayImageData != null) {
						try {
							TasksUiPlugin.getDefault().addOverlayIcon(connector.getConnectorKind(),
									getImageDescriptor(overlayImageData));
						} finally {
							closeQuietly(brandingImageData);
						}
					}
				}
			}

			private void closeQuietly(InputStream in) {
				try {
					in.close();
				} catch (IOException e) {
					// ignore
				}
			}

			@SuppressWarnings("unchecked")
			public <T> T loadAdapter(final AbstractRepositoryConnector connector, Class<T> klass) {
				T adapter = null;
				if (connector instanceof IAdaptable) {
					adapter = (T) ((IAdaptable) connector).getAdapter(klass);
				}
				if (adapter == null) {
					adapter = (T) Platform.getAdapterManager().loadAdapter(connector, klass.getName());
				}
				return adapter;
			}

			private ImageDescriptor getImageDescriptor(InputStream in) {
				return ImageDescriptor.createFromImageData(new ImageData(in));
			}

			private Image getImage(InputStream in) {
				return CommonImages.getImage(getImageDescriptor(in));
			}

			@Override
			public void handleException(Throwable e) {
				StatusHandler.log(new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN, NLS.bind(
						"Loading of connector ui for kind ''{0}'' failed.", connector.getConnectorKind()), e)); //$NON-NLS-1$
			}
		});
	}

	private void registerFromExtensionPoint() {
		IExtensionPoint repositoriesExtensionPoint = registry.getExtensionPoint(EXTENSION_REPOSITORIES);
		IExtension[] repositoryExtensions = repositoriesExtensionPoint.getExtensions();
		for (IExtension repositoryExtension : repositoryExtensions) {
			IConfigurationElement[] elements = repositoryExtension.getConfigurationElements();
			for (IConfigurationElement element : elements) {
				if (!blackList.isDisabled(element)) {
					if (element.getName().equals(ELMNT_REPOSITORY_UI)) {
						registerRepositoryConnectorUi(element);
					}
				}
			}
		}
	}

	private void registerRepositoryConnectorUi(IConfigurationElement element) {
		try {
			Object connectorUiObject = element.createExecutableExtension(ATTR_CLASS);
			if (connectorUiObject instanceof AbstractRepositoryConnectorUi) {
				AbstractRepositoryConnectorUi connectorUi = (AbstractRepositoryConnectorUi) connectorUiObject;
				if (TasksUiPlugin.getConnector(connectorUi.getConnectorKind()) != null) {
					TasksUiPlugin.getDefault().addRepositoryConnectorUi(connectorUi);

					String iconPath = element.getAttribute(ATTR_BRANDING_ICON);
					if (iconPath != null) {
						ImageDescriptor descriptor = AbstractUIPlugin.imageDescriptorFromPlugin(
								element.getContributor().getName(), iconPath);
						if (descriptor != null) {
							TasksUiPlugin.getDefault().addBrandingIcon(connectorUi.getConnectorKind(),
									CommonImages.getImage(descriptor));
						}
					}
					String overlayIconPath = element.getAttribute(ATTR_OVERLAY_ICON);
					if (overlayIconPath != null) {
						ImageDescriptor descriptor = AbstractUIPlugin.imageDescriptorFromPlugin(
								element.getContributor().getName(), overlayIconPath);
						if (descriptor != null) {
							TasksUiPlugin.getDefault().addOverlayIcon(connectorUi.getConnectorKind(), descriptor);
						}
					}
				} else {
					StatusHandler.log(new Status(
							IStatus.ERROR,
							TasksUiPlugin.ID_PLUGIN,
							NLS.bind(
									"Ignoring connector ui for kind ''{0}'' without corresponding core contributed by ''{1}''.", connectorUi.getConnectorKind(), element.getContributor().getName()))); //$NON-NLS-1$
				}
			} else {
				StatusHandler.log(new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN, "Could not load connector ui " //$NON-NLS-1$
						+ connectorUiObject.getClass().getCanonicalName()));
			}
		} catch (Throwable e) {
			StatusHandler.log(new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN, "Could not load connector ui", e)); //$NON-NLS-1$
		}
	}

	private static void setConnectorForConnectorUi(AbstractRepositoryConnector connector,
			AbstractRepositoryConnectorUi connectorUi) {
		// need reflection since the field is private
		try {
			Field field = AbstractRepositoryConnectorUi.class.getDeclaredField("connector"); //$NON-NLS-1$
			field.setAccessible(true);
			field.set(connectorUi, connector);
		} catch (Exception e) {
			StatusHandler.log(new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN, "Unable to call setConnector()", e)); //$NON-NLS-1$
		}
	}

}

Back to the top