function [success,msg] = dialogCallback(this, hDlg, action) 
%DIALOGCALLBACK - Callback for the FD HDLTool dialog.

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

success = true;
msg = '';
hCLI = this.CLIProperties;

switch action
    case 'Help'    
        this.help;
    case 'LanguageChanged'
        tbobj = this.getsubcomponent(DAStudio.message('HDLShared:hdldialog:hdltbComponentName'));
        if ~isempty(tbobj)
            tbobj.TestbenchLanguage = this.TargetLanguage; 
        end
        
        hCLI.TargetLanguage = this.TargetLanguage;
        
        if strcmpi(this.TargetLanguage, 'VHDL')
            hDlg.setWidgetValue([this.getproductname, 'TestBenchPanel_TestbenchLanguage'], 0);            
        else
            hDlg.setWidgetValue([this.getproductname, 'TestBenchPanel_TestbenchLanguage'], 1);
        end
    case 'Generate'
        % Some of the dialog properties are not source from the CLI object.
        % they are stored as class properties of various dialogs. These
        % need to be pushed into the CLI before any action is taken
        this.copyDialogPropsToCLI;
        this.hdlFilter.HDLParameters.CLI = this.CLI;
        
        % This is the workaround for over translation known issue. The
        % refresh will make all translated strings back to normal
        hDlg.refresh;
        
        hDlg.apply;            
        % Added since DDG no longer invoke Apply callback inside callback
        % loop g585939
        dialogCallback(this, hDlg, 'Apply');
        
        %         this.getparam;
        if this.modifyForUSRP2filter
            this.CLIProperties.InputComplex = 'on';
            this.CLIProperties.AddRatePort = 'on';
            this.CLIProperties.ResetType = 'Synchronous';
            this.CLIProperties.ResetAssertedLevel = 'Active-high';
        end
        for i = 1:length(this.SubComponents)
            if isa(this.SubComponents(i), 'tdkfpgacc.fdhdlfpga') % fpga automation tab
                this.SubComponents(i).getparam(this, this.CLIProperties);
                % this should happen in the getparam above but the
                % changes have been held back to prevent a bad merge
                this.CLIProperties.FPGAWorkflowParameters = this.SubComponents(i).FPGAProperties;
            else
                this.SubComponents(i).getparam(this.CLIProperties);
            end
        end
        
        % Generate HDL
        hDlg.setEnabled('Filter_HDL_Dialog_Main_GenerateButton', false); % Disable the generate button
        try
            this.hdlFilter.setupCBSSetting(this.filterobj);
            % generates HDL
            % fix for cascade - all should be same as the top for now
            if isa(this.hdlFilter, 'hdlfilter.abstractmultistage')
                topprop = this.hdlFilter.HDLParameters;
                for n = 1:length(this.hdlFilter.Stage)
                    this.hdlFilter.Stage(n).HDLParameters = copyobj(topprop);
                end
            end
            if isa(this.hdlFilter, 'hdlfilter.usrp2')
                topprop = this.hdlFilter.HDLParameters;
                % for rx
                rx = this.hdlFilter.RxChain;
                rx.HDLParameters = copyobj(topprop);
                for n = 1:length(rx.Stage)
                    rx.Stage(n).HDLParameters = copyobj(topprop);
                end
                tx = this.hdlFilter.TxChain;
                tx.HDLParameters = copyobj(topprop);
                for n = 1:length(tx.Stage)
                    tx.Stage(n).HDLParameters = copyobj(topprop);
                end
            end

            % Adding PersistentHDLResource to allow us to provide ACTUAL (post emit) number of
            % multipliers in the code comments, rather than based on estimate (often incorrect)
            % before filter is emitted
            resrc = struct('comp', this.filterobj,...
                'bom',containers.Map(),...
                'numInst',1);
            PersistentHDLResource(resrc);

            this.hdlFilter.generatehdlcode(this.filterobj);
            tbobj = this.getsubcomponent(DAStudio.message('HDLShared:hdldialog:hdltbComponentName'));
            % generates Testbench if different language is selected
            if  ~strcmpi(hCLI.TargetLanguage, tbobj.TestbenchLanguage) && ...
                    strcmpi(hCLI.GenerateHDLTestbench, 'on')
                warning(message('HDLShared:directemit:mixedLanguageTBdeprecated'));
                if isa(this.filterobj, 'filtergroup.usrp2')
                    generatetb(this.filterobj, tbobj.TestbenchLanguage)
                else
                    privgeneratetb(this.filterobj, tbobj.TestbenchLanguage); %, 'Testbenchname', hCLI.TestbenchName);
                end
            end
        catch e
            if isempty(e.message)
                % This is in the case ctr-c is hit during hdl generation
                msg =  DAStudio.message('HDLShared:fdhdldialog:AbnormalTermination');
            else
                msg = e.message;
            end
            errordlg(cleanerrormsg(msg), DAStudio.message('HDLShared:fdhdldialog:GenerationTitle'), 'modal');
