forked from tidev/titanium-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TiUITableView.java
351 lines (311 loc) · 10.7 KB
/
TiUITableView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2012 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
package ti.modules.titanium.ui.widget;
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiBaseActivity;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.TiDimension;
import org.appcelerator.titanium.TiLifecycle.OnLifecycleEvent;
import org.appcelerator.titanium.proxy.TiViewProxy;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.view.TiBorderWrapperView;
import org.appcelerator.titanium.view.TiUIView;
import ti.modules.titanium.ui.RefreshControlProxy;
import ti.modules.titanium.ui.SearchBarProxy;
import ti.modules.titanium.ui.TableViewProxy;
import ti.modules.titanium.ui.widget.searchbar.TiUISearchBar;
import ti.modules.titanium.ui.widget.searchview.TiUISearchView;
import ti.modules.titanium.ui.widget.tableview.TableViewModel;
import ti.modules.titanium.ui.widget.tableview.TiTableView;
import ti.modules.titanium.ui.widget.tableview.TiTableView.OnItemClickedListener;
import ti.modules.titanium.ui.widget.tableview.TiTableView.OnItemLongClickedListener;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewParent;
import android.widget.ListView;
import android.widget.RelativeLayout;
public class TiUITableView
extends TiUIView implements OnItemClickedListener, OnItemLongClickedListener, OnLifecycleEvent
{
private static final String TAG = "TitaniumTableView";
private static final int SEARCHVIEW_ID = 102;
protected TiTableView tableView;
public TiUITableView(TiViewProxy proxy)
{
super(proxy);
getLayoutParams().autoFillsHeight = true;
getLayoutParams().autoFillsWidth = true;
}
@Override
public void onClick(KrollDict data)
{
proxy.fireEvent(TiC.EVENT_CLICK, data);
}
@Override
public boolean onLongClick(KrollDict data)
{
return proxy.fireEvent(TiC.EVENT_LONGCLICK, data);
}
public void setModelDirty()
{
tableView.getTableViewModel().setDirty();
getTableView().dataSetChanged();
}
public TableViewModel getModel()
{
return tableView.getTableViewModel();
}
public void updateView()
{
tableView.dataSetChanged();
}
public void scrollToIndex(final int index)
{
tableView.getListView().setSelection(index);
}
public void scrollToTop(final int index)
{
tableView.getListView().setSelectionFromTop(index, 0);
}
public void selectRow(final int row_id)
{
tableView.getListView().setSelection(row_id);
}
public TiTableView getTableView()
{
return tableView;
}
public ListView getListView()
{
return tableView.getListView();
}
@Override
public void processProperties(KrollDict d)
{
// Don't create a new table view if one already exists
if (tableView == null) {
tableView = new TiTableView((TableViewProxy) proxy);
}
Activity activity = proxy.getActivity();
if (activity instanceof TiBaseActivity) {
((TiBaseActivity) activity).addOnLifecycleEventListener(this);
}
boolean clickable = true;
if (d.containsKey(TiC.PROPERTY_TOUCH_ENABLED)) {
clickable = TiConvert.toBoolean(proxy.getProperty(TiC.PROPERTY_TOUCH_ENABLED), true);
}
if (clickable) {
tableView.setOnItemClickListener(this);
tableView.setOnItemLongClickListener(this);
}
ListView list = getListView();
if (d.containsKey(TiC.PROPERTY_FOOTER_DIVIDERS_ENABLED)) {
boolean enabled = TiConvert.toBoolean(d, TiC.PROPERTY_FOOTER_DIVIDERS_ENABLED, false);
list.setFooterDividersEnabled(enabled);
} else {
list.setFooterDividersEnabled(false);
}
if (d.containsKey(TiC.PROPERTY_HEADER_DIVIDERS_ENABLED)) {
boolean enabled = TiConvert.toBoolean(d, TiC.PROPERTY_HEADER_DIVIDERS_ENABLED, false);
list.setHeaderDividersEnabled(enabled);
} else {
list.setHeaderDividersEnabled(false);
}
if (d.containsKey(TiC.PROPERTY_REFRESH_CONTROL)) {
Object object = d.get(TiC.PROPERTY_REFRESH_CONTROL);
if (object instanceof RefreshControlProxy) {
((RefreshControlProxy) object).assignTo(this.tableView);
}
}
if (d.containsKey(TiC.PROPERTY_SEARCH)) {
TiViewProxy searchView = (TiViewProxy) d.get(TiC.PROPERTY_SEARCH);
TiUIView search = searchView.getOrCreateView();
if (searchView instanceof SearchBarProxy) {
((TiUISearchBar) search).setOnSearchChangeListener(tableView);
} else {
((TiUISearchView) search).setOnSearchChangeListener(tableView);
}
if (!(d.containsKey(TiC.PROPERTY_SEARCH_AS_CHILD)
&& !TiConvert.toBoolean(d.get(TiC.PROPERTY_SEARCH_AS_CHILD)))) {
View sView = search.getNativeView();
RelativeLayout layout = new RelativeLayout(proxy.getActivity());
layout.setGravity(Gravity.NO_GRAVITY);
layout.setPadding(0, 0, 0, 0);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
p.addRule(RelativeLayout.ALIGN_PARENT_TOP);
p.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
p.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
TiDimension rawHeight;
if (searchView.hasProperty("height")) {
rawHeight = TiConvert.toTiDimension(searchView.getProperty("height"), 0);
} else {
rawHeight = TiConvert.toTiDimension("52dp", 0);
}
p.height = rawHeight.getAsPixels(layout);
//Check to see if searchView has a border
ViewParent parent = sView.getParent();
if (parent instanceof TiBorderWrapperView) {
TiBorderWrapperView v = (TiBorderWrapperView) parent;
v.setId(SEARCHVIEW_ID);
layout.addView(v, p);
} else if (parent == null) {
sView.setId(SEARCHVIEW_ID);
layout.addView(sView, p);
} else {
Log.e(TAG, "Searchview already has parent, cannot add to tableview.", Log.DEBUG_MODE);
}
p = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
p.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
p.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
p.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
p.addRule(RelativeLayout.BELOW, SEARCHVIEW_ID);
layout.addView(tableView, p);
setNativeView(layout);
} else {
setNativeView(tableView);
}
} else {
setNativeView(tableView);
}
if (d.containsKey(TiC.PROPERTY_FILTER_ATTRIBUTE)) {
tableView.setFilterAttribute(TiConvert.toString(d, TiC.PROPERTY_FILTER_ATTRIBUTE));
} else {
// Default to title to match iPhone default.
proxy.setProperty(TiC.PROPERTY_FILTER_ATTRIBUTE, TiC.PROPERTY_TITLE);
tableView.setFilterAttribute(TiC.PROPERTY_TITLE);
}
if (d.containsKey(TiC.PROPERTY_OVER_SCROLL_MODE)) {
if (Build.VERSION.SDK_INT >= 9) {
getListView().setOverScrollMode(
TiConvert.toInt(d.get(TiC.PROPERTY_OVER_SCROLL_MODE), View.OVER_SCROLL_ALWAYS));
}
}
boolean filterCaseInsensitive = true;
if (d.containsKey(TiC.PROPERTY_FILTER_CASE_INSENSITIVE)) {
filterCaseInsensitive = TiConvert.toBoolean(d, TiC.PROPERTY_FILTER_CASE_INSENSITIVE);
}
tableView.setFilterCaseInsensitive(filterCaseInsensitive);
boolean filterAnchored = false;
if (d.containsKey(TiC.PROPERTY_FILTER_ANCHORED)) {
filterAnchored = TiConvert.toBoolean(d, TiC.PROPERTY_FILTER_ANCHORED);
}
tableView.setFilterAnchored(filterAnchored);
super.processProperties(d);
}
@Override
public void onResume(Activity activity)
{
if (tableView != null) {
tableView.dataSetChanged();
}
}
@Override
public void onCreate(Activity activity, Bundle savedInstanceState)
{
}
@Override
public void onStop(Activity activity)
{
}
@Override
public void onStart(Activity activity)
{
}
@Override
public void onPause(Activity activity)
{
}
@Override
public void onDestroy(Activity activity)
{
}
@Override
public void release()
{
// Release search bar if there is one
if (nativeView instanceof RelativeLayout) {
((RelativeLayout) nativeView).removeAllViews();
TiViewProxy searchView = (TiViewProxy) (proxy.getProperty(TiC.PROPERTY_SEARCH));
searchView.release();
}
// If a refresh control is currently assigned, then detach it.
RefreshControlProxy.unassignFrom(this.tableView);
if (tableView != null) {
tableView.release();
tableView = null;
}
if (proxy != null && proxy.getActivity() != null) {
((TiBaseActivity) proxy.getActivity()).removeOnLifecycleEventListener(this);
}
nativeView = null;
super.release();
}
@Override
public void propertyChanged(String key, Object oldValue, Object newValue, KrollProxy proxy)
{
if (Log.isDebugModeEnabled()) {
Log.d(TAG, "Property: " + key + " old: " + oldValue + " new: " + newValue, Log.DEBUG_MODE);
}
if (key.equals(TiC.PROPERTY_TOUCH_ENABLED)) {
boolean clickable = TiConvert.toBoolean(newValue);
if (clickable) {
tableView.setOnItemClickListener(this);
tableView.setOnItemLongClickListener(this);
} else {
tableView.setOnItemClickListener(null);
tableView.setOnItemLongClickListener(null);
}
}
if (key.equals(TiC.PROPERTY_SEPARATOR_COLOR)) {
tableView.setSeparatorColor(TiConvert.toString(newValue));
} else if (key.equals(TiC.PROPERTY_SEPARATOR_STYLE)) {
tableView.setSeparatorStyle(TiConvert.toInt(newValue));
} else if (TiC.PROPERTY_OVER_SCROLL_MODE.equals(key)) {
if (Build.VERSION.SDK_INT >= 9) {
getListView().setOverScrollMode(TiConvert.toInt(newValue, View.OVER_SCROLL_ALWAYS));
}
} else if (TiC.PROPERTY_MIN_ROW_HEIGHT.equals(key)) {
updateView();
} else if (TiC.PROPERTY_HEADER_VIEW.equals(key)) {
if (oldValue != null) {
tableView.removeHeaderView((TiViewProxy) oldValue);
}
tableView.setHeaderView();
} else if (TiC.PROPERTY_FOOTER_VIEW.equals(key)) {
if (oldValue != null) {
tableView.removeFooterView((TiViewProxy) oldValue);
}
tableView.setFooterView();
} else if (key.equals(TiC.PROPERTY_FILTER_ANCHORED)) {
tableView.setFilterAnchored(TiConvert.toBoolean(newValue));
} else if (key.equals(TiC.PROPERTY_FILTER_CASE_INSENSITIVE)) {
tableView.setFilterCaseInsensitive(TiConvert.toBoolean(newValue));
} else if (key.equals(TiC.PROPERTY_REFRESH_CONTROL)) {
if (newValue == null) {
RefreshControlProxy.unassignFrom(this.tableView);
} else if (newValue instanceof RefreshControlProxy) {
((RefreshControlProxy) newValue).assignTo(this.tableView);
} else {
Log.e(TAG, "Invalid value assigned to property '" + key + "'. Must be of type 'RefreshControl'.");
}
} else {
super.propertyChanged(key, oldValue, newValue, proxy);
}
}
@Override
public void registerForTouch()
{
registerForTouch(tableView.getListView());
}
}