ShadowView.java revision a66c979cb64d406c1646a0909bd9a0b6b6ccd802
1package com.xtremelabs.droidsugar.view;
2
3import android.content.Context;
4import android.content.res.Resources;
5import android.view.View;
6import android.view.ViewGroup;
7import com.xtremelabs.droidsugar.ProxyDelegatingHandler;
8
9import java.util.ArrayList;
10import java.util.HashMap;
11import java.util.List;
12import java.util.Map;
13
14@SuppressWarnings({"UnusedDeclaration"})
15public class FakeView {
16    public static final int UNINITIALIZED_ATTRIBUTE = -1000;
17
18    private View realView;
19
20    private int id;
21    private List<View> children = new ArrayList<View>();
22    private FakeView parent;
23    private Context context;
24    public boolean selected;
25    private View.OnClickListener onClickListener;
26    private Object tag;
27    private boolean enabled = true;
28    public int visibility = UNINITIALIZED_ATTRIBUTE;
29    public int height;
30    public int width;
31    public int paddingLeft;
32    public int paddingTop;
33    public int paddingRight;
34    public int paddingBottom;
35    public ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(0, 0);
36    private Map<Integer, Object> tags = new HashMap<Integer, Object>();
37
38    public FakeView(View view) {
39        this.realView = view;
40    }
41
42    public void __constructor__(Context context) {
43        this.context = context;
44    }
45
46    public void setId(int id) {
47        this.id = id;
48    }
49
50    public int getId() {
51        return id;
52    }
53
54    public static View inflate(Context context, int resource, ViewGroup root) {
55        View view = FakeContextWrapper.viewLoader.inflateView(context, resource);
56        if (root != null) {
57            root.addView(view);
58        }
59        return view;
60    }
61
62    public View findViewById(int id) {
63        if (id == this.id) {
64            return realView;
65        }
66
67        for (View child : children) {
68            View found = child.findViewById(id);
69            if (found != null) {
70                return found;
71            }
72        }
73        return null;
74    }
75
76    public View getRootView() {
77        FakeView root = this;
78        while(root.parent != null) {
79            root = root.parent;
80        }
81        return root.realView;
82    }
83
84    public void addView(View child) {
85        children.add(child);
86        childProxy(child).parent = this;
87    }
88
89    private FakeView childProxy(View child) {
90        return (FakeView) ProxyDelegatingHandler.getInstance().proxyFor(child);
91    }
92
93    public int getChildCount() {
94        return children.size();
95    }
96
97    public ViewGroup.LayoutParams getLayoutParams() {
98        return layoutParams;
99    }
100
101    public View getChildAt(int index) {
102        return children.get(index);
103    }
104
105    public void removeAllViews() {
106        for (View child : children) {
107            childProxy(child).parent = null;
108        }
109        children.clear();
110    }
111
112    public final Context getContext() {
113        return context;
114    }
115
116    public Resources getResources() {
117        return context.getResources();
118    }
119
120    public int getVisibility() {
121        return visibility;
122    }
123
124    public void setVisibility(int visibility) {
125        this.visibility = visibility;
126    }
127
128    public void setSelected(boolean selected) {
129        this.selected = selected;
130    }
131
132    public boolean isSelected() {
133        return this.selected;
134    }
135
136    public boolean isEnabled() {
137        return this.enabled;
138    }
139
140    public void setEnabled(boolean enabled) {
141        this.enabled = enabled;
142    }
143
144    public void setOnClickListener(View.OnClickListener onClickListener) {
145        this.onClickListener = onClickListener;
146    }
147
148    public boolean performClick() {
149        if (onClickListener != null) {
150            onClickListener.onClick(realView);
151            return true;
152        } else {
153            return false;
154        }
155    }
156
157    public Object getTag() {
158        return this.tag;
159    }
160
161    public void setTag(Object tag) {
162        this.tag = tag;
163    }
164
165    public final int getHeight() {
166        return height;
167    }
168
169    public final int getWidth() {
170        return width;
171    }
172
173    public void setPadding(int left, int top, int right, int bottom) {
174        paddingLeft = left;
175        paddingTop = top;
176        paddingRight = right;
177        paddingBottom = bottom;
178    }
179
180    public int getPaddingTop() {
181        return paddingTop;
182    }
183
184    public int getPaddingLeft() {
185        return paddingLeft;
186    }
187
188    public int getPaddingRight() {
189        return paddingRight;
190    }
191
192    public int getPaddingBottom() {
193        return paddingBottom;
194    }
195
196    public Object getTag(int key) {
197        return tags.get(key);
198    }
199
200    public void setTag(int key, Object value) {
201        tags.put(key, value);
202    }
203}
204