Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 53acc71d4b945bc64b056aad9a83bd351cc9bee5 (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
/*******************************************************************************
 * Copyright (c) 2014 QNX Software Systems 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:
 *     Doug Schaefer
 *******************************************************************************/
package org.eclipse.cdt.launchbar.ui.internal;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.eclipse.cdt.launchbar.core.ILaunchDescriptor;
import org.eclipse.cdt.launchbar.core.ILaunchTarget;
import org.eclipse.cdt.launchbar.core.ILaunchTargetType;
import org.eclipse.cdt.launchbar.core.internal.ExecutableExtension;
import org.eclipse.cdt.launchbar.core.internal.LaunchBarManager;
import org.eclipse.cdt.launchbar.ui.IHoverProvider;
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.Platform;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.INewWizard;

public class LaunchBarUIManager {

	LaunchBarManager manager;
	Map<String, ExecutableExtension<ILabelProvider>> descriptorLabelProviders = new HashMap<>();
	Map<String, LaunchBarTargetContribution> targetContributions = new HashMap<>();

	private final LaunchBarTargetContribution DEFAULT_CONTRIBUTION = new LaunchBarTargetContribution(null, null, null, null,
	        null, null);

	public LaunchBarUIManager(LaunchBarManager manager) {
		this.manager = manager;

		IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint(Activator.PLUGIN_ID, "launchBarUIContributions");
		IExtension[] extensions = point.getExtensions();
		for (IExtension extension : extensions) {
			for (IConfigurationElement element : extension.getConfigurationElements()) {
				String elementName = element.getName();
				if (elementName.equals("descriptorUI")) {
					String descriptorTypeId = element.getAttribute("descriptorTypeId");
					ExecutableExtension<ILabelProvider> labelProvider = new ExecutableExtension<>(element, "labelProvider");
					descriptorLabelProviders.put(descriptorTypeId, labelProvider);
				} else if (elementName.equals("targetUI")) {
					String targetTypeId = element.getAttribute("targetTypeId");
					String targetName = element.getAttribute("name");
					String iconStr = element.getAttribute("icon");
					ExecutableExtension<ILabelProvider> labelProvider = new ExecutableExtension<ILabelProvider>(element, "labelProvider");

					ExecutableExtension<IHoverProvider> hoverProvider = null;
					if (element.getAttribute("hoverProvider") != null) {
						hoverProvider = new ExecutableExtension<IHoverProvider>(element, "hoverProvider");
					}

					String editCommandId = element.getAttribute("editCommandId");
					
					ExecutableExtension<INewWizard> newWizard = null;
					if (element.getAttribute("newWizard") != null) {
						newWizard = new ExecutableExtension<INewWizard>(element, "newWizard");
					}

					targetContributions.put(targetTypeId, new LaunchBarTargetContribution(targetName, iconStr,
					        labelProvider, hoverProvider, editCommandId, newWizard));
				}
			}
		}
	}

	public LaunchBarManager getManager() {
		return manager;
	}

	public ILabelProvider getLabelProvider(ILaunchDescriptor descriptor) throws CoreException {
		ExecutableExtension<ILabelProvider> provider = descriptorLabelProviders.get(manager.getDescriptorTypeId(descriptor.getType()));
		return provider != null ? provider.get() : null;
	}

	public String getTargetTypeName(ILaunchTarget target) {
		return getTargetTypeName(target.getType());
	}

	public String getTargetTypeName(ILaunchTargetType targetType) {
		String typeId = manager.getTargetTypeId(targetType);
		String name = targetContributions.get(typeId).name;
		return name != null ? name : typeId;
	}

	public Image getTargetTypeIcon(ILaunchTargetType targetType) {
		String typeId = manager.getTargetTypeId(targetType);
		return targetContributions.get(typeId).getIcon();
	}

	public ILabelProvider getLabelProvider(ILaunchTarget target) throws CoreException {
		ExecutableExtension<ILabelProvider> provider = getContribution(target).labelProvider;
		return provider != null ? provider.get() : null;
	}

	public IHoverProvider getHoverProvider(ILaunchTarget target) throws CoreException {
		ExecutableExtension<IHoverProvider> hoverProvider = getContribution(target).hoverProvider;
		return hoverProvider != null ? hoverProvider.get() : null;
	}

	public String getEditCommand(ILaunchTarget target) {
		return getContribution(target).editCommandId;
	}

	public Map<ILaunchTargetType, ExecutableExtension<INewWizard>> getNewTargetWizards() {
		Map<ILaunchTargetType, ExecutableExtension<INewWizard>> wizards = new HashMap<>();
		for (Entry<String, LaunchBarTargetContribution> contrib : targetContributions.entrySet()) {
			if (contrib.getValue().newWizard != null) {
				ILaunchTargetType type = manager.getLaunchTargetType(contrib.getKey());
				if (type != null) {
					wizards.put(type, contrib.getValue().newWizard);
				}
			}
		}
		return wizards;
	}

	public Map<String, Image> getTargetIcons() {
		Map<String, Image> icons = new HashMap<>();
		for (LaunchBarTargetContribution contribution : targetContributions.values()) {
			Image icon = contribution.getIcon();
			if (icon != null) {
				icons.put(contribution.name, icon);
			}
		}
		return icons;
	}

	private LaunchBarTargetContribution getContribution(ILaunchTarget target) {
		LaunchBarTargetContribution c = targetContributions.get(manager.getTargetTypeId(target.getType()));
		if (c == null) {
			return DEFAULT_CONTRIBUTION;
		}
		return c;
	}

	private class LaunchBarTargetContribution {
		String name;
		String iconStr;
		Image icon;
		ExecutableExtension<ILabelProvider> labelProvider;
		ExecutableExtension<IHoverProvider> hoverProvider;
		String editCommandId;
		ExecutableExtension<INewWizard> newWizard;

		LaunchBarTargetContribution(String name, String iconStr,
				ExecutableExtension<ILabelProvider> labelProvider,
		        ExecutableExtension<IHoverProvider> hoverProvider,
		        String editCommand,
		        ExecutableExtension<INewWizard> newWizard) {
			this.name = name;
			this.iconStr = iconStr;
			this.icon = null;
			this.labelProvider = labelProvider;
			this.hoverProvider = hoverProvider;
			this.editCommandId = editCommand;
			this.newWizard = newWizard;
		}

		Image getIcon() {
			if (icon == null) {
				if (iconStr != null && !iconStr.isEmpty()) {
					try {
						icon = ImageDescriptor.createFromURL(new URL(iconStr)).createImage();
					} catch (MalformedURLException e) {
						Activator.log(e);
					}
				}
			}
			return icon;
		}

	}
}

Back to the top