KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > SImageIcon


1 /*
2  * $Id: SImageIcon.java,v 1.4 2005/01/28 14:47:52 blueshift 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.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 JavaDoc;
24 import java.awt.image.PixelGrabber JavaDoc;
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 JavaDoc image) {
43         this(new ImageIcon(image));
44     }
45
46     public SImageIcon(String JavaDoc name) {
47         this(new ImageIcon(name));
48     }
49
50     /**
51      * returns the URL, the icon can be fetched from. This URL may
52      * be relative, usually if generated from the externalizer.
53      */

54     public SimpleURL getURL() {
55         return url;
56     }
57
58     // get the image e.g. if you want to grey it out
59
public java.awt.Image JavaDoc getImage() {
60         return img.getImage();
61     }
62
63     protected String JavaDoc determineMimeType(Image image) {
64         PixelGrabber JavaDoc pg = new PixelGrabber JavaDoc(image, 0, 0, 1, 1, false);
65         try {
66             pg.grabPixels();
67         } catch (InterruptedException JavaDoc e) {
68             log.warn("interrupted waiting for pixels!");
69         }
70
71         String JavaDoc mimeType = "image/";
72         if (!(pg.getColorModel() instanceof IndexColorModel JavaDoc))
73             mimeType += ImageExternalizer.FORMAT_PNG;
74         else
75             mimeType += ImageExternalizer.FORMAT_GIF;
76
77         return mimeType;
78     }
79 }
80
81
82
Popular Tags