%             success = false;
            hDlg.setEnabled('Filter_HDL_Dialog_Main_GenerateButton', true); % Enable the generate button
            return;
        end                
                
        hDlg.setEnabled('Filter_HDL_Dialog_Main_GenerateButton', true); % Enable the generate button
        
        if this.generateMatlabFile
            if this.modifyForUSRP2filter
                matlabFileGeneration(this, []);
            else
                if tbobj.GenerateTestBench == true
                    matlabFileGeneration(this, {tbobj.TestbenchLanguage});
                else
                    matlabFileGeneration(this, []);
                end
            end
        end
        
    case 'Architecture'
        
    case 'Apply'        
        if this.hArchit.AddPipelineRegisters, val = 'on';
        else, val = 'off'; end
        hCLI.AddPipelineRegisters = val;
        tbobj = this.getsubcomponent(DAStudio.message('HDLShared:hdldialog:hdltbComponentName'));
        if ~isempty(tbobj)
            enumType = findtype('FracDelayStimulusStringType');
            if strcmpi(tbobj.TestBenchFracDelayStimulusString, enumType.string(1))
                this.CLIProperties.TestBenchFracDelayStimulus = '';
            elseif strcmpi(tbobj.TestBenchFracDelayStimulusString, enumType.string(2))
                this.CLIProperties.TestBenchFracDelayStimulus = 'RampSweep';
            elseif strcmpi(tbobj.TestBenchFracDelayStimulusString, enumType.string(3))
                this.CLIProperties.TestBenchFracDelayStimulus = 'RandSweep';
            elseif strcmpi(tbobj.TestBenchFracDelayStimulusString, enumType.string(4))
                try
                    temp = evaluatevars(tbobj.TestBenchFracDelay_User_Stimulus);
                    if ~isempty(temp)
                        this.CLIProperties.TestBenchFracDelayStimulus = temp;
                    end
                catch e
                    errordlg(e.message, 'Error');
                end
            end        
            % For cosim model type
            if tbobj.CosimModel == true
                if strcmpi(tbobj.CosimModelType, 'Mentor Graphics ModelSim')
                    this.CLIProperties.GenerateCoSimModel = 'ModelSim';
                else
                    this.CLIProperties.GenerateCoSimModel = 'Incisive';
                end
            else
                this.CLIProperties.GenerateCoSimModel = 'None';
            end
        end                            
                 
        this.CLIProperties.TargetLanguage = this.TargetLanguage;
        this.CLIProperties.FIRAdderStyle = lower(this.hArchit.FIRAdderStyle);
        
        % FPGA workflow tab apply time callback
        fpgatab = this.getsubcomponent(DAStudio.message('HDLShared:fdhdldialog:fdhdlfpgaComponentName'));
        if ~isempty(fpgatab)
            fpgatab.sourceToFpgaProp; % transfer table data to source
        end
        
        % when applied, clear cache if any
        if ~isempty(this.TargetLanguage_bk)
            this.TargetLanguage_bk = '';
            this.InputComplex_bk = '';
            this.AddRatePort_bk = '';
            this.ResetType_bk = '';
            this.ResetAssertedLevel_bk =  '';
            
            tbobj = this.getsubcomponent(DAStudio.message('HDLShared:hdldialog:hdltbComponentName'));
            tbobj.TestbenchLanguage_bk = '';
            
            glblobj = this.getsubcomponent('Global Settings');
            glblobj.InputComplex_bk = '';                        
        end
        
    case 'Close' %called once for cancel, twice for ok         
        if ~isempty(this.hArchit.webdlgHandle) && ishandle(this.hArchit.webdlgHandle)
            delete(this.hArchit.webdlgHandle);
        end
        hDlg.delete;
        send(this,'CloseDialog',handle.EventData(this,'CloseDialog'));
                
    case 'Browse'        
        tdir = uigetdir('','Browse for folder');
        if tdir ~= 0
            this.CLIProperties.TargetDirectory = tdir;            
        end
    
end

% [EOF]

% LocalWords:  hdltb tdkfpgacc fdhdlfpga TBdeprecated Testbenchname fdhdldialog
