1 /*******************************************************************************
2  * Copyright (c) 2007, 2015 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.ui.internal.forms.widgets;
15 
16 import java.util.Arrays;
17 import java.util.HashMap;
18 
19 import org.eclipse.jface.resource.DeviceResourceException;
20 import org.eclipse.jface.resource.FontDescriptor;
21 import org.eclipse.jface.resource.LocalResourceManager;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.graphics.Device;
24 import org.eclipse.swt.graphics.Font;
25 import org.eclipse.swt.graphics.FontData;
26 import org.eclipse.swt.widgets.Display;
27 
28 public class FormFonts {
29 	private static FormFonts instance;
30 
getInstance()31 	public static FormFonts getInstance() {
32 		if (instance == null)
33 			instance = new FormFonts();
34 		return instance;
35 	}
36 
37 	private ResourceManagerManger manager = new ResourceManagerManger();
38 	private HashMap<Font, BoldFontDescriptor> descriptors;
39 
FormFonts()40 	private FormFonts() {
41 	}
42 
43 	private static class BoldFontDescriptor extends FontDescriptor {
44 		private FontData[] fFontData;
45 
BoldFontDescriptor(Font font)46 		BoldFontDescriptor (Font font) {
47 			fFontData = font.getFontData();
48 			for (FontData element : fFontData) {
49 				element.setStyle(element.getStyle() | SWT.BOLD);
50 			}
51 		}
52 
53 		@Override
equals(Object obj)54 		public boolean equals(Object obj) {
55 			if (obj instanceof BoldFontDescriptor) {
56 				BoldFontDescriptor desc = (BoldFontDescriptor)obj;
57 				return Arrays.equals(fFontData, desc.fFontData);
58 			}
59 			return false;
60 		}
61 
62 		@Override
hashCode()63 		public int hashCode() {
64 			return Arrays.hashCode(fFontData);
65 		}
66 
67 		@Override
createFont(Device device)68 		public Font createFont(Device device) throws DeviceResourceException {
69 			return new Font(device, fFontData);
70 		}
71 
72 		@Override
destroyFont(Font previouslyCreatedFont)73 		public void destroyFont(Font previouslyCreatedFont) {
74 			previouslyCreatedFont.dispose();
75 		}
76 	}
77 
getBoldFont(Display display, Font font)78 	public Font getBoldFont(Display display, Font font) {
79 		checkHashMaps();
80 		BoldFontDescriptor desc = new BoldFontDescriptor(font);
81 		Font result = manager.getResourceManager(display).createFont(desc);
82 		descriptors.put(result, desc);
83 		return result;
84 	}
85 
markFinished(Font boldFont, Display display)86 	public boolean markFinished(Font boldFont, Display display) {
87 		checkHashMaps();
88 		BoldFontDescriptor desc = descriptors.get(boldFont);
89 		if (desc != null) {
90 			LocalResourceManager resourceManager = manager.getResourceManager(display);
91 			resourceManager.destroyFont(desc);
92 			if (resourceManager.find(desc) == null) {
93 				descriptors.remove(boldFont);
94 				validateHashMaps();
95 			}
96 			return true;
97 
98 		}
99 		// if the image was not found, dispose of it for the caller
100 		boldFont.dispose();
101 		return false;
102 	}
103 
checkHashMaps()104 	private void checkHashMaps() {
105 		if (descriptors == null)
106 			descriptors = new HashMap<>();
107 	}
108 
validateHashMaps()109 	private void validateHashMaps() {
110 		if (descriptors.isEmpty())
111 			descriptors = null;
112 	}
113 }
114