1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.sdkuilib.internal.repository.icons; 18 19 import com.android.sdklib.internal.repository.Archive; 20 import com.android.sdklib.internal.repository.Package; 21 import com.android.sdklib.internal.repository.RepoSource; 22 import com.android.sdkuilib.internal.repository.RepoSourcesAdapter; 23 24 import org.eclipse.swt.SWTException; 25 import org.eclipse.swt.graphics.Image; 26 import org.eclipse.swt.widgets.Display; 27 28 import java.io.InputStream; 29 import java.util.HashMap; 30 import java.util.Iterator; 31 32 33 /** 34 * An utility class to serve {@link Image} correspond to the various icons 35 * present in this package and dispose of them correctly at the end. 36 */ 37 public class ImageFactory { 38 39 private final Display mDisplay; 40 private final HashMap<String, Image> mImages = new HashMap<String, Image>(); 41 ImageFactory(Display display)42 public ImageFactory(Display display) { 43 mDisplay = display; 44 } 45 46 /** 47 * Loads an image given its filename (with its extension). 48 * Might return null if the image cannot be loaded. 49 * 50 * @param imageName The filename (with extension) of the image to load. 51 * @return A new or existing {@link Image}. The caller must NOT dispose the image (the 52 * image will disposed by {@link #dispose()}). The returned image can be null if the 53 * expected file is missing. 54 */ getImageByName(String imageName)55 public Image getImageByName(String imageName) { 56 57 Image image = mImages.get(imageName); 58 if (image != null) { 59 return image; 60 } 61 62 InputStream stream = getClass().getResourceAsStream(imageName); 63 if (stream != null) { 64 try { 65 image = new Image(mDisplay, stream); 66 } catch (SWTException e) { 67 // ignore 68 } catch (IllegalArgumentException e) { 69 // ignore 70 } 71 } 72 73 // Store the image in the hash, even if this failed. If it fails now, it will fail later. 74 mImages.put(imageName, image); 75 76 return image; 77 } 78 79 /** 80 * Loads and returns the appropriate image for a given package, archive or source object. 81 * 82 * @param object A {@link RepoSource} or {@link Package} or {@link Archive}. 83 * @return A new or existing {@link Image}. The caller must NOT dispose the image (the 84 * image will disposed by {@link #dispose()}). The returned image can be null if the 85 * expected file is missing. 86 */ getImageForObject(Object object)87 public Image getImageForObject(Object object) { 88 89 if (object == null) { 90 return null; 91 } 92 93 String clz = object.getClass().getSimpleName(); 94 if (clz.endsWith(Package.class.getSimpleName())) { 95 String name = clz.replaceFirst(Package.class.getSimpleName(), "").toLowerCase() + //$NON-NLS-1$ 96 "_pkg_16.png"; //$NON-NLS-1$ 97 return getImageByName(name); 98 } 99 100 if (object instanceof RepoSource) { 101 return getImageByName("source_icon16.png"); //$NON-NLS-1$ 102 103 } else if (object instanceof RepoSourcesAdapter.RepoSourceError) { 104 return getImageByName("error_icon16.png"); //$NON-NLS-1$ 105 106 } else if (object instanceof RepoSourcesAdapter.RepoSourceEmpty) { 107 return getImageByName("nopkg_icon16.png"); //$NON-NLS-1$ 108 } 109 110 if (object instanceof Archive) { 111 if (((Archive) object).isCompatible()) { 112 return getImageByName("archive_icon16.png"); //$NON-NLS-1$ 113 } else { 114 return getImageByName("incompat_icon16.png"); //$NON-NLS-1$ 115 } 116 } 117 118 return null; 119 } 120 121 /** 122 * Dispose all the images created by this factory so far. 123 */ dispose()124 public void dispose() { 125 Iterator<Image> it = mImages.values().iterator(); 126 while(it.hasNext()) { 127 Image img = it.next(); 128 if (img != null && img.isDisposed() == false) { 129 img.dispose(); 130 } 131 it.remove(); 132 } 133 } 134 135 } 136