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