1 | |
package org.jtheque.core.managers.resource; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
import org.jtheque.core.managers.AbstractManager; |
20 | |
import org.jtheque.core.managers.Managers; |
21 | |
import org.jtheque.core.managers.beans.IBeansManager; |
22 | |
import org.jtheque.core.managers.beans.ioc.Ioc; |
23 | |
import org.jtheque.core.managers.log.ILoggingManager; |
24 | |
import org.jtheque.utils.ui.ImageUtils; |
25 | |
import org.springframework.core.io.Resource; |
26 | |
|
27 | |
import javax.swing.Action; |
28 | |
import javax.swing.ImageIcon; |
29 | |
import java.awt.Color; |
30 | |
import java.awt.image.BufferedImage; |
31 | |
import java.io.IOException; |
32 | |
import java.io.InputStream; |
33 | |
import java.util.HashMap; |
34 | |
import java.util.Map; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 0 | public final class ResourceManager extends AbstractManager implements IResourceManager { |
44 | |
private static final int DEFAULT_CACHE_SIZE = 50; |
45 | |
|
46 | 0 | private final Map<String, JThequeImage> cache = new HashMap<String, JThequeImage>(DEFAULT_CACHE_SIZE); |
47 | |
|
48 | |
@Override |
49 | |
public ImageIcon getIcon(String baseName, String id, ImageType type) { |
50 | 0 | return getIcon(baseName + '/' + id + '.' + type.getExtension()); |
51 | |
} |
52 | |
|
53 | |
@Override |
54 | |
public ImageIcon getIcon(String id, ImageType type) { |
55 | 0 | return getIcon(id + '.' + type.getExtension()); |
56 | |
} |
57 | |
|
58 | |
@Override |
59 | |
public ImageIcon getIcon(String path) { |
60 | 0 | if (isImageNotCached(path)) { |
61 | 0 | loadImageInCache(path); |
62 | |
} |
63 | |
|
64 | 0 | return cache.get(path) == null ? null : cache.get(path).asIcon(); |
65 | |
} |
66 | |
|
67 | |
@Override |
68 | |
public BufferedImage getImage(String baseName, String id, ImageType type, int width) { |
69 | 0 | return getThumbnail(getImage(baseName, id, type), width); |
70 | |
} |
71 | |
|
72 | |
@Override |
73 | |
public BufferedImage getImage(String baseName, String id, ImageType type) { |
74 | 0 | return getImage(baseName + '/' + id + '.' + type.getExtension()); |
75 | |
} |
76 | |
|
77 | |
@Override |
78 | |
public BufferedImage getImage(String id, ImageType type, int width) { |
79 | 0 | return getThumbnail(getImage(id, type), width); |
80 | |
} |
81 | |
|
82 | |
@Override |
83 | |
public BufferedImage getImage(String id, ImageType type) { |
84 | 0 | return getImage(id + '.' + type.getExtension()); |
85 | |
} |
86 | |
|
87 | |
@Override |
88 | |
public BufferedImage getImage(String id, int width) { |
89 | 0 | return getThumbnail(getImage(id), width); |
90 | |
} |
91 | |
|
92 | |
@Override |
93 | |
public BufferedImage getImage(String path) { |
94 | 0 | if (isImageNotCached(path)) { |
95 | 0 | loadImageInCache(path); |
96 | |
} |
97 | |
|
98 | 0 | return cache.get(path) == null ? null : cache.get(path).get(); |
99 | |
} |
100 | |
|
101 | |
@Override |
102 | |
public Color getColor(String name) { |
103 | 0 | return Managers.getManager(IBeansManager.class).getBean(name); |
104 | |
} |
105 | |
|
106 | |
@Override |
107 | |
public Action getAction(String name) { |
108 | 0 | return Managers.getManager(IBeansManager.class).getBean(name); |
109 | |
} |
110 | |
|
111 | |
@Override |
112 | |
public Resource getResource(String path) { |
113 | 0 | return Ioc.getContainer().getApplicationContext().getResource(path); |
114 | |
} |
115 | |
|
116 | |
@Override |
117 | |
public InputStream getResourceAsStream(String path) { |
118 | 0 | InputStream stream = null; |
119 | |
|
120 | |
try { |
121 | 0 | stream = getResource(path).getInputStream(); |
122 | 0 | } catch (IOException e) { |
123 | 0 | Managers.getManager(ILoggingManager.class).getLogger(getClass()).error("Unable to load stream for {}", path); |
124 | 0 | Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e); |
125 | 0 | } |
126 | |
|
127 | 0 | return stream; |
128 | |
} |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
private boolean isImageNotCached(String index) { |
139 | 0 | return cache.get(index) == null; |
140 | |
} |
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
private void loadImageInCache(String path) { |
148 | 0 | BufferedImage image = getCompatibleImage(path); |
149 | |
|
150 | 0 | cache.put(path, new JThequeImage(image)); |
151 | 0 | } |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
private BufferedImage getCompatibleImage(String path) { |
160 | 0 | return ImageUtils.openCompatibleImage(getResourceAsStream(path)); |
161 | |
} |
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
private static BufferedImage getThumbnail(BufferedImage image, int width) { |
171 | 0 | return image == null ? null : ImageUtils.createThumbnail(image, width); |
172 | |
} |
173 | |
} |