ImageButton: use array instead of Collection
authorSeokYeon Hwang <syeon.hwang@samsung.com>
Fri, 5 Feb 2016 04:36:20 +0000 (13:36 +0900)
committerSeokYeon Hwang <syeon.hwang@samsung.com>
Thu, 11 Feb 2016 06:43:22 +0000 (15:43 +0900)
We can use array instead of Collection if size is fixed. Generally
array is faster than Collection.

Change-Id: I0419fff348ded41c0ac6c6b6d863571b2ac43eec
Signed-off-by: SeokYeon Hwang <syeon.hwang@samsung.com>
src/org/tizen/emulator/manager/ui/renewal/widgets/ImageButton.java

index 6aa2b13..b80a4a5 100644 (file)
@@ -29,9 +29,6 @@
 
 package org.tizen.emulator.manager.ui.renewal.widgets;
 
-import java.util.ArrayList;
-import java.util.List;
-
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.events.DisposeEvent;
 import org.eclipse.swt.events.DisposeListener;
@@ -63,11 +60,12 @@ public class ImageButton extends Canvas {
        private boolean showImage = true;
 
        protected WSTATE state = WSTATE.NORMAL;
+
        // index of list is WSTATE
-       protected List<Image> images = null;
-       protected List<Color> colors = null;
-       protected List<Color> fontColors = null;
-       protected List<Font> fonts = null;
+       protected final Image[] images;
+       protected final Color[] colors;
+       protected final Color[] fontColors;
+       protected final Font[] fonts;
 
        WStateImageMap imageMap = new WStateImageMap();
 
@@ -84,21 +82,23 @@ public class ImageButton extends Canvas {
                }
                this.style = style;
 
+               int numberOfState = WSTATE.values().length;
+               images = new Image[numberOfState];
+               colors = new Color[numberOfState];
+               fontColors = new Color[numberOfState];
+               fonts = new Font[numberOfState];
+
                initButton();
                addListeners();
        }
 
        protected void initButton() {
-               images = new ArrayList<Image>();
-               colors = new ArrayList<Color>();
-               fontColors = new ArrayList<Color>();
-               fonts = new ArrayList<Font>();
 
                for (WSTATE s : WSTATE.values()) {
-                       colors.add(s.getId(), null);
-                       fontColors.add(s.getId(), Colors.get(ColorKind.GRAY_BTN_FONT));
-                       fonts.add(s.getId(), Fonts.get(FontKind.GRAY_BUTTON_FONT));
-                       images.add(s.getId(), null);
+                       images[s.getId()] = null;
+                       colors[s.getId()] =  null;
+                       fontColors[s.getId()] = Colors.get(ColorKind.GRAY_BTN_FONT);
+                       fonts[s.getId()] = Fonts.get(FontKind.GRAY_BUTTON_FONT);
                }
 
 //             fontColors.set(WSTATE.DISABLE_ON.getId(),
@@ -147,26 +147,26 @@ public class ImageButton extends Canvas {
        @Override
        public void setFont(Font font) {
                for (WSTATE s : WSTATE.values()) {
-                       fonts.set(s.getId(), font);
+                       fonts[s.getId()] = font;
                }
        }
 
        public void setFont(WSTATE state, Font font) {
-               fonts.set(state.getId(), font);
+               fonts[state.getId()] = font;
        }
 
        public void setFontColor(Color color) {
                for (WSTATE s : WSTATE.values()) {
-                       fontColors.set(s.getId(), color);
+                       fontColors[s.getId()] = color;
                }
        }
 
        public void setFontColor(WSTATE state, Color color) {
-               fontColors.set(state.getId(), color);
+               fontColors[state.getId()] = color;
        }
 
        public void setImage(WSTATE state, Image image) {
-               images.set(state.getId(), image);
+               images[state.getId()] = image;
        }
 
        public void setImage(WSTATE state, ImageKind imageResource) {
@@ -176,12 +176,12 @@ public class ImageButton extends Canvas {
        @Override
        public void setBackground(Color color) {
                for (WSTATE s : WSTATE.values()) {
-                       colors.set(s.getId(), color);
+                       colors[s.getId()] = color;
                }
        }
 
        public void setBackgroundColor(WSTATE state, Color color) {
-               colors.set(state.getId(), color);
+               colors[state.getId()] = color;
        }
 
        public void setText(String text) {
@@ -194,19 +194,19 @@ public class ImageButton extends Canvas {
        }
 
        public Image getImage() {
-               return images.get(WSTATE.NORMAL.getId());
+               return images[WSTATE.NORMAL.getId()];
        }
 
        public Image getImage(WSTATE state) {
-               return images.get(state.getId());
+               return images[state.getId()];
        }
 
        public Color getBackgroundColor(WSTATE state) {
-               return colors.get(state.getId());
+               return colors[state.getId()];
        }
 
        public Color getFontColor(WSTATE state) {
-               return fontColors.get(state.getId());
+               return fontColors[state.getId()];
        }
 
        protected Image normalImage = null;
@@ -244,10 +244,10 @@ public class ImageButton extends Canvas {
        }
 
        public void setImages(Image normal, Image hover, Image pushed, Image disable) {
-               images.set(WSTATE.NORMAL.getId(), normal);
-               images.set(WSTATE.HOVER.getId(), hover);
-               images.set(WSTATE.PUSH.getId(), pushed);
-               images.set(WSTATE.DISABLE_ON.getId(), disable);
+               images[WSTATE.NORMAL.getId()] = normal;
+               images[WSTATE.HOVER.getId()] = hover;
+               images[WSTATE.PUSH.getId()] = pushed;
+               images[WSTATE.DISABLE_ON.getId()] = disable;
 
                if (checkStyle(style, SWT.PUSH)) {
                        defaultPush = false;
@@ -257,13 +257,13 @@ public class ImageButton extends Canvas {
 
        public void setImages(Image normal, Image hover, Image pushed,
                        Image selected, Image selected_hover, Image selected_push, Image disable) {
-               images.set(WSTATE.NORMAL.getId(), normal);
-               images.set(WSTATE.HOVER.getId(), hover);
-               images.set(WSTATE.PUSH.getId(), pushed);
-               images.set(WSTATE.SELECTED.getId(), selected);
-               images.set(WSTATE.SELECTED_HOVER.getId(), selected_hover);
-               images.set(WSTATE.SELECTED_PUSH.getId(), selected_push);
-               images.set(WSTATE.DISABLE_ON.getId(), disable);
+               images[WSTATE.NORMAL.getId()] = normal;
+               images[WSTATE.HOVER.getId()] = hover;
+               images[WSTATE.PUSH.getId()] = pushed;
+               images[WSTATE.SELECTED.getId()] = selected;
+               images[WSTATE.SELECTED_HOVER.getId()] = selected_hover;
+               images[WSTATE.SELECTED_PUSH.getId()] = selected_push;
+               images[WSTATE.DISABLE_ON.getId()] = disable;
 
                if (checkStyle(style, SWT.PUSH)) {
                        defaultPush = false;
@@ -273,14 +273,14 @@ public class ImageButton extends Canvas {
 
        public void setImages(Image normal, Image hover, Image pushed,
                        Image selected, Image selected_hover, Image selected_push, Image disable_on, Image disable_off) {
-               images.set(WSTATE.NORMAL.getId(), normal);
-               images.set(WSTATE.HOVER.getId(), hover);
-               images.set(WSTATE.PUSH.getId(), pushed);
-               images.set(WSTATE.SELECTED.getId(), selected);
-               images.set(WSTATE.SELECTED_HOVER.getId(), selected_hover);
-               images.set(WSTATE.SELECTED_PUSH.getId(), selected_push);
-               images.set(WSTATE.DISABLE_ON.getId(), disable_on);
-               images.set(WSTATE.DISABLE_OFF.getId(), disable_off);
+               images[WSTATE.NORMAL.getId()] = normal;
+               images[WSTATE.HOVER.getId()] = hover;
+               images[WSTATE.PUSH.getId()] = pushed;
+               images[WSTATE.SELECTED.getId()] = selected;
+               images[WSTATE.SELECTED_HOVER.getId()] = selected_hover;
+               images[WSTATE.SELECTED_PUSH.getId()] = selected_push;
+               images[WSTATE.DISABLE_ON.getId()] = disable_on;
+               images[WSTATE.DISABLE_OFF.getId()] = disable_off;
 
                if (checkStyle(style, SWT.PUSH)) {
                        defaultPush = false;
@@ -289,23 +289,23 @@ public class ImageButton extends Canvas {
        }
 
        public void setForeground(Color normal, Color hover, Color pushed, Color disable) {
-               fontColors.set(WSTATE.NORMAL.getId(), normal);
-               fontColors.set(WSTATE.HOVER.getId(), hover);
-               fontColors.set(WSTATE.PUSH.getId(), pushed);
-               fontColors.set(WSTATE.DISABLE_ON.getId(), disable);
+               fontColors[WSTATE.NORMAL.getId()] = normal;
+               fontColors[WSTATE.HOVER.getId()] = hover;
+               fontColors[WSTATE.PUSH.getId()] = pushed;
+               fontColors[WSTATE.DISABLE_ON.getId()] = disable;
 
                redraw();
        }
 
        public void setForeground(Color normal, Color hover, Color pushed,
                        Color selected, Color selected_hover, Color selected_push, Color disable) {
-               fontColors.set(WSTATE.NORMAL.getId(), normal);
-               fontColors.set(WSTATE.HOVER.getId(), hover);
-               fontColors.set(WSTATE.PUSH.getId(), pushed);
-               fontColors.set(WSTATE.SELECTED.getId(), selected);
-               fontColors.set(WSTATE.SELECTED_HOVER.getId(), selected_hover);
-               fontColors.set(WSTATE.SELECTED_PUSH.getId(), selected_push);
-               fontColors.set(WSTATE.DISABLE_ON.getId(), disable);
+               fontColors[WSTATE.NORMAL.getId()] = normal;
+               fontColors[WSTATE.HOVER.getId()] = hover;
+               fontColors[WSTATE.PUSH.getId()] = pushed;
+               fontColors[WSTATE.SELECTED.getId()] = selected;
+               fontColors[WSTATE.SELECTED_HOVER.getId()] = selected_hover;
+               fontColors[WSTATE.SELECTED_PUSH.getId()] = selected_push;
+               fontColors[WSTATE.DISABLE_ON.getId()] = disable;
 
                redraw();
        }
@@ -314,8 +314,8 @@ public class ImageButton extends Canvas {
        public Point computeSize(int wHint, int hHint, boolean changed) {
                int width = 0, height = 0;
 
-               if (images.get(WSTATE.NORMAL.getId()) != null) {
-                       Rectangle bounds = images.get(WSTATE.NORMAL.getId()).getBounds();
+               if (images[WSTATE.NORMAL.getId()] != null) {
+                       Rectangle bounds = images[WSTATE.NORMAL.getId()].getBounds();
                        height  = bounds.height;
                        width   = bounds.width;
                        if (checkStyle(style, SWT.RADIO)) {
@@ -365,7 +365,7 @@ public class ImageButton extends Canvas {
                        int i = 0;
                        for (WSTATE s : WSTATE.values()) {
                                i = s.getId();
-                               WidgetHelper.tryDispose(images.get(i), colors.get(i), fontColors.get(i), fonts.get(i));
+                               WidgetHelper.tryDispose(images[i], colors[i], fontColors[i], fonts[i]);
                        }
                        WidgetHelper.tryDispose(normalImage, hoverImage, pushImage, disableImage);
                }
@@ -380,13 +380,13 @@ public class ImageButton extends Canvas {
                        }
 
                        Rectangle rect = ((Canvas) e.widget).getClientArea();
-                       Image img = images.get(state.getId());
+                       Image img = images[state.getId()];
                        if (img == null) {
                                img = imageMap.get(state);
                        }
 
-                       if (colors.get(state.getId()) != null) {
-                               e.gc.setBackground(colors.get(state.getId()));
+                       if (colors[state.getId()] != null) {
+                               e.gc.setBackground(colors[state.getId()]);
                                e.gc.fillRectangle(rect);
                        }
 
@@ -419,12 +419,12 @@ public class ImageButton extends Canvas {
                        int fontHeight = e.gc.getFontMetrics().getHeight();
                        y = rect.y + (rect.height - fontHeight) / 2;
 
-                       if (fontColors.get(state.getId()) != null) {
-                               e.gc.setForeground(fontColors.get(state.getId()));
+                       if (fontColors[state.getId()] != null) {
+                               e.gc.setForeground(fontColors[state.getId()]);
                        }
 
-                       if (fonts.get(state.getId()) != null) {
-                               e.gc.setFont(fonts.get(state.getId()));
+                       if (fonts[state.getId()] != null) {
+                               e.gc.setFont(fonts[state.getId()]);
                        }
 
                        if (text != null) {