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

    classdef DatasetListView < mbcgui.multiview.View
    %cageview.optim.DatasetListView class
    %   cageview.optim.DatasetListView extends mbcgui.multiview.View.
    %
    %    cageview.optim.DatasetListView 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.DatasetListView methods:
    %       addItem - Add a datset to the optimization
    %       deleteItem - Remove a dataset from the optimization
    %       editItem - Edit an item
    %       gettitle - Return string to use as title for view
    
    %  Copyright 2005-2015 The MathWorks, Inc. and Ford Global Technologies, Inc.
    
    properties (AbortSet, SetObservable)
        %SELECTEDITEMINDEX Property is of type 'int'
        SelectedItemIndex
        %PROJECTPOINTER Property is of type 'MATLAB array'
        ProjectPointer = [];
    end
    
    properties (Access=protected, AbortSet)
        %HLIST Property is of type 'handle'
        hList = [];
    end
    
    methods  % constructor block
        function obj = DatasetListView(varargin)
        %DATASETLISTVIEW Constructor for DatasetListView
        %  OBJ = DATASETLISTVIEW(PROP, VALUE) constructs a view that displays
        %  information about optimization datasets.
        
        % Call the inherited constructor
        obj@mbcgui.multiview.View(varargin{ : }); % converted super class constructor call
        
        % Make actions
        obj.pMakeActions;
        
        % Construct list object
        obj.hList = mbcwidgets.Table1D('list', ...
            'Parent', obj.Parent, ...
            'Position', obj.Position, ...
            'Visible', obj.Visible, ...
            'Grid', false, ...
            'SelectionMode', 'singlerow', ...
            'UIContextMenu', obj.pCreateContextMenu);
        obj.hList.Peer.setBorder([]);
        obj.ContentHandle = obj.hList;
        
        obj.hList.Peer.setColumnData({'Name', 'Selected Data Set', 'Required Variables', 'Status'});
        obj.hList.Peer.setColumnWidths([120 100 120 240]);
        
        % Add selection change listener
        obj.addListeners([...
            handle.listener(obj.hList, 'SelectionChanged', {@i_updatesel, obj}), ...
            handle.listener(obj.hList.Peer, 'ActionPerformed', @(src, evt) obj.editItem) ...
            ]);
        
        if obj.SelectedItemIndex
            obj.hList.Peer.selectRows(obj.SelectedItemIndex-1)
        end
        % Hook up to the message service if it exists
        if ~isempty(obj.MessageService)
            obj.pPostSetMessageService;
        end
        end  % DatasetListView
        
    end  % constructor block
    
    methods  % public methods
        %----------------------------------------
        function addItem(obj)
        %ADDITEM Add a datset to the optimization
        %  ADDITEM(OBJ) is called to add a new dataset to the optimization.
        
        if obj.hasData
            optim = obj.MessageService.getOptim;
            optim = addOppoint(optim);
            [optim, ok] = guiDatasetSelector(optim, getNumOppoints(optim), obj.ProjectPointer);
            if ok
                % Update the optimization: this will redraw the UI too.
                obj.MessageService.setOptim(optim, 'dataset');
            end
        end
        
        end  % addItem
        
        %----------------------------------------
        function deleteItem(obj)
        %DELETEITEM Remove a dataset from the optimization
        %  DELETEITEM(OBJ) is called when a dataset should be removed from the
        %  optimization.
        
        if obj.hasData && obj.SelectedItemIndex>0
            optim = obj.MessageService.getOptim;
            optim = deleteOppoint(optim, obj.SelectedItemIndex);
            
            % Update the optimization: this will redraw the UI too.
            obj.MessageService.setOptim(optim, 'dataset');
        end
        
        end  % deleteItem
        
        %----------------------------------------
        function editItem(obj)
        %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.
        
        if obj.hasData && obj.SelectedItemIndex>0 && ~isempty(obj.ProjectPointer)
            optim = obj.MessageService.getOptim;
            [optim, ok] = guiDatasetSelector(optim, obj.SelectedItemIndex, obj.ProjectPointer);
            if ok
                % Update the optimization: this will redraw the UI too.
                obj.MessageService.setOptim(optim, 'dataset');
            end
        end
        
        end  % editItem
        
        %----------------------------------------
        function str = gettitle(obj) %#ok<MANU>
        %GETTITLE Return string to use as title for view
        %  STR = GETTITLE(OBJ) returns a string that should be used as a title for
        %  the container the view sits in.
        
        str = 'Data Sets';
        
        end  % gettitle
        
    end  % public methods

    methods (Access=protected)
        %----------------------------------------
        function pCheckActionStatus(obj)
        %PCHECKACTIONSTATUS Update the enable status of the object's actions
        %  PCHECKACTIONSTATUS(OBJ)
        
        if obj.hasData
            optim = obj.MessageService.getOptim;
            opts = getSetup(optim);
            obj.Actions(1).Enabled = canAddOperatingPointSet(opts);
            if obj.SelectedItemIndex>0
                obj.Actions(2).Enabled = (numOperatingPointSets(opts)>0);
                obj.Actions(3).Enabled = canRemoveOperatingPointSet(opts);
            else
                set(obj.Actions(2:end), 'Enabled', false);
            end
        else
            set(obj.Actions, 'Enabled', false);
        end
        
        end  % pCheckActionStatus
        
        %----------------------------------------
        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;
            for n = 1:length(A)
                A(n).createMenuItem(hC);
            end
        else
            hC = [];
        end
        
        end  % pCreateContextMenu
        
        %----------------------------------------
        function pFillList(obj)
        %PFILLLIST Redraw the list
        %  PFILLLIST(OBJ) redraws the list.
        
        selIndex = 1;
        if obj.hasData
            optim = obj.MessageService.getOptim;
            [pDS,ia] = unique(getOppointPtr(optim),'stable');
            Setup = getSetup(optim);
            
            names = getOperatingPointSetLabels(Setup);
            icons = repmat({cgrespath('optimdataset.bmp')}, size(names));
            
            % Get matched data set names
            PtrOK = isvalid(pDS);
            selname = repmat({''}, size(names));
            if any(PtrOK)
                selname(PtrOK) = pveceval(pDS(PtrOK), @getname);
            end
            
            % Get required variables
            reqvar = repmat({''}, size(names));
            for n = 1:numel(reqvar)
                thisreq = getOperatingPointSetVarLabels(Setup, n);
                if ~isempty(thisreq)
                    s = sprintf('%s, ', thisreq{:});
                    reqvar{n} = s(1:end-2);
                else
                    reqvar{n} = '<none>';
                end
            end
            
            % Get status information
            statusicons = cell(size(names));
            statusicons(:)= {''};
            [codes, status] = checkDatasets(optim);
            codes = codes(ia);
            status = status(ia);
            
            statusicons(codes==1) = {xregrespath('warningsmall.bmp')};
            statusicons(codes==2) = {xregrespath('errorsmall.bmp')};
            
            obj.hList.Peer.setData([names(:), selname(:),reqvar(:),status(:)], icons(:));
            obj.hList.Peer.setIconData(statusicons, 3);
            if ~isempty(names)
                selIndex = 1;
            end
        else
            obj.hList.Peer.setData([]);
        end
        obj.SelectedItemIndex = selIndex;
        
        end  % pFillList
        
        %----------------------------------------
        function pMakeActions(obj)
        %PMAKEACTIONS Create the actions for the object
        %  PMAKEACTIONS(OBJ) is called during construction to create the list's
        %  actions.
        
        obj.Actions = [...
            mbcgui.actions.StatefulAction(@(src, evt) obj.addItem, '&Add Data Set...', ...
            'Add a new data set', cgrespath('optimnewdataset.bmp')); ...
            mbcgui.actions.StatefulAction(@(src, evt) obj.editItem, '&Edit Data Set...', ...
            'Edit data set'); ...
            mbcgui.actions.StatefulAction(@(src, evt) obj.deleteItem, '&Delete Data Set', ...
            'Delete data set'); ...
            ];
        set(obj.Actions, 'Enabled', false);
        
        end  % pMakeActions
        
        %----------------------------------------
        function pPostSetMessageService(obj)
        %PPOSTSETMESSAGESERVICE Method that is called when the message service is set
        %  PPOSTSETMESSAGESERVICE(OBJ) is called in response to a new message
        %  service being set in the object.
        
        pPostSetMessageService@mbcgui.multiview.View(obj)
        
        obj.addMessageServiceListener( ...
            { ...
            'DatasetChanged', ...
            }, ...
            { ...
            {@i_redrawlist, obj}, ...
            } ...
            );
        
        % Update display
        obj.pFillList;
        obj.pCheckActionStatus;
        end  % pPostSetMessageService
        
    end
end  % classdef

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

function i_redrawlist(~, ~, obj)
obj.pFillList;
obj.pCheckActionStatus;
end  % i_redrawlist