PickerColumn.java revision b88b36aa081a500eb0e9d4be0bac85b33cd57dde
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.
21b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu * The labels can be dynamically generated from value by {@link #setEntryFormat(String)} or
22b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu * a list of static labels set by {@link #setEntries(CharSequence[])}.
2371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu */
24b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gupublic class PickerColumn {
2571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu
2671d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    private int mCurrentValue;
2771d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    private int mMinValue;
2871d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    private int mMaxValue;
29b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu    private CharSequence[] mStaticEntrys;
30b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu    private String mEntryFormat;
3171d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu
3271d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    public PickerColumn() {
3371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    }
3471d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu
3571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    /**
36b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu     * Set string format to display label for value. For example "%02d".
37b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu     * {@link #setEntries(CharSequence[])} overrides the format.
38b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu     *
39b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu     * @param valueFormat String format to display label for value between minValue and maxValue.
4071d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     */
41b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu    public void setEntryFormat(String valueFormat) {
42b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu        mEntryFormat = valueFormat;
4371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    }
4471d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu
4571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    /**
46b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu     * Return string format to display label for value.  For example "%02d".
4771d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     * @return String format to display label for value.
4871d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     */
49b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu    public String getEntryFormat() {
50b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu        return mEntryFormat;
5171d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    }
5271d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu
5371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    /**
5471d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     * Set static labels for each value, minValue maps to labels[0], maxValue maps to
5571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     * labels[labels.length - 1].
5671d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     * @param labels Static labels for each value between minValue and maxValue.
5771d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     */
58b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu    public void setEntries(CharSequence[] labels) {
59b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu        mStaticEntrys = labels;
6071d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    }
6171d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu
6271d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    /**
63b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu     * Get a label for value. The label can be static ({@link #setEntries(CharSequence[])}
64b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu     * or dynamically generated (@link {@link #setEntryFormat(String)}.
65b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu     *
6671d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     * @param value Value between minValue and maxValue.
6771d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     * @return Label for the value.
6871d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     */
69b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu    public CharSequence getEntryAt(int value) {
70b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu        if (mStaticEntrys == null) {
71b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu            return String.format(mEntryFormat, value);
7271d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu        }
73b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu        return mStaticEntrys[value];
7471d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    }
7571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu
7671d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    /**
7771d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     * Returns current value of the Column.
7871d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     * @return Current value of the Column.
7971d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     */
8071d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    public int getCurrentValue() {
8171d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu        return mCurrentValue;
8271d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    }
8371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu
8471d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    /**
8571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     * Sets current value of the Column.
8671d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     */
87b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu    public void setCurrentValue(int value) {
88b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu        mCurrentValue = value;
8971d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    }
9071d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu
9171d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    /**
92b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu     * Get total items count between minValue and maxValue.
93b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu     * @return Total items count between minValue and maxValue.
9471d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     */
95b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu    public int getItemCount() {
9671d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu        return mMaxValue - mMinValue + 1;
9771d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    }
9871d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu
9971d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    /**
100b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu     * Returns minimal value of the Column.
101b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu     * @return Minimal value of the Column.
10271d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     */
10371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    public int getMinValue() {
10471d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu        return mMinValue;
10571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    }
10671d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu
10771d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    /**
108b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu     * Returns maximum value of the Column.
109b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu     * @return Maximum value of the Column.
11071d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     */
11171d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    public int getMaxValue() {
11271d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu        return mMaxValue;
11371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    }
11471d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu
11571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    /**
116b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu     * Sets minimal value of the Column.
11771d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     * @param minValue New minimal value to set.
11871d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     */
119b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu    public void setMinValue(int minValue) {
120b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu        mMinValue = minValue;
12171d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    }
12271d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu
12371d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    /**
124b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu     * Sets maximum value of the Column.
12571d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     * @param maxValue New maximum value to set.
12671d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu     */
127b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu    public void setMaxValue(int maxValue) {
128b88b36aa081a500eb0e9d4be0bac85b33cd57ddeDake Gu        mMaxValue = maxValue;
12971d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu    }
13071d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu
13171d30e1cf5514761ba8ad4bd3c8c70540d60dbd3Dake Gu}
132