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