4 * Copyright (C) 2011 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
7 * GiWoong Kim <giwoong.kim@samsung.com>
8 * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 package org.tizen.emulator.skin.image;
33 import java.io.InputStream;
34 import java.util.Collection;
35 import java.util.HashMap;
36 import java.util.Iterator;
37 import java.util.List;
39 import java.util.logging.Logger;
41 import org.eclipse.swt.graphics.Image;
42 import org.eclipse.swt.graphics.ImageData;
43 import org.eclipse.swt.widgets.Display;
44 import org.tizen.emulator.skin.config.EmulatorConfig;
45 import org.tizen.emulator.skin.config.EmulatorConfig.ArgsConstants;
46 import org.tizen.emulator.skin.dbi.EmulatorUI;
47 import org.tizen.emulator.skin.dbi.ImageListType;
48 import org.tizen.emulator.skin.dbi.RotationType;
49 import org.tizen.emulator.skin.dbi.RotationsType;
50 import org.tizen.emulator.skin.log.SkinLogger;
51 import org.tizen.emulator.skin.util.IOUtil;
52 import org.tizen.emulator.skin.util.SkinRotation;
59 public class ImageRegistry {
60 private static Logger logger =
61 SkinLogger.getSkinLogger(ImageRegistry.class).getLogger();
63 public static final String SKINS_FOLDER = "skins";
64 public static final String GENERAL_FOLDER = "emul-general";
65 public static final String ICON_FOLDER = "icons";
67 public enum ImageType {
72 public enum IconName {
74 DETAIL_INFO("detail_info.png"),
78 ADVANCED("advanced.png"),
80 SCREENSHOT("screenshot.png"),
81 USB_KEYBOARD("usb_keyboard.png"),
82 HOST_KEYBOARD("host_keyboard.png"),
83 DIAGNOSIS("diagnosis.png"),
84 FORCE_CLOSE("force_close.png"),
87 COPY_SCREEN_SHOT("copy_screenshot_dialog.png"),
88 REFRESH_SCREEN_SHOT("refresh_screenshot_dialog.png"),
89 INCREASE_SCALE("increase_scale.png"),
90 DECREASE_SCALE("decrease_scale.png"),
91 SAVE_SCREEN_SHOT("save_screenshot_dialog.png"),
93 EMULATOR_TITLE("emulator_icon.png"),
94 EMULATOR_TITLE_ICO("emulator_icon.ico");
98 private IconName(String name) {
102 public String getName() {
108 private Display display;
109 private EmulatorUI dbiContents;
111 private Map<String, Image> skinImageMap;
112 private Map<String, Image> iconMap;
114 private String argSkinPath;
116 private static ImageRegistry instance;
117 private static boolean isInitialized;
119 private ImageRegistry() {
123 public static ImageRegistry getInstance() {
124 if (null == instance) {
125 instance = new ImageRegistry();
131 public void initialize(EmulatorConfig config) {
135 isInitialized = true;
137 this.display = Display.getDefault();
139 this.argSkinPath = config.getArg(ArgsConstants.SKIN_PATH);
140 this.dbiContents = config.getDbiContents();
141 this.skinImageMap = new HashMap<String, Image>();
142 this.iconMap = new HashMap<String, Image>();
144 init(this.argSkinPath);
148 public static String getSkinPath(String argSkinPath) {
149 /* When emulator receive a invalid skin path,
150 emulator uses default skin path instead of it */
151 String defaultSkinPath = ".." + //TODO:
152 File.separator + SKINS_FOLDER + File.separator + GENERAL_FOLDER;
154 if (argSkinPath == null) {
155 logger.info("Emulator uses default skin path (" + defaultSkinPath +
156 ") instead of invalid skin path (null).");
158 return defaultSkinPath;
161 File f = new File(argSkinPath);
162 if (f.isDirectory() == false) {
163 logger.info("Emulator uses default skin path (" + defaultSkinPath +
164 ") instead of invalid skin path (" + argSkinPath + ").");
166 return defaultSkinPath;
172 private void init(String argSkinPath) {
174 RotationsType rotations = dbiContents.getRotations();
176 if (null == rotations) {
177 logger.severe("Fail to loading rotations element from dbi.");
181 List<RotationType> rotationList = rotations.getRotation();
183 for (RotationType rotation : rotationList) {
184 SkinRotation.put(rotation);
189 public ImageData getSkinImageData(Short id, ImageType imageType) {
191 Image image = skinImageMap.get(makeKey(id, imageType));
194 return image.getImageData();
196 RotationsType rotations = dbiContents.getRotations();
198 if (null == rotations) {
199 logger.severe("Fail to loading rotations element from dbi.");
203 String skinPath = getSkinPath(argSkinPath);
204 logger.info("get image data of skin from " + skinPath);
206 RotationType targetRotation = SkinRotation.getRotation(id);
207 List<RotationType> rotationList = rotations.getRotation();
209 for (RotationType rotation : rotationList) {
210 ImageListType imageList = rotation.getImageList();
211 if (imageList == null) {
215 String mainImage = imageList.getMainImage();
216 String keyPressedImage = imageList.getKeyPressedImage();
218 if (targetRotation.getName().value().equals(rotation.getName().value())) {
219 String mainKey = makeKey(id, ImageType.IMG_TYPE_MAIN);
220 skinImageMap.put(mainKey,
221 new Image(display, skinPath + File.separator + mainImage));
223 String pressedKey = makeKey(id, ImageType.IMG_TYPE_PRESSED);
224 skinImageMap.put(pressedKey,
225 new Image(display, skinPath + File.separator + keyPressedImage));
231 Image registeredImage = skinImageMap.get(makeKey(id, imageType));
233 if (null != registeredImage) {
234 return registeredImage.getImageData();
242 private String makeKey(Short id, ImageType imageType) {
243 return id + ":" + imageType.ordinal();
246 public Image getIcon( IconName name ) {
248 if ( 0 != iconMap.size() ) {
250 Image image = iconMap.get( name.getName() );
255 // load all of the icons at once.
257 ClassLoader classLoader = this.getClass().getClassLoader();
258 IconName[] values = IconName.values();
260 for ( IconName iconName : values ) {
262 String icoNname = iconName.getName();
264 String iconPath = ICON_FOLDER + "/" + icoNname;
266 InputStream is = null;
268 is = classLoader.getResourceAsStream( iconPath );
270 logger.fine( "load icon:" + iconPath );
271 iconMap.put( icoNname, new Image( display, is ) );
273 logger.severe( "missing icon:" + iconPath );
281 return iconMap.get( name.getName() );
287 public void dispose() {
289 if ( null != skinImageMap ) {
291 Collection<Image> images = skinImageMap.values();
293 Iterator<Image> imageIterator = images.iterator();
295 while ( imageIterator.hasNext() ) {
296 Image image = imageIterator.next();
302 if ( null != iconMap ) {
304 Collection<Image> icons = iconMap.values();
306 Iterator<Image> iconIterator = icons.iterator();
308 while ( iconIterator.hasNext() ) {
309 Image image = iconIterator.next();