KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > SByteArrayIcon


1 /*
2  * $Id: SByteArrayIcon.java,v 1.5 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.wings.externalizer.ExternalizeManager;
17 import org.wings.externalizer.ResourceExternalizer;
18 import org.wings.io.Device;
19 import org.wings.util.ImageInfo;
20
21 import java.io.ByteArrayInputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24
25 /**
26  * actual this is a static resource, but buffering is not neccessary, so to save resources implement it as resource
27  *
28  * @author <a HREF="mailto:haaf@mercatis.de">Armin Haaf</a>
29  * @version $Revision: 1.5 $
30  */

31 public class SByteArrayIcon extends Resource implements SIcon {
32
33     protected ImageInfo imageInfo;
34
35     protected byte[] iconData;
36
37     protected int width = -1;
38     protected int height = -1;
39
40     /**
41      * Title of icon, <code>""</code> if not set.
42      */

43     private String JavaDoc title = null;
44
45
46     public SByteArrayIcon(byte[] pIconData, String JavaDoc pExtension, String JavaDoc pMimeType) {
47         setIconData(pIconData, pExtension, pMimeType);
48     }
49
50     public SByteArrayIcon() {
51     }
52
53     public SByteArrayIcon(byte[] pIconData) {
54         setIconData(pIconData, null, null);
55     }
56
57     protected void externalize() {
58         ExternalizeManager ext = getSession().getExternalizeManager();
59         ext.removeExternalizedResource(ext.getId(id));
60         id = ext.externalize(this, ResourceExternalizer.SHARED_INSTANCE,
61                 null, ExternalizeManager.SESSION | ExternalizeManager.FINAL);
62     }
63
64     protected void removeExternalizedResource() {
65         if (id != null) {
66             ExternalizeManager ext = getSession().getExternalizeManager();
67             ext.removeExternalizedResource(ext.getId(id));
68             id = null;
69         }
70     }
71
72     public void setIconData(byte[] pIconData, String JavaDoc pExtension, String JavaDoc pMimeType) {
73         if (imageInfo == null) {
74             imageInfo = new ImageInfo();
75             imageInfo.setCollectComments(false);
76             imageInfo.setDetermineImageNumber(false);
77         }
78
79         iconData = pIconData;
80         mimeType = pMimeType;
81         extension = pExtension;
82
83         if ((pExtension == null || pMimeType == null) && iconData != null) {
84             ByteArrayInputStream JavaDoc tImageInput = new ByteArrayInputStream JavaDoc(iconData);
85             imageInfo.setInput(tImageInput);
86
87             if (imageInfo.check()) {
88                 if (extension == null) {
89                     extension = imageInfo.getFormatName();
90                 }
91                 if (mimeType == null) {
92                     mimeType = imageInfo.getMimeType();
93                 }
94                 width = imageInfo.getWidth();
95                 height = imageInfo.getHeight();
96             }
97
98             try {
99                 // inputstream isn't needed any longer
100
imageInfo.setInput((InputStream JavaDoc) null);
101                 tImageInput.close();
102             } catch (IOException JavaDoc ex) {
103                 // ignore it, we don't need it anymore...
104
}
105         }
106
107         // force new externalizing
108
removeExternalizedResource();
109     }
110
111     public void setIconData(byte[] pIconData) {
112         setIconData(pIconData, null, null);
113     }
114
115     public byte[] getIconData() {
116         return iconData;
117     }
118
119     public int getEffectiveIconHeight() {
120         return imageInfo != null ? imageInfo.getHeight() : -1;
121     }
122
123     public int getEffectiveIconWidth() {
124         return imageInfo != null ? imageInfo.getWidth() : -1;
125     }
126
127     public String JavaDoc getId() {
128         return id;
129     }
130
131     public SimpleURL getURL() {
132         if (id == null) {
133             externalize();
134         }
135
136         RequestURL requestURL = (RequestURL) getSession().getProperty("request.url");
137         requestURL = (RequestURL) requestURL.clone();
138         requestURL.setResource(id);
139         return requestURL;
140     }
141
142     public void write(Device d) throws IOException JavaDoc {
143         d.write(iconData);
144     }
145
146     public int getIconWidth() {
147         return width;
148     }
149
150     public int getIconHeight() {
151         return height;
152     }
153
154     public void setIconWidth(int pWidth) {
155         width = pWidth;
156     }
157
158     public void setIconHeight(int pHeight) {
159         height = pHeight;
160     }
161
162     protected void finalize() {
163         removeExternalizedResource();
164     }
165
166     public ImageInfo getImageInfo() {
167         return imageInfo;
168     }
169
170     public String JavaDoc getIconTitle() {
171         return (title!=null) ? title : "";
172     }
173     
174     public void setIconTitle(String JavaDoc title) {
175         this.title = title;
176     }
177 }
178
Popular Tags