]> git.argeo.org Git - lgpl/argeo-commons.git/blob - eclipse/org.argeo.cms.swt/src/org/argeo/cms/swt/osgi/BundleCmsSwtTheme.java
ACR compatible with Android.
[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 protected 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 String ext = "";
59 if (lastSlash >= 0)
60 fileName = p.substring(lastSlash + 1);
61 int lastDot = fileName.lastIndexOf('.');
62 if (lastDot >= 0) {
63 ext = fileName.substring(lastDot + 1);
64 fileName = fileName.substring(0, lastDot);
65 }
66
67 if ("svg".equals(ext))
68 continue paths;
69
70 if (fileName.equals(name)) {// matched
71 Image img = getImage(p);
72 int width = img.getBounds().width;
73 if (width == preferredSize) {// perfect match
74 subCache.put(preferredSize, p);
75 image = img;
76 break paths;
77 }
78 if (bestMatchSoFar == null) {
79 bestMatchSoFar = img;
80 } else {
81 if (Math.abs(width - preferredSize) < Math
82 .abs(bestMatchSoFar.getBounds().width - preferredSize))
83 bestMatchSoFar = img;
84 }
85 }
86 }
87
88 if (image == null)
89 image = bestMatchSoFar;
90 } else {
91 image = getImage(subCache.get(preferredSize));
92 }
93
94 if (image != null && !iconPaths.containsKey(name))
95 iconPaths.put(name, subCache);
96
97 return image;
98 }
99
100 }