Skip to content

Commit 03300f2

Browse files
committed
tmf: Add auto-expand level in TmfTreeModel
The auto-expand level is supposed to be used for the input of the tree. The value 0 means that there is no auto-expand; 1 means that top-level elements are expanded, but not their children; 2 means that top-level elements are expanded, and their children, but not grand-children; and so on. The value -1 means that all subtrees should be expanded (default). [Added] Add auto-expand level in TmfTreeModel Signed-off-by: Bernd Hufmann <[email protected]>
1 parent f411690 commit 03300f2

File tree

3 files changed

+60
-27
lines changed

3 files changed

+60
-27
lines changed

tmf/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/model/tree/TmfTreeModelTest.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**********************************************************************
2-
* Copyright (c) 2020, 2025 Ericsson
2+
* Copyright (c) 2020 Ericsson
33
*
44
* All rights reserved. This program and the accompanying materials are
55
* made available under the terms of the Eclipse Public License 2.0 which
@@ -41,6 +41,8 @@ public class TmfTreeModelTest {
4141
private static final String TOOLTIP_PREFIX = "tooltip";
4242
private static final String LABEL_PREFIX = "label";
4343
private static final String TEST_SCOPE = "scope";
44+
private static final int ALL_LEVELS = -1;
45+
private static final int TEST_LEVEL = 1;
4446
private static List<ITmfTreeDataModel> fTestEntries = new ArrayList<>();
4547
private static List<String> fTestHeaders = new ArrayList<>();
4648
private static List<String> fExpectedEmptyTooltips = new ArrayList<>();
@@ -82,13 +84,13 @@ public static void initClass() {
8284
@Test
8385
public void testTmfTreeModelConstructor() {
8486
TmfTreeModel<ITmfTreeDataModel> testInstance = new TmfTreeModel<>(fTestHeaders, fTestEntries);
85-
verifyInstance(testInstance, fExpectedEmptyTooltips, null);
87+
verifyInstance(testInstance, fExpectedEmptyTooltips, null, ALL_LEVELS);
8688

8789
testInstance = new TmfTreeModel<>(fTestHeaders, fTestEntries, TEST_SCOPE);
88-
verifyInstance(testInstance, fExpectedEmptyTooltips, TEST_SCOPE);
90+
verifyInstance(testInstance, fExpectedEmptyTooltips, TEST_SCOPE, ALL_LEVELS);
8991
}
9092

91-
private static void verifyInstance(TmfTreeModel<ITmfTreeDataModel> testInstance, List<String> tooltips, @Nullable String scope) {
93+
private static void verifyInstance(TmfTreeModel<ITmfTreeDataModel> testInstance, List<String> tooltips, @Nullable String scope, int autoExpandLevel) {
9294
assertEquals("Incorrect list of entries", fTestEntries, testInstance.getEntries());
9395
assertEquals("Incorrect list of entries", fTestHeaders, testInstance.getHeaders());
9496

@@ -101,6 +103,7 @@ private static void verifyInstance(TmfTreeModel<ITmfTreeDataModel> testInstance,
101103
assertEquals("Incorrect Collumn descriptor header tooltip", tooltips.get(i), columnDescriptors.get(i).getTooltip());
102104
}
103105
assertEquals("Incorrect scope", scope, testInstance.getScope());
106+
assertEquals("Incorrect autoExpandLevel", autoExpandLevel, testInstance.getAutoExpandLevel());
104107
}
105108

106109
/**
@@ -112,13 +115,17 @@ public void testTmfTreeModelConstructor2() {
112115
TmfTreeModel.Builder<ITmfTreeDataModel> builder = new TmfTreeModel.Builder<>();
113116
builder.setColumnDescriptors(fTestDescriptors).setEntries(fTestEntries);
114117
TmfTreeModel<ITmfTreeDataModel> testInstance = builder.build();
115-
verifyInstance(testInstance, fExpectedTooltips, null);
118+
verifyInstance(testInstance, fExpectedTooltips, null, ALL_LEVELS);
116119

117120
builder = new TmfTreeModel.Builder<>();
118121
builder.setColumnDescriptors(fTestDescriptors).setEntries(fTestEntries).setScope(TEST_SCOPE);
122+
testInstance = builder.build();
123+
verifyInstance(testInstance, fExpectedTooltips, TEST_SCOPE, ALL_LEVELS);
119124

125+
builder = new TmfTreeModel.Builder<>();
126+
builder.setColumnDescriptors(fTestDescriptors).setEntries(fTestEntries).setScope(TEST_SCOPE).setAutoExpandLevel(TEST_LEVEL);
120127
testInstance = builder.build();
121-
verifyInstance(testInstance, fExpectedTooltips, TEST_SCOPE);
128+
verifyInstance(testInstance, fExpectedTooltips, TEST_SCOPE, TEST_LEVEL);
122129

123130
builder = new TmfTreeModel.Builder<>();
124131
testInstance = builder.build();

tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/model/tree/TmfTreeCompositeDataProvider.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**********************************************************************
2-
* Copyright (c) 2017, 2021 Ericsson
2+
* Copyright (c) 2017, 2025 Ericsson
33
*
44
* All rights reserved. This program and the accompanying materials are
55
* made available under the terms of the Eclipse Public License 2.0 which
@@ -106,7 +106,7 @@ public TmfModelResponse<TmfTreeModel<M>> fetchTree(Map<String, Object> fetchPara
106106
boolean isComplete = true;
107107
List<Entry<M, Object>> entries = new ArrayList<>();
108108
List<ITableColumnDescriptor> columnDescriptor = null;
109-
int expandLevel = -1;
109+
int autoExpandLevel = TmfTreeModel.ALL_LEVELS;
110110

111111
Table<Object, Long, @NonNull M> scopedEntries = HashBasedTable.create();
112112
for (P dataProvider : fProviders) {
@@ -156,7 +156,8 @@ public TmfModelResponse<TmfTreeModel<M>> fetchTree(Map<String, Object> fetchPara
156156
if (columnDescriptor == null) {
157157
columnDescriptor = model.getColumnDescriptors();
158158
}
159-
expandLevel = model.getExpandLevel();
159+
// All autoExpandLevel are supposed to be the same
160+
autoExpandLevel = model.getAutoExpandLevel();
160161
}
161162
if (monitor != null && monitor.isCanceled()) {
162163
return new TmfModelResponse<>(null, ITmfResponse.Status.CANCELLED, CommonStatusMessage.TASK_CANCELLED);
@@ -169,7 +170,7 @@ public TmfModelResponse<TmfTreeModel<M>> fetchTree(Map<String, Object> fetchPara
169170
}
170171
treeModelBuilder.setColumnDescriptors(columnDescriptor)
171172
.setEntries(Lists.transform(entries, e -> e.getKey()))
172-
.setExpandLevel(expandLevel);
173+
.setAutoExpandLevel(autoExpandLevel);
173174

174175
if (isComplete) {
175176
return new TmfModelResponse<>(treeModelBuilder.build(), ITmfResponse.Status.COMPLETED, CommonStatusMessage.COMPLETED);

tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/model/tree/TmfTreeModel.java

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**********************************************************************
2-
* Copyright (c) 2019 Ericsson
2+
* Copyright (c) 2019, 2025 Ericsson
33
*
44
* All rights reserved. This program and the accompanying materials are
55
* made available under the terms of the Eclipse Public License 2.0 which
@@ -31,16 +31,23 @@
3131
* @since 5.0
3232
*/
3333
public class TmfTreeModel<T extends ITmfTreeDataModel> {
34+
35+
/**
36+
* Expand level to indicate that all tree levels are expanded. (default)
37+
*
38+
* @since 9.6
39+
*/
40+
public static final int ALL_LEVELS = -1;
41+
3442
private List<String> fHeaders;
3543
private List<ITableColumnDescriptor> fColumnDescriptors;
3644
private List<T> fEntries;
3745
private @Nullable String fScope;
38-
/*
39-
* Indicates which level of the tree should be expanded. If expandLevel is missing or has value of -1 it
40-
* would mean that all the levels are expanded (default behaviour). If the expandLevel is set to 1 for
41-
* example, it will expand to the first nodes under the root.
46+
47+
/**
48+
* Indicates which level of the tree should be expanded.
4249
*/
43-
private int fExpandLevel = -1;
50+
private int fAutoExpandLevel = ALL_LEVELS;
4451

4552
/**
4653
* Constructor
@@ -94,7 +101,7 @@ private TmfTreeModel(Builder<T> builder) {
94101
fColumnDescriptors = builder.fColumnDescriptors;
95102
fEntries = builder.fEntries;
96103
fScope = builder.fScope;
97-
fExpandLevel = builder.fExpandLevel;
104+
fAutoExpandLevel = builder.fAutoExpandLevel;
98105
}
99106

100107
/**
@@ -126,17 +133,33 @@ public List<T> getEntries() {
126133
}
127134

128135
/**
129-
* @return
136+
* Returns the auto-expand level.
137+
*
138+
* @return non-negative level, or <code>ALL_LEVELS</code> if all levels of
139+
* the tree are expanded automatically
140+
* @see #setAutoExpandLevel
141+
* @since 9.6
130142
*/
131-
public int getExpandLevel() {
132-
return fExpandLevel;
143+
public int getAutoExpandLevel() {
144+
return fAutoExpandLevel;
133145
}
134146

135147
/**
136-
* @param expandLevel
148+
* Sets the auto-expand level to be used for the input of the tree. The
149+
* value 0 means that there is no auto-expand; 1 means that top-level
150+
* elements are expanded, but not their children; 2 means that top-level
151+
* elements are expanded, and their children, but not grand-children; and so
152+
* on.
153+
* <p>
154+
* The value {@link #ALL_LEVELS} means that all subtrees should be expanded.
155+
* </p>
156+
*
157+
* @param autoExpandLevel
158+
* The auto expand level to set
159+
* @since 9.6
137160
*/
138-
public void setExpandLevel(int expandLevel) {
139-
this.fExpandLevel = expandLevel;
161+
public void setAutoExpandLevel(int autoExpandLevel) {
162+
this.fAutoExpandLevel = autoExpandLevel;
140163
}
141164

142165
/**
@@ -152,7 +175,7 @@ public static class Builder<T extends ITmfTreeDataModel> {
152175
private List<ITableColumnDescriptor> fColumnDescriptors = new ArrayList<>();
153176
private List<T> fEntries;
154177
private @Nullable String fScope;
155-
private int fExpandLevel = -1;
178+
private int fAutoExpandLevel = ALL_LEVELS;
156179

157180
/**
158181
* Constructor
@@ -200,12 +223,14 @@ public Builder<T> setScope(String scope) {
200223
/**
201224
* Sets which level of the tree should be expanded
202225
*
203-
* @param expandLevel
226+
* @param autoExpandLevel
204227
* expand level of the tree model
205228
* @return this {@link Builder} object
229+
* @see TmfTreeModel#setAutoExpandLevel(int)
230+
* @since 9.6
206231
*/
207-
public Builder<T> setExpandLevel(int expandLevel) {
208-
fExpandLevel = expandLevel;
232+
public Builder<T> setAutoExpandLevel(int autoExpandLevel) {
233+
fAutoExpandLevel = autoExpandLevel;
209234
return this;
210235
}
211236

0 commit comments

Comments
 (0)