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.archives.Archive; 20 import com.android.sdklib.internal.repository.packages.Package; 21 import com.android.sdklib.internal.repository.sources.SdkSource; 22 import com.android.sdklib.internal.repository.sources.SdkSourceCategory; 23 import com.android.sdkuilib.internal.repository.sdkman2.PkgContentProvider; 24 25 import org.eclipse.swt.SWTException; 26 import org.eclipse.swt.graphics.Image; 27 import org.eclipse.swt.widgets.Display; 28 29 import java.io.InputStream; 30 import java.util.HashMap; 31 import java.util.Iterator; 32 import java.util.Locale; 33 import java.util.Map; 34 35 36 /** 37 * An utility class to serve {@link Image} correspond to the various icons 38 * present in this package and dispose of them correctly at the end. 39 */ 40 public class ImageFactory { 41 42 private final Display mDisplay; 43 private final Map<String, Image> mImages = new HashMap<String, Image>(); 44 ImageFactory(Display display)45 public ImageFactory(Display display) { 46 mDisplay = display; 47 } 48 49 /** 50 * Loads an image given its filename (with its extension). 51 * Might return null if the image cannot be loaded. 52 * The image is cached. Successive calls will return the <em>same</em> object. 53 * 54 * @param imageName The filename (with extension) of the image to load. 55 * @return A new or existing {@link Image}. The caller must NOT dispose the image (the 56 * image will disposed by {@link #dispose()}). The returned image can be null if the 57 * expected file is missing. 58 */ getImageByName(String imageName)59 public Image getImageByName(String imageName) { 60 61 Image image = mImages.get(imageName); 62 if (image != null) { 63 return image; 64 } 65 66 InputStream stream = getClass().getResourceAsStream(imageName); 67 if (stream != null) { 68 try { 69 image = new Image(mDisplay, stream); 70 } catch (SWTException e) { 71 // ignore 72 } catch (IllegalArgumentException e) { 73 // ignore 74 } 75 } 76 77 // Store the image in the hash, even if this failed. If it fails now, it will fail later. 78 mImages.put(imageName, image); 79 80 return image; 81 } 82 83 /** 84 * Loads and returns the appropriate image for a given package, archive or source object. 85 * The image is cached. Successive calls will return the <em>same</em> object. 86 * 87 * @param object A {@link SdkSource} or {@link Package} or {@link Archive}. 88 * @return A new or existing {@link Image}. The caller must NOT dispose the image (the 89 * image will disposed by {@link #dispose()}). The returned image can be null if the 90 * object is of an unknown type. 91 */ getImageForObject(Object object)92 public Image getImageForObject(Object object) { 93 94 if (object == null) { 95 return null; 96 } 97 98 if (object instanceof Image) { 99 return (Image) object; 100 } 101 102 String clz = object.getClass().getSimpleName(); 103 if (clz.endsWith(Package.class.getSimpleName())) { 104 String name = clz.replaceFirst(Package.class.getSimpleName(), "") //$NON-NLS-1$ 105 .replace("SystemImage", "sysimg") //$NON-NLS-1$ //$NON-NLS-2$ 106 .toLowerCase(Locale.US); 107 name += "_pkg_16.png"; //$NON-NLS-1$ 108 return getImageByName(name); 109 } 110 111 if (object instanceof SdkSourceCategory) { 112 return getImageByName("source_cat_icon_16.png"); //$NON-NLS-1$ 113 114 } else if (object instanceof SdkSource) { 115 return getImageByName("source_icon_16.png"); //$NON-NLS-1$ 116 117 } else if (object instanceof PkgContentProvider.RepoSourceError) { 118 return getImageByName("error_icon_16.png"); //$NON-NLS-1$ 119 120 } else if (object instanceof PkgContentProvider.RepoSourceNotification) { 121 return getImageByName("nopkg_icon_16.png"); //$NON-NLS-1$ 122 } 123 124 if (object instanceof Archive) { 125 if (((Archive) object).isCompatible()) { 126 return getImageByName("archive_icon16.png"); //$NON-NLS-1$ 127 } else { 128 return getImageByName("incompat_icon16.png"); //$NON-NLS-1$ 129 } 130 } 131 132 if (object instanceof String) { 133 return getImageByName((String) object); 134 } 135 136 137 if (object != null) { 138 // For debugging 139 // System.out.println("No image for object " + object.getClass().getSimpleName()); 140 } 141 142 return null; 143 } 144 145 /** 146 * Dispose all the images created by this factory so far. 147 */ dispose()148 public void dispose() { 149 Iterator<Image> it = mImages.values().iterator(); 150 while(it.hasNext()) { 151 Image img = it.next(); 152 if (img != null && img.isDisposed() == false) { 153 img.dispose(); 154 } 155 it.remove(); 156 } 157 } 158 159 } 160