Skip to content

Commit 78fcb3b

Browse files
committed
work on ListSelector situation when items are deleted, selection should retain
1 parent 4ee8844 commit 78fcb3b

File tree

2 files changed

+120
-16
lines changed

2 files changed

+120
-16
lines changed

test/+wt/+test/ListSelector.m

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
classdef ListSelector < wt.test.BaseWidgetTest
22
% Implements a unit test for a widget or component
33

4-
% Copyright 2020-2025 The MathWorks Inc.
4+
% Copyright 2020-2025 The MathWorks Inc.
55

66
%% Properties
77
properties
@@ -19,10 +19,10 @@ function createFigure(testCase)
1919
testCase.createFigure@wt.test.BaseWidgetTest();
2020

2121
% Adjust grid size
22-
testCase.Figure.Position(3:4) = [500 700];
23-
testCase.Grid.RowHeight = repmat({175},1,3);
22+
testCase.Figure.Position(3:4) = [800 700];
23+
testCase.Grid.RowHeight = repmat({175},1,4);
2424
testCase.Grid.ColumnWidth = {'1x','1x','1x'};
25-
25+
2626
end %function
2727

2828
end %methods
@@ -47,7 +47,7 @@ function setup(testCase)
4747

4848
%% Unit Test
4949
methods (Test)
50-
50+
5151
function testProgrammaticItemsValueSelection(testCase)
5252

5353
% Get the listbox
@@ -67,7 +67,7 @@ function testProgrammaticItemsValueSelection(testCase)
6767
testCase.verifyEqual(testCase.Widget.SelectedIndex, newSelIdx);
6868

6969
end %function
70-
70+
7171

7272
function testProgrammaticItemsDataValueSelection(testCase)
7373

@@ -92,7 +92,7 @@ function testProgrammaticItemsDataValueSelection(testCase)
9292

9393
end %function
9494

95-
95+
9696

9797
function testHighlightedValue(testCase)
9898

@@ -116,7 +116,49 @@ function testHighlightedValue(testCase)
116116
end %function
117117

118118

119+
120+
function testHighlightedValueOnListRemoval(testCase)
121+
122+
% Get the listbox
123+
listControl = testCase.Widget.ListBox;
124+
125+
% Add items to the list
126+
newSelIdx = [1 2 4];
127+
newValue = testCase.ItemNames(newSelIdx);
128+
testCase.verifySetProperty("Value", newValue);
129+
testCase.verifyEqual(testCase.Widget.SelectedIndex, newSelIdx);
130+
131+
% Highlight a value in the list
132+
newHiliteIdx = [1 2 4];
133+
newHilite = testCase.ItemNames(newHiliteIdx);
134+
testCase.verifySetProperty("HighlightedValue", newHilite);
135+
136+
% Remove items from list
137+
testCase.Widget.Items(5) = [];
119138

139+
% List selection value should match
140+
expValue = testCase.ItemNames([1 2 4]);
141+
testCase.verifyEqual(string(listControl.Items), expValue);
142+
143+
% List selection highlight should match
144+
expHighlight = testCase.ItemNames([1 2 4]);
145+
testCase.verifyEqual(testCase.Widget.HighlightedValue, expHighlight);
146+
147+
% Remove items from list
148+
testCase.Widget.Items(2) = [];
149+
150+
% List selection value should match
151+
expValue = testCase.ItemNames([1 4]);
152+
testCase.verifyEqual(string(listControl.Items), expValue);
153+
154+
% List selection highlight should match
155+
expHighlight = testCase.ItemNames([1 4]);
156+
testCase.verifyEqual(testCase.Widget.HighlightedValue, expHighlight);
157+
158+
end %function
159+
160+
161+
120162
function testInteractiveSelection(testCase)
121163

122164
% Get the listbox
@@ -146,7 +188,7 @@ function testInteractiveSelection(testCase)
146188
end %function
147189

148190

149-
191+
150192
function testButtonEnables(testCase)
151193

152194
% Get the listbox and button grid
@@ -205,7 +247,7 @@ function testButtonEnables(testCase)
205247
end %function
206248

207249

208-
250+
209251
function testButtonFunctions(testCase)
210252

