www.gusucode.com > mbcdata 工具箱 matlab 源码程序 > mbcdata/+cgfillsetup/FillPage.m

    classdef FillPage < cgcalsetup.WizardPage
    %CGFILLSETUP.FILLPAGE fill page for table fill wizard
    
    %  Copyright 2009-2012 The MathWorks, Inc. and Ford Global Technologies, Inc.
    
    properties (SetAccess=private)
        %MATCHING matching items [tables,fillitems]
        matching
        %LIST list of tables and their fill factors
        List
        %SELBUTTON selection button with the big arrow
        selbutton
        %NAMELIST list of fill items
        namelist
        %NormalizerInputsList list of normalizers and their inputs
        NormalizerInputsList  
        %SelTableInputButton select button for table inputs
        SelTableInputButton
        %MatchedNormalizerInputs all normalizers have inputs
        MatchedNormalizerInputs = false;
    end
    
    
    methods
        
        function updatePage(P,iFace)
            %UPDATEPAGE update wizard page from TableFiller
            
            F = P.Data;
            
            pSetUpTabs = F.Tables;
            pLinks = F.FillFactors;
            
            % Find each table that could possibly be filled in the list of
            % tables that have already been set up
            inds = findptrs(F.PossibleTables, pSetUpTabs);
            
            % Indices of tables that have been matched (col 1 in
            % layoutUD.matching)
            tabsmatched = find(inds);
            tabsmatched = tabsmatched(:);
            
            % Indices of matched tables (into matched table list) in order they
            % appear in the newmatching list
            indsord = inds(tabsmatched);
            
            % Get index of output factor to fill table
            pOut = F.FillItems;
            P.namelist.Items = pOut;
            
            indoutput = findptrs(pLinks, pOut);
            indoutput = indoutput(:);
            
            % Some of the links may not be valid with this set of optimization
            % results (change in free/fixed variables). Set any invalid links
            % to have a matched index of -1
            indoutput(~indoutput) = -1;
            
            % Form layoutUD.matching
            P.matching =[tabsmatched,  indoutput(indsord)];
            updateFillItems(P)
            updateNormalizerLinks(P)
            
            enableNext(P,iFace);
        end
        
        function updateData(P)
            %UPDATEDATA update data object from wizard
            
            F = P.Data;
            ToFill = P.matching(:,2)~=-1;
            pFill = mbcpointer(1,size(P.matching,1));
            pFill(ToFill) = F.FillItems(P.matching(ToFill,2));
            F.FillFactors = pFill;

        end
    end
    
    
    methods(Access=protected)
        function T = FillPage(fh,iFace,C)
            %TABLEPAGE protected constructor
            T.Data = C;
            T.Layout = createLayout(T,fh,iFace);
            updatePage(T,iFace)
        end
        
        function MainLyt =createLayout(P,fh,iFace)
            %CREATELAYOUT create layout for wizard pane
            %   MainLyt =createLayout(P,fh,iFace)
            
            % Draw the Table ListView control
            t= uicontrol('Parent',fh,...
                'Style','text',...
                'Visible','off',...
                'HorizontalAlignment','left',...
                'String','Tables values to be filled:');
            P.List = mbcwidgets.Table1D( 'list',...
                'Parent', fh,...
                'SelectionMode', 'SingleRow',...
                'SelectionChangedCallback', {@cbSelectTable,P},...
                'Visible', 'off' );
            P.List.Peer.setColumnData( {'CAGE Table','Fill with', 'Tradeoff'} );
            P.List.Peer.setColumnWidths( [150, 100, 119] );
            P.List.Peer.setIconBaseLocation( cgrespath );
            set(P.List,'UserData','TablesToFill')
            
            
            
            % Draw the Table ListView control
            tInputs= uicontrol('Parent',fh,...
                'Style','text',...
                'Visible','off',...
                'HorizontalAlignment','left',...
                'String','Normalizer inputs:');
            P.NormalizerInputsList = mbcwidgets.Table1D( 'list',...
                'Parent', fh,...
                'SelectionMode', 'SingleRow',...
                'SelectionChangedCallback', {@cbSelectNormalizerList,P},...
                'Tag','NormalizerInputs',...
                'Visible', 'off' );
            P.NormalizerInputsList.Peer.setColumnData( {'Normalizer','Input'} );
            P.NormalizerInputsList.Peer.setColumnWidths( [150, 219] );
            P.NormalizerInputsList.Peer.setIconBaseLocation( cgrespath );
            set(P.NormalizerInputsList,'UserData','NormalizerInputs')
            
            
            % Make a title for this ListView
            P.selbutton = xregGui.iconuicontrol('parent',  fh, ...
                'Style', 'pushbutton', ...
                'Visible', 'off', ...
                'TransparentColor', [0 255 0], ...
                'ImageFile', cgrespath('leftarrow.bmp'), ...
                'TooltipString', 'Select optimization result to fill with', ...
                'Tag','SelectFillItem',...
                'Callback', {@cbSelectObj,P,iFace});
            
            
            P.SelTableInputButton = xregGui.iconuicontrol('parent',  fh, ...
                'Style', 'pushbutton', ...
                'Visible', 'off', ...
                'TransparentColor', [0 255 0], ...
                'ImageFile', cgrespath('leftarrow.bmp'), ...
                'TooltipString', 'Select optimization result for normalizer input', ...
                'Tag','SelectNormalizerInput',...
                'Callback', {@cbSelectNormalizerInput,P,iFace});            
            
             btnlyt = xreggridbaglayout(fh, ...
                'Dimension', [3 1], ...
                'RowSizes', [-1 25 -1], ...
                'Elements', {[], P.SelTableInputButton, []});
            
            %RHS list for optimization results
            P.namelist = cgtools.exprList('parent', fh, ...
                'visible', 'off', ...
                'displaytypecolumn', false, ...
                'itemheadertext', 'Optimization Results');
            
            layout = xreggridbaglayout('parent', fh, ...
                'dimension', [8 3], ...
                'gapx', 10, ...
                'border', [7 5 7 10], ...
                'rowsizes', [15 1 -1 30 -1 20 15 80], ...
                'colsizes', [-1 30 -1], ...
                'colratios', [2 0 1], ...
                'mergeblock', {[3 5], [1 1]}, ...
                'mergeblock', {[3 8], [3 3]}, ...
                'elements', {t,[],P.List,[], [],[],tInputs,P.NormalizerInputsList,  ...
                [],[],[], P.selbutton,[],[],[],btnlyt, ...
                [],[],P.namelist,[],[],[], [], []});
            
            MainLyt = addInfoPane(P,fh,'Optimal Result Selection',layout);
            infostr = 'Choose the optimization results that you want to fill each table with.';
            P.hInfoPane.InfoString = infostr;
        end
        
        function f= getNextPage(W) %#ok<MANU>
            %getNextPage no next page for TablePage
            f = @cgfillsetup.RulesPage.createPage;
        end
        
        function updateFillItems(P)
            %UPDATEFILLITEMS update fill items in lhs list
            %  Tradeoff fill factors are used to set default fill factors
            
            % Get the table filler object
            F = P.Data;
            
            % Set up the matching matrix
            
            if isempty(P.matching)
                [~,rhtabind] = ismember(F.Tables, F.PossibleTables);
                P.matching = zeros(length(rhtabind), 2);
                P.matching(:, 1)= rhtabind(:)';
            else
                updateMatch(P);
            end
            
            % existing fill factors
            pSetupTables = F.Tables;
            pFill = F.FillFactors;
            NewTables = F.PossibleTables(P.matching(:,1));
            [OK,loc] = ismember(NewTables,F.Tables);
            if any(OK) && length(pFill)==length(pSetupTables)
                % reuse fill factors
                FillFactors = mbcpointer(size(pFill));
                FillFactors(OK) = pFill(loc(OK));
                F.FillFactors = FillFactors;
            end
            F.Tables = NewTables;
            [pTOs,~,pTradeoffFill]= getTradeOffs(F.Filler,F.Project);
            
            pFill = F.FillFactors;
            
            if ~isempty(pTOs)
                % try and get some fill factors from the tradeoff
                for i=1:length(pTradeoffFill)
                    if isnull(pFill(i)) && ~isnull(pTradeoffFill(i)) && ...
                            ismember(pTradeoffFill(i).getname,F.FillNames)
                        pFill(i) = pTradeoffFill(i);
                    end
                end
                F.FillFactors = pFill;
                for i=1:length(pFill)
                    if ~isnull(pFill(i))
                        [~,loc] = ismember(pFill(i).getname,F.FillNames);
                        P.matching(i,2) = loc;
                    end
                end
            end
            
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            % FILL TABLE <-> FILLFACTOR MATCHING LISTS %
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            
            % Fill the left hand list
            fillTableToFillFactor(P, 1);
            
            % Fill the right hand list box and set initial selected item
            P.namelist.Items = F.FillItems;
            if P.matching(1, 2) > 0
                % Actions for valid match
                P.namelist.SelectedItem = F.FillItems(P.matching(1, 2));
            else
                % Actions when there is no match
                P.namelist.SelectedItem = null(xregpointer);
            end
            
        end
        
        
        function fillTableToFillFactor(P, curRow)
            %FILLTABLETOFILLFACTOR populate left list
            %  [table, fillfactor,tradeoff]
            
            F = P.Data;
            
            % Left list, column 1 : Tables to be filled
            tabSelected = F.Tables;
            nTabs = length(tabSelected);
            tableLabels = pveceval(tabSelected, @getFunctionName);
            tableIcons = repmat( {''}, 1, nTabs);
            for i = 1:nTabs
                tableIcons{i} = iconfile(tabSelected(i).info);
            end
            
            % Left list, column 2 : Matched fill factors
            fillFactorLabels = cell(nTabs, 1);
            fillFactorIcons = repmat( {''}, 1, nTabs);
            TradeoffLabels = cell(nTabs, 1);
            TradeoffIcons = repmat( {''}, 1, nTabs);
            [~,pTab2TradeOff] = getTradeOffs(F.Filler,F.Project,tabSelected);
            for i = 1:nTabs
                matchind = P.matching(i, 2);
                if matchind > 0
                    % Valid link
                    fillFactorLabels{i} = F.FillNames{matchind};
                    fillFactorIcons{i} = iconfile(F.FillItems(matchind).info);
                elseif matchind == -1
                    % Invalid link
                    fillFactorLabels{i} = '';
                    fillFactorIcons{i} = '';
                    
                end
                if ~isempty(pTab2TradeOff{i})
                    %show related tradeoffs
                    TradeoffLabels{i} = mbcListString( pveceval(pTab2TradeOff{i},@getname),' ' );
                    TradeoffIcons{i} = pTab2TradeOff{i}(1).iconfile;
                end
            end
            
            % Left list : Put data into list
            P.List.Peer.setData( ...
                [tableLabels', fillFactorLabels, TradeoffLabels]);
            P.List.Peer.setIconData( tableIcons', 0);
            P.List.Peer.setIconData( fillFactorIcons, 1);
            P.List.Peer.setIconData( TradeoffIcons, 2);
            
            % Set selected row if possible
            if P.matching(curRow, 2) > 0
                % Actions for valid match
                P.List.selectRows(curRow);
            end
        end
        
        function updateMatch(P)
            %UPDATEMATCH update matched item
            
            F = P.Data;
            [~,rhtabind] = ismember(F.Tables, F.PossibleTables);
            
            newmatching = zeros(length(rhtabind), 2);
            for i = 1:length(rhtabind)
                ind = find(P.matching(:, 1) == rhtabind(i));
                newmatching(i, 1) = rhtabind(i);
                if ~isempty(ind)
                    newmatching(i, 2) = P.matching(ind, 2);
                end
            end
            P.matching = newmatching;
        end
        
        
        function enableNext(P,iFace)
            %ENABLENEXT Disable if any table is not fully matched and is valid
            if all(P.matching(:, 2) > 0) && all(P.MatchedNormalizerInputs)
                en = 'on';
            else
                en = 'off';
            end
            
            feval(iFace.setNextButton, en);
            feval(iFace.setFinishButton, false);
            
        end
        
        function updateNormalizerLinks(P)
        %updateNormalizerLinks update normalizer links
        
        F= P.Data;
        % update in TableFiller
        updateNormalizerLinks(F);
        
        links = F.NormalizerLinks;
        

        labels(1:size(links,1),2) = {''};
        icons = labels;
        matched = false(size(links,1),1);
        for i=1:size(links,1)
           if ~isnull(links(i,1))
               % normalizer
               labels{i,1} = links(i,1).getname;
               icons{i,1} = links(i,1).iconfile;
               if ~isnull(links(i,2))
                   % normalizer input
                   labels{i,2} = links(i,2).getname;
                   icons{i,2} = links(i,2).iconfile;
                   matched(i)= true;
               end
           end
            
        end
        
        P.MatchedNormalizerInputs = all(matched);
        % now update table
        P.NormalizerInputsList.Peer.setData(labels);
        P.NormalizerInputsList.Peer.setIconData(icons(:,1),0);
        P.NormalizerInputsList.Peer.setIconData(icons(:,2),1);
        if ~isempty(links)
            P.NormalizerInputsList.selectRows(1);
        end
        
        end
        
    end
    
    methods(Static)
        function layout = createPage( fh, iFace, C )
            %createPage create normalizer wizard page
            %    layout = cgfillsetup.FillPage.createPage( fh, iFace, C )
            if isa(fh, 'xregcontainer')
                N = get(fh,'UserData');
                updatePage(N,iFace)
            else
                N = cgfillsetup.FillPage(fh,iFace,C);
            end
            layout = N.Layout;
        end
    end
    
end

function cbSelectObj(~, ~, P, iFace)
%CBSELECTOBJ select list item
%    cbSelectObj(h, evt, P, iFace)

% Update the matching between fill factors and tables
curRow = P.List.getSelectedRows;
selIdx = P.namelist.SelectedIndex;
if ~isempty(curRow) && ~isempty(selIdx)
    P.matching(curRow, 2) = selIdx;
    
    % Redraw the left hand list
    fillTableToFillFactor(P, curRow);
    
    % Move to the next table in the tables list
    nTabs = size(P.matching, 1);
    if curRow < nTabs
        newRow = curRow+1;
    else
        newRow = 1;
    end
    % The following call does not fire the SelectionChanged callback, so we
    % do it manually
    P.List.selectRows(newRow);
    makeListsConsistent(P);
    
    % Update user data and check to see if we can leave the wizard
    enableNext(P,iFace)
end
end

function cbSelectTable(~, ~, P)
%CBSELECTTABLE selection callback for left list
%     cbSelectTable(~, ~, P)
% This callback is fired when a user selects a table in the main list. This
% callback ensures that the information in the Optimization output column
% list and the solution type/index selector is consistent with the main
% list.

% Make the lists consistent
makeListsConsistent(P);
end


function cbSelectNormalizerList(~, ~, P)
%cbSelectTableInputList selection callback for normalizer list
%     cbSelectNormalizerList(~, ~, P)
% This callback is fired when a user selects a table in the normalizer list. This
% callback ensures that the information in the Optimization output column
% list and the solution type/index selector is consistent with the main
% list.

% Make the lists consistent
idx = P.NormalizerInputsList.getSelectedRows;

F = P.Data;
if ~isempty(idx) && ~isnull(F.NormalizerLinks(idx, 2))
    % Update the selected item in the output results list
    FillIndex = find( F.NormalizerLinks(idx, 2)==F.FillItems );
    P.namelist.SelectedIndex = FillIndex;
else
    % don't select right list
    P.namelist.SelectedIndex = [];
end
end

function cbSelectNormalizerInput(~, ~, P, iFace)
%cbSelectNormalizerInput select normalizer input item
%    cbSelectNormalizerInput(h, evt, P, iFace)

curRow = P.NormalizerInputsList.getSelectedRows;
selIdx = P.namelist.SelectedIndex;
if ~isempty(curRow) && ~isempty(selIdx)
    
    F = P.Data;
    % update input to normalizer
    F.NormalizerLinks(curRow,2) = F.FillItems(selIdx);
    % update normalizer table
    updateNormalizerLinks(P)
    
    % select next item
    nTabs = size(F.NormalizerLinks, 1);
    if curRow < nTabs
        newRow = curRow+1;
    else
        newRow = 1;
    end
    P.NormalizerInputsList.selectRows(newRow);
    % make the next 
    cbSelectNormalizerList([], [], P)

    % enable next if all normalizers have inputs
    enableNext(P,iFace)
end
end

function makeListsConsistent(P)

idx = P.List.getSelectedRows;
if ~isempty(idx) && P.matching(idx, 2) > 0
    % Update the selected item in the output results list
    P.namelist.SelectedIndex = P.matching(idx, 2);
else
    % don't select right list
    P.namelist.SelectedIndex = [];
end
end