Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 413e9148822ce76dae500aece2c25668a9265cea (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
/*******************************************************************************
 * Copyright (c) 2010 Wind River 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:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.dsf.gdb.internal.ui.tracepoints;

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

import org.eclipse.cdt.dsf.gdb.internal.ui.GdbUIPlugin;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.graphics.Image;
import org.osgi.framework.Bundle;

/**
 * Abstract image registry that allows for defining fallback paths for images.
 * This is copy of <code>org.eclipse.cdt.dsf.debug.internal.ui.disassembly.AbstractImageRegistry</code>
 * 
 * @since 2.1
 */
public abstract class AbstractImageRegistry extends ImageRegistry {
	private HashMap<String, String> fPlugins = new HashMap<String, String>();
	private HashMap<String, String[]> fLocations = new HashMap<String, String[]>();
	private URL fBaseUrl;

	protected AbstractImageRegistry(Plugin plugin) {
		fBaseUrl = plugin.getBundle().getEntry("/"); //$NON-NLS-1$
	}
	
	/**
	 * Defines the key for a local image, that must be found below the icons directory
	 * in the plugin.
	 * @param key Key by which the image can be referred by.
	 * @param dir Directory relative to icons/
	 * @param name The name of the file defining the icon. The name will be used as
	 *   key.
	 */
	protected void localImage(String key, String dir, String name) {
		if (dir== null || dir.equals(""))//$NON-NLS-1$
			fLocations.put(key, new String[] {"icons/" + name}); //$NON-NLS-1$
		else
			fLocations.put(key, new String[] {"icons/" + dir + "/" + name}); //$NON-NLS-1$ //$NON-NLS-2$
	}

	/**
	 * Defines the key for a non-local image, that must be found below the icons directory
	 * of some plugin.
	 * @param key Key by which the image can be referred by.
	 * @param plugin The plugin id, where the icon is searched.
	 * @param dirs A couple of directories below icons/ in the plugin. If loading fails,
	 * the next dir will be taken as fallback.
	 * @param name The name of the file defining the icon. The name will be used as
	 *   key.
	 */
	protected void externalImage(String key, String plugin, String[] dirs, String name) {
    	if (plugin != null) {
    		fPlugins.put(key, plugin);
    	}
    	String[] locations = new String[dirs.length];
    	for (int i = 0; i < dirs.length; i++) {
			String dir = dirs[i];
			if (dir== null || dir.equals(""))//$NON-NLS-1$
				locations[i] = "icons/" + name; //$NON-NLS-1$
			else
				locations[i] = "icons/" + dir + "/" + name; //$NON-NLS-1$ //$NON-NLS-2$
    	}
    	fLocations.put(key, locations);
	}
	
	// overrider
	@Override
	final public Image get(String key) {
	    Image i = super.get(key);
	    if (i != null) {
	        return i;
	    }
	    
	    ImageDescriptor d = createFileImageDescriptor(key);
	    if (d != null) {
	        put(key, d);
	        return super.get(key);
	    }
	    return null;
	}

	// overrider
	@Override
	final public ImageDescriptor getDescriptor(String key) {
	    ImageDescriptor d = super.getDescriptor(key);
	    if (d != null) {
	        return d;
	    }
	    
	    d = createFileImageDescriptor(key);
	    if (d != null) {
	        put(key, d);
	        return d;
	    }
	    return null;
	}

	private ImageDescriptor createFileImageDescriptor(String key) {
		URL url = fBaseUrl;
		String pluginId = fPlugins.get(key);
		if (pluginId != null) {
			Bundle bundle= Platform.getBundle(pluginId);
			if (bundle != null) {
				url = bundle.getEntry("/"); //$NON-NLS-1$
			}
		}
		String[] locations= fLocations.get(key);
		if (locations != null) {
			for (int i = 0; i < locations.length; i++) {
				String loc = locations[i];
				URL full;
				try {
					full = new URL(url, loc);
					ImageDescriptor candidate = ImageDescriptor.createFromURL(full);
					if (candidate != null && candidate.getImageData() != null) {
						return candidate;
					}
				} catch (MalformedURLException e) {
					GdbUIPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, GdbUIPlugin.PLUGIN_ID, "Malformed Icon URL", e)); //$NON-NLS-1$
				} catch (SWTException e) {
					// try the next one.
				}
			}
		}
	    return null;
	}

}

Back to the top