Package: ImageDescriptorRegistry$1

ImageDescriptorRegistry$1

nameinstructionbranchcomplexitylinemethod
run()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
{...}
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * #%~
3: * org.overture.ide.ui
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.ide.ui.internal.viewsupport;
23:
24: import java.util.HashMap;
25: import java.util.Iterator;
26:
27: import org.eclipse.core.runtime.Assert;
28:
29: import org.eclipse.jface.resource.ImageDescriptor;
30: import org.eclipse.swt.graphics.Image;
31: import org.eclipse.swt.widgets.Display;
32: import org.overture.ide.ui.internal.util.SWTUtil;
33:
34: public class ImageDescriptorRegistry {
35:         private HashMap<ImageDescriptor, Image> fRegistry= new HashMap<ImageDescriptor, Image>(10);
36:         private Display fDisplay;
37:
38:         /**
39:          * Creates a new image descriptor registry for the current or default display,
40:          * respectively.
41:          */
42:         public ImageDescriptorRegistry() {
43:                 this(SWTUtil.getStandardDisplay());
44:         }
45:
46:         /**
47:          * Creates a new image descriptor registry for the given display. All images
48:          * managed by this registry will be disposed when the display gets disposed.
49:          *
50:          * @param display the display the images managed by this registry are allocated for
51:          */
52:         public ImageDescriptorRegistry(Display display) {
53:                 fDisplay= display;
54:                 Assert.isNotNull(fDisplay);
55:                 hookDisplay();
56:         }
57:
58:         /**
59:          * Returns the image associated with the given image descriptor.
60:          *
61:          * @param descriptor the image descriptor for which the registry manages an image,
62:          * or <code>null</code> for a missing image descriptor
63:          * @return the image associated with the image descriptor or <code>null</code>
64:          * if the image descriptor can't create the requested image.
65:          */
66:         public Image get(ImageDescriptor descriptor) {
67:                 if (descriptor == null)
68:                         descriptor= ImageDescriptor.getMissingImageDescriptor();
69:
70:                 Image result= (Image)fRegistry.get(descriptor);
71:                 if (result != null)
72:                         return result;
73:
74:                 Assert.isTrue(fDisplay == SWTUtil.getStandardDisplay(), "Allocating image for wrong display."); //$NON-NLS-1$
75:                 result= descriptor.createImage();
76:                 if (result != null)
77:                         fRegistry.put(descriptor, result);
78:                 return result;
79:         }
80:
81:         /**
82:          * Disposes all images managed by this registry.
83:          */
84:         public void dispose() {
85:                 for (Iterator<Image> iter= fRegistry.values().iterator(); iter.hasNext(); ) {
86:                         Image image= (Image)iter.next();
87:                         image.dispose();
88:                 }
89:                 fRegistry.clear();
90:         }
91:
92:         private void hookDisplay() {
93:                 fDisplay.disposeExec(new Runnable() {
94:                         public void run() {
95:                                 dispose();
96:                         }
97:                 });
98:         }
99: }