function [success, errmsg] = hdlconfigDlgAction(hDlg, hObj, action, page) %#ok
%HDLCONFIGDLGACTION   HDL Coder action function (used by config. param dlg)

% This file is similar to
% ...\simulink\simulink\private\configDlgAction.m(lx)
% It was added as a workaround to 277506 as described in 280752.

%   Author(s): J. Sun
%   Copyright 2005-2022 The MathWorks, Inc.
%     
success = true;
errmsg = '';
% No action is needed if HDL coder is not installed.
% if ~hdlcoderui.isslhdlcinstalled
if ~hObj.hdlinstalled
    return;
end

if strcmp(action, 'apply')
    try
        % Validate new target settings. If any CLI settings are invalid,
        % two things may happen:
        % (1) If there are no hard validation errors, we will warn about
        % the invalid settings and what they are overriden to
        % (2) If there are hard validation errors, we will error out and
        % not apply the settings to the model
        hObj.runPostTargetValidation;
        hObj.closeTransactionOnDownstreamIntegrationDriver;

        % Apply settings to model
        runDialogApplyCallback(hObj);
    catch ME
        success = false;
        errmsg = ME.message;
    end
elseif strcmp(action, 'cancel')
    % Newer settings in the configset were canceled, so apply
    % parameters from the model back to the downstream integration driver
    % to sync it with the correct settings
    hObj.revertDownstreamIntegrationDriver;
    hObj.closeTransactionOnDownstreamIntegrationDriver;
end

