ShadowView.java revision 51fe0b279e9f5568683d9ef67921f922a51c05d5
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;
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 View findViewById(int id) {
55        if (id == this.id) {
56            return realView;
57        }
58
59        for (View child : children) {
60            View found = child.findViewById(id);
61            if (found != null) {
62                return found;
63            }
64        }
65        return null;
66    }
67
68    public View getRootView() {
69        FakeView root = this;
70        while(root.parent != null) {
71            root = root.parent;
72        }
73        return root.realView;
74    }
75
76    public void addView(View child) {
77        children.add(child);
78        childProxy(child).parent = this;
79    }
80
81    private FakeView childProxy(View child) {
82        return (FakeView) ProxyDelegatingHandler.getInstance().proxyFor(child);
83    }
84
85    public int getChildCount() {
86        return children.size();
87    }
88
89    public ViewGroup.LayoutParams getLayoutParams() {
90        return layoutParams;
91    }
92
93    public View getChildAt(int index) {
94        return children.get(index);
95    }
96
97    public void removeAllViews() {
98        for (View child : children) {
99            childProxy(child).parent = null;
100        }
101        children.clear();
102    }
103
104    public final Context getContext() {
105        return context;
106    }
107
108    public Resources getResources() {
109        return context.getResources();
110    }
111
112    public int getVisibility() {
113        return visibility;
114    }
115
116    public void setVisibility(int visibility) {
117        this.visibility = visibility;
118    }
119
120    public void setSelected(boolean selected) {
121        this.selected = selected;
122    }
123
124    public boolean isSelected() {
125        return this.selected;
126    }
127
128    public boolean isEnabled() {
129        return this.enabled;
130    }
131
132    public void setEnabled(boolean enabled) {
133        this.enabled = enabled;
134    }
135
136    public void setOnClickListener(View.OnClickListener onClickListener) {
137        this.onClickListener = onClickListener;
138    }
139
140    public boolean performClick() {
141        if (onClickListener != null) {
142            onClickListener.onClick(realView);
143            return true;
144        } else {
145            return false;
146        }
147    }
148
149    public Object getTag() {
150        return this.tag;
151    }
152
153    public void setTag(Object tag) {
154        this.tag = tag;
155    }
156
157    public final int getHeight() {
158        return height;
159    }
160
161    public final int getWidth() {
162        return width;
163    }
164
165    public void setPadding(int left, int top, int right, int bottom) {
166        paddingLeft = left;
167        paddingTop = top;
168        paddingRight = right;
169        paddingBottom = bottom;
170    }
171
172    public int getPaddingTop() {
173        return paddingTop;
174    }
175
176    public int getPaddingLeft() {
177        return paddingLeft;
178    }
179
180    public int getPaddingRight() {
181        return paddingRight;
182    }
183
184    public int getPaddingBottom() {
185        return paddingBottom;
186    }
187
188    public Object getTag(int key) {
189        return tags.get(key);
190    }
191
192    public void setTag(int key, Object value) {
193        tags.put(key, value);
194    }
195}
196