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.ImageExternalizer;
016: import org.wings.session.SessionManager;
017: import org.apache.commons.logging.Log;
018: import org.apache.commons.logging.LogFactory;
019:
020: import javax.swing.*;
021: import java.awt.*;
022: import java.awt.image.IndexColorModel;
023: import java.awt.image.PixelGrabber;
024:
025: /**
026: * SIcon implementation that is based on {@link ImageIcon}.
027: */
028: public class SImageIcon extends SAbstractIcon {
029: private final static Log log = LogFactory.getLog(SImageIcon.class);
030:
031: private final ImageIcon img;
032: private final SimpleURL url;
033:
034: public SImageIcon(ImageIcon image) {
035: this (image, determineMimeType(image));
036: }
037:
038: public SImageIcon(ImageIcon image, String mimeType) {
039: if (image == null)
040: throw new IllegalArgumentException(
041: "SImageIcon needs an Argument that's not null");
042:
043: img = image;
044: url = new SimpleURL(SessionManager.getSession()
045: .getExternalizeManager().externalize(image, mimeType));
046: setIconWidth(image.getIconWidth());
047: setIconHeight(image.getIconHeight());
048: }
049:
050: private static String extractMimeTypeFromPath(String description) {
051: if (description == null)
052: return null;
053: String[] supported = ImageExternalizer.SUPPORTED_FORMATS;
054: for (int i = 0; i < supported.length; i++) {
055: if (description.endsWith(supported[i]))
056: return "image/" + supported[i];
057: }
058: // special case jpg for jpeg
059: if (description.endsWith("jpg"))
060: return "image/jpeg";
061: else
062: return null;
063: }
064:
065: public SImageIcon(java.awt.Image image) {
066: this (new ImageIcon(image));
067: }
068:
069: public SImageIcon(java.awt.Image image, String mimeType) {
070: this (new ImageIcon(image), mimeType);
071: }
072:
073: public SImageIcon(String name) {
074: this (new ImageIcon(name));
075: }
076:
077: public SImageIcon(String name, String mimeType) {
078: this (new ImageIcon(name), mimeType);
079: }
080:
081: /**
082: * returns the URL, the icon can be fetched from. This URL may
083: * be relative, usually if generated from the externalizer.
084: */
085: public SimpleURL getURL() {
086: RequestURL requestURL = (RequestURL) SessionManager
087: .getSession().getProperty("request.url");
088: if (requestURL != null) {
089: requestURL = (RequestURL) requestURL.clone();
090: requestURL.setResource(url.toString());
091: return requestURL;
092: } else
093: return url;
094: }
095:
096: // get the image e.g. if you want to grey it out
097: public java.awt.Image getImage() {
098: return img.getImage();
099: }
100:
101: protected static String determineMimeType(ImageIcon imageIcon) {
102: String mimeType = extractMimeTypeFromPath(imageIcon
103: .getDescription());
104: if (mimeType != null)
105: return mimeType;
106: PixelGrabber pg = new PixelGrabber(imageIcon.getImage(), 0, 0,
107: 1, 1, false);
108: try {
109: pg.grabPixels();
110: } catch (InterruptedException e) {
111: log.warn("interrupted waiting for pixels!");
112: }
113:
114: mimeType = "image/";
115: if (!(pg.getColorModel() instanceof IndexColorModel))
116: mimeType += ImageExternalizer.FORMAT_PNG;
117: else
118: mimeType += ImageExternalizer.FORMAT_GIF;
119:
120: return mimeType;
121: }
122: }
|