]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - eclipse/org.argeo.cms.swt/src/org/argeo/cms/swt/osgi/BundleCmsSwtTheme.java
b9b2751a7f55dd3772a1bf494a6cdd25ac897aff
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
.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
;
14 /** Centralises some generic {@link CmsSwtTheme} patterns. */
15 public class BundleCmsSwtTheme
extends BundleCmsTheme
implements CmsSwtTheme
{
16 private Map
<String
, ImageData
> imageCache
= new HashMap
<>();
18 private Map
<String
, Map
<Integer
, String
>> iconPaths
= new HashMap
<>();
20 public Image
getImage(String path
) {
21 if (!imageCache
.containsKey(path
)) {
22 try (InputStream in
= getResourceAsStream(path
)) {
25 ImageData imageData
= new ImageData(in
);
26 imageCache
.put(path
, imageData
);
27 } catch (IOException e
) {
28 throw new IllegalStateException(e
);
31 ImageData imageData
= imageCache
.get(path
);
32 Image image
= new Image(Display
.getCurrent(), imageData
);
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.
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.
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
<>();
51 subCache
= iconPaths
.get(name
);
53 if (!subCache
.containsKey(preferredSize
)) {
54 Image bestMatchSoFar
= null;
55 paths
: for (String p
: getImagesPaths()) {
56 int lastSlash
= p
.lastIndexOf('/');
59 fileName
= p
.substring(lastSlash
+ 1);
60 int lastDot
= fileName
.lastIndexOf('.');
62 fileName
= fileName
.substring(0, lastDot
);
63 if (fileName
.equals(name
)) {// matched
64 Image img
= getImage(p
);
65 int width
= img
.getBounds().width
;
66 if (width
== preferredSize
) {// perfect match
67 subCache
.put(preferredSize
, p
);
71 if (bestMatchSoFar
== null) {
74 if (Math
.abs(width
- preferredSize
) < Math
75 .abs(bestMatchSoFar
.getBounds().width
- preferredSize
))
82 image
= bestMatchSoFar
;
84 image
= getImage(subCache
.get(preferredSize
));
87 if (image
!= null && !iconPaths
.containsKey(name
))
88 iconPaths
.put(name
, subCache
);