function updateInputTable(block, button)
% Callback for the subsystem mask's input table. Process mouseclicks on the
% editing buttons. button can be 'New', 'Delete', 'Up', 'Down', or empty
% which is ignored.
%

% Copyright 2023 The MathWorks, Inc.


maskObj = Simulink.Mask.get(block);
tableControl = maskObj.getDialogControl( 'InputTable' );
% Process editing buttons
switch button
    case 'New'
        rowName = findNewName(tableControl, 'In');
        tableControl.addRow( rowName, 'float32', '', 'inherit' )
    case 'Delete'
        if tableControl.getNumberOfRows() > 1
            rowIndex = tableControl.getSelectedRows();
            if ( ~isempty(rowIndex) )
                tableControl.removeRow( rowIndex(1) );
            end
        end
    case 'Up'
        rowIndex = tableControl.getSelectedRows();
        if ~isempty(rowIndex) && rowIndex>1
            tableControl.swapRows( rowIndex(1)-1, rowIndex(1) );
        end
    case 'Down'
        rowIndex = tableControl.getSelectedRows();
        if ~isempty(rowIndex) && rowIndex<tableControl.getNumberOfRows()
            tableControl.swapRows( rowIndex(1)+1, rowIndex(1) );
        end
    otherwise
        % Ignore
end
end

function inputName = findNewName(tableControl, Stem)
% Find the first unused name of the for <Stem><num>.
numRows = tableControl.getNumberOfRows();
for row = numRows:-1:1
    currentNames{row} = tableControl.getValue([row 1]);
end
for row = 1:numRows
    inputName = [Stem, num2str(row)];
    if ~ismember(inputName, currentNames)
        return;
    end
end
inputName = [Stem, num2str(numRows+1)];
end
