create changelog entry
[debian/openrocket] / android-libraries / TreeViewList / src / pl / polidea / treeview / TreeViewList.java
1 package pl.polidea.treeview;
2
3 import android.content.Context;
4 import android.content.res.TypedArray;
5 import android.graphics.drawable.Drawable;
6 import android.util.AttributeSet;
7 import android.view.Gravity;
8 import android.view.View;
9 import android.widget.AdapterView;
10 import android.widget.ListAdapter;
11 import android.widget.ListView;
12
13 /**
14  * Tree view, expandable multi-level.
15  * 
16  * <pre>
17  * attr ref pl.polidea.treeview.R.styleable#TreeViewList_collapsible
18  * attr ref pl.polidea.treeview.R.styleable#TreeViewList_src_expanded
19  * attr ref pl.polidea.treeview.R.styleable#TreeViewList_src_collapsed
20  * attr ref pl.polidea.treeview.R.styleable#TreeViewList_indent_width
21  * attr ref pl.polidea.treeview.R.styleable#TreeViewList_handle_trackball_press
22  * attr ref pl.polidea.treeview.R.styleable#TreeViewList_indicator_gravity
23  * attr ref pl.polidea.treeview.R.styleable#TreeViewList_indicator_background
24  * attr ref pl.polidea.treeview.R.styleable#TreeViewList_row_background
25  * </pre>
26  */
27 public class TreeViewList extends ListView {
28     private static final int DEFAULT_COLLAPSED_RESOURCE = R.drawable.collapsed;
29     private static final int DEFAULT_EXPANDED_RESOURCE = R.drawable.expanded;
30     private static final int DEFAULT_INDENT = 0;
31     private static final int DEFAULT_GRAVITY = Gravity.LEFT
32             | Gravity.CENTER_VERTICAL;
33     private Drawable expandedDrawable;
34     private Drawable collapsedDrawable;
35     private Drawable rowBackgroundDrawable;
36     private Drawable indicatorBackgroundDrawable;
37     private int indentWidth = 0;
38     private int indicatorGravity = 0;
39     private AbstractTreeViewAdapter< ? > treeAdapter;
40     private boolean collapsible;
41     private boolean handleTrackballPress;
42
43     public TreeViewList(final Context context, final AttributeSet attrs) {
44         this(context, attrs, R.style.treeViewListStyle);
45     }
46
47     public TreeViewList(final Context context) {
48         this(context, null);
49     }
50
51     public TreeViewList(final Context context, final AttributeSet attrs,
52             final int defStyle) {
53         super(context, attrs, defStyle);
54         parseAttributes(context, attrs);
55     }
56
57     private void parseAttributes(final Context context, final AttributeSet attrs) {
58         final TypedArray a = context.obtainStyledAttributes(attrs,
59                 R.styleable.TreeViewList);
60         expandedDrawable = a.getDrawable(R.styleable.TreeViewList_src_expanded);
61         if (expandedDrawable == null) {
62             expandedDrawable = context.getResources().getDrawable(
63                     DEFAULT_EXPANDED_RESOURCE);
64         }
65         collapsedDrawable = a
66                 .getDrawable(R.styleable.TreeViewList_src_collapsed);
67         if (collapsedDrawable == null) {
68             collapsedDrawable = context.getResources().getDrawable(
69                     DEFAULT_COLLAPSED_RESOURCE);
70         }
71         indentWidth = a.getDimensionPixelSize(
72                 R.styleable.TreeViewList_indent_width, DEFAULT_INDENT);
73         indicatorGravity = a.getInteger(
74                 R.styleable.TreeViewList_indicator_gravity, DEFAULT_GRAVITY);
75         indicatorBackgroundDrawable = a
76                 .getDrawable(R.styleable.TreeViewList_indicator_background);
77         rowBackgroundDrawable = a
78                 .getDrawable(R.styleable.TreeViewList_row_background);
79         collapsible = a.getBoolean(R.styleable.TreeViewList_collapsible, true);
80         handleTrackballPress = a.getBoolean(
81                 R.styleable.TreeViewList_handle_trackball_press, true);
82     }
83
84     @Override
85     public void setAdapter(final ListAdapter adapter) {
86         if (!(adapter instanceof AbstractTreeViewAdapter)) {
87             throw new TreeConfigurationException(
88                     "The adapter is not of TreeViewAdapter type");
89         }
90         treeAdapter = (AbstractTreeViewAdapter< ? >) adapter;
91         syncAdapter();
92         super.setAdapter(treeAdapter);
93     }
94
95     private void syncAdapter() {
96         treeAdapter.setCollapsedDrawable(collapsedDrawable);
97         treeAdapter.setExpandedDrawable(expandedDrawable);
98         treeAdapter.setIndicatorGravity(indicatorGravity);
99         treeAdapter.setIndentWidth(indentWidth);
100         treeAdapter.setIndicatorBackgroundDrawable(indicatorBackgroundDrawable);
101         treeAdapter.setRowBackgroundDrawable(rowBackgroundDrawable);
102         treeAdapter.setCollapsible(collapsible);
103         if (handleTrackballPress) {
104             setOnItemClickListener(new OnItemClickListener() {
105                 @Override
106                 public void onItemClick(final AdapterView< ? > parent,
107                         final View view, final int position, final long id) {
108                     treeAdapter.handleItemClick(view, view.getTag());
109                 }
110             });
111         } else {
112             setOnClickListener(null);
113         }
114
115     }
116
117     public void setExpandedDrawable(final Drawable expandedDrawable) {
118         this.expandedDrawable = expandedDrawable;
119         syncAdapter();
120         treeAdapter.refresh();
121     }
122
123     public void setCollapsedDrawable(final Drawable collapsedDrawable) {
124         this.collapsedDrawable = collapsedDrawable;
125         syncAdapter();
126         treeAdapter.refresh();
127     }
128
129     public void setRowBackgroundDrawable(final Drawable rowBackgroundDrawable) {
130         this.rowBackgroundDrawable = rowBackgroundDrawable;
131         syncAdapter();
132         treeAdapter.refresh();
133     }
134
135     public void setIndicatorBackgroundDrawable(
136             final Drawable indicatorBackgroundDrawable) {
137         this.indicatorBackgroundDrawable = indicatorBackgroundDrawable;
138         syncAdapter();
139         treeAdapter.refresh();
140     }
141
142     public void setIndentWidth(final int indentWidth) {
143         this.indentWidth = indentWidth;
144         syncAdapter();
145         treeAdapter.refresh();
146     }
147
148     public void setIndicatorGravity(final int indicatorGravity) {
149         this.indicatorGravity = indicatorGravity;
150         syncAdapter();
151         treeAdapter.refresh();
152     }
153
154     public void setCollapsible(final boolean collapsible) {
155         this.collapsible = collapsible;
156         syncAdapter();
157         treeAdapter.refresh();
158     }
159
160     public void setHandleTrackballPress(final boolean handleTrackballPress) {
161         this.handleTrackballPress = handleTrackballPress;
162         syncAdapter();
163         treeAdapter.refresh();
164     }
165
166     public Drawable getExpandedDrawable() {
167         return expandedDrawable;
168     }
169
170     public Drawable getCollapsedDrawable() {
171         return collapsedDrawable;
172     }
173
174     public Drawable getRowBackgroundDrawable() {
175         return rowBackgroundDrawable;
176     }
177
178     public Drawable getIndicatorBackgroundDrawable() {
179         return indicatorBackgroundDrawable;
180     }
181
182     public int getIndentWidth() {
183         return indentWidth;
184     }
185
186     public int getIndicatorGravity() {
187         return indicatorGravity;
188     }
189
190     public boolean isCollapsible() {
191         return collapsible;
192     }
193
194     public boolean isHandleTrackballPress() {
195         return handleTrackballPress;
196     }
197
198 }