KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > SResourceIcon


1 /*
2  * $Id: SResourceIcon.java,v 1.6 2005/06/07 12:55:58 neurolabs Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18 import org.wings.resource.ClasspathResource;
19 import org.wings.util.ImageInfo;
20
21 import java.io.ByteArrayInputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23
24 /*
25  * Diese Klasse ist nur ein Wrapper, um Eingabestroeme von Grafiken mit dem
26  * ExternalizeManager mit der richtigen Endung und ohne Umweg einer neuen
27  * Codierung (die z.B. keine Transparenz unterstuetzt) uebers WWW zugreifbar zu
28  * machen. Zugleich muss diese Klasse aber auch zu der API der Componenten
29  * passen, also ein Image bzw. ImageIcon sein. ImageIcon ist einfacher zu
30  * benutzen und implementiert schon alles was benoetigt wird...
31  */

32
33 /**
34  * An SIcon of this type is externalized globally. It is not bound
35  * to a session. This SIcon gets the content of the image from an image
36  * found in the classpath, i.e. an image that is deployed in the
37  * classes/jar/war file.
38  *
39  * @author <a HREF="mailto:haaf@mercatis.de">Armin Haaf</a>
40  * @version $Revision: 1.6 $
41  */

42 public class SResourceIcon extends ClasspathResource implements SIcon {
43
44     private final transient static Log log = LogFactory.getLog(SResourceIcon.class);
45
46     /**
47      * Width of icon, <code>-1</code> if not set.
48      */

49     private int width = -1;
50
51     /**
52      * Height of icon, <code>-1</code> if not set.
53      */

54     private int height = -1;
55
56     /**
57      * Title of icon, <code>""</code> if not set.
58      */

59     private String JavaDoc title = null;
60
61
62     public SResourceIcon(String JavaDoc resourceFileName) {
63         this(SResourceIcon.class.getClassLoader(), resourceFileName);
64     }
65
66     public SResourceIcon(ClassLoader JavaDoc classLoader, String JavaDoc resourceFileName) {
67         super(classLoader, resourceFileName);
68
69         try {
70             bufferResource();
71         } catch (Throwable JavaDoc e) {
72             log.fatal("Can not buffer resource " + resourceFileName);
73         }
74
75         if (buffer != null && buffer.isValid()) {
76             ImageInfo tImageInfo = new ImageInfo();
77             ByteArrayInputStream JavaDoc tImageInput = new ByteArrayInputStream JavaDoc(buffer.getBytes());
78             tImageInfo.setInput(tImageInput);
79
80             if (tImageInfo.check()) {
81                 extension = tImageInfo.getFormatName();
82                 mimeType = tImageInfo.getMimeType();
83                 width = tImageInfo.getWidth();
84                 height = tImageInfo.getHeight();
85             }
86
87             try {
88                 tImageInput.close();
89             } catch (IOException JavaDoc ex) {
90                 // ignore it, we don't need it anymore...
91
}
92         }
93     }
94
95     public int getIconWidth() {
96         return width;
97     }
98
99     public int getIconHeight() {
100         return height;
101     }
102
103     public void setIconWidth(int width) {
104         this.width = width;
105     }
106
107     public void setIconHeight(int height) {
108         this.height = height;
109     }
110
111     public String JavaDoc getIconTitle() {
112         return (title!=null) ? title : "";
113     }
114     
115     public void setIconTitle(String JavaDoc title) {
116         this.title = title;
117     }
118 }
119
120
121
Popular Tags