resources: commonize resource registry
authorSeokYeon Hwang <syeon.hwang@samsung.com>
Thu, 4 Feb 2016 09:14:33 +0000 (18:14 +0900)
committerSeokYeon Hwang <syeon.hwang@samsung.com>
Thu, 11 Feb 2016 04:52:34 +0000 (13:52 +0900)
Change-Id: Ib04d6950106a1484a7e2dfdaf4e145a7bffa3cde
Signed-off-by: SeokYeon Hwang <syeon.hwang@samsung.com>
src/org/tizen/emulator/manager/renewal/resources/ColorKind.java
src/org/tizen/emulator/manager/renewal/resources/Colors.java
src/org/tizen/emulator/manager/renewal/resources/FontKind.java
src/org/tizen/emulator/manager/renewal/resources/Fonts.java
src/org/tizen/emulator/manager/renewal/resources/ImageKind.java
src/org/tizen/emulator/manager/renewal/resources/Images.java
src/org/tizen/emulator/manager/renewal/resources/ResourceKind.java [new file with mode: 0644]
src/org/tizen/emulator/manager/renewal/resources/ResourceRegistry.java [new file with mode: 0644]

index db1a320..d2d519d 100644 (file)
@@ -31,7 +31,7 @@ package org.tizen.emulator.manager.renewal.resources;
 
 import org.eclipse.swt.graphics.Color;
 
