1 /*******************************************************************************
2  * Copyright (c) 2000, 2008 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 org.eclipse.swt.graphics.Resource;
14 
15 import java.lang.all;
16 
17 import org.eclipse.swt.SWT;
18 
19 import org.eclipse.swt.graphics.Device;
20 import org.eclipse.swt.internal.c.gdk;
21 
22 /**
23  * This class is the abstract superclass of all graphics resource objects.
24  * Resources created by the application must be disposed.
25  * <p>
26  * IMPORTANT: This class is intended to be subclassed <em>only</em>
27  * within the SWT implementation. However, it has not been marked
28  * final to allow those outside of the SWT development team to implement
29  * patched versions of the class in order to get around specific
30  * limitations in advance of when those limitations can be addressed
31  * by the team.  Any class built using subclassing to access the internals
32  * of this class will likely fail to compile or run between releases and
33  * may be strongly platform specific. Subclassing should not be attempted
34  * without an intimate and detailed understanding of the workings of the
35  * hierarchy. No support is provided for user-written classes which are
36  * implemented as subclasses of this class.
37  * </p>
38  *
39  * @see #dispose
40  * @see #isDisposed
41  * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
42  *
43  * @since 3.1
44  */
45 public abstract class Resource {
46 
47     /**
48      * the device where this resource was created
49      */
50     Device device;
51 
52 public this() {
53 }
54 
55 this(Device device) {
56     if (device is null) device = Device.getDevice();
57     if (device is null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
58     this.device = device;
59 }
60 
61 void destroy() {
62 }
63 
64 /**
65  * Disposes of the operating system resources associated with
66  * this resource. Applications must dispose of all resources
67  * which they allocate.
68  */
69 public void dispose() {
70     if (device is null) return;
71     if (device.isDisposed()) return;
72     destroy();
73     if (device.tracking) device.dispose_Object(this);
74     device = null;
75 }
76 
77 /**
78  * Returns the <code>Device</code> where this resource was
79  * created.
80  *
81  * @return <code>Device</code> the device of the receiver
82  *
83  * @since 3.2
84  */
85 public Device getDevice() {
86     Device device = this.device;
87     if (device is null || isDisposed ()) SWT.error (SWT.ERROR_GRAPHIC_DISPOSED);
88     return device;
89 }
90 
91 void init_() {
92     if (device.tracking) device.new_Object(this);
93 }
94 
95 /**
96  * Returns <code>true</code> if the resource has been disposed,
97  * and <code>false</code> otherwise.
98  * <p>
99  * This method gets the dispose state for the resource.
100  * When a resource has been disposed, it is an error to
101  * invoke any other method using the resource.
102  *
103  * @return <code>true</code> when the resource is disposed and <code>false</code> otherwise
104  */
105 public abstract bool isDisposed();
106 
107 }