001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings;
014:
015: import org.wings.externalizer.ExternalizeManager;
016: import org.wings.externalizer.ResourceExternalizer;
017: import org.wings.io.Device;
018: import org.wings.util.ImageInfo;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.IOException;
022: import java.io.InputStream;
023:
024: /**
025: * An icon implementation that uses a bytearray for data storage.
026: * <p/>
027: * Actually this is a static resource, but buffering is not neccessary,
028: * so to save resources it's implement as a resource.
029: *
030: * @author <a href="mailto:haaf@mercatis.de">Armin Haaf</a>
031: */
032: public class SByteArrayIcon extends Resource implements SIcon {
033:
034: protected transient ImageInfo imageInfo;
035:
036: protected byte[] iconData;
037:
038: protected int width = -1;
039: protected int height = -1;
040:
041: /**
042: * Title of icon, <code>""</code> if not set.
043: */
044: private String title = null;
045:
046: public SByteArrayIcon(byte[] pIconData, String pExtension,
047: String pMimeType) {
048: setIconData(pIconData, pExtension, pMimeType);
049: }
050:
051: public SByteArrayIcon() {
052: }
053:
054: public SByteArrayIcon(byte[] pIconData) {
055: setIconData(pIconData, null, null);
056: }
057:
058: protected void externalize() {
059: ExternalizeManager ext = getSession().getExternalizeManager();
060: ext.removeExternalizedResource(ext.getId(id));
061: id = ext.externalize(this ,
062: ResourceExternalizer.SHARED_INSTANCE, null,
063: ExternalizeManager.SESSION | ExternalizeManager.FINAL);
064: }
065:
066: protected void removeExternalizedResource() {
067: if (id != null) {
068: ExternalizeManager ext = getSession()
069: .getExternalizeManager();
070: ext.removeExternalizedResource(ext.getId(id));
071: id = null;
072: }
073: }
074:
075: public void setIconData(byte[] pIconData, String pExtension,
076: String pMimeType) {
077: if (imageInfo == null) {
078: imageInfo = new ImageInfo();
079: imageInfo.setCollectComments(false);
080: imageInfo.setDetermineImageNumber(false);
081: }
082:
083: iconData = pIconData;
084: mimeType = pMimeType;
085: extension = pExtension;
086:
087: if ((pExtension == null || pMimeType == null)
088: && iconData != null) {
089: ByteArrayInputStream tImageInput = new ByteArrayInputStream(
090: iconData);
091: imageInfo.setInput(tImageInput);
092:
093: if (imageInfo.check()) {
094: if (extension == null) {
095: extension = imageInfo.getFormatName();
096: }
097: if (mimeType == null) {
098: mimeType = imageInfo.getMimeType();
099: }
100: width = imageInfo.getWidth();
101: height = imageInfo.getHeight();
102: }
103:
104: try {
105: // inputstream isn't needed any longer
106: imageInfo.setInput((InputStream) null);
107: tImageInput.close();
108: } catch (IOException ex) {
109: // ignore it, we don't need it anymore...
110: }
111: }
112:
113: // force new externalizing
114: removeExternalizedResource();
115: }
116:
117: public void setIconData(byte[] pIconData) {
118: setIconData(pIconData, null, null);
119: }
120:
121: public byte[] getIconData() {
122: return iconData;
123: }
124:
125: public int getEffectiveIconHeight() {
126: return imageInfo != null ? imageInfo.getHeight() : -1;
127: }
128:
129: public int getEffectiveIconWidth() {
130: return imageInfo != null ? imageInfo.getWidth() : -1;
131: }
132:
133: public String getId() {
134: return id;
135: }
136:
137: public SimpleURL getURL() {
138: if (id == null) {
139: externalize();
140: }
141:
142: RequestURL requestURL = (RequestURL) getSession().getProperty(
143: "request.url");
144: requestURL = (RequestURL) requestURL.clone();
145: requestURL.setResource(id);
146: return requestURL;
147: }
148:
149: public void write(Device d) throws IOException {
150: d.write(iconData);
151: }
152:
153: public int getIconWidth() {
154: return width;
155: }
156:
157: public int getIconHeight() {
158: return height;
159: }
160:
161: public void setIconWidth(int pWidth) {
162: width = pWidth;
163: }
164:
165: public void setIconHeight(int pHeight) {
166: height = pHeight;
167: }
168:
169: protected void finalize() {
170: removeExternalizedResource();
171: }
172:
173: public ImageInfo getImageInfo() {
174: return imageInfo;
175: }
176:
177: public String getIconTitle() {
178: return (title != null) ? title : "";
179: }
180:
181: public void setIconTitle(String title) {
182: this.title = title;
183: }
184: }
|