View Javadoc
1   package org.argeo.cms.ui.eclipse.forms;
2   
3   import java.util.HashMap;
4   
5   import org.eclipse.jface.resource.DeviceResourceException;
6   import org.eclipse.jface.resource.FontDescriptor;
7   import org.eclipse.jface.resource.JFaceResources;
8   import org.eclipse.jface.resource.LocalResourceManager;
9   import org.eclipse.swt.SWT;
10  import org.eclipse.swt.graphics.Device;
11  import org.eclipse.swt.graphics.Font;
12  import org.eclipse.swt.graphics.FontData;
13  //import org.eclipse.swt.internal.graphics.Graphics;
14  import org.eclipse.swt.widgets.Display;
15  
16  public class FormFonts {
17  	private static FormFonts instance;
18  
19  	public static FormFonts getInstance() {
20  		if (instance == null)
21  			instance = new FormFonts();
22  		return instance;
23  	}
24  
25  	private LocalResourceManager resources;
26  	private HashMap descriptors;
27  
28  	private FormFonts() {
29  	}
30  
31  	private class BoldFontDescriptor extends FontDescriptor {
32  		private FontData[] fFontData;
33  
34  		BoldFontDescriptor(Font font) {
35  			// RAP [if] Changes due to different way of creating fonts
36  			// fFontData = font.getFontData();
37  			// for (int i = 0; i < fFontData.length; i++) {
38  			// fFontData[i].setStyle(fFontData[i].getStyle() | SWT.BOLD);
39  			// }
40  			FontData fontData = font.getFontData()[0];
41  			// Font boldFont = Graphics.getFont( fontData.getName(),
42  			// fontData.getHeight(),
43  			// fontData.getStyle() | SWT.BOLD );
44  			Font boldFont = new Font(Display.getCurrent(), fontData.getName(), fontData.getHeight(),
45  					fontData.getStyle() | SWT.BOLD);
46  			fFontData = boldFont.getFontData();
47  		}
48  
49  		public boolean equals(Object obj) {
50  			if (obj instanceof BoldFontDescriptor) {
51  				BoldFontDescriptor desc = (BoldFontDescriptor) obj;
52  				if (desc.fFontData.length != fFontData.length)
53  					return false;
54  				for (int i = 0; i < fFontData.length; i++)
55  					if (!fFontData[i].equals(desc.fFontData[i]))
56  						return false;
57  				return true;
58  			}
59  			return false;
60  		}
61  
62  		public int hashCode() {
63  			int hash = 0;
64  			for (int i = 0; i < fFontData.length; i++)
65  				hash = hash * 7 + fFontData[i].hashCode();
66  			return hash;
67  		}
68  
69  		public Font createFont(Device device) throws DeviceResourceException {
70  			// RAP [if] Changes due to different way of creating fonts
71  			return new Font(device, fFontData[0]);
72  			// return Graphics.getFont( fFontData[ 0 ] );
73  		}
74  
75  		public void destroyFont(Font previouslyCreatedFont) {
76  			// RAP [if] unnecessary
77  			// previouslyCreatedFont.dispose();
78  		}
79  	}
80  
81  	public Font getBoldFont(Display display, Font font) {
82  		checkHashMaps();
83  		BoldFontDescriptor desc = new BoldFontDescriptor(font);
84  		Font result = getResourceManager().createFont(desc);
85  		descriptors.put(result, desc);
86  		return result;
87  	}
88  
89  	public boolean markFinished(Font boldFont) {
90  		checkHashMaps();
91  		BoldFontDescriptor desc = (BoldFontDescriptor) descriptors.get(boldFont);
92  		if (desc != null) {
93  			getResourceManager().destroyFont(desc);
94  			if (getResourceManager().find(desc) == null) {
95  				descriptors.remove(boldFont);
96  				validateHashMaps();
97  			}
98  			return true;
99  
100 		}
101 		// if the image was not found, dispose of it for the caller
102 		// RAP [if] unnecessary
103 		// boldFont.dispose();
104 		return false;
105 	}
106 
107 	private LocalResourceManager getResourceManager() {
108 		if (resources == null)
109 			resources = new LocalResourceManager(JFaceResources.getResources());
110 		return resources;
111 	}
112 
113 	private void checkHashMaps() {
114 		if (descriptors == null)
115 			descriptors = new HashMap();
116 	}
117 
118 	private void validateHashMaps() {
119 		if (descriptors.size() == 0)
120 			descriptors = null;
121 	}
122 }