1 /*******************************************************************************
2  * Copyright (c) 2010, 2015 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 
15 package org.eclipse.ui.internal.forms.widgets;
16 
17 import java.util.HashMap;
18 import java.util.Iterator;
19 import java.util.Set;
20 
21 import org.eclipse.jface.resource.JFaceResources;
22 import org.eclipse.jface.resource.LocalResourceManager;
23 import org.eclipse.swt.widgets.Display;
24 
25 /**
26  * Class which can get the appropriate resource manager for a Display
27  * In most applications there will be only one display but see
28  * Bug 295981 for an example where this is not the case
29  */
30 
31 public class ResourceManagerManger {
32 
33 	private HashMap<Display, LocalResourceManager> resourceManagers;
34 
getResourceManager(Display display)35 	public LocalResourceManager getResourceManager(Display display) {
36 		if (resourceManagers == null) {
37 			resourceManagers = new HashMap<>();
38 		}
39 		LocalResourceManager resources = resourceManagers.get(display);
40 		if (resources == null) {
41 			pruneResourceManagers();
42 			resources = new LocalResourceManager(JFaceResources.getResources(display));
43 			resourceManagers.put(display, resources);
44 		}
45 		return resources;
46 	}
47 
pruneResourceManagers()48 	private void pruneResourceManagers() {
49 		Set<Display> displays = resourceManagers.keySet();
50 		for (Iterator<Display> iter = displays.iterator(); iter.hasNext();) {
51 			Display display = iter.next();
52 			if (display.isDisposed()) {
53 				resourceManagers.remove(display);
54 				iter = displays.iterator();
55 			}
56 		}
57 	}
58 }
59