KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtriver > open > vectorvisuals > FontManager


1 /*
2  * FontManager.java
3  *
4  * Created on 4 June 2003, 10:56
5  */

6
7 package com.thoughtriver.open.vectorvisuals;
8
9 import java.awt.*;
10 import java.util.*;
11
12 /**
13  * This is a simple utility to assist in the quick use of <CODE>Font</CODE>
14  * objects. Each instance of <CODE>FontManager</CODE> maintains a cache of
15  * created fonts that is populated as the fonts are requested with the <CODE>getFont</CODE>
16  * method.
17  *
18  * @author Brandon Franklin
19  * @version $Date: 2006/11/25 09:18:36 $
20  */

21 public class FontManager {
22
23     /** The singleton instance of <CODE>FontManager</CODE> */
24     static private FontManager singleton = null;
25
26     /**
27      * Returns the shared instance of <CODE>FontManager</CODE>, which is
28      * useful if a unified <CODE>Font</CODE> cache is desired.
29      *
30      * @return the <CODE>FontManager</CODE> singleton
31      */

32     static public FontManager getSharedInstance() {
33
34         if (singleton == null) {
35             singleton = new FontManager();
36         }
37
38         return singleton;
39     }
40
41     /** A name-to-<CODE>Font</CODE> record of all loaded fonts */
42     private Map<String JavaDoc, Font> cache = null;
43
44     /**
45      * Creates a new instance of <CODE>FontManager</CODE> and prepares it for
46      * use.
47      */

48     public FontManager() {
49         cache = new HashMap<String JavaDoc, Font>();
50     }
51
52     /**
53      * Retrieves and returns the <CODE>Font</CODE> with the supplied name.
54      * This will involve either creating the <CODE>Font</CODE> based on the
55      * attributes described in the string, or returning it directly from the
56      * <CODE>FontManager</CODE>'s cache. The string supplied should be
57      * formatted as described in the <CODE>Font</CODE> class's static <CODE>decode</CODE>
58      * method.
59      *
60      * @param name the name of the font to retrieve
61      * @return the <CODE>Font</CODE> requested
62      */

63     public synchronized Font getFont(final String JavaDoc name) {
64
65         Font font = null;
66
67         // First check the cache
68
if (cache.containsKey(name)) {
69             font = cache.get(name);
70
71         }
72         else { // Not in the cache, so try creating it
73

74             font = Font.decode(name);
75
76             // Store the font in the cache
77
if (font != null) {
78                 cache.put(name, font);
79             }
80         }
81
82         return font;
83     }
84
85 }
86
Popular Tags