KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > resource > LocalResourceManager


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  *******************************************************************************/

11 package org.eclipse.jface.resource;
12
13 import org.eclipse.swt.events.DisposeEvent;
14 import org.eclipse.swt.events.DisposeListener;
15 import org.eclipse.swt.graphics.Device;
16 import org.eclipse.swt.graphics.Image;
17 import org.eclipse.swt.widgets.Control;
18
19
20 /**
21  * A local registry that shares its resources with some global registry.
22  * LocalResourceManager is typically used to safeguard against leaks. Clients
23  * can use a nested registry to allocate and deallocate resources in the
24  * global registry. Calling dispose() on the nested registry will deallocate
25  * everything allocated for the nested registry without affecting the rest
26  * of the global registry.
27  * <p>
28  * A nested registry can be used to manage the resources for, say, a dialog
29  * box.
30  * </p>
31  * @since 3.1
32  */

33 public final class LocalResourceManager extends AbstractResourceManager {
34
35     private ResourceManager parentRegistry;
36     
37     /**
38      * Creates a local registry that delegates to the given global registry
39      * for all resource allocation and deallocation.
40      *
41      * @param parentRegistry global registry
42      */

43     public LocalResourceManager(ResourceManager parentRegistry) {
44         this.parentRegistry = parentRegistry;
45     }
46     
47     /**
48      * Creates a local registry that wraps the given global registry. Anything
49      * allocated by this registry will be automatically cleaned up with the given
50      * control is disposed. Note that registries created in this way should not
51      * be used to allocate any resource that must outlive the given control.
52      *
53      * @param parentRegistry global registry that handles resource allocation
54      * @param owner control whose disposal will trigger cleanup of everything
55      * in the registry.
56      */

57     public LocalResourceManager(ResourceManager parentRegistry, Control owner) {
58         this(parentRegistry);
59         
60         owner.addDisposeListener(new DisposeListener() {
61             /* (non-Javadoc)
62              * @see org.eclipse.swt.events.DisposeListener#widgetDisposed(org.eclipse.swt.events.DisposeEvent)
63              */

64             public void widgetDisposed(DisposeEvent e) {
65                 LocalResourceManager.this.dispose();
66             }
67         });
68     }
69     
70     /* (non-Javadoc)
71      * @see org.eclipse.jface.resource.ResourceManager#getDevice()
72      */

73     public Device getDevice() {
74         return parentRegistry.getDevice();
75     }
76     
77     /* (non-Javadoc)
78      * @see org.eclipse.jface.resource.AbstractResourceManager#allocate(org.eclipse.jface.resource.DeviceResourceDescriptor)
79      */

80     protected Object JavaDoc allocate(DeviceResourceDescriptor descriptor)
81             throws DeviceResourceException {
82         return parentRegistry.create(descriptor);
83     }
84     
85     /* (non-Javadoc)
86      * @see org.eclipse.jface.resource.AbstractResourceManager#deallocate(java.lang.Object, org.eclipse.jface.resource.DeviceResourceDescriptor)
87      */

88     protected void deallocate(Object JavaDoc resource,
89             DeviceResourceDescriptor descriptor) {
90         
91         parentRegistry.destroy(descriptor);
92     }
93
94     /* (non-Javadoc)
95      * @see org.eclipse.jface.resource.ResourceManager#getDefaultImage()
96      */

97     protected Image getDefaultImage() {
98         return parentRegistry.getDefaultImage();
99     }
100 }
101
Popular Tags