• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.AddonPackage;
20 import com.android.sdklib.internal.repository.Archive;
21 import com.android.sdklib.internal.repository.DocPackage;
22 import com.android.sdklib.internal.repository.ExtraPackage;
23 import com.android.sdklib.internal.repository.Package;
24 import com.android.sdklib.internal.repository.PlatformPackage;
25 import com.android.sdklib.internal.repository.RepoSource;
26 import com.android.sdklib.internal.repository.ToolPackage;
27 import com.android.sdkuilib.internal.repository.RepoSourcesAdapter;
28 
29 import org.eclipse.swt.SWTException;
30 import org.eclipse.swt.graphics.Image;
31 import org.eclipse.swt.widgets.Display;
32 
33 import java.io.InputStream;
34 import java.util.HashMap;
35 import java.util.Iterator;
36 
37 
38 /**
39  * An utility class to serve {@link Image} correspond to the various icons
40  * present in this package and dispose of them correctly at the end.
41  */
42 public class ImageFactory {
43 
44     private final Display mDisplay;
45     private final HashMap<String, Image> mImages = new HashMap<String, Image>();
46 
ImageFactory(Display display)47     public ImageFactory(Display display) {
48         mDisplay = display;
49     }
50 
51     /**
52      * Loads an image given its filename (with its extension).
53      * Might return null if the image cannot be loaded.
54      *
55      * @param imageName The filename (with extension) of the image to load.
56      * @return A new or existing {@link Image}. The caller must NOT dispose the image (the
57      *  image will disposed by {@link #dispose()}). The returned image can be null if the
58      *  expected file is missing.
59      */
getImageByName(String imageName)60     public Image getImageByName(String imageName) {
61 
62         Image image = mImages.get(imageName);
63         if (image != null) {
64             return image;
65         }
66 
67         InputStream stream = getClass().getResourceAsStream(imageName);
68         if (stream != null) {
69             try {
70                 image = new Image(mDisplay, stream);
71             } catch (SWTException e) {
72                 // ignore
73             } catch (IllegalArgumentException e) {
74                 // ignore
75             }
76         }
77 
78         // Store the image in the hash, even if this failed. If it fails now, it will fail later.
79         mImages.put(imageName, image);
80 
81         return image;
82     }
83 
84     /**
85      * Loads and returns the appropriate image for a given package, archive or source object.
86      *
87      * @param object A {@link RepoSource} 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      *  expected file is missing.
91      */
getImageForObject(Object object)92     public Image getImageForObject(Object object) {
93         if (object instanceof RepoSource) {
94             return getImageByName("source_icon16.png");                         //$NON-NLS-1$
95 
96         } else if (object instanceof RepoSourcesAdapter.RepoSourceError) {
97             return getImageByName("error_icon16.png");                          //$NON-NLS-1$
98 
99         } else if (object instanceof RepoSourcesAdapter.RepoSourceEmpty) {
100             return getImageByName("nopkg_icon16.png");                          //$NON-NLS-1$
101 
102         } else if (object instanceof PlatformPackage) {
103             return getImageByName("android_icon_16.png");                       //$NON-NLS-1$
104 
105         } else if (object instanceof AddonPackage) {
106             return getImageByName("addon_icon16.png");                          //$NON-NLS-1$
107 
108         } else if (object instanceof ToolPackage) {
109             return getImageByName("tool_icon16.png");                           //$NON-NLS-1$
110 
111         } else if (object instanceof DocPackage) {
112             return getImageByName("doc_icon16.png");                            //$NON-NLS-1$
113 
114         } else if (object instanceof ExtraPackage) {
115             return getImageByName("extra_icon16.png");                          //$NON-NLS-1$
116 
117         } else if (object instanceof Archive) {
118             if (((Archive) object).isCompatible()) {
119                 return getImageByName("archive_icon16.png");                    //$NON-NLS-1$
120             } else {
121                 return getImageByName("incompat_icon16.png");                   //$NON-NLS-1$
122             }
123         }
124         return null;
125     }
126 
127     /**
128      * Dispose all the images created by this factory so far.
129      */
dispose()130     public void dispose() {
131         Iterator<Image> it = mImages.values().iterator();
132         while(it.hasNext()) {
133             Image img = it.next();
134             if (img != null && img.isDisposed() == false) {
135                 img.dispose();
136             }
137             it.remove();
138         }
139     }
140 
141 }
142