function fptoolcallback(event, modelHndl)
    %FPTOOLCALLBACK Responds to engine events in order to save the shortcuts in
    %an external file.
    
    %   Copyright 2011-2020 The MathWorks, Inc.
    
    
    % Update the FPTDatset mapping if saving under a new name which will change
    % the SID.
    bd = get_param(modelHndl,'Object');
    if (isempty(bd) || ~isa(bd, 'Simulink.BlockDiagram')), return; end
    
    switch lower(event)
        case 'presave'
            fptRepositoryInstance = fxptds.FPTRepository.getInstance;
            oldMdlName = fptRepositoryInstance.getModelNameFromHandle(modelHndl);
            
            newFile = get_param(modelHndl,'FileName');
            [~, newMdlName,~] = fileparts(newFile);
            
            if ~isempty(get_param(modelHndl,'MinMaxOverflowArchiveData'))
                sources = fptRepositoryInstance.getAllSources;
                % We need to reconstruct the SID based on new name
                for i = 1:length(sources)
                    curSource = sources{i};
                    idx = regexp(curSource,['^',regexptranslate('escape',oldMdlName),'$'],'once');
                    if isempty(idx)
                        idx = regexp(curSource,['^',regexptranslate('escape',oldMdlName),'\s*:\s*\d+','$'],'once');
                    end
                    % The source needs to be updated
                    if ~isempty(idx)
                        idx1 = regexp(curSource,':','start');
                        if ~isempty(idx1)
                            newSID = [newMdlName curSource(idx1:end)];
                        else
                            newSID = newMdlName;
                        end
                        if ~strcmp(curSource, newSID)
                            fptRepositoryInstance.updateSourceNameForDataset(curSource, newSID);
                            % The new source should match the dataset source
                            % strings. For Model references, the dataset source is the block SID.
                            fxptui.FPTModelSaveAsUtility.updateScopingEngine(curSource, newSID);
                        end
                    else
                        % check if this is a referenced model block that is pointing
                        % to a model that just got renamed.
                        if Simulink.ID.isValid(curSource)
                            blkHndl = Simulink.ID.getHandle(curSource);
                            blkObj = get_param(blkHndl, 'Object');
                            % This additional check only applies to model
                            % reference. All other cases should be resolved in the
                            % above 'if' branch.
                            if isa(blkObj,'Simulink.ModelReference')
                                modelName = get_param(blkHndl,'ModelName');
                                % If the model name points to the one being
                                % renamed, then clear out the results contained.
                                % This gives the same behavior as if the model it
                                % was pointing to was closed (since it it no longer
                                % in memory)
                                idx = regexp(modelName,['^',regexptranslate('escape',oldMdlName),'$'],'once');
                                if ~isempty(idx)
                                    topModel = Simulink.ID.getModel(curSource);
                                    topApplicationData = SimulinkFixedPoint.getApplicationData(topModel);
                                    if topApplicationData.subDatasetMap.isKey(blkHndl)
                                        subDataset = topApplicationData.subDatasetMap(blkHndl);
                                        runNames = subDataset.getAllRunNames();
                                        subDataset.clearResultsInRuns;
                                        
                                        for ridx = 1:numel(runNames)
                                            subDataset.cleanupForRunDeletion(runNames(ridx));
                                        end
                                        
                                    end
                                end
                            end
                        end
                    end
                end
            end
            
            fptInstance = fxptui.FixedPointTool.getExistingInstance;
            if ~isempty(fptInstance)
                topModel = fptInstance.getModelObject;
                shortcutMgr = fptInstance.getShortcutManager;
                % Respond to events only if the FPT object is valid.
                if isequal(topModel, bd)
                    % Update only if the model name is different.
                    if ~strcmpi(newMdlName, oldMdlName)
                        shortcutMgr.updateShortcutsForModelNameChange(bd, oldMdlName);
                    end
                    shortcutMgr.saveShortcuts;
                elseif ~isempty(oldMdlName)
                    % If the model being saved is a referenced model, then just
                    % edit the shortcut maps with the new name.
                    try
                        % @todo update the usage of edit-time filter filterOutCodeInactiveVariantSubsystemChoices()
                        % instead use the post-compile filter codeCompileVariants() - g2604331
                        [refMdls, ~]      = find_mdlrefs(topModel.getFullName ,'MatchFilter',@Simulink.match.internal.filterOutCodeInactiveVariantSubsystemChoices); % look only inside code active choice of VSS
                        % Update only if the modle name is different.
                        if ~strcmpi(newMdlName, oldMdlName) && any(ismember(refMdls,oldMdlName))
                            shortcutMgr.updateShortcutsForModelNameChange(bd, oldMdlName);
                        end
                    catch mdl_not_found_exception % Model not on path.
                        fxptui.showdialog('modelnotfound',mdl_not_found_exception);
                        return;
                    end
                end
            end
        otherwise
            % Do nothing.
    end
end

% [EOF]

% LocalWords:  presave FPT fxptui updatesiglogdata modelnotfound fxpopt
