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