From: SeokYeon Hwang Date: Thu, 4 Feb 2016 09:14:33 +0000 (+0900) Subject: resources: commonize resource registry X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e1a2f41d30370c0a228a98f95235375ba9114a41;p=sdk%2Femulator%2Femulator-manager.git resources: commonize resource registry Change-Id: Ib04d6950106a1484a7e2dfdaf4e145a7bffa3cde Signed-off-by: SeokYeon Hwang --- diff --git a/src/org/tizen/emulator/manager/renewal/resources/ColorKind.java b/src/org/tizen/emulator/manager/renewal/resources/ColorKind.java index db1a320..d2d519d 100644 --- a/src/org/tizen/emulator/manager/renewal/resources/ColorKind.java +++ b/src/org/tizen/emulator/manager/renewal/resources/ColorKind.java @@ -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), diff --git a/src/org/tizen/emulator/manager/renewal/resources/Colors.java b/src/org/tizen/emulator/manager/renewal/resources/Colors.java index 709d3b2..265a62c 100644 --- a/src/org/tizen/emulator/manager/renewal/resources/Colors.java +++ b/src/org/tizen/emulator/manager/renewal/resources/Colors.java @@ -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 colors = new HashMap(); - +final class ColorRegistry extends ResourceRegistry { 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 diff --git a/src/org/tizen/emulator/manager/renewal/resources/FontKind.java b/src/org/tizen/emulator/manager/renewal/resources/FontKind.java index d676def..31e2796 100644 --- a/src/org/tizen/emulator/manager/renewal/resources/FontKind.java +++ b/src/org/tizen/emulator/manager/renewal/resources/FontKind.java @@ -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), diff --git a/src/org/tizen/emulator/manager/renewal/resources/Fonts.java b/src/org/tizen/emulator/manager/renewal/resources/Fonts.java index 3e5992a..2b4b120 100644 --- a/src/org/tizen/emulator/manager/renewal/resources/Fonts.java +++ b/src/org/tizen/emulator/manager/renewal/resources/Fonts.java @@ -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 fonts = new HashMap(); - +final class FontRegistry extends ResourceRegistry { 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 diff --git a/src/org/tizen/emulator/manager/renewal/resources/ImageKind.java b/src/org/tizen/emulator/manager/renewal/resources/ImageKind.java index f941b1a..96cb8a7 100644 --- a/src/org/tizen/emulator/manager/renewal/resources/ImageKind.java +++ b/src/org/tizen/emulator/manager/renewal/resources/ImageKind.java @@ -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"), diff --git a/src/org/tizen/emulator/manager/renewal/resources/Images.java b/src/org/tizen/emulator/manager/renewal/resources/Images.java index 175fc73..fdbd338 100644 --- a/src/org/tizen/emulator/manager/renewal/resources/Images.java +++ b/src/org/tizen/emulator/manager/renewal/resources/Images.java @@ -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 images = new HashMap(); +final class ImageRegistry extends ResourceRegistry { 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 index 0000000..54f1ca6 --- /dev/null +++ b/src/org/tizen/emulator/manager/renewal/resources/ResourceKind.java @@ -0,0 +1,34 @@ +/* + * Emulator Manager + * + * Copyright (C) 2016 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: + * JiHye Kim + * Minkee Lee + * SeokYeon Hwang + * + * 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 index 0000000..0fc7899 --- /dev/null +++ b/src/org/tizen/emulator/manager/renewal/resources/ResourceRegistry.java @@ -0,0 +1,62 @@ +/* + * Emulator Manager + * + * Copyright (C) 2016 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: + * JiHye Kim + * Minkee Lee + * SeokYeon Hwang + * + * 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 { + protected final Map resources = new HashMap(); + + + 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(); + } + } +}