1 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 ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 25 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 43 private String title = null; 44 45 46 public SByteArrayIcon(byte[] pIconData, String pExtension, String 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 pExtension, String 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 tImageInput = new ByteArrayInputStream (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 imageInfo.setInput((InputStream ) null); 101 tImageInput.close(); 102 } catch (IOException ex) { 103 } 105 } 106 107 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 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 { 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 getIconTitle() { 171 return (title!=null) ? title : ""; 172 } 173 174 public void setIconTitle(String title) { 175 this.title = title; 176 } 177 } 178 | Popular Tags |