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

    classdef Actions < handle
    %mbcmodelview.project.Actions project actions
    
    %  Copyright 2015-2016 The MathWorks, Inc. and Ford Global Technologies, Inc.
    
    properties (SetAccess=private)
        %MessageService projectmessage service
        MessageService
        %Listeners storage
        Listeners
    end
    
    properties(SetAccess=private)
        %DesignExperiment create a new DoE
        DesignExperiment
        %ImportData import data with choice of data source (file/workspace)
        ImportData 
        %ImportFile import a data file
        ImportFile
        %ImportWorkspace import data from workspace
        ImportWorkspace
        %FitModels fit models to data
        FitModels
        %NewData create a new data set in data editor
        NewData
        %CopyData copy selected data set
        CopyData
        %EditData edit selected data set
        EditData
        %DeleteData delete selected data set
        DeleteData
        %NewNote create new project note
        NewNote
        %NoteInformation configure note information fields
        NoteInformation
    end
    
    methods 
        function obj = Actions(MessageService)
        %Actions constructor
        obj.MessageService = MessageService;
        
        %Main Workflow actions
        obj.DesignExperiment = mbcgui.actions.StatefulAction(@obj.onDesignExperiment, ...
            'Design Experiment...', 'Design Experiment', xregrespath('doe.bmp'));

        obj.ImportData = mbcgui.actions.StatefulAction(@obj.onImportData, ...
            'Import Data...', 'Import data', xregrespath('import_ts_16.bmp'));
        
        obj.ImportFile = mbcgui.actions.StatefulAction(@obj.onImportFile, ...
            '&Import Data from File...', 'Import data from file', xregrespath('import_ts_16.bmp'));
        obj.ImportWorkspace = mbcgui.actions.StatefulAction(@obj.onImportWorkspace, ...
            'Import Data from &Workspace...', 'Import data from workspace', xregrespath('variable_import.bmp'));
        obj.FitModels = mbcgui.actions.StatefulAction(@obj.onFitModels, ...
            'Fit &Models...', 'Fit models', xregrespath('viewPlot.bmp'));
        
        % data set manipulation actions
        obj.NewData = mbcgui.actions.StatefulAction(@obj.onNewData, ...
            '&New Data...', 'New data', xregrespath('newData.bmp'));
        obj.CopyData = mbcgui.actions.StatefulAction(@obj.onCopyData, ...
            '&Copy Data...', 'Copy data', xregrespath('copyData.bmp'));
        obj.EditData = mbcgui.actions.StatefulAction(@obj.onEditData, ...
            '&Edit Data...', 'Edit data', xregrespath('data.bmp'));        
        obj.DeleteData = mbcgui.actions.StatefulAction(@obj.onDeleteData, ...
            '&Delete Data', 'Delete data', []);    
        
        % project note actions
        obj.NewNote = mbcgui.actions.StatefulAction(@obj.onNewNote, ...
            'New N&ote...', 'New note', xregrespath('newnote.bmp'));        
        obj.NoteInformation = mbcgui.actions.StatefulAction(@obj.onNoteInformation, ...
            'Note &Information...', 'Note information', []);            
        
        obj.Listeners = event.listener(obj.MessageService,'NodeUpdated',@obj.onNodeUpdated);
        
        end
        
        function enable(obj)
        %enable enable actions
        ms = obj.MessageService;
        obj.CopyData.Enabled = ~isnull(ms.CurrentData);
        obj.EditData.Enabled = ~isnull(ms.CurrentData);
        obj.DeleteData.Enabled = ~isnull(ms.CurrentData);
        end
        
        function createMenus(obj,hParents,~)
        %createMenus create project menus
        createDataMenu(obj,hParents(1))
        createViewMenu(obj,hParents(2))
        end
        
        function createDataMenu(obj,hParent)
        %createDataMenu create data menu items
        AG = mbcgui.actions.ActionGroup([],'Data');
        AG.Actions =  [obj.ImportFile,obj.ImportWorkspace,obj.FitModels];
        AG.MenuType = 'separate';
        createMenuItem(AG,hParent);
        
        AG.Actions =  [obj.NewData,obj.CopyData,obj.EditData,obj.DeleteData];
        createMenuItem(AG,hParent);
        end
        
        function createViewMenu(obj,hParent)
        %createViewMenu create view menu items
        createMenuItem(obj.NoteInformation,hParent);
        
        end
        
        function createToolbar(obj,hParent)
        %createToolbar create project toolbar
        
        AG = mbcgui.actions.ActionGroup([],'Workflow');
        AG.MenuType = 'separate';
        
        AG.Actions = [obj.ImportFile,obj.ImportWorkspace,obj.FitModels];
        tb = createToolbutton(AG,hParent);
        tb(1).Separator = 'on';
        
        AG.Actions = [obj.NewData,obj.CopyData,obj.EditData];
        tb = createToolbutton(AG,hParent);
        tb(1).Separator = 'on';

        tb = createToolbutton(obj.NewNote,hParent);
        tb.Separator = 'on';
        end
        
        function createWorkflowItems(obj,hWorkflow)
        %createWorkflowItems create project workflow items
        
        AG = mbcgui.actions.ActionGroup([],'Workflow');
        % could have New Model as well
        AG.Actions = obj.DesignExperiment;
        AG.MenuType = 'separate';
        wf = createWorkflowItems(AG,hWorkflow);
        wf.Separator = 'on';
        
        AG.Actions = [obj.ImportFile,obj.ImportWorkspace,obj.FitModels];
        % AG.Actions = [obj.ImportFile obj.ImportWorkspace obj.FitModels];
        createWorkflowItems(AG,hWorkflow);
        
        end
    end
    
    methods (Access=private)
        function onNodeUpdated(obj,~,~)
        %onNodeUpdated MessageService.NodeUpdated event handling
        enable(obj)
        end        
        
        function onDesignExperiment(obj,~,~) %#ok<INUSD>
        %onDesignExperiment design a new experiment
        
        % create a new test plan and open DoE editor
        mbh = MBrowser;
        pCurrent = get(mbh,'CurrentNode');
        
        NewNode(mbh,mbh.RootNode)
        p = get(mbh,'CurrentNode');
        if pCurrent~=p && strcmp(p.guid,'testplan')
            % launch the design editor if a test plan is created
            frameWork = GetViewData(mbh);
            openDesignEditor(frameWork)
        end
        end
        
        function onImportFile(obj,~,~)
        %onImportFile import data file
        importFile(obj,true)
        
        end
        
        function importFile(obj,openEditor)
        %importFile import data from file
        ms = obj.MessageService;
        busy(ms);
        
        % Get the data using the data loading wizard
        [ss,OK] = xregImportDataFile;
        
        % Did it all work out OK
        if OK
            % Create a viable sweepsetfilter object
            createData(ms,ss);
            if openEditor
                hData = editData(ms);
                registerDataEditor(hData);
            end
        end
        
        % Change the watch back
        idle(ms)        
        end
        
        function onImportWorkspace(obj,~,~)
        %onImportWorkspace import data from workspace
        
        importWorkspace(obj,true)
        end
        
        function importWorkspace(obj,openEditor)
        %importWorkspace import data from MATLAB workspace

        ms = obj.MessageService;
        busy(ms);
        
        % Find the names of the variables in the base workspace
        varNames = evalin('base','who');
        % Initialise the base structure to ensure a suitable result if there are no
        % variables in the workspace
        base = struct;
        for i = 1:length(varNames)
            % Get copies of the variables in the base workspace
            base.(varNames{i}) = evalin('base',varNames{i});
        end
        % Open the import wizard
        [OK, ss] = xregImportDataStructure(base, 'Workspace', get(MBrowser,'Figure'));
        % Did the user click OK?
        if OK
            createData(ms,ss);
            if openEditor
                hData = editData(ms);
                registerDataEditor(hData);
            end
        end
        
        % Change the watch back
        idle(ms)
        end
        
        function onImportData(obj,~,~)
        %onImportData task button 
        
        % could check for variables in workspace and prompt for source
        [dataSource,openEditor] = importDataSourceDlg;
        switch dataSource
            case 'File'
                importFile(obj,openEditor);
            case 'Workspace'
                importWorkspace(obj,openEditor);
        end
        
        
            function [dataSource,openEditor] = importDataSourceDlg
            %importDataSourceDlg dialog to ask for data source and open
            %data editor
            
            openEditor = true;

            % check the base MATLAB workspace
            vars = evalin('base','whos');
            % check for table objects
            hasTable = strcmp('table',{vars.class});
            % check for MBC data structures
            hasStruct = strcmp('struct',{vars.class});
            for i=find(hasStruct)
                hasStruct(i) = isSweepsetStruct(sweepset, evalin('base',vars(i).name));
            end
            % check for double matrices
            hasMatrix = strcmp('double',{vars.class});
            for i=find(hasStruct)
                hasMatrix(i) = ismatrix(evalin('base',vars(i).name));
            end
            hasWkSpace = any(hasTable) || any(hasStruct) || any(hasMatrix);

            % only show dialog if data is available in workspace
            dataOptions = {'File';'Workspace'};
            dlg = mbcgui.container.Dialog('Name','Import Data',...
                'Buttons','OK_CANCEL',...
                'ContentBorder',[7 7 7 7],...
                'Size',[300 150]);
            pnl = mbcgui.container.layoutpanel('Parent',dlg.Figure,...
                'BorderType','etchedin',...
                'Title','Data Source');
            rb = xregGui.rbgroup('Parent',pnl,...
                'nx', 1, 'ny', length(dataOptions),...
                'EnableArray',[true hasWkSpace],...
                'String',dataOptions);
            lyt = xreggridbaglayout(pnl,...
                'dimension',[2,1],...
                'rowsizes',[20*length(dataOptions),-1 ],...
                'elements',{rb,[]});
            pnl.LayoutComponent = lyt;
            % checkbox to open data editor
            chk = uicontrol('Parent',dlg.Figure,...
                'Style','checkbox',...
                'Value',openEditor,...
                'String','Preprocess data in Data Editor');
            contentLyt = xreggridbaglayout(dlg.Figure,...
                'dimension',[2,1],...
                'rowsizes',[-1,20 ],...
                'gapy',10,...
                'elements',{pnl,chk});
            
            dlg.Content = contentLyt;
            cls = dlg.showDialog();
            if strcmpi(cls,'OK')
                dataSource = dataOptions{rb.Selected};
                openEditor = chk.Value;
            else
                dataSource = '';
            end
            delete(dlg)
            
            
            end
        end

        function onFitModels(obj,~,~)
        %onFitModels fit models to data
        ms = obj.MessageService;
        
        busy(ms);
        Model = tpsetup.ModelData(ms.Pointer,ms.CurrentData);
        tpsetup.DataDrivenDialog.create(Model);
        
        idle(ms)
        end

        function onNewData(obj,~,~)
        %onNewData create a new data set
        ms = obj.MessageService;
        busy(ms);
        
        createData(ms);
        % Send the new data to the data editor
        hData = editData(ms);
        registerSubFigure(ms,hData.Figure);

        end

        function onCopyData(obj,~,~)
        %onCopyData copy current data set
        ms = obj.MessageService;
        busy(ms);        
        
        copyData(ms)
        % Send the new data to the data editor
        hData = editData(ms);
        registerSubFigure(ms,hData.Figure);
        
        % Change the watch back
        idle(ms)
        
        end

        function onEditData(obj,~,~)
        %onEditData edit current data set
        ms = obj.MessageService;
        busy(ms);      
        
        % Send the new data to the data editor
        hData = editData(ms);
        registerSubFigure(ms,hData.Figure);
        % Change the watch back
        idle(ms)
        
        end        
        
        function onDeleteData(obj,~,~)
        %onDeleteData delete current data set
        ms = obj.MessageService;
        
        deleteData(ms)
        end
        
        
        function onNewNote(obj,~,~)
        %onNewNote create a new project note
        ms = obj.MessageService;
        usr = initfromprefs(mbcuser);
        % update the file history
        time = now;
        histArray = history(ms.Project);
        histArray= [histArray struct('User',usr,'Action','','Date',time)];
        history(ms.Project,histArray);
        update(ms)

        end        
        
        function onNoteInformation(obj,~,~)
        %onNoteInformation configure project note fields for display
        ms = obj.MessageService;
        
        prfs = getpref(mbcprefs('mbc'),'mdevproject');
        old_colstoshow = prfs.NotesListColumns;
        
        % Call a method to edit the columns-viewed preference
        gui_notessetup(ms.Project);
        
        % check whether any settings have changed
        prfs = getpref(mbcprefs('mbc'),'mdevproject');
        colstoshow = prfs.NotesListColumns;
        if any(colstoshow~=old_colstoshow)
            % check list columns are correct
            update(ms)
        end
        end
        
        
    end
    
end

function registerDataEditor(hData)

if strcmp(hData.Visible,'on')
    viewData = GetViewData(MBrowser);
    registerSubFigure(viewData.MessageService,hData.Figure);
end
end