7e77c002a9c603b7bd4ace1c29e19dd1c7c2dba8
[sdk/emulator/qemu.git] / tizen / src / skin / client / src / org / tizen / emulator / skin / image / ImageRegistry.java
1 /**
2  * 
3  *
4  * Copyright (C) 2011 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:
7  * GiWoong Kim <giwoong.kim@samsung.com>
8  * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
9  * HyunJun Son
10  *
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.
15  *
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.
20  *
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.
24  *
25  * Contributors:
26  * - S-Core Co., Ltd
27  *
28  */
29
30 package org.tizen.emulator.skin.image;
31
32 import java.io.File;
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;
38 import java.util.Map;
39 import java.util.logging.Logger;
40
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;
53
54
55 /**
56  * 
57  *
58  */
59 public class ImageRegistry {
60         private static Logger logger =
61                         SkinLogger.getSkinLogger(ImageRegistry.class).getLogger();
62
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";
66         
67         public enum ImageType {
68                 IMG_TYPE_MAIN,
69                 IMG_TYPE_PRESSED
70         }
71         
72         public enum IconName {
73                 
74                 DETAIL_INFO("detail_info.png"),
75                 ROTATE("rotate.png"),
76                 SCALE("scale.png"),
77                 SHELL("shell.png"),
78                 ADVANCED("advanced.png"),
79                 CLOSE("close.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"),
85                 ABOUT("about.png"),
86
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"),
92
93                 EMULATOR_TITLE("emulator_icon.png"),
94                 EMULATOR_TITLE_ICO("emulator_icon.ico");
95                 
96                 private String name;
97                 
98                 private IconName(String name) {
99                         this.name = name;
100                 }
101                 
102                 public String getName() {
103                         return this.name;
104                 }
105                 
106         }
107         
108         private Display display;
109         private EmulatorUI dbiContents;
110
111         private Map<String, Image> skinImageMap;
112         private Map<String, Image> iconMap;
113
114         private String argSkinPath;
115
116         private static ImageRegistry instance;
117         private static boolean isInitialized;
118
119         private ImageRegistry() {
120                 /* do nothing */
121         }
122
123         public static ImageRegistry getInstance() {
124                 if (null == instance) {
125                         instance = new ImageRegistry();
126                 }
127
128                 return instance;
129         }
130
131         public void initialize(EmulatorConfig config) {
132                 if (isInitialized) {
133                         return;
134                 }
135                 isInitialized = true;
136
137                 this.display = Display.getDefault();
138
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>();
143
144                 init(this.argSkinPath);
145
146         }
147
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;
153
154                 if (argSkinPath == null) {
155                         logger.info("Emulator uses default skin path (" + defaultSkinPath +
156                                         ") instead of invalid skin path (null).");
157
158                         return defaultSkinPath;
159                 }
160
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 + ").");
165
166                         return defaultSkinPath;
167                 }
168
169                 return argSkinPath;
170         }
171
172         private void init(String argSkinPath) {
173
174                 RotationsType rotations = dbiContents.getRotations();
175
176                 if (null == rotations) {
177                         logger.severe("Fail to loading rotations element from dbi.");
178                         return;
179                 }
180
181                 List<RotationType> rotationList = rotations.getRotation();
182
183                 for (RotationType rotation : rotationList) {
184                         SkinRotation.put(rotation);
185                 }
186
187         }
188
189         public ImageData getSkinImageData(Short id, ImageType imageType) {
190
191                 Image image = skinImageMap.get(makeKey(id, imageType));
192
193                 if (null != image) {
194                         return image.getImageData();
195                 } else {
196                         RotationsType rotations = dbiContents.getRotations();
197
198                         if (null == rotations) {
199                                 logger.severe("Fail to loading rotations element from dbi.");
200                                 return null;
201                         }
202
203                         String skinPath = getSkinPath(argSkinPath);
204                         logger.info("get image data of skin from " + skinPath);
205
206                         RotationType targetRotation = SkinRotation.getRotation(id);
207                         List<RotationType> rotationList = rotations.getRotation();
208
209                         for (RotationType rotation : rotationList) {
210                                 ImageListType imageList = rotation.getImageList();
211                                 if (imageList == null) {
212                                         continue;
213                                 }
214
215                                 String mainImage = imageList.getMainImage();
216                                 String keyPressedImage = imageList.getKeyPressedImage();
217
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));
222
223                                         String pressedKey = makeKey(id, ImageType.IMG_TYPE_PRESSED);
224                                         skinImageMap.put(pressedKey,
225                                                         new Image(display, skinPath + File.separator + keyPressedImage));
226
227                                         break;
228                                 }
229                         }
230
231                         Image registeredImage = skinImageMap.get(makeKey(id, imageType));
232
233                         if (null != registeredImage) {
234                                 return registeredImage.getImageData();
235                         } else {
236                                 return null;
237                         }
238
239                 }
240         }
241
242         private String makeKey(Short id, ImageType imageType) {
243                 return id + ":" + imageType.ordinal();
244         }
245
246         public Image getIcon( IconName name ) {
247
248                 if ( 0 != iconMap.size() ) {
249
250                         Image image = iconMap.get( name.getName() );
251                         return image;
252
253                 } else {
254
255                         // load all of the icons at once.
256
257                         ClassLoader classLoader = this.getClass().getClassLoader();
258                         IconName[] values = IconName.values();
259
260                         for ( IconName iconName : values ) {
261
262                                 String icoNname = iconName.getName();
263
264                                 String iconPath = ICON_FOLDER + "/" + icoNname;
265
266                                 InputStream is = null;
267                                 try {
268                                         is = classLoader.getResourceAsStream( iconPath );
269                                         if ( null != is ) {
270                                                 logger.fine( "load icon:" + iconPath );
271                                                 iconMap.put( icoNname, new Image( display, is ) );
272                                         } else {
273                                                 logger.severe( "missing icon:" + iconPath );
274                                         }
275                                 } finally {
276                                         IOUtil.close( is );
277                                 }
278
279                         }
280
281                         return iconMap.get( name.getName() );
282
283                 }
284
285         }
286
287         public void dispose() {
288
289                 if ( null != skinImageMap ) {
290
291                         Collection<Image> images = skinImageMap.values();
292
293                         Iterator<Image> imageIterator = images.iterator();
294
295                         while ( imageIterator.hasNext() ) {
296                                 Image image = imageIterator.next();
297                                 image.dispose();
298                         }
299
300                 }
301
302                 if ( null != iconMap ) {
303
304                         Collection<Image> icons = iconMap.values();
305
306                         Iterator<Image> iconIterator = icons.iterator();
307
308                         while ( iconIterator.hasNext() ) {
309                                 Image image = iconIterator.next();
310                                 image.dispose();
311                         }
312
313                 }
314
315         }
316         
317 }