BindingTarget.java revision 019c36b97c7c172ac03997f6bf170e65b2ed7fe4
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
17package android.databinding.tool;
18
19import android.databinding.tool.expr.Expr;
20import android.databinding.tool.expr.ExprModel;
21import android.databinding.tool.reflection.ModelAnalyzer;
22import android.databinding.tool.reflection.ModelClass;
23import android.databinding.tool.store.ResourceBundle;
24import android.databinding.tool.store.SetterStore;
25
26import java.util.ArrayList;
27import java.util.List;
28
29public class BindingTarget {
30    List<Binding> mBindings = new ArrayList<Binding>();
31    ExprModel mModel;
32    ModelClass mResolvedClass;
33
34    // if this target presents itself in multiple layout files with different view types,
35    // it receives an interface type and should use it in the getter instead.
36    private ResourceBundle.BindingTargetBundle mBundle;
37
38    public BindingTarget(ResourceBundle.BindingTargetBundle bundle) {
39        mBundle = bundle;
40    }
41
42    public boolean isUsed() {
43        return mBundle.isUsed();
44    }
45
46    public void addBinding(String name, Expr expr) {
47        mBindings.add(new Binding(this, name, expr));
48    }
49
50    public String getInterfaceType() {
51        return mBundle.getInterfaceType() == null ? mBundle.getFullClassName() : mBundle.getInterfaceType();
52    }
53
54    public String getId() {
55        return mBundle.getId();
56    }
57
58    public String getTag() {
59        return mBundle.getTag();
60    }
61
62    public String getOriginalTag() {
63        return mBundle.getOriginalTag();
64    }
65
66    public String getViewClass() {
67        return mBundle.getFullClassName();
68    }
69
70    public ModelClass getResolvedType() {
71        if (mResolvedClass == null) {
72            mResolvedClass = ModelAnalyzer.getInstance().findClass(mBundle.getFullClassName(),
73                    mModel.getImports());
74        }
75        return mResolvedClass;
76    }
77
78    public String getIncludedLayout() {
79        return mBundle.getIncludedLayout();
80    }
81
82    public boolean isBinder() {
83        return getIncludedLayout() != null;
84    }
85
86    public boolean supportsTag() {
87        return !SetterStore.get(ModelAnalyzer.getInstance())
88                .isUntaggable(mBundle.getFullClassName());
89    }
90
91    public List<Binding> getBindings() {
92        return mBindings;
93    }
94
95    public ExprModel getModel() {
96        return mModel;
97    }
98
99    public void setModel(ExprModel model) {
100        mModel = model;
101    }
102}
103