1 14 package org.wings; 15 16 import org.wings.externalizer.ImageExternalizer; 17 import org.wings.session.SessionManager; 18 import org.apache.commons.logging.Log; 19 import org.apache.commons.logging.LogFactory; 20 21 import javax.swing.*; 22 import java.awt.*; 23 import java.awt.image.IndexColorModel ; 24 import java.awt.image.PixelGrabber ; 25 26 public class SImageIcon extends SAbstractIcon { 27 private final static transient Log log = LogFactory.getLog(SImageIcon.class); 28 29 private final ImageIcon img; 30 private final SimpleURL url; 31 32 public SImageIcon(ImageIcon image) { 33 this.img = image; 34 url = new SimpleURL(SessionManager.getSession() 35 .getExternalizeManager() 36 .externalize(image, determineMimeType(image.getImage()))); 37 38 setIconWidth(img.getIconWidth()); 39 setIconHeight(img.getIconHeight()); 40 } 41 42 public SImageIcon(java.awt.Image image) { 43 this(new ImageIcon(image)); 44 } 45 46 public SImageIcon(String name) { 47 this(new ImageIcon(name)); 48 } 49 50 54 public SimpleURL getURL() { 55 return url; 56 } 57 58 public java.awt.Image getImage() { 60 return img.getImage(); 61 } 62 63 protected String determineMimeType(Image image) { 64 PixelGrabber pg = new PixelGrabber (image, 0, 0, 1, 1, false); 65 try { 66 pg.grabPixels(); 67 } catch (InterruptedException e) { 68 log.warn("interrupted waiting for pixels!"); 69 } 70 71 String mimeType = "image/"; 72 if (!(pg.getColorModel() instanceof IndexColorModel )) 73 mimeType += ImageExternalizer.FORMAT_PNG; 74 else 75 mimeType += ImageExternalizer.FORMAT_GIF; 76 77 return mimeType; 78 } 79 } 80 81 82 | Popular Tags |