KickJava   Java API By Example, From Geeks To Geeks.

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


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.graphics.Color;
14 import org.eclipse.swt.graphics.Device;
15 import org.eclipse.swt.graphics.RGB;
16
17 /**
18  * Lightweight descriptor for an SWT color. Each ColorDescriptor will create a particular SWT
19  * Color on demand. This object will be compared so hashCode(...) and equals(...) must
20  * return meaningful values.
21  *
22  * @since 3.1
23  */

24 public abstract class ColorDescriptor extends DeviceResourceDescriptor {
25     
26     /**
27      * Creates a ColorDescriptor from an existing Color, given the Device associated
28      * with the original Color. This is the usual way to convert a Color into
29      * a ColorDescriptor. Note that the returned ColorDescriptor depends on the
30      * original Color, and disposing the Color will invalidate the ColorDescriptor.
31      *
32      * @deprecated use {@link ColorDescriptor#createFrom(Color)}
33      *
34      * @since 3.1
35      *
36      * @param toCreate Color to convert into a ColorDescriptor.
37      * @param originalDevice this must be the same Device that was passed into the
38      * original Color's constructor.
39      * @return a newly created ColorDescriptor that describes the given Color.
40      */

41     public static ColorDescriptor createFrom(Color toCreate, Device originalDevice) {
42         return new RGBColorDescriptor(toCreate);
43     }
44     
45     /**
46      * Creates a ColorDescriptor from an existing color.
47      *
48      * The returned ColorDescriptor depends on the original Color. Disposing
49      * the original colour while the color descriptor is still in use may cause
50      * SWT to throw a graphic disposed exception.
51      *
52      * @since 3.1
53      *
54      * @param toCreate Color to generate a ColorDescriptor from
55      * @return a newly created ColorDescriptor
56      */

57     public static ColorDescriptor createFrom(Color toCreate) {
58         return new RGBColorDescriptor(toCreate);
59     }
60     
61     /**
62      * Returns a color descriptor for the given RGB values
63      * @since 3.1
64      *
65      * @param toCreate RGB values to create
66      * @return a new ColorDescriptor
67      */

68     public static ColorDescriptor createFrom(RGB toCreate) {
69         return new RGBColorDescriptor(toCreate);
70     }
71     
72     /**
73      * Returns the Color described by this descriptor.
74      *
75      * @param device SWT device on which to allocate the Color
76      * @return a newly allocated SWT Color object (never null)
77      * @throws DeviceResourceException if unable to allocate the Color
78      */

79     public abstract Color createColor(Device device) throws DeviceResourceException;
80     
81     /**
82      * Undoes whatever was done by createColor.
83      *
84      * @since 3.1
85      *
86      * @param toDestroy a Color that was previously allocated by an equal ColorDescriptor
87      */

88     public abstract void destroyColor(Color toDestroy);
89     
90     /* (non-Javadoc)
91      * @see org.eclipse.jface.resource.DeviceResourceDescriptor#createResource(org.eclipse.swt.graphics.Device)
92      */

93     public final Object JavaDoc createResource(Device device) throws DeviceResourceException {
94         return createColor(device);
95     }
96     
97     /* (non-Javadoc)
98      * @see org.eclipse.jface.resource.DeviceResourceDescriptor#destroyResource(java.lang.Object)
99      */

100     public final void destroyResource(Object JavaDoc previouslyCreatedObject) {
101         destroyColor((Color)previouslyCreatedObject);
102     }
103 }
104
Popular Tags