9
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2004, 2006 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.resource.LocalResourceManager;
|
|
14
|
|
15 import dwtx.jface.resource.AbstractResourceManager;
|
|
16 import dwtx.jface.resource.ResourceManager;
|
|
17 import dwtx.jface.resource.DeviceResourceDescriptor;
|
|
18
|
|
19 import dwt.events.DisposeEvent;
|
|
20 import dwt.events.DisposeListener;
|
|
21 import dwt.graphics.Device;
|
|
22 import dwt.graphics.Image;
|
|
23 import dwt.widgets.Control;
|
|
24
|
|
25 import dwt.dwthelper.utils;
|
|
26
|
|
27 /**
|
|
28 * A local registry that shares its resources with some global registry.
|
|
29 * LocalResourceManager is typically used to safeguard against leaks. Clients
|
|
30 * can use a nested registry to allocate and deallocate resources in the
|
|
31 * global registry. Calling dispose() on the nested registry will deallocate
|
|
32 * everything allocated for the nested registry without affecting the rest
|
|
33 * of the global registry.
|
|
34 * <p>
|
|
35 * A nested registry can be used to manage the resources for, say, a dialog
|
|
36 * box.
|
|
37 * </p>
|
|
38 * @since 3.1
|
|
39 */
|
|
40 public final class LocalResourceManager : AbstractResourceManager {
|
|
41
|
|
42 private ResourceManager parentRegistry;
|
|
43
|
|
44 /**
|
|
45 * Creates a local registry that delegates to the given global registry
|
|
46 * for all resource allocation and deallocation.
|
|
47 *
|
|
48 * @param parentRegistry global registry
|
|
49 */
|
|
50 public this(ResourceManager parentRegistry) {
|
|
51 this.parentRegistry = parentRegistry;
|
|
52 }
|
|
53
|
|
54 /**
|
|
55 * Creates a local registry that wraps the given global registry. Anything
|
|
56 * allocated by this registry will be automatically cleaned up with the given
|
|
57 * control is disposed. Note that registries created in this way should not
|
|
58 * be used to allocate any resource that must outlive the given control.
|
|
59 *
|
|
60 * @param parentRegistry global registry that handles resource allocation
|
|
61 * @param owner control whose disposal will trigger cleanup of everything
|
|
62 * in the registry.
|
|
63 */
|
|
64 public this(ResourceManager parentRegistry, Control owner) {
|
|
65 this(parentRegistry);
|
|
66
|
|
67 owner.addDisposeListener(new class DisposeListener {
|
|
68 /* (non-Javadoc)
|
|
69 * @see dwt.events.DisposeListener#widgetDisposed(dwt.events.DisposeEvent)
|
|
70 */
|
|
71 public void widgetDisposed(DisposeEvent e) {
|
|
72 this.outer.dispose();
|
|
73 }
|
|
74 });
|
|
75 }
|
|
76
|
|
77 /* (non-Javadoc)
|
|
78 * @see dwtx.jface.resource.ResourceManager#getDevice()
|
|
79 */
|
|
80 public Device getDevice() {
|
|
81 return parentRegistry.getDevice();
|
|
82 }
|
|
83
|
|
84 /* (non-Javadoc)
|
|
85 * @see dwtx.jface.resource.AbstractResourceManager#allocate(dwtx.jface.resource.DeviceResourceDescriptor)
|
|
86 */
|
|
87 protected Object allocate(DeviceResourceDescriptor descriptor) {
|
|
88 return parentRegistry.create(descriptor);
|
|
89 }
|
|
90
|
|
91 /* (non-Javadoc)
|
|
92 * @see dwtx.jface.resource.AbstractResourceManager#deallocate(java.lang.Object, dwtx.jface.resource.DeviceResourceDescriptor)
|
|
93 */
|
|
94 protected void deallocate(Object resource,
|
|
95 DeviceResourceDescriptor descriptor) {
|
|
96
|
|
97 parentRegistry.destroy(descriptor);
|
|
98 }
|
|
99
|
|
100 /* (non-Javadoc)
|
|
101 * @see dwtx.jface.resource.ResourceManager#getDefaultImage()
|
|
102 */
|
|
103 protected Image getDefaultImage() {
|
|
104 return parentRegistry.getDefaultImage_();
|
|
105 }
|
|
106 }
|