forked from pharo-project/pharo
/
SpMorphicTreeTableDataSource.class.st
218 lines (160 loc) · 5.44 KB
/
SpMorphicTreeTableDataSource.class.st
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
"
I'm a dataSource to be used in MorphicTableAdapter to realise a TablePresenter.
I define my cells following the definition on columns (MorphicTableColumn).
I use a builder (MorphicTreeTableCellBuilder) to realise the cell content.
"
Class {
#name : #SpMorphicTreeTableDataSource,
#superclass : #FTTreeDataSource,
#traits : 'SpTMorphicTableDataSourceCommons',
#classTraits : 'SpTMorphicTableDataSourceCommons classTrait',
#category : #'Spec2-Adapters-Morphic-Table'
}
{ #category : #'search strategies' }
SpMorphicTreeTableDataSource class >> allItemsStrategy [
^ SpMorphicAllItemsStrategy
]
{ #category : #'search strategies' }
SpMorphicTreeTableDataSource class >> defaultStrategy [
^ SpMorphicVisibleItemsStrategy
]
{ #category : #'search strategies' }
SpMorphicTreeTableDataSource class >> rootsOnlyStrategy [
^ SpMorphicRootItemsStrategy
]
{ #category : #accessing }
SpMorphicTreeTableDataSource >> allShownItems [
^ rootItem children flatCollect: [ :e | e withExpandedChildren ].
]
{ #category : #accessing }
SpMorphicTreeTableDataSource >> buttonFor: item [
^ (self shouldDisplayExpandableArrowFor: item)
ifTrue: [ item generateButton ]
ifFalse: [ self class emptyMorph ]
]
{ #category : #accessing }
SpMorphicTreeTableDataSource >> cellColumn: column row: rowIndex [
| cell builder item |
item := self elementAt: rowIndex.
cell := FTIndentedCellMorph new.
cell indentBy: (self cellIndentFor: item).
(self isFirstColumn: column) ifTrue: [
cell addMorphBack: (self buttonFor: item) ].
builder := SpMorphicTreeTableCellBuilder on: self.
builder
cell: cell;
item: item data;
visit: column model.
^ cell
]
{ #category : #accessing }
SpMorphicTreeTableDataSource >> cellIndentFor: item [
^ item depth * 16
]
{ #category : #accessing }
SpMorphicTreeTableDataSource >> expandPath: anArray [
self expandPath: anArray root: self rootItem children
]
{ #category : #accessing }
SpMorphicTreeTableDataSource >> expandPath: anArray root: nodes [
| node |
anArray ifEmpty: [ ^ self ].
nodes ifEmpty: [ ^ self ].
node := nodes at: anArray first.
node expand.
self expandPath: anArray allButFirst root: node children
]
{ #category : #paths }
SpMorphicTreeTableDataSource >> indexFromPath: anArray [
^ anArray sum
]
{ #category : #accessing }
SpMorphicTreeTableDataSource >> indexOfItem: anItem [
^ self allShownItems indexOf: anItem
]
{ #category : #testing }
SpMorphicTreeTableDataSource >> isFirstColumn: column [
^ (table columns indexOf: column) = 1
]
{ #category : #accessing }
SpMorphicTreeTableDataSource >> itemAtIndex: anInteger [
^ self allShownItems at: anInteger
]
{ #category : #accessing }
SpMorphicTreeTableDataSource >> itemAtPath: aPath [
^ self itemAtPath: aPath expanding: false
]
{ #category : #accessing }
SpMorphicTreeTableDataSource >> itemAtPath: aPath expanding: shouldExpandItems [
| item |
(aPath isNil or: [aPath isEmpty]) ifTrue: [ ^ nil ].
item := self rootItem.
aPath do: [ :index |
(shouldExpandItems and: [item isRoot not]) ifTrue: [ item expand ].
item children size < index ifTrue: [ ^ nil ].
item := item children at: index.
].
^ item
]
{ #category : #'drag and drop' }
SpMorphicTreeTableDataSource >> passengerAt: rowIndex [
"Answer 'passenger' for drag&drop operation (a passenger is the object who will travel between
drag and drop). By default, passenger is same as current element"
^ (self allShownItems at: rowIndex) data
]
{ #category : #paths }
SpMorphicTreeTableDataSource >> pathFromIndex: anInteger [
| item |
item := self allShownItems at: anInteger ifAbsent: [ ^ #() ].
^ self pathOfNode: item root: rootItem
]
{ #category : #accessing }
SpMorphicTreeTableDataSource >> pathOfItem: anItem root: aNode [
"Looks for the path of an item (contained in a node)"
aNode isExpanded ifFalse: [ ^#() ].
aNode children
detect: [ :each | each data = anItem ]
ifFound: [ :childNode | ^ { aNode children indexOf: childNode } ].
aNode children withIndexDo: [ :aChild :index | | aSubPath |
aSubPath := self pathOfItem: anItem root: aChild.
aSubPath ifNotEmpty: [ ^ { index } , aSubPath ] ].
^ #()
]
{ #category : #accessing }
SpMorphicTreeTableDataSource >> pathOfNode: anItem root: aRoot [
aRoot isExpanded ifFalse: [ ^#() ].
(aRoot children includes: anItem)
ifTrue: [ ^ { aRoot children indexOf: anItem } ].
aRoot children withIndexDo: [ :aChild :index | | aSubPath |
aSubPath := self pathOfNode: anItem root: aChild.
aSubPath ifNotEmpty: [ ^ { index } , aSubPath ] ].
^ #().
]
{ #category : #accessing }
SpMorphicTreeTableDataSource >> rootItem: anItem [
super rootItem: anItem.
self addSourceToRootItem
]
{ #category : #accessing }
SpMorphicTreeTableDataSource >> searchText: aString [
^ (self class perform: (self searchStrategy, 'Strategy') asSymbol)
searchWith: aString
dataSource: self
]
{ #category : #testing }
SpMorphicTreeTableDataSource >> shouldDisplayExpandableArrowFor: item [
^ (self canDisplayChildrenOf: item) and: [
self model shouldLazilyComputeChildren
ifTrue: [ item hasComputedChildren ]
ifFalse: [ item hasChildren ] ]
]
{ #category : #sorting }
SpMorphicTreeTableDataSource >> sortElements: aSortFunction [
unsortedElements ifNil: [ unsortedElements := self rootsItems ].
self rootItem data: ((self rootItem children collect: #data) sorted: aSortFunction)
]
{ #category : #accessing }
SpMorphicTreeTableDataSource >> toString: anItem [
"Override me if you expect something else."
^ super toString: anItem data
]