www.gusucode.com > mbcview 工具箱matlab源码程序 > mbcview/CageList.m

    classdef CageList < mbcgui.widget.BasicContainer
    %CAGELIST list view for CAGE browser
    %   This class provides an interface for handling the main list in the CAGE
    %   browser. Currently, the list is used for models and variables.
    
    %  Copyright 2011-2016 The MathWorks, Inc.
    
    properties (SetAccess=private)
        %pList list of node pointers in current view
        pList = mbcpointer(0,1);
        %pSubList list of sub pointers in current view.
        %   This list is only used for variables at present
        pSubList = mbcpointer(0,1);
        %TypeFilter cgtypes object for filtering current list
        TypeFilter
        %Project pointer to current project
        Project
    end
    
    properties (Dependent)
        %PointerList active pointer list
        %   The active pointer depends on whether node pointers or sub pointers
        %   are being used.
        PointerList
        %Current pointer to currently selected item
        Current
        %IsSubList true if the current list is using sub pointers (variables)
        IsSubList
    end
    
    properties
        UIContextMenu
    end
    
    methods
        function C=CageList(varargin)
            %CAGELIST constructor
            
            C@mbcgui.widget.BasicContainer(varargin{:});
            
            % create main list
            C.ContentHandle = mbcwidgets.List('IconLocation',cgrespath,...
                'Parent',C.Parent,...
                'Visible',C.Visible,...
                'Position',C.Position,...
                'SelectionMode','SingleRow',...
                'Editable',true,...
                'EditableColumns',1,...
                'ValueChangedCallBack',{@iRename,C},...
                'KeyTypedCallback',{@iDelete,C},...
                'UserData','list',...
                'Tag','CAGEList',...
                'UIContextMenu',C.UIContextMenu,...
                'SelectionChangedCallback',{@iSelect,C});
        end
        
        function pList = get.PointerList(C)
            if C.IsSubList
                pList = C.pSubList;
            else
                pList = C.pList;
            end
            
        end
        
        function set.PointerList(C,p)
            if C.IsSubList
                C.pSubList = p;
            else
                C.pList = p;
            end
        end
        
        function set.UIContextMenu(C,m)
            if ~isempty(C.ContentHandle)
                C.ContentHandle.UIContextMenu = m;
            end
            C.UIContextMenu = m;
        end
        
        function OK = get.IsSubList(C)
            OK = length(C.pList)==1 && any(~isnull(C.pSubList));
        end
        
        function buildlist(C,cgp,tpfilter)
            %BUILDLIST build list for a new type
            C.Project=cgp;
            [nds,cheaders,vals,colsize,p_sub]= nodelist(info(cgp),tpfilter);
            % set up columns 
            C.ContentHandle.Peer.setColumnData([{'Name'} cheaders]);
            C.ContentHandle.Peer.setColumnWidths([150 colsize]);
            C.pList = nds;
            C.pSubList = p_sub;
            C.TypeFilter = tpfilter;
            if ~isempty(nds)
                refresh(C,vals)
            else
                C.ContentHandle.Peer.setData([])
                C.ContentHandle.UIContextMenu = [];
            end
        end
        
        function remove(C,p,p_sub)
            %remove remove item from list
            
            if nargin<2
                [p,p_sub] = C.getPointers;
            end
            ind = getIndex(C,p,p_sub);
            
            % get data from table and remove row
            D=C.ContentHandle.getData();
            D(ind,:)=[];
            
            C.PointerList(ind) = [];
            ic = pveceval(C.PointerList,@iconfile);
            C.ContentHandle.Peer.setData(D,ic)
            
            % reselect row
            ind= min(ind+1,length(C.PointerList));
            if ind>0
                C.ContentHandle.selectRows(ind);
            end
        end
        
        
        function refresh(C,vals)
            %refresh refresh list in CAGE browser

            [p,psub]= getPointers(C);
            if nargin<2
                % get data 
                [~,~,vals]=nodelist(info(C.Project),C.TypeFilter);
            end
            if isempty(vals)
                C.ContentHandle.Peer.setData([]);
                C.ContentHandle.UIContextMenu = [];

            else
                % icons and names
                ic = pveceval(C.PointerList,@iconfile);
                names = pveceval(C.PointerList,@getname);
                
                C.ContentHandle.Peer.setData([names(:) vals],ic(:));
                C.ContentHandle.UIContextMenu = C.UIContextMenu;
            end
            % select the first item
            if ~isempty(C.pList)
                if isempty(p)
                    % select first row
                    [p,psub]= getPointers(C,1);
                end
                select(C,p,psub)
            end
        end
        
        function p = get.Current(C)
           
            row = C.ContentHandle.getSelectedRows;
            if ~isempty(row)
               p = C.PointerList(row); 
            end
        end
        
        function select(C,p,psub)
            %select item in CAGE list
            %     select(C,p,psub)
            row = getIndex(C,p,psub);
            if ~isequal(row,C.ContentHandle.getSelectedRows)
                C.ContentHandle.selectRows(row);
            end
        end
        
        
        function update(C,p,p_sub)
            %UPDATE update item in list
            %    update(C,p,p_sub)
            
            if nargin<2
                % get current item
                [p,p_sub]=getPointers(C);
            end
            if C.IsSubList
                [~,vals] = listvals(info(p),p_sub);
                n = p_sub.getname;
                ic = p_sub.iconfile;
            else
                n = p.getname;
                ic = p.iconfile;
                [~,~,vals]=listinfo(info(p),C.TypeFilter);
            end
            C.ContentHandle.Peer.setRowData([{n} vals],ic,getIndex(C,p,p_sub)-1)
        end
        function [p,p_sub]= getPointers(C,row)
            %getPointers get node and sub pointer 
            %   [p,p_sub]= getPointers(C,row)
            %   [p,p_sub]= getPointers(C)
            %   The default row is the selected row
            
            if nargin<2
                row = C.ContentHandle.getSelectedRows;
            end
            
            if ~isempty(row)
                if row>length(C.pList)
                    p= C.pList;
                else
                    p= C.pList(row);
                end
                p_sub = C.pSubList(row);
            else
                p=xregpointer;
                p_sub=p;
            end
        end 
        
        function startEdit(C)
            %startEdit start editing node at selected row and first column
            C.ContentHandle.editCellAt(C.ContentHandle.SelectedRows,1);
        end
    end
    
    methods(Access=private)
        function ind = getIndex(C,p,p_sub)
            %getIndex get list index from node and sub pointer
            %   ind = getIndex(C,p,p_sub)
            
            if C.IsSubList
               ind = find(C.pSubList==p_sub);
            else
               ind = find(C.pList==p);
           end
        end
        

    end
    
