1+ classdef ListSelection < wt .abstract .BaseInternalDialog & ...
2+ wt .mixin .FieldColorable & ...
3+ wt .mixin .FontStyled
4+ % Implements a simple selection from a list dialog (similar to listdlg)
5+
6+ % Copyright 2025 The MathWorks Inc.
7+
8+
9+ %% Public properties
10+ properties (AbortSet )
11+
12+ end % properties
13+
14+
15+ %% Public dependent properties
16+ properties (Dependent )
17+
18+ % Prompt text
19+ Prompt (1 ,1 ) string
20+
21+ % List of items to add to the list
22+ Items (1 ,: ) string
23+
24+ % Data associated with items (optional)
25+ ItemsData (1 ,: )
26+
27+ % Allow multi-select?
28+ Multiselect
29+
30+ % The current selection
31+ Value (1 ,: )
32+
33+ end % properties
34+
35+
36+ properties (Dependent , SetAccess = immutable )
37+
38+ % Indices of displayed items that are currently added to the list
39+ ValueIndex (1 ,: )
40+
41+ end % properties
42+
43+
44+ methods
45+
46+ function value = get .Prompt(obj )
47+ value = string( obj .PromptLabel .Text );
48+ end
49+
50+ function set .Prompt(obj ,value )
51+ obj.PromptLabel.Text = value ;
52+ end
53+
54+ function value = get .Items(obj )
55+ value = string( obj .ListBox .Items );
56+ end
57+
58+ function set .Items(obj ,value )
59+ obj.ListBox.Items = value ;
60+ end
61+
62+ function value = get .ItemsData(obj )
63+ value = obj .ListBox .ItemsData ;
64+ end
65+
66+ function set .ItemsData(obj ,value )
67+ obj.ListBox.ItemsData = value ;
68+ end
69+
70+ function value = get .Value(obj )
71+ value = obj .ListBox .Value ;
72+ if isempty(obj .ListBox .ItemsData )
73+ value = string(value );
74+ end
75+ end
76+
77+ function set .Value(obj ,value )
78+ obj.ListBox.Value = value ;
79+ end
80+
81+ function value = get .ValueIndex(obj )
82+ value = obj .getListBoxSelectedIndex();
83+ end
84+
85+ function value = get .Multiselect(obj )
86+ value = obj .ListBox .Multiselect ;
87+ end
88+
89+ function set .Multiselect(obj ,value )
90+ obj.ListBox.Multiselect = value ;
91+ end
92+
93+ end % methods
94+
95+
96+ %% Internal Properties
97+ properties (Transient , NonCopyable , Hidden , SetAccess = private )
98+
99+ PromptLabel matlab.ui.control.Label
100+
101+ ListBox matlab.ui.control.ListBox
102+
103+ end % properties
104+
105+
106+ %% Protected methods
107+ methods (Access = protected )
108+
109+ function setup(obj )
110+ % Configure the widget
111+
112+ % Defaults
113+ obj.Size = [300 ,300 ];
114+
115+ % This is normally a modal dialog
116+ obj.Modal = true ;
117+
118+ % Configure which actions close the dialog
119+ obj.DeleteActions = [" close" ," ok" ," cancel" ];
120+
121+ % Call superclass method
122+ obj .setup @wt .abstract .BaseInternalDialog();
123+
124+ % Add buttons
125+ obj.DialogButtonText = [" OK" ," Cancel" ];
126+ obj.DialogButtonTag = [" ok" ," cancel" ];
127+ obj.DialogButtonEnable = [true , true ];
128+
129+ % Configure grid
130+ obj.Grid.RowHeight = {' fit' ,' 1x' };
131+ obj.Grid.ColumnWidth = {' 1x' };
132+
133+ % Set title
134+ obj.Title = " " ;
135+
136+ % Add controls
137+ obj.PromptLabel = uilabel(obj .Grid );
138+ obj.PromptLabel.Text = " " ;
139+
140+ obj.ListBox = uilistbox(obj .Grid );
141+ obj.ListBox.ValueChangedFcn = @(~,~)onValueChanged(obj );
142+
143+ % Update component lists
144+ % obj.BackgroundColorableComponents = [obj.Grid]
145+ obj.FieldColorableComponents = [obj .ListBox ];
146+ obj.FontStyledComponents = [obj .PromptLabel , obj .ListBox ];
147+
148+ end % function
149+
150+
151+ function update(obj )
152+
153+ % Configure list
154+ obj.ListBox.Items = obj .Items ;
155+ obj.ListBox.ItemsData = obj .ItemsData ;
156+
157+ % Check if OK button can be enabled
158+ obj .updateButtonEnables();
159+
160+ end % function
161+
162+
163+ function updateButtonEnables(obj )
164+
165+ % enable OK button if a value is selected
166+ hasSelection = ~isempty(obj .Value );
167+ % hasPass = strlength(obj.PasswordField.Value) > 0;
168+ obj .DialogButtonEnable(1 ) = hasSelection ;
169+
170+ end % function
171+
172+
173+ function onValueChanged(obj )
174+
175+ % Check if Login button can be enabled
176+ obj .updateButtonEnables();
177+
178+ end % function
179+
180+
181+ function assignOutput(obj )
182+
183+ % Assign output
184+ if obj .LastAction == " ok"
185+ output.Value = obj .Value ;
186+ output.ValueIndex = obj .ValueIndex ;
187+ else
188+ output.Value = [];
189+ output.ValueIndex = [];
190+ end
191+
192+ obj.Output = output ;
193+
194+ end % function
195+
196+
197+ function selIdx = getListBoxSelectedIndex(obj )
198+ % Get the current selected row indices in the listbox
199+
200+ if isMATLABReleaseOlderThan(" R2023b" )
201+ warnState = warning(' off' ,' MATLAB:structOnObject' );
202+ s = struct(obj .ListBox );
203+ warning(warnState );
204+ selIdx = s .SelectedIndex ;
205+ if isequal(selIdx , - 1 )
206+ selIdx = [];
207+ end
208+ else
209+ selIdx = obj .ListBox .ValueIndex ;
210+ end
211+
212+ end % function
213+
214+ end % methods
215+
216+ end % classdef
0 commit comments