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

    classdef CodeDataController < appdesservices.internal.interfaces.controller.AbstractController
    %CodeDataController Controller for the code model data
    
    % Copyright 2015-2016 The MathWorks, Inc.
    
    methods
        %------------------------------------------------------------------
        
        function obj = CodeDataController(model, proxyView)
            % constructor for the controller
            obj = obj@appdesservices.internal.interfaces.controller.AbstractController(model, [], proxyView);
            
            % notify the client that this controller has been
            % instantiated. Some code generation activity has already
            % occured by the time this is instantiated.
            proxyView.sendEventToClient('codeDataControllerCreated', {});
        end
        %------------------------------------------------------------------
        
        function createProxyView(~, ~)
            % No-Op implemented for Base Class
        end
        %------------------------------------------------------------------
    end
    
    methods(Access = protected)
        %------------------------------------------------------------------
        
        function handleEvent(obj, ~, event)
            % handler for peer node events from the client
            switch event.Data.Name
                case 'callbackAdded'
                    obj.addCallback(event.Data.CallbackData);
                    
                case 'callbackUpdated'
                    obj.updateCallback(event.Data.CallbackData);
                    
                case 'callbackRemoved'
                    obj.removeCallback(event.Data.CallbackData);
                    
                case 'editableSectionCodeChanged'
                    obj.handleEditableCodeChanged(event.Data.CodeData);
                    
            end
        end
        %------------------------------------------------------------------
        
        function unhandledProperties = handlePropertiesChanged(obj, changedPropertiesStruct)
            % handles setting the generated code from the client to the
            % server
            
            
            % check the generated code against code serialized in a file to
            % check if the app code should be dirty. This will not be
            % checked for new apps where their is no file code
            if(isfield(changedPropertiesStruct, 'GeneratedCode'))
                
                isDirty = false;
                generatedCode = strjoin(changedPropertiesStruct.GeneratedCode, '\n');
                
                % if the app has serialzed code in a file and if it is not
                % the same as the generated code from the client set the
                % code to be dirty
                if (~isempty(obj.Model.AppFileCode) && ~strcmp(generatedCode, obj.Model.AppFileCode))
                    isDirty = true;
                end
                
                obj.ProxyView.setProperties({'IsDirty', isDirty});
            end
            
            % call superclass to handle property set
            unhandledProperties = handlePropertiesChanged@appdesservices.internal.interfaces.controller.AbstractController(obj, changedPropertiesStruct);
            
        end
        %------------------------------------------------------------------
        
        function getPropertiesForView(~, ~)
            % No-Op implemented for Base Class
        end
        %------------------------------------------------------------------
    end
    
    methods(Access = private)
        %------------------------------------------------------------------
        
        function handleEditableCodeChanged(obj, codeData)
            if(isempty(codeData))
                return
            end
            
            if(iscell(codeData))
                % handle client-side events that are cell arrays
                code = codeData;
            else
                code = {codeData.Code}';
            end
            
            % update the editable section code
            obj.Model.EditableSection.Code = code;
            
            % set the exist property to true for compatibility
            obj.Model.EditableSection.Exist = true;
        end
        %------------------------------------------------------------------
        
        function addCallback(obj, data)
            % transfers the data from the peer node event to the MCOS
            % object for each Callback
            
            import appdesigner.internal.codegeneration.model.AppCallback;
            
            if(strcmp(data.Type, 'AppStartupFunction'))
                obj.addStartupFunction(data);
                return;
            end
            
            callback = AppCallback(data.Id);
            obj.setCallbackProperties(callback, data);
            
            % add the callback to the Callbacks property for the
            % CodeData
            obj.Model.Callbacks(end+1) = callback;
        end
        %------------------------------------------------------------------
        
        function addStartupFunction(obj, startupFcnData)
            import appdesigner.internal.codegeneration.model.AppCallback;
            
            % create a startup function for the app
            congfigurableStartup = AppCallback(startupFcnData.Id);
            obj.setCallbackProperties(congfigurableStartup, startupFcnData);
            
            obj.Model.ConfigurableStartupFcn = congfigurableStartup;
        end
        %------------------------------------------------------------------
        
        function removeStartupFunction(obj, ~)
            % remove the startup function by deleting it
            delete(obj.Model.ConfigurableStartupFcn);
            obj.Model.ConfigurableStartupFcn = [];
        end
        %------------------------------------------------------------------
        
        function removeCallback(obj, data)
            if(strcmp(data.Type, 'AppStartupFunction'))
                obj.removeStartupFunction(data);
                return;
            end
            
            % find the callback and remove it from the CodeData
            callbackIds = {obj.Model.Callbacks.CallbackId};
            callbackIdx = strcmp(data.Id, callbackIds);
            obj.Model.Callbacks(callbackIdx) = [];
        end
        %------------------------------------------------------------------
        
        function updateCallback(obj, data)
            if(strcmp(data.Type, 'AppStartupFunction'))
                callback = obj.Model.ConfigurableStartupFcn;
            else
                % find the callback and remove it from the CodeData
                callbackIds = {obj.Model.Callbacks.CallbackId};
                callbackIdx = strcmp(data.Id, callbackIds);
                callback = obj.Model.Callbacks(callbackIdx);
            end
            
            
            obj.setCallbackProperties(callback, data);
        end
        %------------------------------------------------------------------
        
        function appCallback = setCallbackProperties(~, appCallback, data)
            import appdesigner.internal.codegeneration.model.CallbackComponentData;
            
            appCallback.Name = data.Name;
            appCallback.Comment = data.Comment;
            appCallback.Code = data.Code;
            appCallback.Args = data.Args;
            appCallback.ReturnArgs = data.ReturnArgs;
            appCallback.Type = data.Type;
            
            if(isempty(data.ComponentData))
                % for orphaned callbacks set ComponentData to be empty
                appCallback.ComponentData = CallbackComponentData.empty;
            else
                for i = 1:length(data.ComponentData)
                    if(isfield(data.ComponentData(i), 'CodeName'))
                        componentData = CallbackComponentData(...
                            data.ComponentData(i).CodeName, ...
                            data.ComponentData(i).CallbackPropertyName, ...
                            data.ComponentData(i).ComponentType);
                        appCallback.ComponentData(i) = componentData;
                    end
                end
            end
            
        end
        %------------------------------------------------------------------
    end
end