www.gusucode.com > mbcexpr 工具箱 matlab 源码程序 > mbcexpr/cgFillCardTwo.m

    function layout = cgFillCardTwo(fh, iFace, localData)
%CGFILLCARDTWO feature fill wizard card two
%
% layout = cgFillCardTwo(fh, iFace, localData)

%  Copyright 2005-2015 The MathWorks, Inc.


% Have we been called to create the layout or simply update?
if isa(fh, 'xregcontainer')
    layout = fh;
    layoutUD = get(layout, 'UserData');
else
    iFace.setWizardSize( [600, 350] );    
    % model selection
    txtModel = uicontrol('Parent', fh,...
        'Style', 'text',...
        'String', 'Filled by:',...
        'Visible', 'off',...
        'HorizontalAlignment', 'left');
    selModel = uicontrol('Parent', fh,...
        'Style', 'push',...
        'String', 'Select...',...
        'Tag', 'SelectFillItem',...
        'TooltipString','Select filling item',...
        'Visible', 'off',...
        'Callback',{@iSetModel,iFace},...
        'HorizontalAlignment', 'left');

    % constraint model selection
    txtConstraint = uicontrol('Parent', fh,...
        'Style', 'text',...
        'String', 'Constraint:',...
        'Tag', 'constraintText', ...
        'Visible', 'off',...
        'HorizontalAlignment', 'left');
    selConstraint = uicontrol('Parent', fh,...
        'Style', 'push',...
        'String', 'Select...',...
        'Tag', 'SelectConstraint',...
        'TooltipString','Select constraint',...
        'Visible', 'off',...
        'Callback',{@iSetConstraint,iFace},...
        'HorizontalAlignment', 'left');
    remConstraint = uicontrol('Parent', fh,...
        'Style', 'push',...
        'String', 'Deselect',...
        'Tag', 'DeselectConstraint',...
        'TooltipString','Deselect constraint',...
        'Visible', 'off',...
        'Callback',{@iRemoveConstraint,iFace},...
        'HorizontalAlignment', 'left');

    
    % link variables list
    txtVariables = uicontrol('Parent', fh,...
        'Style', 'text',...
        'String', 'Variables:',...
        'Visible', 'off',...
        'HorizontalAlignment', 'left');

    inputList = mbcwidgets.Table1D( 'list',...
        'parent', fh,...
        'SelectionMode', 'SingleRow',...
        'SelectionChangedCallback', {@iSelectVariable,iFace},... 
        'visible', 'off', ...
        'Tag', 'InputsList');
    inputList.Peer.setColumnData( {'Variable', 'Linked to'} );
    inputList.Peer.setColumnWidths( [110, 110] );
        
    % list of models available for linking
    txtLinks = uicontrol('Parent', fh,...
        'Style', 'text',...
        'String', 'Links:',...
        'Visible', 'off',...
        'HorizontalAlignment', 'left');
    
    linkList = mbcwidgets.Table1D( 'list',...
        'Parent', fh,...
        'ColumnWidth', 180,...
        'SelectionChangedCallback', {@iLinkVariableSelected,iFace},... 
        'SelectionMode', 'singlerow',...
        'Visible', 'off', ...
        'Tag', 'LinksList');
    linkList.Peer.setColumnWidths( [130, 60] )
    linkList.Peer.setColumnData( {'Name', 'Type'} );
    
    % selection and removal buttons for links
    pushLink = uicontrol('Parent', fh,...
        'Style', 'pushbutton',...
        'String', 'Link', ...
        'Callback',{@iSelectLink,iFace},...
        'Visible', 'off');
    pushUnLink = uicontrol('Parent', fh,...
        'Style', 'pushbutton',...
        'String', 'Unlink', ...
        'Callback',{@iRemoveLink,iFace},...
        'Visible', 'off');

    % main layout for card
    layout = xreggridbaglayout(fh,...
        'dimension', [7 6], ...
        'rowsizes', [25 25 2 15 25 25 -1], ...
        'colsizes', [200 -1 -1 100 120 90], ...
        'border', [10 10 10 10], ...
        'gapy', 5,...
        'gapx', 7, ...
        'mergeblock', {[1 1], [1 2]},...
        'mergeblock', {[2 3], [1 2]},...
        'mergeblock', {[5 7], [1 3]},...
        'mergeblock', {[5 7], [5 6]},...
        'elements', {...
        txtModel,      [], [], selModel,      [],            [];...
        txtConstraint, [], [], selConstraint, remConstraint, [];...
        [],            [], [], [],            [],            [];...        
        txtVariables,  [], [], [],            txtLinks,      [];...
        inputList,     [], [], pushLink,      linkList      [];...
        [],            [], [], pushUnLink,    [],            [];...
        [],            [], [], [],            [],            [] } );

    infoStr = 'Select the item to fill the tables from. Optionally, select a constraint model, and link inputs to other items from the project.';
    layout = xreglayerlayout( fh,...
        'Elements', {mbcgui.container.InfoPane( 'Parent', fh,...
        'Visible', 'off',...
        'Title', 'Choose Filling Item and Links',...
        'InfoString', infoStr,...
        'Center', layout )} );   
    
    layoutUD.inputList = inputList;
    layoutUD.linkList = linkList;
    layoutUD.txtConstraint = txtConstraint;
    layoutUD.txtModel = txtModel;
    layoutUD.selConstraint = selConstraint;
    layoutUD.remConstraint = remConstraint;
    layoutUD.selModel = selModel;
    layoutUD.pushLink = pushLink;
    layoutUD.pushUnLink = pushUnLink;
    layoutUD.nextFcn = @iCardTwoNext;
    layoutUD.finishFcn = @iCardTwoFinish;
