www.gusucode.com > mbctools 工具箱 matlab 源码程序 > mbctools/+mbcmodelview/+project/MessageService.m

    classdef MessageService < mbcmodelview.MessageService
    %MessageService project MessageService
    
    %  Copyright 2015-2016 The MathWorks, Inc. and Ford Global Technologies, Inc.
    
    properties
        %CurrentData currently selected data
        CurrentData = xregpointer;
    end
    properties (Dependent)
        %Project mdevproject object
        Project
    end
    
    properties(Dependent,SetAccess=private)
        %Title standard title is the name of the response
        Title
        %Datasets list of data set pointers
        Datasets
    end
    
    
    methods
        function obj=MessageService()
        %MessageService Constructor for MessageService
        
        obj.Actions = mbcmodelview.project.Actions(obj);
        end
        
        function MP = get.Project(obj)
        % get dynamic copy of project
        MP = info(obj.Pointer);
        
        end
        
        function t = get.Title(obj)
        %project name
        t = name(obj.Project);
        
        end
        
        function set.Project(obj,MP)
        if obj.Pointer==address(MP)
            xregpointer(MP);
        else
            error('Invalid project');
        end
        end
        
        function dp = get.Datasets(obj)
        %list of data sets
        dp = dataptrs(obj.Project);
        end
    end
    
    methods
        function setNewProject(obj,pNode)
        %setNewProject set new project pointer
        obj.Pointer = pNode;
        dp = dataptrs(obj.Project);
        % set current data object
        if isempty(dp)
            obj.CurrentData = xregpointer;
        else
            obj.CurrentData = dp(1);
        end
        fireNodeChanged(obj)
        end
        
        function update(obj)
        %update update message service
        fireNodeUpdated(obj)
        end
        
        function pSSF = createData(obj,ss)
        %createData create new data object
        [obj.Project, pSSF] = createDataObject(obj.Project);
        
        if nargin>1
            pSSF.info = setSweepsetData(pSSF.info,ss);
        end
        obj.CurrentData = pSSF;
        update(obj);
        end
        
        function copyData(obj)
        %copyData copy data object
        
        pSSF = obj.CurrentData;
        % Duplicate the appropriate dataset
        pcSSF = xregpointer(pSSF.info);
        % May need to cast to the correct data type
        if isTestplanData(obj.Project, pSSF)
            pcSSF.info = pcSSF.sweepsetfilter;
        end
        % Change label after casting to ssf to ensure the testplan bit is removed
        pcSSF.info = pcSSF.set('label',  ['Copy of ' pcSSF.get('label')]);
        % Update the project datalist with the new sweepset object
        addData(obj.Project, pcSSF);
        
        obj.CurrentData = pcSSF;
        update(obj)

        end
        
        function hData = editData(obj,fProcessData,ReadOnly)
        %editData edit current data object
        %   editData(obj,fProcessData,ReadOnly)
        %     fProcessData function handle to process data on exit
        %     ReadOnly open data editor in read-only mode
        if nargin<2
            fProcessData = @obj.applyDataChanges;
        end
        
        if nargin<3
            % Is this testplan data
            IS_TESTPLAN_DATA = isTestplanData(obj.Project, obj.CurrentData);
            ReadOnly = IS_TESTPLAN_DATA;
        else
            IS_TESTPLAN_DATA = false;
        end
        if ReadOnly
            % no post-processing of data when data editor closes
            fCloseEditor = [];
        else
            % set up a callback to process data if it is editable
            fCloseEditor = @(hData, evt) obj.closeDataEditor(hData, evt,fProcessData);
        end
        hData = xregdatagui.Editor.open(obj.CurrentData,~ReadOnly,IS_TESTPLAN_DATA,fCloseEditor);
        
        end
        
        function deleteData(ms)
        %deleteData delete current data set
        
        % Get the pointer to the data object being deleted
        pSSF = ms.CurrentData;
        % Can we really delete this data
        if isTestplanData(ms.Project, pSSF)
            h=xregerror('Error Deleting Data',...
                'Data can only be deleted if it is not used by any test plan.');
            uiwait(h)
            return
        end
        
        % Get the data editor
        hData = xregdatagui.Editor.find('dataEditor');
        % May need to close the data editor if this object is being edited
        if ~isempty(hData) ...
                && isvalid(hData) ...
                && ~isempty(hData.UserData) ...
                && hData.UserData.ObjectBeingEdited == pSSF
            
            OK = hData.close;
            if ~OK
                % Can't delete data that is being kept open in the data editor
                return
            end
        end        
        
        % Data is not associated with a testplan and hence can safely be deleted -
        % does the user really mean it?
        answer = questdlg(...
            sprintf('Are you sure you want to delete ''%s''?', pSSF.get('Label')), ...
            'Confirm Data Delete', ...
            'Yes', 'No', 'Yes');
        if ~strcmpi(answer, 'yes')
            return
        end

        % Remove the data from the project
        removeData(ms.Project, pSSF);
        if ~isempty(ms.Datasets)
            ms.CurrentData = ms.Datasets(1);
        else
            ms.CurrentData = xregpointer;
        end

        % Update the view
        update(ms)
        end
        
        function closeDataEditor(obj, hData, evt,fProcessData)
        %closeDataEditor close data editor and process data
        
        % Make sure that MP is up-to-date
        % Which data pointer is being edited
        pSSF = hData.UserData.ObjectBeingEdited;
        % Is the pointer location to copy back to still valid?
        if isvalid(pSSF)
            % Get the old and new data
            oldData = pSSF.info;
            newData = hData.NewData;
            newData = setCacheState(newData, false);
            % Did the old data have anything in it?
            if isempty(oldData)
                OK = 'yes';
            else
                dispStr = 'Accept changes to data?';
                % Should we ask the user to confirm changes
                if isequal(oldData, newData)
                    OK = 'yes';
                else
                    OK = questdlg(dispStr, 'Data Changed', 'Yes', 'No', 'Cancel', 'No');
                end
            end
            
            switch lower(OK)
                case 'yes'
                    fProcessData(obj.Project,pSSF,newData);
                case 'no'
                    % No updating required
                case {'cancel', ''}
                    % Signal to the Application that it should not close
                    evt.CancelClose = true;
            end
        end
        end
        
        function applyDataChanges(obj,MP,pSSF,newData)
        %applyDataChanges apply data changes in project
        
        % update data in project
        modifyData(MP, pSSF, newData);
        % Update the view
        obj.CurrentData = pSSF;
        update(obj)
        end
        
        function OK = leaveNode(obj)
        %leaveNode get ready to leave node
        
        OK = closeSubFigures(obj);
        if OK
            notify(obj,'NodeLeft');
        end
        end        
        
    end
    
end