function testbenchCallback(hObj, hDlg, tag, action, hTopObj) %#ok
%TESTBENCHCALLBACK - Callback for the TB dialog.

%   Author(s): J. SUN
%   Copyright 2005-2023 The MathWorks, Inc.

% Check if the Configuration Parameters dialog has unapplied changes, if
% so, prompt the user.  See geck: 284913
try
    commitBuild = slprivate('checkSimPrm', hTopObj.getSourceObject.getConfigSet);
    if commitBuild
        src = hDlg.getSource;
        if isa(src, 'configset.dialog.HTMLView')
            src = src.Source.Source;
        end
        hdlcc = gethdlcconfigset(src);

        % This has to be locked explicitly, as this is the contained object
        % by hdlcc.
        if ishandle(hdlcc)
            % Lock it only if dialog is available
            hdlcc.lock;
        end
        
        try
            isWebDialog = isa(hDlg.getDialogSource, 'configset.dialog.HTMLView');
            web = ConfigSet.DDGWrapper(hDlg);
            cs = getActiveConfigSet(hdlcc.getModelName);
            if isWebDialog
                web.disableDialog;
            else
                cs.ReadOnly = 'on';
                cs.refreshDialog;
            end
            hdriver = hTopObj.getHDLCoder;
            hdriver.makehdltb({'HDLSubsystem', hdlcc.CLI.HDLSubsystem});
            if isWebDialog
                web.enableDialog;
            else
                cs.ReadOnly = 'off';
                cs.refreshDialog;
            end
        catch e
            % enable the dialog on failure
            if isWebDialog
                web.enableDialog;
            else
                cs.ReadOnly = 'off';
                cs.refreshDialog;
            end
            % update code to use new message viewer instead of the simulink viewer.
            Simulink.output.Stage('HDLCoder', 'ModelName', gcs(), 'UIMode', true);
            Simulink.output.error( e,'Component','HDLCoder','Category','HDL');
        end
        
        if ishandle(hdlcc)
            %Unlock it only if dialog is available
            hdlcc.unlock;
        end
    end
catch e
    % update code to use new message viewer instead of the simulink viewer.
    Simulink.output.Stage('HDLCoder', 'ModelName', gcs(), 'UIMode', true);
    Simulink.output.error( e,'Component','HDLCoder','Category','HDL');
end
end
