|
13 | 13 | *******************************************************************************/ |
14 | 14 | package org.eclipse.lsp4e.debug; |
15 | 15 |
|
16 | | -import java.net.MalformedURLException; |
17 | 16 | import java.net.URL; |
18 | 17 |
|
| 18 | +import org.eclipse.core.runtime.FileLocator; |
| 19 | +import org.eclipse.core.runtime.Path; |
19 | 20 | import org.eclipse.core.runtime.Platform; |
20 | 21 | import org.eclipse.jdt.annotation.Nullable; |
21 | 22 | import org.eclipse.jface.resource.ImageDescriptor; |
22 | 23 | import org.eclipse.jface.resource.ImageRegistry; |
23 | 24 | import org.eclipse.swt.graphics.Image; |
| 25 | +import org.osgi.framework.Bundle; |
24 | 26 |
|
25 | 27 | public final class DSPImages { |
26 | 28 | private DSPImages() { |
27 | 29 | // private constructor to avoid instances, requested by sonar |
28 | 30 | } |
29 | 31 |
|
30 | | - private static final String NAME_PREFIX = DSPPlugin.PLUGIN_ID + '.'; |
31 | | - private static final int NAME_PREFIX_LENGTH = NAME_PREFIX.length(); |
| 32 | + private static @Nullable ImageRegistry imageRegistry; |
| 33 | + private static final String ICONS_PATH = "$nl$/icons/"; //$NON-NLS-1$ |
| 34 | + private static final String VIEWS = ICONS_PATH + "view16/"; |
32 | 35 |
|
33 | | - // The plugin registry |
34 | | - private static ImageRegistry imageRegistry = new ImageRegistry(); |
| 36 | + public static final String IMG_VIEW_DEBUGGER_TAB = "IMG_DEBUGGER_TAB"; //$NON-NLS-1$ |
35 | 37 |
|
36 | | - // Subdirectory (under the package containing this class) where 16 color images |
37 | | - // are |
38 | | - private static URL fgIconBaseURL; |
39 | | - static { |
40 | | - fgIconBaseURL = Platform.getBundle(DSPPlugin.PLUGIN_ID).getEntry("/icons/"); //$NON-NLS-1$ |
| 38 | + public static void initialize(ImageRegistry registry) { |
| 39 | + imageRegistry = registry; |
| 40 | + declareRegistryImage(IMG_VIEW_DEBUGGER_TAB, VIEWS + "debugger_tab.svg"); |
41 | 41 | } |
42 | 42 |
|
43 | | - private static final String T_TABS = "view16/"; //$NON-NLS-1$ |
44 | | - @SuppressWarnings("unused") // none yet, leave for the future |
45 | | - private static final String T_OBJS = "obj16/"; //$NON-NLS-1$ |
46 | | - |
47 | | - public static final String IMG_VIEW_DEBUGGER_TAB = NAME_PREFIX + "debugger_tab.svg"; //$NON-NLS-1$ |
48 | | - |
49 | | - public static final ImageDescriptor DESC_TAB_DEBUGGER = createManaged(T_TABS, IMG_VIEW_DEBUGGER_TAB); |
50 | | - |
51 | | - public static void initialize() { |
52 | | - } |
53 | | - |
54 | | - private static ImageDescriptor createManaged(String prefix, String name) { |
55 | | - return createManaged(imageRegistry, prefix, name); |
56 | | - } |
57 | | - |
58 | | - private static ImageDescriptor createManaged(ImageRegistry registry, String prefix, String name) { |
59 | | - ImageDescriptor result = ImageDescriptor |
60 | | - .createFromURL(makeIconFileURL(prefix, name.substring(NAME_PREFIX_LENGTH))); |
61 | | - registry.put(name, result); |
62 | | - return result; |
| 43 | + private static void declareRegistryImage(String key, String path) { |
| 44 | + ImageDescriptor desc = ImageDescriptor.getMissingImageDescriptor(); |
| 45 | + Bundle bundle = Platform.getBundle(DSPPlugin.PLUGIN_ID); |
| 46 | + URL url = null; |
| 47 | + if (bundle != null) { |
| 48 | + url = FileLocator.find(bundle, new Path(path), null); |
| 49 | + if (url != null) { |
| 50 | + desc = ImageDescriptor.createFromURL(url); |
| 51 | + } |
| 52 | + } |
| 53 | + getImageRegistry().put(key, desc); |
63 | 54 | } |
64 | 55 |
|
65 | 56 | public static @Nullable Image get(String key) { |
66 | | - return imageRegistry.get(key); |
67 | | - } |
68 | | - |
69 | | - private static @Nullable URL makeIconFileURL(String prefix, String name) { |
70 | | - final var buffer = new StringBuilder(prefix); |
71 | | - buffer.append(name); |
72 | | - try { |
73 | | - return new URL(fgIconBaseURL, buffer.toString()); |
74 | | - } catch (MalformedURLException e) { |
75 | | - DSPPlugin.logError(e); |
76 | | - return null; |
77 | | - } |
| 57 | + return getImageRegistry().get(key); |
78 | 58 | } |
79 | 59 |
|
80 | 60 | /** |
81 | 61 | * Helper method to access the image registry from the JavaPlugin class. |
82 | 62 | */ |
83 | 63 | static ImageRegistry getImageRegistry() { |
| 64 | + ImageRegistry imageRegistry = DSPImages.imageRegistry; |
| 65 | + if (imageRegistry == null) { |
| 66 | + imageRegistry = DSPImages.imageRegistry = DSPPlugin.getDefault().getImageRegistry(); |
| 67 | + } |
84 | 68 | return imageRegistry; |
85 | 69 | } |
86 | 70 | } |
0 commit comments