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

    function [pNewMdlExpr, ok] = gui_SelectItem(CGP,Title,DispStr,pMdlExpr,ListType)
%GUI_SELECTITEM Dialog for selecting a new item
%
%  [pNewMdlExpr, ok] = gui_SelectItem(CGP,Title,DispStr,pMdlExpr,ListType)
%  pops up a small dialog that lists all of the items in the project pointed to 
%  by pPROJ and lets the user select a new one

%  Copyright 2005-2010 The MathWorks, Inc. and Ford Global Technologies, Inc.

ok = false; 
pPROJ= address(CGP);


if nargin<2
    Title= 'Select Model';
end
if nargin<3
    DispStr= 'Select model:';
end
if nargin<4
    pMdlExpr= xregpointer;
end
if nargin<5
    ListType= cgtypes.cgmodeltype;
end

fH = xregdialog('name',Title , ...
    'resize', 'off');
xregcenterfigure(fH, [250 300]);
    
t = uicontrol('Parent', fH, ...
    'Style', 'text', ...
    'Enable', 'inactive', ...
    'String', DispStr, ...
    'HorizontalAlignment', 'left');

mdlList = cgtools.projectList('parent', fH, ...
    'DisplayTypeColumn', false, ...
    'DisplayHeaderRow', false, ...
    'FilterTypeObject', ListType, ...
    'Project', pPROJ);
dblClickList = handle.listener(mdlList, 'Open', {@i_invokeOK, fH});

% Find the current model and select it
pItems = mdlList.Items;
for n = 1:length(pItems)
    if pItems(n).getdata==pMdlExpr
        mdlList.SelectedItems = pItems(n);
        break
    end
end

% OK and cancel buttons

btOK = uicontrol('Parent', fH, ...
    'Style', 'pushbutton', ...
    'String', 'OK', ...
    'Enable', 'off',...
    'Callback', @(s,e)set(fH, 'Tag', 'ok', 'Visible', 'off'));
btCanc = uicontrol('Parent', fH, ...
    'Style', 'pushbutton', ...
    'String', 'Cancel', ...
    'Callback', @(s,e)set(fH, 'Tag', 'cancel', 'Visible', 'off'));
btHelp = cghelpbutton(fH, 'CGFEATURESELECTMODEL');

lyt = xreggridbaglayout(fH, ...
    'packstatus', 'off', ...
    'dimension', [4 4], ...
    'rowsizes', [15 -1 3 25], ...
    'colsizes', [-1 65 65 65], ...
    'gapx', 7, ...
    'gapy', 2, ...
    'border', [7 7 7 10], ...
    'mergeblock', {[1 1], [1 4]}, ...
    'mergeblock', {[2 2], [1 4]}, ...
    'elements', {t, mdlList, [], [], ....
        [], [], [], btOK, ...
        [], [], [], btCanc, ...
        [], [], [], btHelp});

fH.LayoutManager = lyt;
set(lyt, 'packstatus', 'on');


selectionList = handle.listener(mdlList, mdlList.findprop('SelectedIndex'), 'PropertyPostSet', {@i_selectionChanged, btOK}); 


validSel = false;
pNewMdlExpr= xregpointer;
while ~validSel
    set(fH, 'Tag', 'Feature_ModelSelection');
    fH.showDialog(btOK);
    
    % GUI goes modal and execution blocks here
    
    tg = get(fH, 'Tag');
    if strcmp(tg, 'ok')
        % Get selection and check the model type is OK
        pNewMdlNode = mdlList.SelectedItems;
        validSel = true;
        ok= true;
        if ~isempty(pNewMdlNode)
            pNewMdlExpr = pNewMdlNode.getdata;
        end
    else
        % Cancel has been pressed
         validSel = true;
         ok= false;
    end
end

delete(fH);

function i_selectionChanged(src, evt, okbutton)
if ~isempty( evt.NewValue )
    set( okbutton, 'Enable', 'on' )
else
    set( okbutton, 'Enable', 'off' )
end
    

function i_invokeOK(src, evt, fH)
set(fH, 'Tag', 'ok', 'Visible', 'off');