KickJava   Java API By Example, From Geeks To Geeks.

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


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.Device;
14 import org.eclipse.swt.graphics.Font;
15 import org.eclipse.swt.graphics.FontData;
16
17 /**
18  * Describes a Font using an array of FontData
19  *
20  * @since 3.1
21  */

22 final class ArrayFontDescriptor extends FontDescriptor {
23
24     private FontData[] data;
25     private Font originalFont = null;
26     
27     /**
28      * Creates a font descriptor for a font with the given name, height,
29      * and style. These arguments are passed directly to the constructor
30      * of Font.
31      *
32      * @param data FontData describing the font to create
33      *
34      * @see org.eclipse.swt.graphics.Font#Font(org.eclipse.swt.graphics.Device, org.eclipse.swt.graphics.FontData)
35      * @since 3.1
36      */

37     public ArrayFontDescriptor(FontData[] data) {
38         this.data = data;
39     }
40  
41     /**
42      * Creates a font descriptor that describes the given font.
43      *
44      * @param originalFont font to be described
45      *
46      * @see FontDescriptor#createFrom(org.eclipse.swt.graphics.Font)
47      * @since 3.1
48      */

49     public ArrayFontDescriptor(Font originalFont) {
50         this(originalFont.getFontData());
51         this.originalFont = originalFont;
52     }
53     
54     /* (non-Javadoc)
55      * @see org.eclipse.jface.resource.FontDescriptor#getFontData()
56      */

57     public FontData[] getFontData() {
58         // Copy the original array to ensure that callers will not modify it
59
return copy(data);
60     }
61     
62     
63     /* (non-Javadoc)
64      * @see org.eclipse.jface.resource.FontDescriptor#createFont(org.eclipse.swt.graphics.Device)
65      */

66     public Font createFont(Device device) {
67         
68         // If this descriptor is an existing font, then we can return the original font
69
// if this is the same device.
70
if (originalFont != null) {
71             // If we're allocating on the same device as the original font, return the original.
72
if (originalFont.getDevice() == device) {
73                 return originalFont;
74             }
75         }
76         
77         return new Font(device, data);
78     }
79
80     /* (non-Javadoc)
81      * @see java.lang.Object#equals(java.lang.Object)
82      */

83     public boolean equals(Object JavaDoc obj) {
84         if ((obj.getClass() == ArrayFontDescriptor.class)) {
85             ArrayFontDescriptor descr = (ArrayFontDescriptor)obj;
86             
87             if (descr.originalFont != originalFont) {
88                 return false;
89             }
90             
91             if (originalFont != null) {
92                 return true;
93             }
94             
95             if (data.length != descr.data.length) {
96                 return false;
97             }
98             
99             for (int i = 0; i < data.length; i++) {
100                 FontData fd = data[i];
101                 FontData fd2 = descr.data[i];
102                 
103                 if (!fd.equals(fd2)) {
104                     return false;
105                 }
106             }
107             
108             return true;
109         }
110         
111         return false;
112     }
113     
114     /* (non-Javadoc)
115      * @see java.lang.Object#hashCode()
116      */

117     public int hashCode() {
118         if (originalFont != null) {
119             return originalFont.hashCode();
120         }
121         
122         int code = 0;
123         
124         for (int i = 0; i < data.length; i++) {
125             FontData fd = data[i];
126             code += fd.hashCode();
127         }
128         return code;
129     }
130
131     /* (non-Javadoc)
132      * @see org.eclipse.jface.resource.FontDescriptor#destroyFont(org.eclipse.swt.graphics.Font)
133      */

134     public void destroyFont(Font previouslyCreatedFont) {
135         if (previouslyCreatedFont == originalFont) {
136             return;
137         }
138         previouslyCreatedFont.dispose();
139     }
140
141 }
142
Popular Tags