211253
% Get the listbox and button grid
@@ -265,22 +307,22 @@ function testButtonFunctions(testCase)
265307

266308
% Verify new highlight
267309
testCase.verifyEqual(w.HighlightedValue, testCase.ItemNames(1));
268-
310+
269311
% Verify button enables
270312
testCase.verifyEquality(buttonGrid.ButtonEnable(2:4), [1 0 1]);
271313

272314
end %function
273315

274316

275-
317+
276318
function testUserButtons(testCase)
277319

278320
% Get the widget
279321
w = testCase.Widget;
280322

281323
% Add User buttons
282324
w.UserButtons.Icon = ["plot_24.png","play_24.png"];
283-
drawnow
325+
drawnow
284326

285327
% Press the buttons
286328
b = w.UserButtons.Button;
@@ -291,7 +333,7 @@ function testUserButtons(testCase)
291333
end %function
292334

293335

294-
336+
295337
function testStyleProperties(testCase)
296338

297339
% Set ButtonWidth

widgets/+wt/ListSelector.m

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,66 @@
4141

4242
end %properties
4343

44+
methods
45+
46+
function set.Items(obj, newItems)
47+
48+
% Get original items and highlight
49+
oldValue = obj.Value; %#ok<MCSUP>
50+
oldHighlight = obj.HighlightedValue; %#ok<MCSUP>
51+
52+
% What's currently selected?
53+
oldLBHighlight = obj.getListBoxSelectedIndex();
54+
55+
% Retain the matching original value selection
56+
isPresent = ismember(oldValue, newItems);
57+
newValue = oldValue(isPresent);
58+
59+
% Retain the matching original highlight indices
60+
isPresentIdx = find(isPresent);
61+
newHighlightIdx = intersect(oldLBHighlight, isPresentIdx, 'stable');
62+
63+
% In case items were removed, we can back up to the highlight
64+
% values
65+
isPresent = ismember(oldHighlight, newItems);
66+
newHighlight = oldHighlight(isPresent);
67+
68+
% Set new items
69+
obj.Items = newItems;
70+
% newValue = intersect(oldValue, value, 'stable')
71+
72+
% Set selection and highlight for consistency
73+
% newHighlight = intersect(oldHighlightValue, value, 'stable')
74+
obj.Value = newValue; %#ok<MCSUP>
75+
% obj.HighlightedValue = newHighlight; %#ok<MCSUP>
76+
77+
% Set highlight for consistency
78+
if obj.AllowDuplicates %#ok<MCSUP>
79+
% If duplicates allowed, it can be inconsistent, so must
80+
% deselect all instead
81+
obj.HighlightedValue = []; %#ok<MCSUP>
82+
else
83+
% This is the best guess we can make
84+
obj.HighlightedValue = newHighlight; %#ok<MCSUP>
85+
end
86+
87+
% elseif isempty(newHighlightIdx)
88+
%
89+
% % Set empty selection
90+
% obj.ListBox.ValueIndex = []; %#ok<MCSUP>
91+
%
92+
% else
93+
%
94+
% % Keep the same indices selected
95+
% obj.ListBox.ValueIndex = newHighlightIdx; %#ok<MCSUP>
96+
%
97+
% end
98+
99+
end
100+
101+
102+
end %methods
103+
44104

45105
%% Public dependent properties
46106
properties (AbortSet, Dependent)
@@ -77,10 +137,12 @@
77137
end
78138

79139
function value = get.Value(obj)
140+
itemIdx = obj.ListBox.ItemsData;
141+
itemIdx(itemIdx > numel(obj.Items)) = [];
80142
if isempty(obj.ItemsData)
81-
value = obj.Items(:,obj.ListBox.ItemsData);
143+
value = obj.Items(:, itemIdx);
82144
else
83-
value = obj.ItemsData(:,obj.ListBox.ItemsData);
145+
value = obj.ItemsData(:, itemIdx);
84146
end
85147
end
86148

@@ -126,7 +188,7 @@
126188
[~, obj.ListBox.Value] = ismember(value, obj.ItemsData);
127189
end
128190
end
129-
191+
130192

131193
function value = get.ButtonWidth(obj)
132194
value = obj.Grid.ColumnWidth{2};

0 commit comments

Comments
 (0)