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