ShadowView.java revision 1c46c94a5affe7beeb8c6bfcb5ec0ed509e5fdbd
1package com.xtremelabs.droidsugar.fakes;
2
3import android.content.Context;
4import android.content.res.Resources;
5import android.util.AttributeSet;
6import android.view.View;
7import android.view.ViewGroup;
8import android.view.ViewParent;
9import com.xtremelabs.droidsugar.ProxyDelegatingHandler;
10import com.xtremelabs.droidsugar.util.Implements;
11
12import java.util.ArrayList;
13import java.util.HashMap;
14import java.util.List;
15import java.util.Map;
16
17@SuppressWarnings({"UnusedDeclaration"})
18@Implements(View.class)
19public class FakeView {
20    public static final int UNINITIALIZED_ATTRIBUTE = -1000;
21
22    protected View realView;
23
24    private int id;
25    private List<View> children = new ArrayList<View>();
26    private FakeView parent;
27    private Context context;
28    public boolean selected;
29    private View.OnClickListener onClickListener;
30    private Object tag;
31    private boolean enabled = true;
32    public int visibility = UNINITIALIZED_ATTRIBUTE;
33    public int height;
34    public int width;
35    public int paddingLeft;
36    public int paddingTop;
37    public int paddingRight;
38    public int paddingBottom;
39    public ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(0, 0);
40    private Map<Integer, Object> tags = new HashMap<Integer, Object>();
41    public boolean clickable;
42    public boolean focusable;
43    public int backgroundResourceId = -1;
44    protected View.OnKeyListener onKeyListener;
45    public boolean hasFocus;
46    private View.OnFocusChangeListener onFocusChangeListener;
47    public boolean wasInvalidated;
48
49    public FakeView(View view) {
50        this.realView = view;
51    }
52
53    public void __constructor__(Context context) {
54        this.context = context;
55    }
56
57    public void __constructor__(Context context, AttributeSet attrs) {
58        __constructor__(context);
59    }
60
61    public void setId(int id) {
62        this.id = id;
63    }
64
65    public void setClickable(boolean clickable) {
66        this.clickable = clickable;
67    }
68
69    public void setFocusable(boolean focusable) {
70        this.focusable = focusable;
71    }
72
73    public int getId() {
74        return id;
75    }
76
77    public static View inflate(Context context, int resource, ViewGroup root) {
78        View view = FakeContextWrapper.resourceLoader.viewLoader.inflateView(context, resource);
79        if (root != null) {
80            root.addView(view);
81        }
82        return view;
83    }
84
85    public View findViewById(int id) {
86        if (id == this.id) {
87            return realView;
88        }
89
90        for (View child : children) {
91            View found = child.findViewById(id);
92            if (found != null) {
93                return found;
94            }
95        }
96        return null;
97    }
98
99    public View getRootView() {
100        FakeView root = this;
101        while(root.parent != null) {
102            root = root.parent;
103        }
104        return root.realView;
105    }
106
107    public void addView(View child) {
108        children.add(child);
109        childProxy(child).parent = this;
110    }
111
112    private FakeView childProxy(View child) {
113        return (FakeView) ProxyDelegatingHandler.getInstance().proxyFor(child);
114    }
115
116    public int getChildCount() {
117        return children.size();
118    }
119
120    public ViewGroup.LayoutParams getLayoutParams() {
121        return layoutParams;
122    }
123
124    public void setLayoutParams(ViewGroup.LayoutParams params) {
125        layoutParams = params;
126    }
127
128    public View getChildAt(int index) {
129        return children.get(index);
130    }
131
132    public final ViewParent getParent() {
133        return (ViewParent) parent.realView;
134    }
135
136    public void removeAllViews() {
137        for (View child : children) {
138            childProxy(child).parent = null;
139        }
140        children.clear();
141    }
142
143    public final Context getContext() {
144        return context;
145    }
146
147    public Resources getResources() {
148        return context.getResources();
149    }
150
151    public void setBackgroundResource(int backgroundResourceId) {
152        this.backgroundResourceId = backgroundResourceId;
153    }
154
155    public int getVisibility() {
156        return visibility;
157    }
158
159    public void setVisibility(int visibility) {
160        this.visibility = visibility;
161    }
162
163    public void setSelected(boolean selected) {
164        this.selected = selected;
165    }
166
167    public boolean isSelected() {
168        return this.selected;
169    }
170
171    public boolean isEnabled() {
172        return this.enabled;
173    }
174
175    public void setEnabled(boolean enabled) {
176        this.enabled = enabled;
177    }
178
179    public void setOnClickListener(View.OnClickListener onClickListener) {
180        this.onClickListener = onClickListener;
181    }
182
183    public boolean performClick() {
184        if (onClickListener != null) {
185            onClickListener.onClick(realView);
186            return true;
187        } else {
188            return false;
189        }
190    }
191
192    public void setOnKeyListener(View.OnKeyListener onKeyListener) {
193        this.onKeyListener = onKeyListener;
194    }
195
196    public Object getTag() {
197        return this.tag;
198    }
199
200    public void setTag(Object tag) {
201        this.tag = tag;
202    }
203
204    public final int getHeight() {
205        return height;
206    }
207
208    public final int getWidth() {
209        return width;
210    }
211
212    public void setPadding(int left, int top, int right, int bottom) {
213        paddingLeft = left;
214        paddingTop = top;
215        paddingRight = right;
216        paddingBottom = bottom;
217    }
218
219    public int getPaddingTop() {
220        return paddingTop;
221    }
222
223    public int getPaddingLeft() {
224        return paddingLeft;
225    }
226
227    public int getPaddingRight() {
228        return paddingRight;
229    }
230
231    public int getPaddingBottom() {
232        return paddingBottom;
233    }
234
235    public Object getTag(int key) {
236        return tags.get(key);
237    }
238
239    public void setTag(int key, Object value) {
240        tags.put(key, value);
241    }
242
243    public void setViewFocus(boolean hasFocus) {
244        this.hasFocus = hasFocus;
245        if (onFocusChangeListener != null) {
246            onFocusChangeListener.onFocusChange(realView, hasFocus);
247        }
248    }
249
250    public boolean hasFocus() {
251        return hasFocus;
252    }
253
254    public void setOnFocusChangeListener(View.OnFocusChangeListener listener) {
255        onFocusChangeListener = listener;
256    }
257
258    public void invalidate() {
259        wasInvalidated = true;
260    }
261}
262