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

    classdef ListView < mbcgui.multiview.View
    %ListView Model selection listview
    
    %  Copyright 2015-2015 The MathWorks, Inc. and Ford Global Technologies, Inc.
    
    properties(SetAccess=private)
        %hListText list header text
        hListText
        %ChildImage icon for child models
        ChildImage
        %hListView mbcwidgets.List
        hListView
        %Column selected column in list
        Column = 1;
        %Redraw redraw list
        Redraw = true;
    end
    
    methods 
        function obj = ListView(varargin)
        %ListView constructor
        
        obj@mbcgui.multiview.View(varargin{:})
        create(obj)
        addMessageServiceListener(obj, 'NodeUpdated',@obj.onNodeUpdated)
        
        end
        
        function update(obj)
        %update main update method
        
        if obj.Redraw
            ms = obj.MessageService;
            [Data,Icons,ColHead]= childstats(ms);
            
            if ~isempty(Data);
                % calculate some column widths
                w = zeros(1,length(ColHead));
                for i= 2:length(ColHead)-1
                    % add column headers
                    % guess column width based on size of header
                    IsUpper= upper(ColHead{i})==ColHead{i};
                    w(i)= 10*sum(IsUpper)+6*sum(~IsUpper);
                end
                w= max(w,70);
                w(1) = 144;
                w(end)= 100;
                Changed = obj.hListView.getColumnCount~=length(w);
                obj.hListView.setColumnData(ColHead);
                if Changed
                    obj.hListView.setColumnWidths(w);
                end
                % BestModel column is editable
                obj.hListView.setData({});
                
                obj.hListView.EditableColumns = size(Data,2);
                
                obj.hListView.setData(Data,Icons);
                
                
                % select list using MessageService Pointer
                Index = ms.CurrentIndex;
                if isscalar(Index)
                    obj.Column = min(obj.Column,size(Data,2));
                    % select model
                    obj.hListView.selectCell(Index,obj.Column);
                end
            else
                % clear list
                obj.hListView.setColumnData({'Name'});
                obj.hListView.setColumnWidths(144);
                obj.hListView.setData({});
            end
        else
            obj.Redraw = true;
        end
        end
    end
    
    methods (Access=private)
        
        function onSelectList(obj,~,evt)
        %onSelectList update view with list view selection
        row = evt.data.SelectedDataRows;
        if ~isempty(row)
            if isempty(evt.data.SelectedDataColumns)
                obj.Column = 1;
            else
                obj.Column = evt.data.SelectedDataColumns;
            end
            ms = obj.MessageService;
            if isempty(ms.CurrentIndex) || ms.CurrentIndex~=row
                % have selected a different submodel
                % select alternative model in message service
                % don't want to update list after a selection
                obj.Redraw = false;
                selectAlternative(ms,row)
                obj.Redraw = true;
            end
        end
        end
        
        function onNodeUpdated(obj,~,~)
        %onNodeUpdated message service listener
        update(obj)
        end
        
        function onBestModelSelected(obj,~,evt)
        %onBestModelSelected select best model through checkbox list
        
        row = evt.data.Rows;
        ms = obj.MessageService;
        if evt.data.NewValue && ms.Status
            selectAlternative(ms,row,true)
        end
        update(obj);
        end
        
        function create(obj)
        %create create graphical components
        
        % List view header
        hListTitle= mbcgui.container.layoutpanel(...
            'Parent', obj.Parent,...
            'BorderType', 'beveledout', ...
            'LayoutBorder', [5 0 0 0]);
        obj.hListText=mbcgui.widget.Label('Parent',hListTitle,...
            'FontSize',10,...
            'HorizontalAlignment','left');
        obj.ChildImage=mbcgui.widget.Image('Parent',hListTitle);
        [description,ic] = listHeader(obj.MessageService);
        set(obj.hListText,'String',description);
        set(obj.ChildImage,'CData',xregresload(ic,'bmp'));        
        
        grd=xreggridbaglayout(hListTitle,...
            'dimension',[3 2],...
            'rowsizes',[-1 16 -1],...
            'colsizes',[16 -1],...
            'gapx',5,...
            'mergeblock',{[1 3],[2 2]},...
            'elements',{[],obj.ChildImage,[],obj.hListText});
        set(hListTitle, 'LayoutComponent', {grd});
        
        listPanel = mbcgui.container.layoutpanel(...
            'Parent', obj.Parent, ...
            'BorderType', 'beveledin');
        % main list
        obj.hListView = mbcwidgets.List('Parent',listPanel,...
            'Grid',true,...
            'Editable',true,...
            'SelectionChangedCallback',@obj.onSelectList,...
            'ValueChangedCallback',@obj.onBestModelSelected,...
            'IconTransparentColor',[255 0 255],...
            'IconLocation',xregrespath,...
            'SelectionMode','SingleCell');
        
        listGrid = xreglayerlayout(listPanel, ...
            'dimension', [2 1], ...
            'gapy',10,...
            'elements', {obj.hListView});
        set(listPanel, 'LayoutComponent', {listGrid});
        lyt = xreggridbaglayout(obj.Parent, ...
            'dimension', [2 1], ...
            'rowsizes', [22 -1], ...
            'elements', {hListTitle,listPanel});
        
        attachContentHandle(obj,lyt);
        end
    end
    
    
end