171d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu/*
271d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * Copyright (C) 2015 The Android Open Source Project
371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu *
471d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * in compliance with the License. You may obtain a copy of the License at
671d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu *
771d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * http://www.apache.org/licenses/LICENSE-2.0
871d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu *
971d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * Unless required by applicable law or agreed to in writing, software distributed under the License
1071d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
1171d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * or implied. See the License for the specific language governing permissions and limitations under
1271d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * the License.
1371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu */
1471d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu
1571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gupackage android.support.v17.leanback.widget.picker;
1671d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu
1771d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu/**
1871d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * Picker column class used by {@link Picker}, defines a contiguous value ranges and associated
1971d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * labels.  A PickerColumn has a minValue and maxValue to choose between.  The Picker column has
2071d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu * a current value.
21942f79291db75ccf6ecd0351d23a444a43dd0501Dake Gu * The labels can be dynamically generated from value by {@link #setLabelFormat(String)} or
22942f79291db75ccf6ecd0351d23a444a43dd0501Dake Gu * a list of static labels set by {@link #setStaticLabels(CharSequence[])}.
2371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu */
24b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gupublic class PickerColumn {
2571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu
2671d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    private int mCurrentValue;
2771d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    private int mMinValue;
2871d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    private int mMaxValue;
29942f79291db75ccf6ecd0351d23a444a43dd0501Dake Gu    private CharSequence[] mStaticLabels;
30942f79291db75ccf6ecd0351d23a444a43dd0501Dake Gu    private String mLabelFormat;
3171d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu
3271d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    public PickerColumn() {
3371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    }
3471d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu
3571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    /**
36942f79291db75ccf6ecd0351d23a444a43dd0501Dake Gu     * Set string format (see {@link String#format}) to display label for an
37942f79291db75ccf6ecd0351d23a444a43dd0501Dake Gu     * integer value.  {@link #setStaticLabels(CharSequence[])} overrides the format.
38b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu     *
39942f79291db75ccf6ecd0351d23a444a43dd0501Dake Gu     * @param labelFormat String format to display label for value between minValue and maxValue.
4071d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     */
41942f79291db75ccf6ecd0351d23a444a43dd0501Dake Gu    public void setLabelFormat(String labelFormat) {
42942f79291db75ccf6ecd0351d23a444a43dd0501Dake Gu        mLabelFormat = labelFormat;
4371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    }
4471d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu
4571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    /**
46942f79291db75ccf6ecd0351d23a444a43dd0501Dake Gu     * Return string format (see {@link String#format}) to display label for
47942f79291db75ccf6ecd0351d23a444a43dd0501Dake Gu     * value.
4871d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     * @return String format to display label for value.
4971d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     */
50942f79291db75ccf6ecd0351d23a444a43dd0501Dake Gu    public String getLabelFormat() {
51942f79291db75ccf6ecd0351d23a444a43dd0501Dake Gu        return mLabelFormat;
5271d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    }
5371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu
5471d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    /**
5571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     * Set static labels for each value, minValue maps to labels[0], maxValue maps to
5671d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     * labels[labels.length - 1].
5771d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     * @param labels Static labels for each value between minValue and maxValue.
5871d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     */
59942f79291db75ccf6ecd0351d23a444a43dd0501Dake Gu    public void setStaticLabels(CharSequence[] labels) {
60942f79291db75ccf6ecd0351d23a444a43dd0501Dake Gu        mStaticLabels = labels;
6171d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    }
6271d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu
6371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    /**
64942f79291db75ccf6ecd0351d23a444a43dd0501Dake Gu     * Returns static labels for each value, minValue maps to labels[0], maxValue maps to
65942f79291db75ccf6ecd0351d23a444a43dd0501Dake Gu     * labels[labels.length - 1].  When null, {@link #getLabelFormat()} will be used.
66942f79291db75ccf6ecd0351d23a444a43dd0501Dake Gu     */
67942f79291db75ccf6ecd0351d23a444a43dd0501Dake Gu    public CharSequence[] getStaticLabels() {
68942f79291db75ccf6ecd0351d23a444a43dd0501Dake Gu        return mStaticLabels;
69942f79291db75ccf6ecd0351d23a444a43dd0501Dake Gu    }
70942f79291db75ccf6ecd0351d23a444a43dd0501Dake Gu
71942f79291db75ccf6ecd0351d23a444a43dd0501Dake Gu    /**
72377357a8c26c8c54ba8cb876ae775265635a8448Elliot Waite     * Get a label for value. The label can be static {@link #setStaticLabels(CharSequence[])}
73377357a8c26c8c54ba8cb876ae775265635a8448Elliot Waite     * or dynamically generated {@link #setLabelFormat(String)} when static labels is null.
74b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu     *
7571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     * @param value Value between minValue and maxValue.
7671d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     * @return Label for the value.
7771d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     */
78942f79291db75ccf6ecd0351d23a444a43dd0501Dake Gu    public CharSequence getLabelFor(int value) {
79942f79291db75ccf6ecd0351d23a444a43dd0501Dake Gu        if (mStaticLabels == null) {
80942f79291db75ccf6ecd0351d23a444a43dd0501Dake Gu            return String.format(mLabelFormat, value);
8171d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu        }
82942f79291db75ccf6ecd0351d23a444a43dd0501Dake Gu        return mStaticLabels[value];
8371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    }
8471d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu
8571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    /**
8671d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     * Returns current value of the Column.
8771d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     * @return Current value of the Column.
8871d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     */
8971d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    public int getCurrentValue() {
9071d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu        return mCurrentValue;
9171d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    }
9271d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu
9371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    /**
9471d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     * Sets current value of the Column.
9571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     */
96b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu    public void setCurrentValue(int value) {
97b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu        mCurrentValue = value;
9871d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    }
9971d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu
10071d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    /**
101b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu     * Get total items count between minValue and maxValue.
102b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu     * @return Total items count between minValue and maxValue.
10371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     */
104942f79291db75ccf6ecd0351d23a444a43dd0501Dake Gu    public int getCount() {
10571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu        return mMaxValue - mMinValue + 1;
10671d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    }
10771d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu
10871d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    /**
109b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu     * Returns minimal value of the Column.
110b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu     * @return Minimal value of the Column.
11171d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     */
11271d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    public int getMinValue() {
11371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu        return mMinValue;
11471d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    }
11571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu
11671d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    /**
117b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu     * Returns maximum value of the Column.
118b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu     * @return Maximum value of the Column.
11971d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     */
12071d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    public int getMaxValue() {
12171d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu        return mMaxValue;
12271d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    }
12371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu
12471d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    /**
125b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu     * Sets minimal value of the Column.
12671d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     * @param minValue New minimal value to set.
12771d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     */
128b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu    public void setMinValue(int minValue) {
129b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu        mMinValue = minValue;
13071d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    }
13171d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu
13271d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    /**
133b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu     * Sets maximum value of the Column.
13471d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     * @param maxValue New maximum value to set.
13571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     */
136b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu    public void setMaxValue(int maxValue) {
137b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu        mMaxValue = maxValue;
13871d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    }
13971d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu
14071d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu}
141