]> git.argeo.org Git - lgpl/argeo-commons.git/blob - eclipse/org.argeo.cms.swt/src/org/argeo/cms/swt/osgi/BundleCmsSwtTheme.java
Prepare next development cycle
[lgpl/argeo-commons.git] / eclipse / org.argeo.cms.swt / src / org / argeo / cms / swt / osgi / BundleCmsSwtTheme.java
1 package org.argeo.cms.swt.osgi;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.util.HashMap;
6 import java.util.Map;
7
8 import org.argeo.cms.osgi.BundleCmsTheme;
9 import org.argeo.cms.swt.CmsSwtTheme;
10 import org.eclipse.swt.graphics.Image;
11 import org.eclipse.swt.graphics.ImageData;
12 import org.eclipse.swt.widgets.Display;
13
14 /** Centralises some generic {@link CmsSwtTheme} patterns. */
15 public class BundleCmsSwtTheme extends BundleCmsTheme implements CmsSwtTheme {
16 private Map<String, ImageData> imageCache = new HashMap<>();
17
18 private Map<String, Map<Integer, String>> iconPaths = new HashMap<>();
19
20 public Image getImage(String path) {
21 if (!imageCache.containsKey(path)) {
22 try (InputStream in = getResourceAsStream(path)) {
23 if (in == null)
24 return null;
25 ImageData imageData = new ImageData(in);
26 imageCache.put(path, imageData);
27 } catch (IOException e) {
28 throw new IllegalStateException(e);
29 }
30 }
31 ImageData imageData = imageCache.get(path);
32 Image image = new Image(Display.getCurrent(), imageData);
33 return image;
34 }
35
36 /**
37 * And icon with this file name (without the extension), with a best effort to
38 * find the appropriate size, or <code>null</code> if not found.
39 *
40 * @param name An icon file name without path and extension.
41 * @param preferredSize the preferred size, if <code>null</code>,
42 * {@link #getDefaultIconSize()} will be tried.
43 */
44 public Image getIcon(String name, Integer preferredSize) {
45 if (preferredSize == null)
46 preferredSize = getDefaultIconSize();
47 Map<Integer, String> subCache;
48 if (!iconPaths.containsKey(name))
49 subCache = new HashMap<>();
50 else
51 subCache = iconPaths.get(name);
52 Image image = null;
53 if (!subCache.containsKey(preferredSize)) {
54 Image bestMatchSoFar = null;
55 paths: for (String p : getImagesPaths()) {
56 int lastSlash = p.lastIndexOf('/');
57 String fileName = p;
58 if (lastSlash >= 0)
59 fileName = p.substring(lastSlash + 1);
60 int lastDot = fileName.lastIndexOf('.');
61 if (lastDot >= 0)
62 fileName = fileName.substring(0, lastDot);
63 if (fileName.equals(name)) {// matched
64 Image img = getImage(p);
65 int width = img.getBounds().width;
66 if (width == preferredSize) {// perfect match
67 subCache.put(preferredSize, p);
68 image = img;
69 break paths;
70 }
71 if (bestMatchSoFar == null) {
72 bestMatchSoFar = img;
73 } else {
74 if (Math.abs(width - preferredSize) < Math
75 .abs(bestMatchSoFar.getBounds().width - preferredSize))
76 bestMatchSoFar = img;
77 }
78 }
79 }
80
81 if (image == null)
82 image = bestMatchSoFar;
83 } else {
84 image = getImage(subCache.get(preferredSize));
85 }
86
87 if (image != null && !iconPaths.containsKey(name))
88 iconPaths.put(name, subCache);
89
90 return image;
91 }
92
93 }