KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > util > SWTResourceUtil


1 /*******************************************************************************
2  * Copyright (c) 2000, 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
12 package org.eclipse.ui.internal.util;
13
14 import java.util.Hashtable JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.swt.graphics.Color;
19 import org.eclipse.swt.graphics.Font;
20 import org.eclipse.swt.graphics.Image;
21
22 /**
23  * SWTResourceUtil is a class that holds onto Colors, Fonts and Images and
24  * disposes them on shutdown.
25  */

26 public class SWTResourceUtil {
27
28     /**
29      * The cache of images that have been dispensed by this provider. Maps
30      * ImageDescriptor->Image. Caches are all static to avoid creating extra
31      * system resources for very common images, font and colors.
32      */

33     private static Map JavaDoc imageTable = new Hashtable JavaDoc(40);
34
35     /**
36      * The cache of colors that have been dispensed by this provider. Maps
37      * RGB->Color.
38      */

39     private static Map JavaDoc colorTable = new Hashtable JavaDoc(7);
40
41     /**
42      * The cache of fonts that have been dispensed by this provider. Maps
43      * FontData->Font.
44      */

45     private static Map JavaDoc fontTable = new Hashtable JavaDoc(7);
46
47     /**
48      * Disposes of all allocated images, colors and fonts when shutting down the
49      * plug-in.
50      */

51     public static final void shutdown() {
52         if (imageTable != null) {
53             for (Iterator JavaDoc i = imageTable.values().iterator(); i.hasNext();) {
54                 ((Image) i.next()).dispose();
55             }
56             imageTable = null;
57         }
58         if (colorTable != null) {
59             for (Iterator JavaDoc i = colorTable.values().iterator(); i.hasNext();) {
60                 ((Color) i.next()).dispose();
61             }
62             colorTable = null;
63         }
64         if (fontTable != null) {
65             for (Iterator JavaDoc i = fontTable.values().iterator(); i.hasNext();) {
66                 ((Font) i.next()).dispose();
67             }
68             fontTable = null;
69         }
70     }
71
72     /**
73      * Get the Map of RGBs to Colors.
74      * @return Returns the colorTable.
75      */

76     public static Map JavaDoc getColorTable() {
77         return colorTable;
78     }
79
80     /**
81      * Return the map of FontDatas to Fonts.
82      * @return Returns the fontTable.
83      */

84     public static Map JavaDoc getFontTable() {
85         return fontTable;
86     }
87
88     /**
89      * Return the map of ImageDescriptors to Images.
90      * @return Returns the imageTable.
91      */

92     public static Map JavaDoc getImageTable() {
93         return imageTable;
94     }
95 }
96
Popular Tags