end

if nargin > 2
    layoutUD.Feature = localData.Feature;
    m= localData.Feature.Model;
    if isempty(m) || isnull(m)
        feval(iFace.setNextButton, 0);
    else
        feval(iFace.setNextButton, 1);
    end
end

feval(iFace.setFinishButton, 0);

iCardTwoUpdate(layoutUD, iFace);
set(layout, 'UserData', layoutUD);

%------------------------------------------------------------------------
function iCardTwoUpdate(layoutUD, ~)
% update display for card two

F= layoutUD.Feature;

% Set the model name
pModel= F.Model;
if isempty(pModel) || isnull(pModel)
    mstr= 'Filled by: <none>';
else
    mstr= sprintf('Filled by: %s',pModel.getname);
end
set(layoutUD.txtModel,'String', mstr);

% Set the constraint name
exprConstraint = F.Constraint;
if isempty( exprConstraint )
    cstr= 'Constraint: <none>';
else
    cstr= sprintf('Constraint: %s', charlistshort( exprConstraint ) );
end
set(layoutUD.txtConstraint,'String', cstr );

if isempty(getmodels(info(F.Project)))
    % disable constraints if there are no models
    set([layoutUD.selConstraint layoutUD.remConstraint],'Enable','off')
else
    set([layoutUD.selConstraint layoutUD.remConstraint],'Enable','on')
end   

% Choose which input to select.
pVar = iGetSelectedInput( layoutUD );
iUpdateLinkList( layoutUD, pVar );
iUpdateLinkButtonEnable( layoutUD, pVar );

% update the list of possible links.
pList= getFillLinksList(F);
ListNames= pveceval(pList,@getname);
ListTypes = pveceval(pList,@getCageType); 
listData = [ListNames.',ListTypes.'];
ListIcons = pveceval(pList,@(i)cgrespath(iconfile(i)) );

