]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui/src/org/argeo/cms/ui/util/AbstractCmsTheme.java
1 package org
.argeo
.cms
.ui
.util
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
5 import java
.util
.HashMap
;
8 import org
.argeo
.cms
.ui
.CmsTheme
;
9 import org
.eclipse
.swt
.graphics
.Image
;
10 import org
.eclipse
.swt
.graphics
.ImageData
;
11 import org
.eclipse
.swt
.widgets
.Display
;
13 /** Centralises some generic {@link CmsTheme} patterns. */
14 public abstract class AbstractCmsTheme
implements CmsTheme
{
15 private Map
<String
, Image
> imageCache
= new HashMap
<>();
17 private Map
<String
, Map
<Integer
, String
>> iconPaths
= new HashMap
<>();
19 private Integer defaultIconSize
= 16;
21 public Image
getImage(String path
) {
22 if (!imageCache
.containsKey(path
)) {
23 try (InputStream in
= getResourceAsStream(path
)) {
26 ImageData imageData
= new ImageData(in
);
27 Image image
= new Image(Display
.getDefault(), imageData
);
28 imageCache
.put(path
, image
);
29 } catch (IOException e
) {
30 throw new IllegalStateException(e
);
33 return imageCache
.get(path
);
37 public Image
getIcon(String name
, Integer preferredSize
) {
38 if (preferredSize
== null)
39 preferredSize
= defaultIconSize
;
40 Map
<Integer
, String
> subCache
;
41 if (!iconPaths
.containsKey(name
))
42 subCache
= new HashMap
<>();
44 subCache
= iconPaths
.get(name
);
46 if (!subCache
.containsKey(preferredSize
)) {
47 Image bestMatchSoFar
= null;
48 paths
: for (String p
: getImagesPaths()) {
49 int lastSlash
= p
.lastIndexOf('/');
52 fileName
= p
.substring(lastSlash
+ 1);
53 int lastDot
= fileName
.lastIndexOf('.');
55 fileName
= fileName
.substring(0, lastDot
);
56 if (fileName
.equals(name
)) {// matched
57 Image img
= getImage(p
);
58 int width
= img
.getBounds().width
;
59 if (width
== preferredSize
) {// perfect match
60 subCache
.put(preferredSize
, p
);
64 if (bestMatchSoFar
== null) {
67 if (Math
.abs(width
- preferredSize
) < Math
68 .abs(bestMatchSoFar
.getBounds().width
- preferredSize
))
75 image
= bestMatchSoFar
;
77 image
= getImage(subCache
.get(preferredSize
));
80 if (image
!= null && !iconPaths
.containsKey(name
))
81 iconPaths
.put(name
, subCache
);
87 public Integer
getDefaultIconSize() {
88 return defaultIconSize
;