end


function iSelect(h,evt,C)
%iSelect select item from list

row= evt.data.SelectedDataRows;
if ~isempty(row)
    % don't deselect node
    objH = cgbrowser;
    
    [p,p_sub]= getPointers(C,row);
    if p~=objH.CurrentNode || p_sub~=objH.CurrentSubItem
        PR=xregGui.PointerRepository;
        ptrID=PR.stackSetPointer(objH.Figure,'watch');
        if objH.lock
            objH.setnewnode(p,p_sub);
            objH.unlock;
        end
        PR.stackRemovePointer(objH.Figure,ptrID);
    end
end

end

function iDelete(h,evt,C)
%iDelete delete key callback

objH = cgbrowser;
if ~isempty(evt.data.Rows) && evt.data.Modifiers==0 && evt.data.KeyCode==mbcgui.util.KeyCode.DELETE
    % Delete
    if objH.lock
        if objH.IsDeleteEnabled
            objH.deletenode;
        end
        objH.unlock;
    end
end
end

function iRename(h,evt,C)
%iRename rename item callback

objH = cgbrowser;

row= evt.data.Rows;
[p,p_sub]= getPointers(C,row);

newstring = evt.data.NewValue;

if objH.HasRelabelManager
    % custom rename procedure
    ok=feval(objH.getRelabelFunction,p.info,p_sub,newstring);
else
    % standard rename method - operates on primary item
    ok=p.rename(newstring);
end
if ~ok
    %redo current row to revert
    C.update(p,p_sub);
else
    % need to redraw current node
    objH.doDrawText;
    objH.ViewNode;
    xregcallback(objH.getBrowserOptions('relabelcallback'), objH, [])
end


end