]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - swt/org.argeo.cms.swt/src/org/argeo/cms/swt/osgi/BundleCmsSwtTheme.java
1 package org
.argeo
.cms
.swt
.osgi
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
5 import java
.util
.HashMap
;
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
;
15 /** Centralises some generic {@link CmsSwtTheme} patterns. */
16 public class BundleCmsSwtTheme
extends BundleCmsTheme
implements CmsSwtTheme
{
17 private Map
<String
, ImageData
> imageCache
= new HashMap
<>();
19 private Map
<String
, Map
<Integer
, String
>> iconPaths
= new HashMap
<>();
21 protected Image
getImage(String path
) {
22 if (!imageCache
.containsKey(path
)) {
23 try (InputStream in
= getResourceAsStream(path
)) {
26 ImageData imageData
= new ImageData(in
);
27 imageCache
.put(path
, imageData
);
28 } catch (IOException e
) {
29 throw new IllegalStateException(e
);
32 ImageData imageData
= imageCache
.get(path
);
33 Image image
= new Image(Display
.getCurrent(), imageData
);
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.
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.
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
<>();
52 subCache
= iconPaths
.get(name
);
54 if (!subCache
.containsKey(preferredSize
)) {
55 Image bestMatchSoFar
= null;
56 paths
: for (String p
: getImagesPaths()) {
57 int lastSlash
= p
.lastIndexOf('/');
61 fileName
= p
.substring(lastSlash
+ 1);
62 int lastDot
= fileName
.lastIndexOf('.');
64 ext
= fileName
.substring(lastDot
+ 1);
65 fileName
= fileName
.substring(0, lastDot
);
68 if ("svg".equals(ext
))
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
);
79 if (bestMatchSoFar
== null) {
82 if (Math
.abs(width
- preferredSize
) < Math
83 .abs(bestMatchSoFar
.getBounds().width
- preferredSize
))
90 image
= bestMatchSoFar
;
92 image
= getImage(subCache
.get(preferredSize
));
95 if (image
!= null && !iconPaths
.containsKey(name
))
96 iconPaths
.put(name
, subCache
);
102 public Image
getSmallIcon(CmsIcon icon
) {
103 return getIcon(icon
.name(), getSmallIconSize());
107 public Image
getBigIcon(CmsIcon icon
) {
108 return getIcon(icon
.name(), getBigIconSize());