All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.wings.SResourceIcon Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2000,2005 wingS development team.
 *
 * This file is part of wingS (http://wingsframework.org).
 *
 * wingS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1
 * of the License, or (at your option) any later version.
 *
 * Please see COPYING for the complete licence.
 */
package org.wings;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wings.resource.ClassPathResource;
import org.wings.util.ImageInfo;

import java.io.ByteArrayInputStream;
import java.io.IOException;

/*
 * Diese Klasse ist nur ein Wrapper, um Eingabestroeme von Grafiken mit dem
 * ExternalizeManager mit der richtigen Endung und ohne Umweg einer neuen
 * Codierung (die z.B. keine Transparenz unterstuetzt) uebers WWW zugreifbar zu
 * machen. Zugleich muss diese Klasse aber auch zu der API der Componenten
 * passen, also ein Image bzw. ImageIcon sein. ImageIcon ist einfacher zu
 * benutzen und implementiert schon alles was benoetigt wird...
 */

/**
 * SIcon that gets it's content from an image found in the classpath.
 * i.e. an image that is deployed in the classes/jar/war file.
 * 

* An SIcon of this type is externalized globally. It is not bound * to a session. * * @author Armin Haaf */ public class SResourceIcon extends ClassPathResource implements SIcon { private final transient static Logger log = LoggerFactory.getLogger(SResourceIcon.class); /** * Width of icon, -1 if not set. */ private int width = -1; /** * Height of icon, -1 if not set. */ private int height = -1; /** * Title of icon, "" if not set. */ private String title = null; public SResourceIcon(String resourceFileName) { this(Thread.currentThread().getContextClassLoader(), resourceFileName); } public SResourceIcon(ClassLoader classLoader, String resourceFileName) { super(classLoader, resourceFileName); try { bufferResource(); } catch (Throwable e) { log.error("Can not buffer resource " + resourceFileName); } if (buffer != null && buffer.isValid()) { ImageInfo tImageInfo = new ImageInfo(); ByteArrayInputStream tImageInput = new ByteArrayInputStream(buffer.getBytes()); tImageInfo.setInput(tImageInput); if (tImageInfo.check()) { extension = tImageInfo.getFormatName(); mimeType = tImageInfo.getMimeType(); width = tImageInfo.getWidth(); height = tImageInfo.getHeight(); } try { tImageInput.close(); } catch (IOException ex) { // ignore it, we don't need it anymore... } } } @Override public int getIconWidth() { return width; } @Override public int getIconHeight() { return height; } @Override public void setIconWidth(int width) { this.width = width; } @Override public void setIconHeight(int height) { this.height = height; } @Override public String getIconTitle() { return (title!=null) ? title : ""; } @Override public void setIconTitle(String title) { this.title = title; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy