1/*
2 * Copyright (C) 2011 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
17package com.android.ide.common.rendering.api;
18
19import com.android.resources.ResourceType;
20
21import java.util.ArrayList;
22import java.util.Collections;
23import java.util.Iterator;
24import java.util.List;
25
26/**
27 * A data binding item. It contain a {@link ResourceReference} to the view used to represent it.
28 * It also contains how many items of this type the AdapterView should display.
29 *
30 * It can also contain an optional list of children in case the AdapterView is an
31 * ExpandableListView. In this case, the count value is used as a repeat count for the children,
32 * similar to {@link AdapterBinding#getRepeatCount()}.
33 *
34 */
35public class DataBindingItem implements Iterable<DataBindingItem> {
36    private final ResourceReference mReference;
37    private final int mCount;
38    private List<DataBindingItem> mChildren;
39
40    public DataBindingItem(ResourceReference reference, int count) {
41        mReference = reference;
42        mCount = count;
43    }
44
45    public DataBindingItem(String name, boolean platformLayout, int count) {
46        this(new ResourceReference(name, platformLayout), count);
47    }
48
49    public DataBindingItem(String name, boolean platformLayout) {
50        this(name, platformLayout, 1);
51    }
52
53    public DataBindingItem(String name, int count) {
54        this(name, false /*platformLayout*/, count);
55    }
56
57    public DataBindingItem(String name) {
58        this(name, false /*platformLayout*/, 1);
59    }
60
61    /**
62     * Returns the {@link ResourceReference} for the view. The {@link ResourceType} for the
63     * referenced resource is implied to be {@link ResourceType#LAYOUT}.
64     */
65    public ResourceReference getViewReference() {
66        return mReference;
67    }
68
69    /**
70     * The repeat count for this object or the repeat count for the children if there are any.
71     */
72    public int getCount() {
73        return mCount;
74    }
75
76    public void addChild(DataBindingItem child) {
77        if (mChildren == null) {
78            mChildren = new ArrayList<DataBindingItem>();
79        }
80
81        mChildren.add(child);
82    }
83
84    public List<DataBindingItem> getChildren() {
85        if (mChildren != null) {
86            return mChildren;
87        }
88
89        return Collections.emptyList();
90    }
91
92    @Override
93    public Iterator<DataBindingItem> iterator() {
94        List<DataBindingItem> list = getChildren();
95        return list.iterator();
96    }
97}
98