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

    classdef InfoPane < mbcgui.multiview.View
    %InfoPane panel containing recent files and examples.

    properties (Constant)
        %Examples list of examples to be displayed
        Examples = struct('FileName',{'gasolineOneStage.mat','CI_MultiInject_PointbyPoint.mat','Gasoline_project.mat'},...
            'Description',{'Dual CAM gasoline engine with spark optimized during testing','Multi-injection diesel tested with pilot injection on and off','Dual CAM gasoline engine with spark'},...
            'Type',{'One-stage','Point-by-point','Two-Stage'},...
            'Icon',{'gloreg.bmp','modelSwitch.bmp','mle2.bmp'});
    end
    
    properties (SetAccess=private)
        RecentList
        RecentFiles
        ExamplesList
        OpenRecentButton
        OldLocations
        Title
    end
    
    methods
        
        function obj = InfoPane(varargin)
        
        obj@mbcgui.multiview.View(varargin{:})
        
        create(obj);
        
        end
        
        function t = gettitle(obj)
        
        t = 'Project Manager';
        end
        
        function delete(obj)
        
        restoreOldLocations(obj)
        
        end
        
        
        function create(obj)
        %create create layout
        
        col = [ 0.8706    0.9216    0.9804];

        backGround = mbcgui.container.layoutpanel('Parent',obj.Parent, ...
            'LayoutBorder',[10 0 10 3],...
            'BorderType','etchedin',...
            'BackgroundColor', col);
        obj.Title = uicontrol('Parent',backGround,...
            'Style','text',...
            'HorizontalAlignment','left',...
            'FontSize',10,...
            'BackgroundColor',col,...
            'FontWeight','bold',...
            'String','Projects');
        div1 = xregGui.dividerline('Parent',backGround);
        div2 = xregGui.dividerline('Parent',backGround);

        newButton =mbcWorkflowItem('Parent',backGround,...
            'Label','New Project',...
            'IconFile',xregrespath('fileNew.bmp'),...
            'Callback',@obj.onNewProject,...
            'TransparentColor', [0 255 0]); 
        openButton =mbcWorkflowItem('Parent',backGround,...
            'Label','Open Project',...
             'Callback',@obj.onOpenProject,...
           'IconFile',xregrespath('fileOpen.bmp'),...
            'TransparentColor', [0 255 0]);
        
        t = uicontrol('Parent',backGround,...;
            'Style','text',...
            'HorizontalAlignment','left',...
            'BackgroundColor',col,...
            'FontSize',10,...
            'String','Recent projects:');
        obj.RecentList = uicontrol('Parent',backGround,...
            'Callback',@obj.onOpenRecent,...
            'Style','list');
        obj.OpenRecentButton = uicontrol('Parent',backGround,...
            'Style','pushbutton',...
            'HorizontalAlignment','left',...
            'Callback',@obj.onOpenRecentButton,...
            'String','Open Recent Project');          

        obj.ExamplesList = mbcwidgets.List('Parent',backGround,...
            'IconLocation',xregrespath,...
            'ColumnWidths',250,...
            'ActionPerformedCallback',@obj.onOpenExample);
        obj.ExamplesList.setData( {obj.Examples.Description}', {obj.Examples.Icon}');
        obj.ExamplesList.ColumnHeaders = {'Examples'};
        obj.ExamplesList.SelectedRows = 1;
        hOpenExample = uicontrol('Parent',backGround,...
            'Style','pushbutton',...
            'HorizontalAlignment','left',...
            'Callback',@obj.onOpenExampleButton,...
            'String','Open Example');   
        hHelp = uicontrol('Parent',backGround,...
            'Style','pushbutton',...
            'HorizontalAlignment','left',...
            'String','Help');   
        
        lyt = xreggridbaglayout(backGround,...
            'dimension',[12 2],...
            'gapy',5,...
            'gapx',10,...
            'ColSizes',[-1 -1],...
            'rowsizes',[18 3 25 25 3 18 120 25 2 120 25 -1],...
            'elements',{obj.Title,[]
            div1,[]
            newButton,[]
            openButton,[]
            [] []
            t []
            obj.RecentList []
            [] obj.OpenRecentButton
            div2 []
            obj.ExamplesList []
            hHelp hOpenExample 
            [] []},...
            'MergeBlock',{[1 1],[1 2]},...
            'MergeBlock',{[2 2],[1 2]},...
            'MergeBlock',{[3 3],[1 2]},...
            'MergeBlock',{[4 4],[1 2]},...
            'MergeBlock',{[7 7],[1 2]},...
            'MergeBlock',{[9 9],[1 2]},...
            'MergeBlock',{[10 10],[1 2]});
        backGround.LayoutComponent = lyt;
        obj.ContentHandle = backGround;
        
        addMessageServiceListener(obj, 'NodeChanged',@obj.onNodeChanged)
        end
    
        
        function onNodeChanged(obj,~,~)
        %onNodeChanged update recent files on load
        update(obj)
        end
        
        
        function onNewProject(obj,~,~)
        
        mbh = MBrowser;
        pCurrent = mbh.RootNode;
        NewProject(mbh);
        if pCurrent ~= mbh.RootNode
            restoreOldLocations(obj)
        end
        
        end
  
        function onOpenProject(obj,~,~)
        %onBrowse browse for a new project
        
        busy(obj.MessageService,'Openning project');
        OK = OpenProject(MBrowser);
        if OK
            restoreOldLocations(obj)
        end
        idle(obj.MessageService)
        end
        
        function onOpenRecent(obj,~,~)
        %onOpenRecent open recent file by double clicking on list
        fig = ancestor(obj.Parent,'figure');
        if strcmp(fig.SelectionType,'open')
            openRecent(obj)
        end
        end
        
        function onOpenRecentButton(obj,~,~)
        %onOpenRecentButton open recent file by double clicking on button
        openRecent(obj)
        end
        
        function openRecent(obj)
        %onOpenRecent open recent file
        file = obj.RecentFiles{obj.RecentList.Value};
        busy(obj.MessageService,sprintf('Opening %s',file));
        OK = OpenProject(MBrowser,file);
        if OK
            restoreOldLocations(obj)
        end
        idle(obj.MessageService)
        end        
        
        function update(obj)

        AP=mbcprefs('mbc');
        LastOpened=getpref(AP,'LastOpened');
        lbls = cell(1,length(LastOpened));
        files = lbls;
        for i=1:length(LastOpened)
            [lbls{i},files{i}] = displayFileName(LastOpened(i));
        end
        set(obj.OpenRecentButton,'Enable',mbconoff(~isempty(LastOpened)))
        set(obj.RecentList,'String',lbls);
        obj.RecentFiles = files;
        
        end
        
        function onOpenExample(obj,~,evt)
        %onOpenExample open example by double clicking on list

        row = evt.Data.SelectedRows;
        openExample(obj,row)
        end
        function onOpenExampleButton(obj,~,~)
        %onOpenExample open example by button

        row = obj.ExamplesList.SelectedRows;
        openExample(obj,row)
        end
            
        function openExample(obj,row)
        if isscalar(row)
            ex = obj.Examples(row);
            file = fullfile(mbcpath,'mbctraining',ex.FileName);
            OpenProject(MBrowser,file,false);
            % set up preferences to use mbctraining
            setExampleLocations(obj)
        end
        end
        
        function setExampleLocations(obj)
        %setExampleLocations set preferences to use files and templates from mbctraining
        
        % set directory locations to 
        files.Models = fullfile(mbcpath,'mbctraining');
        files.Projects = fullfile(mbcpath,'mbctraining');
        files.Data = fullfile(mbcpath,'mbctraining');
        files.Designs = fullfile(mbcpath,'mbctraining');
        
        AP= mbcprefs('mbc');
        allfiles = getpref(AP,'FileDefaults');
        oldValues = allfiles.mbcmodel;
        allfiles.mbcmodel = files;
        setpref(AP,'FileDefaults',allfiles);
        
        if ~isequal(oldValues,files)
            obj.OldLocations = oldValues;
        end
        end
        
        function restoreOldLocations(obj)
        %restoreOldLocations restore preferences
        if ~isempty(obj.OldLocations)
            % restore default locations
            AP= mbcprefs('mbc');
            allfiles = getpref(AP,'FileDefaults');
            allfiles.mbcmodel = obj.OldLocations;
            setpref(AP,'FileDefaults',allfiles);
            obj.OldLocations = [];
        end

        end

        
    end
    
end

function [lbl,file] = displayFileName(LastOpened)
%displayFileName display name for recent file
file=fullfile(LastOpened.Path,LastOpened.File);
if length(file)>35
    % replace inner folder names with ...
    lbl=[file(1:3) '...',filesep,LastOpened.File];
else
    lbl = file;
end
end