www.gusucode.com > appdesigner工具箱matlab源码程序 > appdesigner/+appdesigner/+internal/+appalert/AppAlertController.m

    classdef AppAlertController < handle
    %APPALERTCONTROLLER Mixin controller class to send app startup and
    %run time alerts to the client.
    
    % Copyright 2014 - 2015 The MathWorks, Inc.  
    
    properties(Access = protected)
         % Listen to 'CallbackErrored' events from AppBase
        CallbackErroredListener
    end
    
    methods(Abstract, Access = protected)
        doSendErrorAlertToClient(obj, appException)
            % DOSENDERRORALERTTOCLIENT(obj, appException) sends app
            % startup/run-time error information to the client.
            %
            % Inputs:
            %
            % appException - a decorated MException with a truncated stack
    end
    
    methods
        
        function sendErrorAlertToClient(obj, mException, appFullFileName)
            % SENDERRORALERTTOCLIENT(obj, mException, appFullFileName)
            % Creates an AppException based on the MException that the
            % app threw upon instantiation or callback execution and
            % sends it to the client.
            %
            % Inputs:
            %
            % mException - MException thrown by the running app
            %
            % appFullFileName - The full file name of the running app
            
            appException = appdesigner.internal.appalert.AppException(...
                mException, appFullFileName);
            obj.doSendErrorAlertToClient(appException);
        end
        
        function addErrorAlertListener(obj, appModel)
            % ADDERRORALERTLISTENER(obj, appModel)
            % Adds a listener for when a run time error occurs in the app.
            % 
            % Inputs:
            %
            % appModel - The AppModel of the running app
            if isempty(obj.CallbackErroredListener)
                obj.CallbackErroredListener = ...
                    addlistener(appdesigner.internal.service.AppManagementService.instance(), 'CallbackErrored',...
                    @(src,event)handleCallbackErrored(obj, src, event, appModel));
            end
        end
        
        function delete(obj)
            % DELETE(OBJ) delete the controller.
            %
            % Cleans up the listener
            
            if ~isempty(obj.CallbackErroredListener)
                delete(obj.CallbackErroredListener);
            end
        end
    end
    
    methods(Access = private)
        function handleCallbackErrored(obj, src, event, appModel)
            % Private handler for all events being fired from a running app
            appFullFileName = appModel.FullFileName;
            if strcmp(event.AppFullFileName, appFullFileName)
                obj.sendErrorAlertToClient(event.Exception, appFullFileName);
            end
        end
    end
end