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.apache.commons.logging.Log;
016: import org.apache.commons.logging.LogFactory;
017: import org.wings.resource.ClassPathResource;
018: import org.wings.util.ImageInfo;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.IOException;
022:
023: /*
024: * Diese Klasse ist nur ein Wrapper, um Eingabestroeme von Grafiken mit dem
025: * ExternalizeManager mit der richtigen Endung und ohne Umweg einer neuen
026: * Codierung (die z.B. keine Transparenz unterstuetzt) uebers WWW zugreifbar zu
027: * machen. Zugleich muss diese Klasse aber auch zu der API der Componenten
028: * passen, also ein Image bzw. ImageIcon sein. ImageIcon ist einfacher zu
029: * benutzen und implementiert schon alles was benoetigt wird...
030: */
031:
032: /**
033: * SIcon that gets it's content from an image found in the classpath.
034: * i.e. an image that is deployed in the classes/jar/war file.
035: * <p/>
036: * An SIcon of this type is externalized globally. It is not bound
037: * to a session.
038: *
039: * @author <a href="mailto:haaf@mercatis.de">Armin Haaf</a>
040: */
041: public class SResourceIcon extends ClassPathResource implements SIcon {
042:
043: private final transient static Log log = LogFactory
044: .getLog(SResourceIcon.class);
045:
046: /**
047: * Width of icon, <code>-1</code> if not set.
048: */
049: private int width = -1;
050:
051: /**
052: * Height of icon, <code>-1</code> if not set.
053: */
054: private int height = -1;
055:
056: /**
057: * Title of icon, <code>""</code> if not set.
058: */
059: private String title = null;
060:
061: public SResourceIcon(String resourceFileName) {
062: this (Thread.currentThread().getContextClassLoader(),
063: resourceFileName);
064: }
065:
066: public SResourceIcon(ClassLoader classLoader,
067: String resourceFileName) {
068: super (classLoader, resourceFileName);
069:
070: try {
071: bufferResource();
072: } catch (Throwable e) {
073: log.fatal("Can not buffer resource " + resourceFileName);
074: }
075:
076: if (buffer != null && buffer.isValid()) {
077: ImageInfo tImageInfo = new ImageInfo();
078: ByteArrayInputStream tImageInput = new ByteArrayInputStream(
079: buffer.getBytes());
080: tImageInfo.setInput(tImageInput);
081:
082: if (tImageInfo.check()) {
083: extension = tImageInfo.getFormatName();
084: mimeType = tImageInfo.getMimeType();
085: width = tImageInfo.getWidth();
086: height = tImageInfo.getHeight();
087: }
088:
089: try {
090: tImageInput.close();
091: } catch (IOException ex) {
092: // ignore it, we don't need it anymore...
093: }
094: }
095: }
096:
097: public int getIconWidth() {
098: return width;
099: }
100:
101: public int getIconHeight() {
102: return height;
103: }
104:
105: public void setIconWidth(int width) {
106: this .width = width;
107: }
108:
109: public void setIconHeight(int height) {
110: this .height = height;
111: }
112:
113: public String getIconTitle() {
114: return (title != null) ? title : "";
115: }
116:
117: public void setIconTitle(String title) {
118: this.title = title;
119: }
120: }
|