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

    classdef AppController < ...
        appdesservices.internal.interfaces.controller.AbstractController & ...
        appdesservices.internal.interfaces.controller.AppDesignerParentingController & ...
        appdesigner.internal.appalert.AppAlertController
     
    % AppController is the controller for an App.
    
    % Copyright 2013-2016 The MathWorks, Inc.
    
    methods
        function obj = AppController(varargin)                                    
            obj = obj@appdesservices.internal.interfaces.controller.AbstractController(varargin{:});
            
            % construct the DesignTimeParentingController with the factory to
            % create model child objects
            factory = appdesigner.internal.model.AppChildModelFactory;
            obj = obj@appdesservices.internal.interfaces.controller.AppDesignerParentingController(factory);
        end
        
        function proxyView = createProxyView(obj, propertyPVPairs)
            % PROXYVIEW = CREATEPROXYVIEW(OBJ, PVPAIRS) This method is abstract
            % in the base class and creates the ProxyView class.
            
            % AppController is a DesignTimeParentingController so the proxyView
            % for it is constructed in the DesignTimeParentingController and
            % passed into this class via the model.  Need to overload
            % becuase it is abstract
        end
    end
    
    methods(Access = 'protected')
        
        function propertyNames = getAdditionalPropertyNamesForView(obj)
            % Additional properties needed by the App Controller
            propertyNames = {'Name', 'FullFileName', 'IsLaunching', 'IsDebugging'};
        end
        
        function pvPairsForView = getPropertiesForView(obj, propertyNames)
            % GETPROPERTIESFORVIEW(OBJ, PROPERTYNAMES) gets the properties that
            % will be needed by the view when properties change.
            %
            % Inputs:
            %
            %  propertyNames - a cell array of strings containing the names
            %                  of the property names
            
            % Right now, there are no App-related properties that need
            % specific conversion
            pvPairsForView = {};
        end        
        
        function handleEvent(obj, source, event)
            % HANDLEEVENT(OBJ, SOURCE, EVENT) This method receives event from
            % ProxyView class each time a user interacts with the visual
            % representation through mouse or keyboard. The controller sets
            % appropriate properties of the model each time it receives
            % these events.
            %
            % Inputs:
            %
            %   source  - object generating event, i.e ProxyView class object.
            %
            %   event   - the event data that is sent from the ProxyView. The
            %             data is translated to property value of the model.
            
            % right now, there are no App related events
            
            
            switch ( event.Data.Name )       
                
                case 'AppModelClosed'
                    % when an App is closed at the client, the client sends this event
                    % so the server-side MCOS figure  object
                    % can be deleted immediately.
                    delete(obj.Model.CodeData);
                    delete(obj.Model.UIFigure);

                case 'saveApp'
                    obj.handleSaveApp(event);
                    
                case 'runApp'
                    % For Save, the CodeModel update must be handled before
                    % saving
                    
                    fileName = event.Data.FullFileName;
                    runApp(obj.Model, fileName);
                    
                case 'openPackageApp'
                    fullFileName = event.Data.FullFileName;
                    callbackId = event.Data.CallbackId;
                    obj.handleOpenPackageApp(fullFileName, callbackId); 
                    
                case 'ping'
                    obj.handlePing(event);
            end
            
        end
        
        function handlePing(obj, event)
            % HANDLEPING(obj, event) send result back to the client
            
            % Send response to client side of the result
            obj.ProxyView.sendEventToClient('pingResult', { ...
                'CallbackId', event.Data.CallbackId, ...
                });
        end
        
        function handleSaveApp(obj, event)
            % HANDLESAVEAPP(obj, event) save app and send result back to
            % the client
            
            % Initialize the status output
            status = 'success';
            message = '';
            fullFileName = event.Data.FullFileName;
            try
                % Save the AppModel to the filename
                save(obj.Model, event.Data.FullFileName);
                
                % Store the FullFileName assigned to App after saving
                fullFileName = obj.Model.FullFileName;
            catch me
                % Return the Status and Message to be used in the error
                % dialog
                status = 'error';
                message = me.message;
            end

            % Send response to client side of the result
            obj.ProxyView.sendEventToClient('saveAppResult', {
                'Status', status, ...
                'FullFileName', fullFileName, ...
                'CallbackId', event.Data.CallbackId, ...
                'Message', message});
        end

        function handleOpenPackageApp(obj, fullFileName, callbackId)
            % HANDLEOPENPACKAGEAPP(obj, fullFileName, callbackId) package 
            % app and send result back to the client

            % Initialize the status output
            status = 'success';
            errorMessage = '';
            titleMessage = '';
            try
                % package the AppModel for the app saved in fullFileName
                openPackageApp(obj.Model, fullFileName);
            catch me
                % Return the Status, Message and MessageTitle to be used in
                % the error dialog. The MessageTitle is used for the title 
                % of the dialog window.
                status = 'error';
                errorMessage = me.message;
                titleMessage = message('MATLAB:appdesigner:appdesigner:PackageAppError').getString();
            end

            % Send response to client side of the result
            obj.ProxyView.sendEventToClient('openPackageAppResult', {
                'Status', status, ...
                'FullFileName', fullFileName, ...
                'CallbackId', callbackId, ...
                'Message', errorMessage, ...
                'MessageTitle', titleMessage});
        end
        
        function 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
            
            message = appException.getReport('extended', 'hyperlinks','off');
            line = appException.ErrorLineInApp;
            obj.ProxyView.sendEventToClient('runningAppCallbackError',...
                {'line', line, 'message', message});
        end
    end
    
     methods (Access = { ...
            ?appdesservices.internal.interfaces.controller.AbstractController,...
            ?appdesservices.internal.interfaces.controller.AbstractControllerMixin,...
            ?appdesservices.internal.interfaces.model.AbstractModel, ...
            ?appdesservices.internal.interfaces.controller.DesignTimeParentingController,...
            ?appdesigner.AppDesignEnvironment})
        
        function postProcess(~, ~, ~)
           % implements appdesservices.internal.interfaces.controller.PostProcessEventHandler
        end
    end
end