www.gusucode.com > mbcview 工具箱matlab源码程序 > mbcview/+cageview/+optim/OptimItemListView.m

    classdef OptimItemListView < mbcgui.multiview.View
    %cageview.optim.OptimItemListView class
    %   cageview.optim.OptimItemListView extends mbcgui.multiview.View.
    %
    %    cageview.optim.OptimItemListView properties:
    %       Parent - Property is of type 'MATLAB array'
    %       Position - Property is of type 'rect'
    %       Enable - Property is of type 'on/off'
    %       Visible - Property is of type 'on/off'
    %       UserData - Property is of type 'MATLAB array'
    %       Tag - Property is of type 'string'
    %       MessageService - Property is of type 'handle'
    %       Options - Property is of type 'handle vector'
    %       Actions - Property is of type 'handle vector'
    %       UIContextMenu - Property is of type 'MATLAB array'
    %       SelectedItemIndex - Property is of type 'int'
    %       ProjectPointer - Property is of type 'MATLAB array'
    %
    %    cageview.optim.OptimItemListView methods:
    %       editItem - Edit an item
    
    %  Copyright 2005-2015 The MathWorks, Inc. and Ford Global Technologies, Inc.
    
    properties (AbortSet, SetObservable)
        %SELECTEDITEMINDEX Property is of type 'int'
        SelectedItemIndex=0
        %PROJECTPOINTER Property is of type 'MATLAB array'
        ProjectPointer = [];
    end
    
    properties (SetAccess=protected, AbortSet)
        %HLIST Property is of type 'handle'
        hList = [];
    end
    
    methods  % constructor block
        function obj = OptimItemListView(varargin)
        %OPTIMITEMLISTVIEW Constructor for OptimSetupInfoView
        %  OBJ = OPTIMITEMLISTVIEW(PROP, VALUE) constructs a view that displays
        %  information about optimization items.  This object does not in fact show
        %  information for any items but is the superclass for the separate
        %  objective and constraint item lists.
        
        % Call the inherited constructor
        obj@mbcgui.multiview.View(varargin{ : }); % converted super class constructor call
        
        % Construct list object
        P = com.mathworks.toolbox.mbc.gui.peer.OptimItemSetupTablePeer;
        obj.hList = mbcwidgets.Table1D(P, ...
            'Parent', obj.Parent, ...
            'Grid', true, ...
            'SelectionMode', 'singlerow');
        obj.hList.Peer.setBorder([]);
        obj.ContentHandle = obj.hList;
        cnamesext = obj.pGetAdditionalDataNames;
        cnames = [{'Name', 'Description'}, cnamesext(:)', {'Status'}];
        obj.hList.Peer.setColumnData(cnames);
        obj.hList.Peer.setColumnWidths([120 170 repmat(130, 1, length(cnamesext)), 300]);
        
        % Add selection change listener
        obj.addListeners([...
            handle.listener(obj.hList, 'SelectionChanged', {@i_updatesel, obj}), ...
            handle.listener(obj.hList, 'ActionPerformed', @(src, evt) obj.editItem) ...
            ]);
        
        % Hook up to the message service if it exists
        if ~isempty(obj.MessageService)
            obj.pPostSetMessageService;
        end
        end  % OptimItemListView
        
    end  % constructor block
    
    methods  % public methods
        %----------------------------------------
        function editItem(obj) %#ok<MANU>
        %EDITITEM Edit an item
        %  EDITITEM(OBJ) is called when the currently selected item is
        %  double-clicked or otherwise activated.  If the item is editable, an
        %  appropriate dialog should be displayed for editing it.
        
        end  % editItem
        
    end  % public methods
    
    methods(Access=protected)
        %----------------------------------------
        function hC = pCreateContextMenu(obj)
        %PCREATECONTEXTMENU Create a context menu for the list
        %  HC = PCREATECONTEXTMENU(OBJ) creates and returns a handle to a context
        %  menu that should be used for the list.
        
        if ~isempty(obj.Actions)
            hC = uicontextmenu('Parent', ancestor(obj.Parent,'figure'));
            A = obj.Actions.AllActions;
            A.MenuType = 'separate';
            createMenuItem(A,hC);
        else
            hC = [];
        end
        
        end  % pCreateContextMenu
        
        %----------------------------------------
        function pFillList(obj)
        %PFILLLIST Redraw the list
        %  PFILLLIST(OBJ) redraws the list.
        
        if obj.hasData
            items = obj.pGetItems;
            
            extdata = obj.pGetAdditionalDataValues(items);
            names = cell(length(items),1);
            icons = cell(length(items),1);
            desc = cell(length(items),1);
            for n = 1:length(items)
                names{n} = getName(items{n});
                icons{n} = smallIcon(items{n});
                desc{n} = char(items{n});
            end
            statusicons(1:length(items),1)= {''};
            [codes, status] = obj.pGetStatusStrings(items);
            statusicons(codes==1) = {xregrespath('warningsmall.bmp')};
            statusicons(codes==2) = {xregrespath('errorsmall.bmp')};
            
            obj.hList.Peer.setData([names, desc, extdata, status(:)], icons);
            obj.hList.Peer.setIconData(statusicons, 2+size(extdata,2));
            
            if obj.SelectedItemIndex>length(names)
                obj.SelectedItemIndex = 0;
            end
            
        else
            obj.hList.Peer.setData([]);
            obj.SelectedItemIndex = 0;
        end
        
        end  % pFillList
        
        %----------------------------------------
        function names = pGetAdditionalDataNames(~)
        %PGETADDITIONALDATANAMES Return the column names for additional data
        %  PGETADDITIONALDATANAMES(OBJ) returns a cell array containing the names
        %  of the additional data columns that should be added to the list.  The
        %  length of this cell array must match the number of columns in the output
        %  of PGETADDITIONALDATAVALUES.
        
        names = {};
        
        end  % pGetAdditionalDataNames
        
        %----------------------------------------
        function values = pGetAdditionalDataValues(obj, items) %#ok<INUSL>
        %PGETADDITIONALDATAVALUES Return the values for additional data
        %  VALUES = PGETADDITIONALDATAVALUES(OBJ, ITEMS) returns a cell array
        %  containing the values of the additional data  that should be added to
        %  the list. The number of columns of this cell array must match the length
        %  of the output of PGETADDITIONALDATANAMES and the number of rows must
        %  match the length of ITEMS.
        
        values = cell(length(items), 0);
        
        end  % pGetAdditionalDataValues
        
        %----------------------------------------
        function items = pGetItems(obj) %#ok<MANU>
        %PGETITEMS Get the items that should be listed
        %  ITEMS = PGETITEMS(OBJ) returns a cell array containing the items that
        %  should be contained in the list.
        
        items = cell(1,0);
        
        end  % pGetItems
        
        %----------------------------------------
        function [codes, status] = pGetStatusStrings(obj, items)
        %PGETSTATUSSTRINGS Return the status strings for the items.
        %   [CODES, STATUS] = PGETSTATUSSTRINGS(OBJ, ITEMS) returns the status
        %   codes and strings for the items specified.  The default implementation
        %   call checkitemstatus on the cgoptim object.
        
        [codes, status] = checkitemstatus(obj.MessageService.getOptim, items);
        
        end  % pGetStatusStrings
        
        %----------------------------------------
        function pSelectionChanged(obj) %#ok<MANU>
        %PSELECTIONCHANGED Perform actions when the selection changes
        %  PSELECTIONCHANGED(OBJ) is called when the selection in the list is
        %  altered.
        
        end  % pSelectionChanged
        
    end
    
end  % classdef

function i_updatesel(~, evt, obj)
if ~isempty(evt.data.SelectedDataRows)
    obj.SelectedItemIndex = evt.data.SelectedDataRows;
else
    obj.SelectedItemIndex = 0;
end

% Call method that can bve used to update Action enable status
obj.pSelectionChanged;
end  % i_updatesel