-
Notifications
You must be signed in to change notification settings - Fork 885
/
reflectmodels.go
420 lines (324 loc) · 8.7 KB
/
reflectmodels.go
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// Copyright 2013 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build windows
package walk
import (
"fmt"
"reflect"
"sort"
)
type reflectModel interface {
Items() interface{}
}
type bindingAndDisplayMemberSetter interface {
setBindingMember(member string)
setDisplayMember(member string)
}
type reflectListModel struct {
ListModelBase
bindingMember string
displayMember string
dataSource interface{}
items interface{}
value reflect.Value
}
func newReflectListModel(dataSource interface{}) (ListModel, error) {
items, err := itemsFromReflectModelDataSource(dataSource, "ReflectListModel")
if err != nil {
return nil, err
}
m := &reflectListModel{
dataSource: dataSource,
items: items,
value: reflect.ValueOf(items),
}
if rlm, ok := dataSource.(ReflectListModel); ok {
rlm.setValueFunc(func(index int) interface{} {
return m.Value(index)
})
rlm.ItemChanged().Attach(func(index int) {
m.PublishItemChanged(index)
})
rlm.ItemsReset().Attach(func() {
m.items = rlm.Items()
m.value = reflect.ValueOf(m.items)
m.PublishItemsReset()
})
rlm.ItemsInserted().Attach(func(from, to int) {
m.items = rlm.Items()
m.value = reflect.ValueOf(m.items)
m.PublishItemsInserted(from, to)
})
rlm.ItemsRemoved().Attach(func(from, to int) {
m.items = rlm.Items()
m.value = reflect.ValueOf(m.items)
m.PublishItemsRemoved(from, to)
})
}
return m, nil
}
func (m *reflectListModel) setBindingMember(member string) {
m.bindingMember = member
}
func (m *reflectListModel) setDisplayMember(member string) {
m.displayMember = member
}
func (m *reflectListModel) ItemCount() int {
return m.value.Len()
}
func (m *reflectListModel) BindingValue(index int) interface{} {
return valueFromSlice(m.dataSource, m.value, m.bindingMember, index)
}
func (m *reflectListModel) Value(index int) interface{} {
return valueFromSlice(m.dataSource, m.value, m.displayMember, index)
}
type lessFuncsSetter interface {
setLessFuncs(lessFuncs []func(i, j int) bool)
}
type dataMembersSetter interface {
setDataMembers(dataMembers []string)
}
type reflectTableModel struct {
TableModelBase
sorterBase *SorterBase
lessFuncs []func(i, j int) bool
dataMembers []string
dataSource interface{}
items interface{}
value reflect.Value
}
func newReflectTableModel(dataSource interface{}) (TableModel, error) {
items, err := itemsFromReflectModelDataSource(dataSource, "ReflectTableModel")
if err != nil {
return nil, err
}
m := &reflectTableModel{
dataSource: dataSource,
items: items,
value: reflect.ValueOf(items),
}
if rtm, ok := dataSource.(ReflectTableModel); ok {
rtm.setValueFunc(func(row, col int) interface{} {
return m.Value(row, col)
})
rtm.RowChanged().Attach(func(index int) {
m.PublishRowChanged(index)
})
rtm.RowsReset().Attach(func() {
m.items = rtm.Items()
m.value = reflect.ValueOf(m.items)
m.PublishRowsReset()
if is, ok := dataSource.(interceptedSorter); ok {
sb := is.sorterBase()
m.sort(sb.SortedColumn(), sb.SortOrder())
}
})
rtm.RowsChanged().Attach(func(from, to int) {
m.PublishRowsChanged(from, to)
})
rtm.RowsInserted().Attach(func(from, to int) {
m.items = rtm.Items()
m.value = reflect.ValueOf(m.items)
m.PublishRowsInserted(from, to)
})
rtm.RowsRemoved().Attach(func(from, to int) {
m.items = rtm.Items()
m.value = reflect.ValueOf(m.items)
m.PublishRowsRemoved(from, to)
})
} else {
m.sorterBase = new(SorterBase)
}
if is, ok := dataSource.(interceptedSorter); ok {
m.sorterBase = is.sorterBase()
is.setSortFunc(func(col int, order SortOrder) error {
return m.sort(col, order)
})
}
_, isImageProvider := dataSource.(ImageProvider)
_, isSortable := dataSource.(Sorter)
if !isSortable {
isSortable = m.sorterBase != nil
}
if isImageProvider {
if isSortable {
return &sortedImageReflectTableModel{reflectTableModel: m}, nil
} else {
return &imageReflectTableModel{reflectTableModel: m}, nil
}
} else if isSortable {
return &sortedReflectTableModel{reflectTableModel: m}, nil
}
return m, nil
}
func (m *reflectTableModel) setLessFuncs(lessFuncs []func(i, j int) bool) {
m.lessFuncs = lessFuncs
}
func (m *reflectTableModel) setDataMembers(dataMembers []string) {
m.dataMembers = dataMembers
}
func (m *reflectTableModel) RowCount() int {
return m.value.Len()
}
func (m *reflectTableModel) Value(row, col int) interface{} {
return valueFromSlice(m.dataSource, m.value, m.dataMembers[col], row)
}
func (m *reflectTableModel) Checked(row int) bool {
if m.value.Index(row).IsNil() {
return false
}
if checker, ok := m.dataSource.(ItemChecker); ok {
return checker.Checked(row)
}
return false
}
func (m *reflectTableModel) SetChecked(row int, checked bool) error {
if m.value.Index(row).IsNil() {
return nil
}
if checker, ok := m.dataSource.(ItemChecker); ok {
return checker.SetChecked(row, checked)
}
return nil
}
func (m *reflectTableModel) ColumnSortable(col int) bool {
if sorter, ok := m.dataSource.(Sorter); ok {
return sorter.ColumnSortable(col)
}
return true
}
func (m *reflectTableModel) SortChanged() *Event {
if sorter, ok := m.dataSource.(Sorter); ok {
return sorter.SortChanged()
}
if m.sorterBase != nil {
return m.sorterBase.SortChanged()
}
return nil
}
func (m *reflectTableModel) SortedColumn() int {
if sorter, ok := m.dataSource.(Sorter); ok {
return sorter.SortedColumn()
}
if m.sorterBase != nil {
return m.sorterBase.SortedColumn()
}
return -1
}
func (m *reflectTableModel) SortOrder() SortOrder {
if sorter, ok := m.dataSource.(Sorter); ok {
return sorter.SortOrder()
}
if m.sorterBase != nil {
return m.sorterBase.SortOrder()
}
return SortAscending
}
func (m *reflectTableModel) sort(col int, order SortOrder) error {
if sb := m.sorterBase; sb != nil {
sb.col, sb.order = col, order
sort.Stable(m)
sb.changedPublisher.Publish()
return nil
}
if sorter, ok := m.dataSource.(Sorter); ok {
return sorter.Sort(col, order)
}
return nil
}
func (m *reflectTableModel) Len() int {
return m.RowCount()
}
func (m *reflectTableModel) Less(i, j int) bool {
col := m.SortedColumn()
if lt := m.lessFuncs[col]; lt != nil {
ls := lt(i, j)
if m.SortOrder() == SortAscending {
return ls
} else {
return !ls
}
}
return less(m.Value(i, col), m.Value(j, col), m.SortOrder())
}
func (m *reflectTableModel) Swap(i, j int) {
vi := m.value.Index(i)
vj := m.value.Index(j)
viv := vi.Interface()
vjv := vj.Interface()
vi.Set(reflect.ValueOf(vjv))
vj.Set(reflect.ValueOf(viv))
}
type imageReflectTableModel struct {
*reflectTableModel
}
func (m *imageReflectTableModel) Image(index int) interface{} {
if m.value.Index(index).IsNil() {
return nil
}
return m.dataSource.(ImageProvider).Image(index)
}
type sortedReflectTableModel struct {
*reflectTableModel
}
func (m *sortedReflectTableModel) Sort(col int, order SortOrder) error {
return m.reflectTableModel.sort(col, order)
}
type sortedImageReflectTableModel struct {
*reflectTableModel
}
func (m *sortedImageReflectTableModel) Sort(col int, order SortOrder) error {
return m.reflectTableModel.sort(col, order)
}
func (m *sortedImageReflectTableModel) Image(index int) interface{} {
if m.value.Index(index).IsNil() {
return nil
}
return m.dataSource.(ImageProvider).Image(index)
}
func itemsFromReflectModelDataSource(dataSource interface{}, requiredInterfaceName string) (interface{}, error) {
var items interface{}
if rm, ok := dataSource.(reflectModel); ok {
items = rm.Items()
} else {
items = dataSource
}
if requiredInterfaceName == "ReflectListModel" {
if _, ok := dataSource.([]string); ok {
return items, nil
}
}
if t := reflect.TypeOf(items); t != nil &&
t.Kind() == reflect.Slice &&
(t.Elem().Kind() == reflect.Struct ||
(t.Elem().Kind() == reflect.Interface || t.Elem().Kind() == reflect.Ptr) &&
t.Elem().Elem().Kind() == reflect.Struct) {
return items, nil
}
return nil, newError(fmt.Sprintf("dataSource must be a slice of struct or interface or pointer to struct or must implement %s.", requiredInterfaceName))
}
func valueFromSlice(dataSource interface{}, itemsValue reflect.Value, member string, index int) interface{} {
if member == "" {
if strs, ok := dataSource.([]string); ok {
return strs[index]
}
return ""
}
v := itemsValue.Index(index)
if v.Kind() == reflect.Ptr && v.IsNil() {
if populator, ok := dataSource.(Populator); ok {
if err := populator.Populate(index); err != nil {
return err
}
}
if v.IsNil() {
return nil
}
}
_, vv, err := reflectValueFromPath(v, member)
if err != nil {
return err
}
return vv.Interface()
}