]>
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
, ImageData
> 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 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 public Image
getIcon(String name
, Integer preferredSize
) {
39 if (preferredSize
== null)
40 preferredSize
= defaultIconSize
;
41 Map
<Integer
, String
> subCache
;
42 if (!iconPaths
.containsKey(name
))
43 subCache
= new HashMap
<>();
45 subCache
= iconPaths
.get(name
);
47 if (!subCache
.containsKey(preferredSize
)) {
48 Image bestMatchSoFar
= null;
49 paths
: for (String p
: getImagesPaths()) {
50 int lastSlash
= p
.lastIndexOf('/');
53 fileName
= p
.substring(lastSlash
+ 1);
54 int lastDot
= fileName
.lastIndexOf('.');
56 fileName
= fileName
.substring(0, lastDot
);
57 if (fileName
.equals(name
)) {// matched
58 Image img
= getImage(p
);
59 int width
= img
.getBounds().width
;
60 if (width
== preferredSize
) {// perfect match
61 subCache
.put(preferredSize
, p
);
65 if (bestMatchSoFar
== null) {
68 if (Math
.abs(width
- preferredSize
) < Math
69 .abs(bestMatchSoFar
.getBounds().width
- preferredSize
))
76 image
= bestMatchSoFar
;
78 image
= getImage(subCache
.get(preferredSize
));
81 if (image
!= null && !iconPaths
.containsKey(name
))
82 iconPaths
.put(name
, subCache
);
88 public Integer
getDefaultIconSize() {
89 return defaultIconSize
;