Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bd0a2031614d2ee71e40fdc80422abf9c44ad5b5 (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
/*******************************************************************************
 * Copyright (c) 2005, 2019 IBM Corporation and others.
 *
 * 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.ui.internal.cheatsheets.composite.views;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.internal.cheatsheets.CheatSheetPlugin;
import org.eclipse.ui.internal.cheatsheets.ICheatSheetResource;
import org.eclipse.ui.internal.cheatsheets.Messages;
import org.eclipse.ui.internal.cheatsheets.registry.CheatSheetRegistryReader;
import org.eclipse.ui.internal.provisional.cheatsheets.TaskExplorer;
import org.osgi.framework.Bundle;

public class TaskExplorerManager {
private static TaskExplorerManager instance;

	private Map<String, Image> images;

	private TaskExplorerManager() {

	}

	public static TaskExplorerManager getInstance() {
		if (instance == null) {
			instance = new TaskExplorerManager();
		}
		return instance;
	}

	public TaskExplorer getExplorer(String explorerKind) {
		CheatSheetRegistryReader.TaskExplorerNode explorerInfo =
			CheatSheetRegistryReader.getInstance().findTaskExplorer(explorerKind);
		if (explorerInfo != null) {
			TaskExplorer explorerInstance = null;
			Class<?> extClass = null;
			String className = explorerInfo.getClassName();
			try {
				Bundle bundle = Platform.getBundle(explorerInfo.getPluginId());
				extClass = bundle.loadClass(className);
			} catch (Exception e) {
				String message = NLS.bind(Messages.ERROR_LOADING_CLASS, (new Object[] {className}));
				Status status = new Status(IStatus.ERROR, ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, IStatus.OK, message, e);
				CheatSheetPlugin.getPlugin().getLog().log(status);
			}
			try {
				if (extClass != null) {
					Constructor<?> c = extClass.getConstructor();
					explorerInstance = (TaskExplorer) c.newInstance();
				}
			} catch (Exception e) {
				String message = NLS.bind(Messages.ERROR_CREATING_CLASS, (new Object[] {className}));
				IStatus status = new Status(IStatus.ERROR, ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, IStatus.OK, message, e);
				CheatSheetPlugin.getPlugin().getLog().log(status);
			}

			return explorerInstance;
		}

		return null;
	}

	private ImageDescriptor getImageDescriptor(String explorerKind) {
		CheatSheetRegistryReader.TaskExplorerNode explorerInfo =
			CheatSheetRegistryReader.getInstance().findTaskExplorer(explorerKind);
		if (explorerInfo == null) {
			return null;
		}
		String iconPath = explorerInfo.getIconPath();
		if (iconPath == null) {
			return null;
		}
		Bundle bundle = Platform.getBundle(explorerInfo.getPluginId());
		URL url = FileLocator.find(bundle, new Path(iconPath), null);
		try {
			url = FileLocator.resolve(url);
			return ImageDescriptor.createFromURL(url);
		} catch (IOException e) {
			return null;
		}
	}

	private Map<String, Image> getImages() {
		if (images == null) {
			initImages();
		}
		return images;
	}


	private void initImages() {
		if (images == null) {
			images = new HashMap<>();
			String[] ids = CheatSheetRegistryReader.getInstance().getExplorerIds();
			for (String id : ids) {
				ImageDescriptor descriptor = getImageDescriptor(id);
				if (descriptor != null) {
					images.put(id, descriptor.createImage());
				}
			}
		}
	}

	public String getName(String explorerKind) {
		CheatSheetRegistryReader.TaskExplorerNode explorerInfo =
			CheatSheetRegistryReader.getInstance().findTaskExplorer(explorerKind);
		if (explorerInfo != null) {
			return explorerInfo.getName();
		}
		return null;
	}

	public Image getImage(String id) {
		return getImages().get(id);
	}

}

Back to the top