|
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$ |
41 | | - } |
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$ |
| 38 | + @Deprecated(forRemoval = true) |
| 39 | + public static @Nullable ImageDescriptor DESC_TAB_DEBUGGER = null; |
48 | 40 |
|
49 | | - public static final ImageDescriptor DESC_TAB_DEBUGGER = createManaged(T_TABS, IMG_VIEW_DEBUGGER_TAB); |
50 | | - |
51 | | - public static void initialize() { |
52 | | - } |
| 41 | + public static void initialize(ImageRegistry registry) { |
| 42 | + imageRegistry = registry; |
| 43 | + declareRegistryImage(IMG_VIEW_DEBUGGER_TAB, VIEWS + "debugger_tab.svg"); |
53 | 44 |
|
54 | | - private static ImageDescriptor createManaged(String prefix, String name) { |
55 | | - return createManaged(imageRegistry, prefix, name); |
| 45 | + // For backwards compatibility. Remove later on. |
| 46 | + DESC_TAB_DEBUGGER = getImageRegistry().getDescriptor(IMG_VIEW_DEBUGGER_TAB); |
56 | 47 | } |
57 | 48 |
|
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; |
| 49 | + private static void declareRegistryImage(String key, String path) { |
| 50 | + ImageDescriptor desc = ImageDescriptor.getMissingImageDescriptor(); |
| 51 | + Bundle bundle = Platform.getBundle(DSPPlugin.PLUGIN_ID); |
| 52 | + URL url = null; |
| 53 | + if (bundle != null) { |
| 54 | + url = FileLocator.find(bundle, new Path(path), null); |
| 55 | + if (url != null) { |
| 56 | + desc = ImageDescriptor.createFromURL(url); |
| 57 | + } |
| 58 | + } |
| 59 | + getImageRegistry().put(key, desc); |
63 | 60 | } |
64 | 61 |
|
65 | 62 | 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 | | - } |
| 63 | + return getImageRegistry().get(key); |
78 | 64 | } |
79 | 65 |
|
80 | 66 | /** |
81 | 67 | * Helper method to access the image registry from the JavaPlugin class. |
82 | 68 | */ |
83 | 69 | static ImageRegistry getImageRegistry() { |
| 70 | + ImageRegistry imageRegistry = DSPImages.imageRegistry; |
| 71 | + if (imageRegistry == null) { |
| 72 | + imageRegistry = DSPImages.imageRegistry = DSPPlugin.getDefault().getImageRegistry(); |
| 73 | + } |
84 | 74 | return imageRegistry; |
85 | 75 | } |
86 | 76 | } |
0 commit comments