KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Describes a color by its RGB values.
19  *
20  * @since 3.1
21  */

22 class RGBColorDescriptor extends ColorDescriptor {
23
24     private RGB color;
25     
26     /**
27      * Color being copied, or null if none
28      */

29     private Color originalColor = null;
30     
31     /**
32      * Creates a new RGBColorDescriptor given some RGB values
33      *
34      * @param color RGB values (not null)
35      */

36     public RGBColorDescriptor(RGB color) {
37         this.color = color;
38     }
39     
40     /**
41      * Creates a new RGBColorDescriptor that describes an existing color.
42      *
43      * @since 3.1
44      *
45      * @param originalColor a color to describe
46      */

47     public RGBColorDescriptor(Color originalColor) {
48         this(originalColor.getRGB());
49         this.originalColor = originalColor;
50     }
51     
52     /* (non-Javadoc)
53      * @see java.lang.Object#equals(java.lang.Object)
54      */

55     public boolean equals(Object JavaDoc obj) {
56         if (obj instanceof RGBColorDescriptor) {
57             RGBColorDescriptor other = (RGBColorDescriptor) obj;
58             
59             return other.color.equals(color) && other.originalColor == originalColor;
60         }
61         
62         return false;
63     }
64     
65     /* (non-Javadoc)
66      * @see java.lang.Object#hashCode()
67      */

68     public int hashCode() {
69         return color.hashCode();
70     }
71     
72     /* (non-Javadoc)
73      * @see org.eclipse.jface.resources.ColorDescriptor#createColor()
74      */

75     public Color createColor(Device device) {
76         // If this descriptor is wrapping an existing color, then we can return the original color
77
// if this is the same device.
78
if (originalColor != null) {
79             // If we're allocating on the same device as the original color, return the original.
80
if (originalColor.getDevice() == device) {
81                 return originalColor;
82             }
83         }
84         
85         return new Color(device, color);
86     }
87
88     /* (non-Javadoc)
89      * @see org.eclipse.jface.resource.ColorDescriptor#destroyColor(org.eclipse.swt.graphics.Color)
90      */

91     public void destroyColor(Color toDestroy) {
92         if (toDestroy == originalColor) {
93             return;
94         }
95         
96         toDestroy.dispose();
97     }
98 }
99
Popular Tags