001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: Images.java 3711 2007-04-06 07:38:30Z gbevin $
007: */
008: package com.uwyn.rife.swing;
009:
010: import com.uwyn.rife.rep.Participant;
011: import com.uwyn.rife.rep.Rep;
012: import com.uwyn.rife.resources.ResourceFinderClasspath;
013: import com.uwyn.rife.tools.ClasspathUtils;
014: import java.io.File;
015: import java.io.FileFilter;
016: import java.util.ArrayList;
017: import java.util.HashMap;
018: import javax.swing.ImageIcon;
019:
020: public class Images {
021: public final static String DEFAULT_PARTICIPANT_NAME = "ParticipantImages";
022:
023: private String mPath = null;
024: private HashMap<String, ImageIcon> mImages = null;
025:
026: public Images() {
027: mImages = new HashMap<String, ImageIcon>();
028: }
029:
030: public Images(String path) {
031: if (null == path)
032: throw new IllegalArgumentException("path can't be null.");
033: if (0 == path.length())
034: throw new IllegalArgumentException("path can't be empty.");
035:
036: mPath = path;
037: initialize();
038:
039: assert mPath != null;
040: assert mPath.length() > 0;
041: assert mImages != null;
042: }
043:
044: private void initialize() {
045: mImages = new HashMap<String, ImageIcon>();
046:
047: ArrayList<String> resources = ClasspathUtils
048: .getResourcesInDirectory(mPath, new FileFilter() {
049: public boolean accept(File file) {
050: if (file.getName().equals("framework")
051: || file.getName().equals("unittests")
052: || file.getName().equals("templates")
053: || file.getName().equals("elements")) {
054: return false;
055: }
056:
057: return true;
058: }
059: });
060:
061: String graphics_filename_short = null;
062:
063: for (String graphics_filename : resources) {
064: graphics_filename_short = graphics_filename.substring(mPath
065: .length());
066:
067: mImages.put(graphics_filename_short, new ImageIcon(
068: ResourceFinderClasspath.getInstance().getResource(
069: graphics_filename)));
070: }
071: }
072:
073: public static boolean hasRepInstance() {
074: return Rep.hasParticipant(DEFAULT_PARTICIPANT_NAME);
075: }
076:
077: public static Images getRepInstance() {
078: Participant participant = Rep
079: .getParticipant(DEFAULT_PARTICIPANT_NAME);
080: if (null == participant) {
081: return null;
082: }
083:
084: return (Images) participant.getObject();
085: }
086:
087: public ImageIcon getImageIcon(String path) {
088: if (null == path)
089: throw new IllegalArgumentException("path can't be null.");
090:
091: return mImages.get(path);
092: }
093:
094: public ArrayList<String> getImageIconNames(String prefix) {
095: if (null == prefix)
096: throw new IllegalArgumentException("prefix can't be null.");
097:
098: ArrayList<String> matching_images = new ArrayList<String>();
099:
100: for (String image_filename : mImages.keySet()) {
101: if (image_filename.startsWith(prefix)) {
102: matching_images.add(image_filename);
103: }
104: }
105:
106: return matching_images;
107: }
108: }
|