1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.xulux.guilayer.swing.util;
17
18 import java.awt.Dimension;
19 import java.awt.Image;
20 import java.awt.Insets;
21 import java.awt.MediaTracker;
22 import java.net.URL;
23
24 import javax.swing.ImageIcon;
25
26 import org.xulux.api.gui.IWidget;
27 import org.xulux.api.gui.IWidgetRectangle;
28 import org.xulux.utils.ClassLoaderUtils;
29 import org.xulux.utils.XuluxCollectionUtils;
30
31
32
33
34
35
36
37
38
39 public class SwingUtils {
40
41
42
43
44 private static final String DEFAULT_CUSTOMIMAGELOADER = "org.xulux.swing.util.JimiImageLoader";
45
46
47
48
49 private static ImageLoaderInterface imageLoader = null;
50
51
52
53
54
55
56 static {
57 initializeImageLoader();
58 }
59
60
61
62
63 protected SwingUtils() {
64 }
65
66
67
68
69 protected static void initializeImageLoader() {
70 String il = System.getProperty("xulux.swing.imageloader", DEFAULT_CUSTOMIMAGELOADER);
71 try {
72 imageLoader = (ImageLoaderInterface) ClassLoaderUtils.getObjectFromClassString(il);
73
74 if (!imageLoader.isUsable()) {
75 imageLoader = null;
76 throw new Exception();
77 }
78 } catch (Exception e) {
79 System.out.println("Custom ImageLoader " + il + " could not be loaded. Using Swing imageloader");
80 }
81 }
82
83
84
85
86
87
88 protected static ImageLoaderInterface getImageLoader() {
89 return imageLoader;
90 }
91
92
93
94
95
96
97
98
99
100
101
102
103 public static Image getImage(String resource, Object object) {
104 if (resource == null || object == null) {
105 return null;
106 }
107 ImageCache cache = null;
108 if (object instanceof IWidget) {
109 IWidget w = (IWidget) object;
110 if (w.getPart() != null) {
111
112 }
113 }
114 Image retValue = null;
115 if (cache != null) {
116 retValue = cache.getImage(resource);
117 }
118 if (retValue == null) {
119 if (imageLoader != null) {
120 URL imageURL = object.getClass().getClassLoader().getResource(resource);
121 retValue = imageLoader.getImage(imageURL);
122 } else {
123 ImageIcon icon = getIcon(resource, object);
124 if (icon != null) {
125 retValue = icon.getImage();
126 }
127 }
128 if (retValue != null && cache != null) {
129 cache.addImage(resource, retValue);
130 }
131 }
132
133 return retValue;
134 }
135
136
137
138
139
140
141
142 public static ImageIcon getIcon(String resource, Object object) {
143 if (object == null) {
144 return null;
145 }
146 ImageCache cache = null;
147 if (object instanceof IWidget) {
148 IWidget w = (IWidget) object;
149 if (w.getPart() != null) {
150
151 }
152 }
153 ImageIcon icon = null;
154 if (cache != null) {
155 icon = cache.getImageIcon(resource);
156 }
157 if (icon == null) {
158 URL imageURL = object.getClass().getClassLoader().getResource(resource);
159 if (imageLoader != null) {
160 icon = imageLoader.getIcon(imageURL);
161 } else {
162 if (imageURL != null) {
163 icon = new ImageIcon(imageURL);
164 if (icon.getImageLoadStatus() == MediaTracker.ERRORED) {
165 icon = null;
166 System.out.println(
167 "Image type "
168 + resource
169 + " not supported by swing "
170 + "we advice you to add jimi to your classpath or convert your "
171 + "image to an image type supported by swing");
172 }
173 } else {
174 System.out.println("Image " + resource + " cannot be found");
175 }
176 }
177 if (icon != null && cache != null) {
178 cache.addImage(resource, icon);
179 }
180 }
181 return icon;
182 }
183
184
185
186
187
188 public static Dimension getDimension(IWidgetRectangle rectangle) {
189 if (rectangle == null) {
190 return null;
191 }
192 Dimension dim = new Dimension();
193 dim.setSize(rectangle.getWidth(), rectangle.getHeight());
194 return dim;
195 }
196
197
198
199
200
201
202
203
204 public static Insets getInsets(String margin) {
205 if (margin == null || "".equals(margin.trim())) {
206 return null;
207 }
208 Object[] ins = XuluxCollectionUtils.getListFromCSV(margin).toArray();
209 if (ins.length == 4) {
210 try {
211 int top = Integer.parseInt(((String) ins[0]).trim());
212 int left = Integer.parseInt(((String) ins[1]).trim());
213 int bottom = Integer.parseInt(((String) ins[2]).trim());
214 int right = Integer.parseInt(((String) ins[3]).trim());
215 return new Insets(top, left, bottom, right);
216 } catch (Exception e) {
217 return null;
218 }
219 }
220 return null;
221 }
222 }