001: /*
002: * Beryl - A web platform based on XML, XSLT and Java
003: * This file is part of the Beryl XML GUI
004: *
005: * Copyright (C) 2004 Wenzel Jakob <wazlaf@tigris.org>
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011:
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-3107 USA
020: */
021:
022: package org.beryl.gui;
023:
024: import java.net.URL;
025: import java.util.ArrayList;
026: import java.util.HashMap;
027:
028: import javax.swing.ImageIcon;
029:
030: import org.apache.log4j.Logger;
031:
032: public class ImageIconFactory {
033: private static Logger log = Logger
034: .getLogger(ImageIconFactory.class);
035: private static ArrayList searchPaths = new ArrayList();
036: private static ImageIconFactory iif;
037: private static HashMap hmImageIconCache = new HashMap();
038:
039: private static final String TYPE_JPG = ".jpg";
040: private static final String TYPE_GIF = ".gif";
041: private static final String TYPE_PNG = ".png";
042:
043: public static void addSearchPath(String path) {
044: if (!searchPaths.contains(path)) {
045: searchPaths.add(path);
046: }
047: }
048:
049: public static ImageIcon getIcon(String sName) {
050: if (sName == null) {
051: return null;
052: }
053:
054: String sImageName = "";
055:
056: if (sName.toLowerCase().endsWith(TYPE_GIF)
057: || sName.toLowerCase().endsWith(TYPE_JPG)
058: || sName.toLowerCase().endsWith(TYPE_PNG)) {
059: sImageName = sName;
060: } else {
061: /* PNG should be the preferred image file format because
062: * it supports real alpha transparency */
063: sImageName = sName + TYPE_PNG;
064: }
065:
066: if (hmImageIconCache.containsKey(sImageName)) {
067: return (ImageIcon) hmImageIconCache.get(sImageName);
068: }
069:
070: URL url = null;
071: for (int i = searchPaths.size() - 1; i >= 0; i--) {
072: String sPath = "/" + searchPaths.get(i) + "/" + sImageName;
073: url = ImageIconFactory.class.getResource(sPath);
074: if (url != null) {
075: break;
076: }
077: }
078:
079: if (url == null) {
080: if (sName.equals("broken")) {
081: throw new RuntimeException(
082: "Error while trying to load the 'broken' image substitute, maybe the XML GUI has not been initialized?");
083: }
084: log
085: .warn("ImageIconFactory: Image ["
086: + sName
087: + "] could not be found at any of the search paths");
088: return getIcon("broken");
089: }
090:
091: ImageIcon imageIcon = new ImageIcon(url);
092: imageIcon.setDescription(sName);
093: if (imageIcon != null) {
094: hmImageIconCache.put(sImageName, imageIcon);
095: } else {
096: return getIcon("broken");
097: }
098: return imageIcon;
099: }
100: }
|