ShadowView.java revision 810ca4e6f0dc6d69504b3e03ef61155f10083fbe
1package com.xtremelabs.droidsugar.view;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import android.content.Context;
7import android.content.res.Resources;
8import android.view.View;
9import com.xtremelabs.droidsugar.ProxyDelegatingHandler;
10
11@SuppressWarnings({"ALL"})
12public class FakeView {
13    private View realView;
14
15    private int id;
16    private List<View> children = new ArrayList<View>();
17    private FakeView parent;
18    private Context context;
19    private int visibility;
20    public boolean selected;
21    private View.OnClickListener onClickListener;
22
23    public FakeView(View view) {
24        this.realView = view;
25    }
26
27    public void __constructor__(Context context) {
28        this.context = context;
29    }
30
31    public void setId(int id) {
32        this.id = id;
33    }
34
35    public int getId() {
36        return id;
37    }
38
39    public View findViewById(int id) {
40        if (id == this.id) {
41            return realView;
42        }
43
44        for (View child : children) {
45            View found = child.findViewById(id);
46            if (found != null) {
47                return found;
48            }
49        }
50        return null;
51    }
52
53    public View getRootView() {
54        FakeView root = this;
55        while(root.parent != null) {
56            root = root.parent;
57        }
58        return root.realView;
59    }
60
61    public void addView(View child) {
62        children.add(child);
63        childProxy(child).parent = this;
64    }
65
66    private FakeView childProxy(View child) {
67        return (FakeView) ProxyDelegatingHandler.getInstance().proxyFor(child);
68    }
69
70    public int getChildCount() {
71        return children.size();
72    }
73
74    public View getChildAt(int index) {
75        return children.get(index);
76    }
77
78    public void removeAllViews() {
79        for (View child : children) {
80            childProxy(child).parent = null;
81        }
82        children.clear();
83    }
84
85    public final Context getContext() {
86        return context;
87    }
88
89    public Resources getResources() {
90        return context.getResources();
91    }
92
93    public int getVisibility() {
94        return visibility;
95    }
96
97    public void setVisibility(int visibility) {
98        this.visibility = visibility;
99    }
100
101    public void setSelected(boolean selected) {
102        this.selected = selected;
103    }
104
105    public void setOnClickListener(View.OnClickListener onClickListener) {
106        this.onClickListener = onClickListener;
107    }
108
109    public boolean performClick() {
110        if (onClickListener != null) {
111            onClickListener.onClick(realView);
112            return true;
113        } else {
114            return false;
115        }
116    }
117}
118