Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / ui / android / java / src / org / chromium / ui / ColorPickerSimple.java
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.ui;
6
7 import android.content.Context;
8 import android.graphics.Color;
9 import android.util.AttributeSet;
10 import android.widget.ListView;
11
12 import org.chromium.ui.ColorSuggestionListAdapter.OnColorSuggestionClickListener;
13
14 /**
15  * Draws a grid of (predefined) colors and allows the user to choose one of
16  * those colors.
17  */
18 public class ColorPickerSimple extends ListView implements OnColorSuggestionClickListener {
19
20     private OnColorChangedListener mOnColorChangedListener;
21
22     private static final int[] DEFAULT_COLORS = {
23         Color.RED,
24         Color.CYAN,
25         Color.BLUE,
26         Color.GREEN,
27         Color.MAGENTA,
28         Color.YELLOW,
29         Color.BLACK,
30         Color.WHITE
31     };
32
33     private static final int[] DEFAULT_COLOR_LABEL_IDS = {
34         R.string.color_picker_button_red,
35         R.string.color_picker_button_cyan,
36         R.string.color_picker_button_blue,
37         R.string.color_picker_button_green,
38         R.string.color_picker_button_magenta,
39         R.string.color_picker_button_yellow,
40         R.string.color_picker_button_black,
41         R.string.color_picker_button_white
42     };
43
44     public ColorPickerSimple(Context context) {
45         super(context);
46     }
47
48     public ColorPickerSimple(Context context, AttributeSet attrs) {
49         super(context, attrs);
50     }
51
52     public ColorPickerSimple(Context context, AttributeSet attrs, int defStyle) {
53         super(context, attrs, defStyle);
54     }
55
56     /**
57      * Initializes the listener and sets the adapter for the given list of suggestions. If the
58      * suggestions is null a default set of colors will be used.
59      *
60      * @param suggestions The list of suggestions that should be displayed.
61      * @param onColorChangedListener The listener that gets notified when the user touches
62      *                               a color.
63      */
64     public void init(ColorSuggestion[] suggestions,
65                      OnColorChangedListener onColorChangedListener) {
66         mOnColorChangedListener = onColorChangedListener;
67
68         if (suggestions == null) {
69             suggestions = new ColorSuggestion[DEFAULT_COLORS.length];
70             for (int i = 0; i < suggestions.length; ++i) {
71                 suggestions[i] = new ColorSuggestion(DEFAULT_COLORS[i],
72                         getContext().getString(DEFAULT_COLOR_LABEL_IDS[i]));
73             }
74         }
75
76         ColorSuggestionListAdapter adapter = new ColorSuggestionListAdapter(
77                 getContext(), suggestions);
78         adapter.setOnColorSuggestionClickListener(this);
79         setAdapter(adapter);
80     }
81
82     @Override
83     public void onColorSuggestionClick(ColorSuggestion suggestion) {
84         mOnColorChangedListener.onColorChanged(suggestion.mColor);
85     }
86 }