Debian Codegraph @

extensions/baseAdapters/src/main/java/android/databinding/adapters/TableLayoutBindingAdapter.java

raw
1 /*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package android.databinding.adapters;
17
18 import android.databinding.BindingAdapter;
19 import android.util.SparseBooleanArray;
20 import android.widget.TableLayout;
21
22 import java.util.regex.Pattern;
23
24 public class TableLayoutBindingAdapter {
25
26 private static Pattern sColumnPattern = Pattern.compile("\\s*,\\s*");
27
28 private static final int MAX_COLUMNS = 20;
29
30 @BindingAdapter({"android:collapseColumns"})
31 public static void setCollapseColumns(TableLayout view, CharSequence columnsStr) {
32 SparseBooleanArray columns = parseColumns(columnsStr);
33 for (int i = 0; i < MAX_COLUMNS; i++) {
34 boolean isCollapsed = columns.get(i, false);
35 if (isCollapsed != view.isColumnCollapsed(i)) {
36 view.setColumnCollapsed(i, isCollapsed);
37 }
38 }
39 }
40
41 @BindingAdapter({"android:shrinkColumns"})
42 public static void setShrinkColumns(TableLayout view, CharSequence columnsStr) {
43 if (columnsStr != null && columnsStr.length() > 0 && columnsStr.charAt(0) == '*') {
44 view.setShrinkAllColumns(true);
45 } else {
46 view.setShrinkAllColumns(false);
47 SparseBooleanArray columns = parseColumns(columnsStr);
48 int columnCount = columns.size();
49 for (int i = 0; i < columnCount; i++) {
50 int column = columns.keyAt(i);
51 boolean shrinkable = columns.valueAt(i);
52 if (shrinkable) {
53 view.setColumnShrinkable(column, shrinkable);
54 }
55 }
56 }
57 }
58
59 @BindingAdapter({"android:stretchColumns"})
60 public static void setStretchColumns(TableLayout view, CharSequence columnsStr) {
61 if (columnsStr != null && columnsStr.length() > 0 && columnsStr.charAt(0) == '*') {
62 view.setStretchAllColumns(true);
63 } else {
64 view.setStretchAllColumns(false);
65 SparseBooleanArray columns = parseColumns(columnsStr);
66 int columnCount = columns.size();
67 for (int i = 0; i < columnCount; i++) {
68 int column = columns.keyAt(i);
69 boolean stretchable = columns.valueAt(i);
70 if (stretchable) {
71 view.setColumnStretchable(column, stretchable);
72 }
73 }
74 }
75 }
76
77 private static SparseBooleanArray parseColumns(CharSequence sequence) {
78 SparseBooleanArray columns = new SparseBooleanArray();
79 if (sequence == null) {
80 return columns;
81 }
82 String[] columnDefs = sColumnPattern.split(sequence);
83
84 for (String columnIdentifier : columnDefs) {
85 try {
86 int columnIndex = Integer.parseInt(columnIdentifier);
87 // only valid, i.e. positive, columns indexes are handled
88 if (columnIndex >= 0) {
89 // putting true in this sparse array indicates that the
90 // column index was defined in the XML file
91 columns.put(columnIndex, true);
92 }
93 } catch (NumberFormatException e) {
94 // we just ignore columns that don't exist
95 }
96 }
97
98 return columns;
99 }
100 }