FileDocCategorySizeDatePackage
ImageLoader.javaAPI DocAzureus 3.0.3.47086Sun Sep 09 19:16:06 BST 2007com.aelitis.azureus.ui.swt.utils

ImageLoader

public class ImageLoader extends Object
author
TuxPaper
created
Jun 7, 2006

Fields Summary
private org.eclipse.swt.widgets.Display
display
public static org.eclipse.swt.graphics.Image
noImage
private final Map
mapImages
private final ArrayList
notFound
private com.aelitis.azureus.ui.skin.SkinProperties
skinProperties
private final ClassLoader
classLoader
Constructors Summary
public ImageLoader(ClassLoader classLoader, org.eclipse.swt.widgets.Display display, com.aelitis.azureus.ui.skin.SkinProperties skinProperties)

		this.classLoader = classLoader;
		mapImages = new HashMap();
		notFound = new ArrayList();
		this.display = display;
		this.skinProperties = skinProperties;
	
Methods Summary
private org.eclipse.swt.graphics.Image[]findResources(java.lang.String sKey)

		if (Collections.binarySearch(notFound, sKey) >= 0) {
			return null;
		}

		String[] sSuffixChecks = {
			"-over",
			"-down",
			"-disabled",
		};

		for (int i = 0; i < sSuffixChecks.length; i++) {
			String sSuffix = sSuffixChecks[i];

			if (sKey.endsWith(sSuffix)) {
				//System.out.println("YAY " + sSuffix + " for " + sKey);
				String sParentName = sKey.substring(0, sKey.length() - sSuffix.length());
				String[] sParentFiles = skinProperties.getStringArray(sParentName);
				if (sParentFiles != null) {
					boolean bFoundOne = false;
					Image[] images = new Image[sParentFiles.length];

					for (int j = 0; j < sParentFiles.length; j++) {
						int index = sParentFiles[j].lastIndexOf('.");
						if (index > 0) {
							String sTryFile = sParentFiles[j].substring(0, index) + sSuffix
									+ sParentFiles[j].substring(index);
							images[j] = loadImage(display, sTryFile, sKey);

							if (images[j] == null) {
								sTryFile = sParentFiles[j].substring(0, index)
										+ sSuffix.replace('-", '_")
										+ sParentFiles[j].substring(index);
								images[j] = loadImage(display, sTryFile, sKey);
							}

							if (!bFoundOne && images[j] != null) {
								bFoundOne = true;
							}
						}
					}

					if (bFoundOne) {
						return images;
					}
				}
			}
		}

		int i = Collections.binarySearch(notFound, sKey) * -1 - 1;
		notFound.add(i, sKey);
		return null;
	
public org.eclipse.swt.graphics.ImagegetImage(java.lang.String sKey)

		Image[] images = getImages(sKey);
		if (images == null || images.length == 0) {
			return null;
		}
		return images[0];
	
public org.eclipse.swt.graphics.Image[]getImages(java.lang.String sKey)

		if (sKey == null) {
			return new Image[] {
				getNoImage()
			};
		}

		Image[] images = (Image[]) mapImages.get(sKey);

		if (images != null) {
			return images;
		}
		String[] locations = skinProperties.getStringArray(sKey);
		//		System.out.println(sKey + "=" + properties.getStringValue(sKey)
		//				+ ";" + ((locations == null) ? "null" : "" + locations.length));
		if (locations == null || locations.length == 0) {
			images = findResources(sKey);

			if (images == null) {
				return new Image[] {
					getNoImage()
				};
			}

			for (int i = 0; i < images.length; i++) {
				if (images[i] == null) {
					images[i] = getNoImage();
				}
			}
		} else {
			images = new Image[locations.length];
			for (int i = 0; i < locations.length; i++) {
				images[i] = loadImage(display, locations[i], sKey);
				if (images[i] == null) {
					images[i] = getNoImage();
				}
			}
		}

		mapImages.put(sKey, images);

		return images;
	
private static org.eclipse.swt.graphics.ImagegetNoImage()

		if (noImage == null) {
			Display display = Display.getDefault();
			final int SIZE = 10;
			noImage = new Image(display, SIZE, SIZE);
			GC gc = new GC(noImage);
			gc.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
			gc.fillRectangle(0, 0, SIZE, SIZE);
			gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
			gc.drawRectangle(0, 0, SIZE - 1, SIZE - 1);
			gc.dispose();
		}
		return noImage;
	
public booleanimageExists(java.lang.String name)

		return isRealImage(getImage(name));
	
public static booleanisRealImage(org.eclipse.swt.graphics.Image image)

		return image != null && image != getNoImage() && !image.isDisposed();
	
private org.eclipse.swt.graphics.ImageloadImage(org.eclipse.swt.widgets.Display display, java.lang.String key)

		return loadImage(display, skinProperties.getStringValue(key), key);
	
private org.eclipse.swt.graphics.ImageloadImage(org.eclipse.swt.widgets.Display display, java.lang.String res, java.lang.String sKey)

		Image img = null;

		//System.out.println("LoadImage " + sKey + " - " + res);
		if (res == null) {
			String[] sSuffixChecks = {
				"-over",
				"-down",
				"-disabled",
			};

			for (int i = 0; i < sSuffixChecks.length; i++) {
				String sSuffix = sSuffixChecks[i];

				if (sKey.endsWith(sSuffix)) {
					//System.out.println("Yay " + sSuffix + " for " + sKey);
					String sParentName = sKey.substring(0, sKey.length()
							- sSuffix.length());
					String sParentFile = skinProperties.getStringValue(sParentName);
					if (sParentFile != null) {
						int index = sParentFile.lastIndexOf('.");
						if (index > 0) {
							String sTryFile = sParentFile.substring(0, index) + sSuffix
									+ sParentFile.substring(index);
							img = loadImage(display, sTryFile, sKey);

							if (img != null) {
								break;
							}

							sTryFile = sParentFile.substring(0, index)
									+ sSuffix.replace('-", '_") + sParentFile.substring(index);
							img = loadImage(display, sTryFile, sKey);

							if (img != null) {
								break;
							}
						}
					}
				}
			}
		}

		if (img == null) {
			try {
				InputStream is = classLoader.getResourceAsStream(res);
				if (is != null) {
					img = new Image(display, is);
				}

				if (img == null) {
					//IMP.log("ImageRepository:loadImage:: Resource not found: " + res);
				}
			} catch (Throwable e) {
				System.err.println("ImageRepository:loadImage:: Resource not found: "
						+ res + "\n" + e);
			}
		}

		return img;
	
public voidunLoadImages()

		Iterator iter;
		iter = mapImages.values().iterator();
		while (iter.hasNext()) {
			Image[] images = (Image[]) iter.next();
			if (images != null) {
				for (int i = 0; i < images.length; i++) {
					Image image = images[i];
					if (image != null && !image.isDisposed()) {
						image.dispose();
					}
				}
			}
		}