layoutUD.linkList.Peer.setData( listData,  ListIcons.' );
sortColumn = 1;
sortDirection = 1;
layoutUD.linkList.Peer.setSortBy( sortColumn, sortDirection );
% select the right item based on other list
iSelectLinkVariable( layoutUD, pVar );


%------------------------------------------------------------------------
function iUpdateLinkList( layoutUD, pVar )
% update the left-hand list
[listdata, linkicons] = iGetVariablesAndLinks( layoutUD.Feature );
layoutUD.inputList.Peer.setIconData(linkicons, 1);
layoutUD.inputList.Peer.setData( listdata );

% selectedRow might be the variable we want to select.
selectedRow = find( layoutUD.Feature.Inputs==pVar );
layoutUD.inputList.Peer.selectModelRows(selectedRow-1);

%------------------------------------------------------------------------
function iSelectLink(~, ~, iFace)
% select link model for current variable

layoutUD = feval(iFace.getCardUserdata);
pList = getFillLinksList( layoutUD.Feature );
index = layoutUD.linkList.getSelectedRows();
if ~isempty( index )
    % Get the two pointers
    pLink = pList(index);    
    pVar = iGetSelectedInput( layoutUD );
    % Make the link.
    layoutUD.Feature.makeLink( pVar, pLink );
    % Update the wizard
    iUpdateLinkList( layoutUD, pVar );
    iUpdateLinkButtonEnable( layoutUD, pVar );
end
%------------------------------------------------------------------------
function iRemoveLink(~, ~, iFace)
% remove link model for current variable

layoutUD = feval(iFace.getCardUserdata);
pVar = iGetSelectedInput( layoutUD );
layoutUD.Feature.removeLink( pVar );
iUpdateLinkList( layoutUD, pVar );
iUpdateLinkButtonEnable( layoutUD, pVar );

%------------------------------------------------------------------------
function [listdata, icons] = iGetVariablesAndLinks( F )

pInputs = F.Inputs;
nValues = length( pInputs );
listdata = repmat( {''}, nValues, 2);
icons = repmat( {''}, nValues, 1);

for n = 1:nValues
    pLink = F.getLink( pInputs(n) );
    listdata{n,1} = pInputs(n).getname();
    if ~isempty( pLink )
        listdata{n,2} = pLink.getname();
        icons{n} = cgrespath( pLink.iconfile() );
    end
end

%------------------------------------------------------------------------
function iSelectLinkVariable( layoutUD, pVar )
pLink = iGetLink(layoutUD.Feature,pVar);
if ~isempty(pLink)
    % update link model list
    pList = getFillLinksList( layoutUD.Feature );
    pos= find( ismember( pList, pLink ) );
    selectRows( layoutUD.linkList, pos );
end

%------------------------------------------------------------------------
function iLinkVariableSelected(~, ~, iFace)
% Response to a selection in the RH list

layoutUD = feval(iFace.getCardUserdata);

% And the current selection in LH list
pVar = iGetSelectedInput( layoutUD );
iUpdateLinkButtonEnable( layoutUD, pVar );

%------------------------------------------------------------------------
function iSelectVariable(~, ~, iFace)
% select variable for linking

layoutUD = feval(iFace.getCardUserdata);
pVar = iGetSelectedInput( layoutUD );
iUpdateLinkButtonEnable(layoutUD, pVar);
iSelectLinkVariable( layoutUD, pVar );

%------------------------------------------------------------------------
function iUpdateLinkButtonEnable(layoutUD, pVar, ~)
% update the link/unlink buttons' enable status based up the selection in
% the LH list

% by default we will enable a button
linkButtonEnableStatus = 'on';
unlinkButtonEnableStatus = 'on';

% if no variable selected in LH list, or no linkvariable selected in the RH list
% we disable the link/unlink buttons.
if isempty( pVar )
    linkButtonEnableStatus = 'off';
    unlinkButtonEnableStatus = 'off';
else
    pLink = iGetLink(layoutUD.Feature,pVar);
    
    % if the current input is not linked we disable the unlink button.
    if isempty( pLink )
        unlinkButtonEnableStatus = 'off';
    end

    % see if a link would be allowed between the pVar and the currently
    % highlighted link variable.
    pSelectedLink = iGetSelectedLinkVariable( layoutUD );  
    if isempty( pSelectedLink ) || ~checkLink( layoutUD.Feature, pVar, pSelectedLink );
        linkButtonEnableStatus = 'off';
    end
end

%Set the enable status
set( layoutUD.pushLink, 'Enable', linkButtonEnableStatus );
set( layoutUD.pushUnLink, 'Enable', unlinkButtonEnableStatus );

%------------------------------------------------------------------------
function pLink = iGetLink(F, pVar)
% get link for current variable
pLink = [];
if ~isempty( pVar )
    pLink = F.getLink( pVar );
end

%------------------------------------------------------------------------
function pVar = iGetSelectedInput( layoutUD )
% Get the pointer to the input selected in the inputList

index = layoutUD.inputList.getSelectedRows();
if isempty( index )
    index = 1;
end
pVar = layoutUD.Feature.Inputs( index );

%------------------------------------------------------------------------
function pLink = iGetSelectedLinkVariable( layoutUD )
% Get the pointer to the link pointer selected in the link list.

index = layoutUD.linkList.getSelectedRows();
if ~isempty( index )
    pAllLinks = layoutUD.Feature.getFillLinksList();
    pLink = pAllLinks( index );
else
    pLink = mbcpointer(0,1);
end

%------------------------------------------------------------------------
function iSetModel(~, ~, iFace)
% set model for feature fill

layoutUD = feval(iFace.getCardUserdata);
F= layoutUD.Feature;

[selectedModel, ok] = F.gui_SelectItem();


if ok
    if isempty(selectedModel) || isnull(selectedModel)
        feval(iFace.setNextButton, 0);
    else
        feval(iFace.setNextButton, 1);
    end    
    % update variable list
    iCardTwoUpdate(layoutUD,iFace);
end

%------------------------------------------------------------------------
function iSetConstraint(~, ~, iFace)
% set constraint model for feature fill

layoutUD = feval(iFace.getCardUserdata);
F= layoutUD.Feature;
[exprConstraint, ok] = F.guiSelectConstraint();
if ok
    if isComplete( exprConstraint )
        F.Constraint = exprConstraint;
        % update variable list
        iCardTwoUpdate(layoutUD,iFace);
    else
        % User pressed OK but the constraint was not completely setup - let
        % the user know that this is the case and that we haven't accepted
        % the constraint.
        msg = { 'Constraint setup incomplete.'};
        uiwait( warndlg( msg, 'No Constraint Selected', 'modal' ) );
    end
end

%------------------------------------------------------------------------
function iRemoveConstraint(~, ~, iFace)
% remove constraint model for feature fill

layoutUD = feval(iFace.getCardUserdata);
F= layoutUD.Feature;
F.Constraint= [];
% update variable list
iCardTwoUpdate(layoutUD,iFace);

%------------------------------------------------------------------------
function outputUD= iLocalDataTwo(layoutUD)
outputUD.Feature= layoutUD.Feature;

%------------------------------------------------------------------------
function iCardTwoFinish(layoutUD, iFace)
% finish wizard
outputUD= iLocalDataTwo(layoutUD);
feval(iFace.setOutputData, outputUD);

%------------------------------------------------------------------------
function [nextCardID, localData] = iCardTwoNext(layoutUD, ~)
% next card for wizard
localData  = iLocalDataTwo(layoutUD);
nextCardID = @cgFillCardThree;