-public enum ColorKind {
+public enum ColorKind implements ResourceKind {
        WHITE(255, 255, 255),
        BLACK(0, 0, 0),
        RED(255, 0, 0),
index 709d3b2..265a62c 100644 (file)
@@ -29,9 +29,6 @@
 
 package org.tizen.emulator.manager.renewal.resources;
 
-import java.util.HashMap;
-import java.util.Map;
-
 import org.eclipse.swt.graphics.Color;
 import org.eclipse.swt.graphics.RGB;
 import org.eclipse.swt.widgets.Display;
@@ -41,7 +38,7 @@ public final class Colors {
        private static ColorRegistry rootRegistry = new ColorRegistry();
 
        public static Color get(ColorKind kind) {
-               return rootRegistry.getColor(kind);
+               return rootRegistry.get(kind);
        }
 
        public static Color get(Platform platform, ColorKind kind) {
@@ -56,26 +53,11 @@ public final class Colors {
        }
 }
 
-final class ColorRegistry {
-       private final Map<ColorKind, Color> colors = new HashMap<ColorKind, Color>();
-
+final class ColorRegistry extends ResourceRegistry<ColorKind, Color> {
        ColorRegistry() {}
 
-       Color getColor(ColorKind kind) {
-               Color c = colors.get(kind);
-
-               if (c == null) {
-                       c = new Color(Display.getCurrent(), new RGB(kind.r, kind.g, kind.b));
-
-                       colors.put(kind, c);
-               }
-
-               return c;
-       }
-
-       void dispose() {
-               for (Color c : colors.values()) {
-                       c.dispose();
-               }
+       @Override
+       Color getInternal(ColorKind kind) {
+               return new Color(Display.getCurrent(), new RGB(kind.r, kind.g, kind.b));
        }
 }
\ No newline at end of file
index d676def..31e2796 100644 (file)
@@ -33,7 +33,7 @@ import org.eclipse.swt.SWT;
 import org.eclipse.swt.widgets.Display;
 import org.tizen.emulator.manager.EmulatorManager;
 
-public enum FontKind {
+public enum FontKind implements ResourceKind {
        DEFAULT_FONT_8("default_font_8", 8, SWT.NORMAL),
        DEFAULT_FONT_9("default_font_9", 9, SWT.NORMAL),
        DEFAULT_FONT_10("default_font_10", 10, SWT.NORMAL),
index 3e5992a..2b4b120 100644 (file)
@@ -29,9 +29,6 @@
 
 package org.tizen.emulator.manager.renewal.resources;
 
-import java.util.HashMap;
-import java.util.Map;
-
 import org.eclipse.swt.graphics.Font;
 import org.eclipse.swt.graphics.FontData;
 import org.eclipse.swt.widgets.Display;
@@ -42,7 +39,7 @@ public final class Fonts {
 
 
        public static Font get(FontKind kind) {
-               return rootRegistry.getFont(kind);
+               return rootRegistry.get(kind);
        }
 
        public static Font get(Platform platform, FontKind kind) {
@@ -57,26 +54,11 @@ public final class Fonts {
        }
 }
 
-final class FontRegistry {
-       private final Map<FontKind, Font> fonts = new HashMap<FontKind, Font>();
-
+final class FontRegistry extends ResourceRegistry<FontKind, Font> {
        FontRegistry() {}
 
-       Font getFont(FontKind kind) {
-               Font f = fonts.get(kind);
-
-               if (f == null) {
-                       f = new Font(Display.getCurrent(), new FontData(kind.fontName, kind.size, kind.style));
-
-                       fonts.put(kind, f);
-               }
-
-               return f;
-       }
-
-       void dispose() {
-               for (Font f : fonts.values()) {
-                       f.dispose();
-               }
+       @Override
+       Font getInternal(FontKind kind) {
+               return new Font(Display.getCurrent(), new FontData(kind.fontName, kind.size, kind.style));
        }
 }
\ No newline at end of file
index f941b1a..96cb8a7 100644 (file)
@@ -29,7 +29,7 @@
 
 package org.tizen.emulator.manager.renewal.resources;
 
-public enum ImageKind {
+public enum ImageKind implements ResourceKind {
        // 9p button disable
        BTN_DISABLE_B("9p_btn_disable_b"),
        BTN_DISABLE_L("9p_btn_disable_l"),
index 175fc73..fdbd338 100644 (file)
@@ -31,8 +31,6 @@ package org.tizen.emulator.manager.renewal.resources;
 
 import java.io.IOException;
 import java.io.InputStream;
-import java.util.HashMap;
-import java.util.Map;
 
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.graphics.Image;
@@ -45,7 +43,7 @@ public final class Images {
 
 
        public static Image get(ImageKind kind) {
-               return rootRegistry.getImage(kind);
+               return rootRegistry.get(kind);
        }
 
        public static Image get(Platform platform, ImageKind kind) {
@@ -57,7 +55,7 @@ public final class Images {
                        platform.setImageRegistry(new ImageRegistry(platform));
                }
 
-               return ((ImageRegistry)platform.getImageRegistry()).getImage(kind);
+               return ((ImageRegistry)platform.getImageRegistry()).get(kind);
        }
 
        public static void dispose() {
@@ -69,45 +67,36 @@ public final class Images {
        }
 }
 
-final class ImageRegistry {
-       private final Map<ImageKind, Image> images = new HashMap<ImageKind, Image>();
+final class ImageRegistry extends ResourceRegistry<ImageKind, Image> {
        private final Platform platform;
 
+
        ImageRegistry(Platform platform) {
                // If platform is null, we will use root class loader.
                this.platform = platform;
        }
 
-       Image getImage(ImageKind kind) {
-               Image i = images.get(kind);
-
-               if (i == null) {
-                       InputStream is = ResourceLoader.load(kind.name + "." + kind.type, platform); //$NON-NLS-1$ //$NON-NLS-2$
-                       if (is != null) {
-                               i = new Image(Display.getCurrent(), is);
-                               try {
-                                       is.close();
-                               } catch (IOException e) {
-                                       EMLogger.getLogger().info(e.getMessage());
-                               }
-                       }
+       @Override
+       Image getInternal(ImageKind kind) {
+               Image i = null;
 
-                       if (i == null) {
-                               i = Display.getCurrent().getSystemImage(SWT.ICON_WARNING);
+               InputStream is = ResourceLoader.load(kind.name + "." + kind.type, platform); //$NON-NLS-1$ //$NON-NLS-2$
+               if (is != null) {
+                       i = new Image(Display.getCurrent(), is);
+                       try {
+                               is.close();
+                       } catch (IOException e) {
+                               EMLogger.getLogger().info(e.getMessage());
                        }
+               }
 
-                       images.put(kind, i);
+               if (i == null) {
+                       i = Display.getCurrent().getSystemImage(SWT.ICON_WARNING);
                }
 
                return i;
        }
 
-       void dispose() {
-               for (Image i : images.values()) {
-                       i.dispose();
-               }
-       }
-
        @Deprecated
        static Image getImage(ImageResource res, ImageType type) {
                Image i = null;
diff --git a/src/org/tizen/emulator/manager/renewal/resources/ResourceKind.java b/src/org/tizen/emulator/manager/renewal/resources/ResourceKind.java
new file mode 100644 (file)
index 0000000..54f1ca6
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * Emulator Manager
+ *
+ * Copyright (C) 2016 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * JiHye Kim <jihye424.kim@samsung.com>
+ * Minkee Lee <minkee.lee@samsung.com>
+ * SeokYeon Hwang <syeon.hwang@samsung.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+
+package org.tizen.emulator.manager.renewal.resources;
+
+interface ResourceKind {
+       // empty interface
+}
diff --git a/src/org/tizen/emulator/manager/renewal/resources/ResourceRegistry.java b/src/org/tizen/emulator/manager/renewal/resources/ResourceRegistry.java
new file mode 100644 (file)
index 0000000..0fc7899
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * Emulator Manager
+ *
+ * Copyright (C) 2016 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * JiHye Kim <jihye424.kim@samsung.com>
+ * Minkee Lee <minkee.lee@samsung.com>
+ * SeokYeon Hwang <syeon.hwang@samsung.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+
+package org.tizen.emulator.manager.renewal.resources;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.eclipse.swt.graphics.Resource;
+
+public abstract class ResourceRegistry<K extends ResourceKind, T extends Resource> {
+       protected final Map<K, T> resources = new HashMap<K, T>();
+
+
+       ResourceRegistry() {}
+
+       T get(K kind) {
+               T r = resources.get(kind);
+
+               if (r == null) {
+                       r = getInternal(kind);
+
+                       resources.put(kind, r);
+               }
+
+               return r;
+       }
+
+       abstract T getInternal(K kind);
+
+       void dispose() {
+               for (T r : resources.values()) {
+                       r.dispose();
+               }
+       }
+}