KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.eclipse.swt.widgets.Display;
17
18 /**
19  * Lightweight descriptor for a font. Creates the described font on demand.
20  * Subclasses can implement different ways of describing a font. These objects
21  * will be compared, so hashCode(...) and equals(...) must return something
22  * meaningful.
23  *
24  * @since 3.1
25  */

26 public abstract class FontDescriptor extends DeviceResourceDescriptor {
27     
28     /**
29      * Creates a FontDescriptor that describes an existing font. The resulting
30      * descriptor depends on the Font. Disposing the Font while the descriptor
31      * is still in use may throw a graphic disposed exception.
32      *
33      * @since 3.1
34      *
35      * @deprecated use {@link FontDescriptor#createFrom(Font)}
36      *
37      * @param font a font to describe
38      * @param originalDevice must be the same Device that was passed into
39      * the font's constructor when it was first created.
40      * @return a newly created FontDescriptor.
41      */

42     public static FontDescriptor createFrom(Font font, Device originalDevice) {
43         return new ArrayFontDescriptor(font);
44     }
45     
46     /**
47      * Creates a FontDescriptor that describes an existing font. The resulting
48      * descriptor depends on the original Font, and disposing the original Font
49      * while the descriptor is still in use may cause SWT to throw a graphic
50      * disposed exception.
51      *
52      * @since 3.1
53      *
54      * @param font font to create
55      * @return a newly created FontDescriptor that describes the given font
56      */

57     public static FontDescriptor createFrom(Font font) {
58         return new ArrayFontDescriptor(font);
59     }
60     
61     /**
62      * Creates a new FontDescriptor given the an array of FontData that describes
63      * the font.
64      *
65      * @since 3.1
66      *
67      * @param data an array of FontData that describes the font (will be passed into
68      * the Font's constructor)
69      * @return a FontDescriptor that describes the given font
70      */

71     public static FontDescriptor createFrom(FontData[] data) {
72         return new ArrayFontDescriptor(data);
73     }
74     
75     /**
76      * Creates a new FontDescriptor given the associated FontData
77      *
78      * @param data FontData describing the font to create
79      * @return a newly created FontDescriptor
80      */

81     public static FontDescriptor createFrom(FontData data) {
82         return new ArrayFontDescriptor(new FontData[]{data});
83     }
84     
85     /**
86      * Creates a new FontDescriptor given an OS-specific font name, height, and style.
87      *
88      * @see Font#Font(org.eclipse.swt.graphics.Device, java.lang.String, int, int)
89      *
90      * @param name os-specific font name
91      * @param height height (pixels)
92      * @param style a bitwise combination of NORMAL, BOLD, ITALIC
93      * @return a new FontDescriptor
94      */

95     public static FontDescriptor createFrom(String JavaDoc name, int height, int style) {
96         return createFrom(new FontData(name, height, style));
97     }
98     
99     /**
100      * Returns the set of FontData associated with this font. Modifying the elements
101      * in the returned array has no effect on the original FontDescriptor.
102      *
103      * @return the set of FontData associated with this font
104      * @since 3.3
105      */

106     public FontData[] getFontData() {
107         Font tempFont = createFont(Display.getCurrent());
108         FontData[] result = tempFont.getFontData();
109         destroyFont(tempFont);
110         return result;
111     }
112     
113     /**
114      * Returns an array of FontData containing copies of the FontData
115      * from the original.
116      *
117      * @param original array to copy
118      * @return a deep copy of the original array
119      * @since 3.3
120      */

121     public static FontData[] copy(FontData[] original) {
122         FontData[] result = new FontData[original.length];
123         for (int i = 0; i < original.length; i++) {
124             FontData next = original[i];
125             
126             result[i] = copy(next);
127         }
128         
129         return result;
130     }
131     
132     /**
133      * Returns a copy of the original FontData
134      *
135      * @param next FontData to copy
136      * @return a copy of the given FontData
137      * @since 3.3
138      */

139     public static FontData copy(FontData next) {
140         FontData result = new FontData(next.getName(), next.getHeight(), next.getStyle());
141         result.setLocale(next.getLocale());
142         return result;
143     }
144
145     /**
146      * Returns a FontDescriptor that is equivalent to the reciever, but uses
147      * the given style bits.
148      *
149      * <p>Does not modify the reciever.</p>
150      *
151      * @param style a bitwise combination of SWT.NORMAL, SWT.ITALIC and SWT.BOLD
152      * @return a new FontDescriptor with the given style
153      *
154      * @since 3.3
155      */

156     public final FontDescriptor setStyle(int style) {
157         FontData[] data = getFontData();
158         
159         for (int i = 0; i < data.length; i++) {
160             FontData next = data[i];
161             
162             next.setStyle(style);
163         }
164
165         // Optimization: avoid holding onto extra instances by returning the reciever if
166
// if it is exactly the same as the result
167
FontDescriptor result = new ArrayFontDescriptor(data);
168         if (result.equals(this)) {
169             return this;
170         }
171         
172         return result;
173     }
174     
175     /**
176      * <p>Returns a FontDescriptor that is equivalent to the reciever, but
177      * has the given style bits, in addition to any styles the reciever already has.</p>
178      *
179      * <p>Does not modify the reciever.</p>
180      *
181      * @param style a bitwise combination of SWT.NORMAL, SWT.ITALIC and SWT.BOLD
182      * @return a new FontDescriptor with the given additional style bits
183      * @since 3.3
184      */

185     public final FontDescriptor withStyle(int style) {
186         FontData[] data = getFontData();
187         
188         for (int i = 0; i < data.length; i++) {
189             FontData next = data[i];
190             
191             next.setStyle(next.getStyle() | style);
192         }
193         
194         // Optimization: avoid allocating extra instances by returning the reciever if
195
// if it is exactly the same as the result
196
FontDescriptor result = new ArrayFontDescriptor(data);
197         if (result.equals(this)) {
198             return this;
199         }
200         
201         return result;
202     }
203     
204     /**
205      * <p>Returns a new FontDescriptor that is equivalent to the reciever, but
206      * has the given height.</p>
207      *
208      * <p>Does not modify the reciever.</p>
209      *
210      * @param height a height, in points
211      * @return a new FontDescriptor with the height, in points
212      * @since 3.3
213      */

214     public final FontDescriptor setHeight(int height) {
215         FontData[] data = getFontData();
216         
217         for (int i = 0; i < data.length; i++) {
218             FontData next = data[i];
219             
220             next.setHeight(height);
221         }
222         
223         // Optimization: avoid holding onto extra instances by returning the reciever if
224
// if it is exactly the same as the result
225
FontDescriptor result = new ArrayFontDescriptor(data);
226         if (result.equals(this)) {
227             return this;
228         }
229         
230         return result;
231     }
232
233     /**
234      * <p>Returns a FontDescriptor that is equivalent to the reciever, but whose height
235      * is larger by the given number of points.</p>
236      *
237      * <p>Does not modify the reciever.</p>
238      *
239      * @param heightDelta a change in height, in points. Negative values will return smaller
240      * fonts.
241      * @return a FontDescriptor whose height differs from the reciever by the given number
242      * of points.
243      * @since 3.3
244      */

245     public final FontDescriptor increaseHeight(int heightDelta) {
246         if (heightDelta == 0) {
247             return this;
248         }
249         FontData[] data = getFontData();
250         
251         for (int i = 0; i < data.length; i++) {
252             FontData next = data[i];
253             
254             next.setHeight(next.getHeight() + heightDelta);
255         }
256         
257         return new ArrayFontDescriptor(data);
258     }
259     
260     /**
261      * Creates the Font described by this descriptor.
262      *
263      * @since 3.1
264      *
265      * @param device device on which to allocate the font
266      * @return a newly allocated Font (never null)
267      * @throws DeviceResourceException if unable to allocate the Font
268      */

269     public abstract Font createFont(Device device) throws DeviceResourceException;
270     
271     /**
272      * Deallocates anything that was allocated by createFont, given a font
273      * that was allocated by an equal FontDescriptor.
274      *
275      * @since 3.1
276      *
277      * @param previouslyCreatedFont previously allocated font
278      */

279     public abstract void destroyFont(Font previouslyCreatedFont);
280     
281     /* (non-Javadoc)
282      * @see org.eclipse.jface.resource.DeviceResourceDescriptor#create(org.eclipse.swt.graphics.Device)
283      */

284     public final Object JavaDoc createResource(Device device) throws DeviceResourceException {
285         return createFont(device);
286     }
287     
288     /* (non-Javadoc)
289      * @see org.eclipse.jface.resource.DeviceResourceDescriptor#destroy(java.lang.Object)
290      */

291     public final void destroyResource(Object JavaDoc previouslyCreatedObject) {
292         destroyFont((Font)previouslyCreatedObject);
293     }
294 }
295
Popular Tags