1 /*
2 Copyright 2002-2006 Martin van den Bemt
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
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 * Contains several utilities to make life with swing easier.
33 *
34 * @todo Reinstate imagecache and see how we can handle that for multiple
35 * guilayers.
36 * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
37 * @version $Id: SwingUtils.java,v 1.1 2005/12/18 12:58:22 mvdb Exp $
38 */
39 public class SwingUtils {
40
41 /**
42 * The default custom images loader (uses jimi)
43 */
44 private static final String DEFAULT_CUSTOMIMAGELOADER = "org.xulux.swing.util.JimiImageLoader";
45
46 /**
47 * Contains the custom image loader
48 */
49 private static ImageLoaderInterface imageLoader = null;
50
51 /**
52 * Static initializer to check if jimi is present
53 * or not. If it is present if will be used for icon
54 * and image processing
55 */
56 static {
57 initializeImageLoader();
58 }
59
60 /**
61 * Override constructor.
62 */
63 protected SwingUtils() {
64 }
65
66 /**
67 * Initializes the imageloader from system properties.
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 //imageLoader = (ImageLoaderInterface) Class.forName(il).newInstance();
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 * @return the imagelaoder that was initialized. If the default is used (swing) than null
86 * is returned
87 */
88 protected static ImageLoaderInterface getImageLoader() {
89 return imageLoader;
90 }
91
92 /**
93 * Returns the image based on the resource.
94 * The resource should be available to the classloader,
95 * otherwize it will fail loading.
96 * It will use the ImageInterface when jimi is supported
97 *
98 * @param resource - the resource from the classpath
99 * @param object - the object to get the classLoader from.
100 * At this time it cannot be null
101 * @return the Image retrieved
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 // cache = w.getPart().getImageCache();
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 * @param resource the resource of the image
139 * @param object the object to get the classloader from
140 * @return the imageIcon found or null if not found
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 // cache = w.getPart().getImageCache();
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 * @param rectangle the rectangle to get the dimensions for
186 * @return the dimensions for the rectangle specified
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 * Creates an insets object from a comma delimited string.
199 * If the string is incomplete null will be returned.
200 *
201 * @param margin - the margin in the format top,left,bottom,right
202 * @return the insets depending on the margin
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 }