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

    function varargout = LinkDlg(D,action,varargin) %#ok<INUSL>
%LINKDLG

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


if nargin>1 && ischar(action)
    switch lower(action)
        case 'get_callbacks'
            varargout{1} = i_GetCallbacks;
        case 'createpage'
            [varargout{1:2}] = createPage(varargin{:});   
    end
else
    cb = i_GetCallbacks;
    varargout = {action cb};
end

%-----------------------------------------------------------------------
function cb = i_GetCallbacks
%-----------------------------------------------------------------------
cb = [];
cb.Show = @i_Show;
cb.View = @i_View;
cb.Setup = @i_Setup;
cb.Click = @i_Click;
cb.Enable = @i_Enable;

function [d,newview]=createPage(d)

cb = i_GetCallbacks;

newview = struct('ID','linkdlg',...
    'icon','cglinkbut.bmp',...
    'tooltip','Link Factors',...
    'label','&Links...',...
    'dialog',1,...
    'card','dlg',...
    'tpclick',cb.Click,...
    'bmclick',cb.Click,...
    'enablestatus',cb.Enable,...
    'drawcb',cb.Setup,...
    'view',cb.View,...
    'show',cb.Show);

%-----------------------------------------------------------------------
function [d,lyt] = i_Setup(d)
%-----------------------------------------------------------------------
lyt = [];
return

%-----------------------------------------------------------------------
function en = i_Enable(op)
%-----------------------------------------------------------------------
en = ~isempty(op);

%-----------------------------------------------------------------------
function d = i_Show(d)
%-----------------------------------------------------------------------
set(d.Dlg.Lyt,'Visible','off');
el = get(d.Dlg.MainLyt,'elements');
rows = get(d.Dlg.MainLyt,'rowsizes');
el{3} = d.Dlg.Checks; rows(3) = 30;
set(d.Dlg.MainLyt,'elements',el,...
    'rowsizes',rows);
set(d.Dlg.Figure,'Name','Link Data Set');

page = d.ViewInfo(d.currentviewinfo);
d.Dlg.LeftList.SelectionCallback = page.tpclick;
d.Dlg.RightList.SelectionCallback = page.bmclick;

set(d.Dlg.ButtonCard, 'currentcard', 1);
set(d.Dlg.OK, 'Callback',@cb_OK);
set(d.Dlg.Cancel, 'Enable','on','Callback',@cb_Cancel);

%%%%% UNITS CHECKBOX - TO BE ENABLED SOMETIME AFTER VERSION 1 %%%%%%%
%set(d.Dlg.Check1,'callback',@cb_Units,...
%    'value',d.CheckUnits,...
%    'string','Check units');
%set(d.Dlg.Checks,'elements',{d.Dlg.Check1 [] [] []});

% Remove this line when units are disabled
set(d.Dlg.Checks,'elements',{[] [] [] []});

xregGui.iconuicontrol(d.Dlg.TopButton,...
    'imagefile',cgrespath('cglinkbut.bmp'),...
    'transparentcolor',[0 255 0],...
    'tooltip','Link factor',...
    'enable','on',...
    'callback',@cb_Link);
xregGui.iconuicontrol(d.Dlg.BottomButton,...
    'imagefile',cgrespath('cgbreaklinkbut.bmp'),...
    'transparentcolor',[0 255 0],...
    'enable','on',...
    'tooltip','Break link',...
    'callback',@cb_BreakLink);
set(d.Dlg.InfoText, 'String', 'Select factor in left list and link in right list.');
d.LinkDlg.LeftIndex = [];
d.LinkDlg.RightIndex = [];
% Take a copy of the current dataset.
d.Dlg.tmp = d.pD.info;

% Work out possible groups - cannot allow more than one assignment per group.
% Build up a temp oppoint containing all inputs.
%d.Dlg.tmp = CheckGroup(d.Dlg.tmp);
d.LinkDlg.Group = get(d.Dlg.tmp,'group');

set(d.Dlg.Lyt,'Visible','on');
set(d.Dlg.Figure,'Visible','on','CloseRequestFcn',@cb_Cancel, 'WindowStyle', 'modal');

%-----------------------------------------------------------------------
function d = i_View(d)
%-----------------------------------------------------------------------
d = i_RefreshLeftList(d);
d = i_RefreshRightList(d);
d = i_ButtonState(d);

%-----------------------------------------------------------------------
function d = i_Message(d,mess)
%-----------------------------------------------------------------------
if ~isempty(mess)
    set(d.Dlg.MsgPane,'String',mess);
    pause(1.5);
end
set(d.Dlg.MsgPane,'String','');
%------------------------------------------------------------------
function fstr = i_prettycell(namec)
%------------------------------------------------------------------
fstr = '';
for i = 1:length(namec)
    fstr = [fstr namec{i} ', ']; %#ok<*AGROW>
end
fstr = fstr(1:end-2);

%-----------------------------------------------------------------------
function cb_OK(varargin)
%-----------------------------------------------------------------------
% Go back to where we were before
d = pr_GetViewData;
d.Dlg.tmp = eval_fill(d.Dlg.tmp);
d.pD.info = d.Dlg.tmp;
% Addition - clear the status message
d = i_Message(d, '');
set(d.Dlg.Figure,'Visible','off');
d.Exprs.recalc = [0 1 1 0];
d = pr_ChangeView(d,d.OldViewID);
pr_SetViewData(d);

%-----------------------------------------------------------------------
function cb_Cancel(varargin)
%-----------------------------------------------------------------------
% Go back to where we were before
%  (Been working on a temp copy of the dataset, so don't need to do
%   anything to restore old version)
d = pr_GetViewData;
if isfield(d,'nd') && isvalid(address(d.nd))
    % Addition - clear the status message
    d = i_Message(d, '');
    set(d.Dlg.Figure,'Visible','off');
    d = pr_ChangeView(d,d.OldViewID);
    % delete(d.Dlg.Figure);
    % d.Dlg.Figure = [];
    pr_SetViewData(d);
end

%-----------------------------------------------------------------------
function cb_Link(varargin)
%-----------------------------------------------------------------------
d = pr_GetViewData;
fact_i = d.Dlg.LeftList.Selected;
indR = d.Dlg.RightList.Selected;
ptr = d.Exprs.ptrs(indR);
allptrstoFacs = get(d.Dlg.tmp, 'ptrlist');
ptrtobelinked = allptrstoFacs(fact_i);
link_i = d.Exprs.factor_index(indR);

mess = ''; err = 0;

if ~isvalid(ptr)
    % create new
    [d.Dlg.tmp,~,ptr] = AddCage(d.Dlg.tmp,link_i);
    %  Bit of a fudge here - rely on this to select link.
    d.Exprs.ptrs(indR) = ptr;
end

%---
%factors = get(d.Dlg.tmp,'orig_name');
% Already linked - show message
if isLink(d.Dlg.tmp,fact_i)
    lptr = getlink(d.Dlg.tmp,fact_i);
    if isequal(double(lptr), double(ptr))
        % Trying to link factor to the thing it's already linked to
        mess = [ptrtobelinked.getname ' is already linked to ' lptr.getname '.'];
    else
        mess = ['Breaking link to ' lptr.getname '.'];
    end
end
% factor in group?  Can only assign one thing - unassign other ones.
group = get(d.Dlg.tmp,'group');
gno = group(fact_i);
if gno && ~err
    allgroup_i = find(group==gno);
    group_i = setdiff(allgroup_i,fact_i);
    f = find(isLink(d.Dlg.tmp,group_i));
    if ~isempty(f)
        old_link_ptrs = getlink(d.Dlg.tmp,f);
        if ~all(old_link_ptrs == ptr)
            d.Dlg.tmp = BreakLink(d.Dlg.tmp,group_i);
            mess = ['Only one link permitted within group ' ...
                    i_prettycell(d.Exprs.names(allgroup_i)) '.'];
        end
    end
end
if ~err
    d.Dlg.tmp = Link(d.Dlg.tmp,fact_i,ptr);
end
d = i_Message(d,mess);
d = i_View(d);
pr_SetViewData(d);

%-----------------------------------------------------------------------
function cb_BreakLink(varargin)
%-----------------------------------------------------------------------
d = pr_GetViewData;
fact_i = d.Dlg.LeftList.Selected;

mess = '';
d.Dlg.tmp = BreakLink(d.Dlg.tmp,fact_i);
d = i_Message(d,mess);
d = i_View(d);
pr_SetViewData(d);

%-----------------------------------------------------------------------
function d = i_ButtonState(d)
%-----------------------------------------------------------------------
fact_i = d.Dlg.LeftList.Selected;
indR = d.Dlg.RightList.Selected;
if ~isempty(indR)
    set(d.Dlg.TopButton,'Enable','on');
else
    set(d.Dlg.TopButton,'Enable','off');
end
if isLink(d.Dlg.tmp,fact_i)
    set(d.Dlg.BottomButton,'Enable','on');
else
    set(d.Dlg.BottomButton,'Enable','off');
end

%-----------------------------------------------------------------------
function d = i_Click(d,list)
%-----------------------------------------------------------------------
switch list
case 'top'
    deselect(d.Dlg.RightList);
    d = i_RefreshRightList(d);
end
d = i_ButtonState(d);

%-----------------------------------------------------------------------
function d = i_RefreshLeftList(d)
%-----------------------------------------------------------------------
list = d.Dlg.LeftList;
select_index = list.Selected;
title = 'Data Set Factors';
[ListData,IconData,ind] = i_getLeftListData(d);
if isempty(ind)
    % Nothing to select

    configureEmptyList(list,...
        'No data set factors may be linked',title);    
    
else
    heads = {'Name' 'Units' 'Linked'};
    widths = [150 80 80];
    if ~d.CheckUnits
        heads(2)=[];
        widths(2)=[];
    end
    
    list.MultiSelect = false;
    configureList(list,heads,widths,title);
    % Index variable
    d.LinkDlg.LeftIndex = length(heads)-1;
    
    refreshList(list,ListData,IconData,ind,select_index);
end




%-----------------------------------------------------------------------
function [ListData,IconData,ind] = i_getLeftListData(d)


ind = find(canLink(d.Dlg.tmp));

islink = isLink(d.Dlg.tmp);
n = length(ind);
ListData = cell(n,3);
IconData = cell(n,2);

found = true(n,1);
for i = 1:n
    index = d.Exprs.shown_factors(ind(i));
    if index
        ListData{i,1} = d.Exprs.names{index};
        ListData{i,2} = d.Exprs.unitchar{index};
        IconData{i,1} = d.Exprs.tpiconfiles{index};
        
        if islink(ind(i))
            lptr = getlink(d.Dlg.tmp,ind(i));
            ListData{i,3} = lptr.getname;
            icon = 'cglink.bmp';
            IconData{i,2} = icon;
        else
            ListData{i,3} = '';
            IconData{i,2} = '';
        end
    else
        % remove factors that can't be found
        found(i)= false;
    end
end

if ~all(found)
   ListData= ListData(found,:);
   IconData= IconData(found,:);
   ind = ind(found);
end

if ~d.CheckUnits
    % remove units
    ListData(:,2)= [];
end

%-----------------------------------------------------------------------
function d = i_RefreshRightList(d)
%-----------------------------------------------------------------------
% This call ensures that the lists are updated with the latest changes
% In particular, stops the right hand list claiming that x is linked to
% y when you've just broken the link
savedOppt = d.pD.info;
d.pD.info = d.Dlg.tmp;
d.Exprs = pr_makeProjectDisplay(d.Exprs,d.pD,d.nd,d.ILmanager, [0 0 1 0]);
fact_i = d.Dlg.LeftList.Selected;
list = d.Dlg.RightList;
select_index = list.Selected;
d.pD.info = savedOppt;
title = 'Possible Links';
if isempty(fact_i)
    % Nothing selected
    
    configureEmptyList(d.Dlg.RightList,...
        '<- Select a data set factor.',title);
else
    
    % Work out what is valid link for this factor (unassigned factors can be linked)
    ind = find(isValidLink(d.Dlg.tmp, fact_i, d.Exprs.ptrs, d.Exprs.names));
    if isempty(ind)
        % Nothing selected
        
        configureEmptyList(d.Dlg.RightList,...
            'No valid links for this factor.',title);        
    else
        
        heads = {'Name' 'Units' 'Type'};
        widths = [150 80 80 ];
        if ~d.CheckUnits
            heads(2)=[];
            widths(2)=[];
        end
        list.MultiSelect = false;
        configureList(list,heads,widths,title);        
        
        
        d.LinkDlg.RightIndex = length(heads)-1;
        lptr = getlink(d.Dlg.tmp,fact_i);
        if isvalid(lptr) && isempty(select_index)
             select_index = ind(d.Exprs.ptrs(ind)==lptr);
        end

        [ListData,IconData,ind] = i_getRightListData(d,ind);
        refreshList(list,ListData,IconData,ind,select_index);

    end
end



%-----------------------------------------------------------------------
function [ListData,IconData,ind] = i_getRightListData(d,ind)


n = length(ind);
ListData = cell(n,3);
IconData = cell(n,1);

for i = 1:n
    ListData{i,1}= d.Exprs.names{ind(i)};
    IconData{i,1} = d.Exprs.tpiconfiles{ind(i)};
    ListData{i,2}= d.Exprs.unitchar{ind(i)};
    
    ListData{i,3}= d.Exprs.types{ind(i)};
end
if ~d.CheckUnits
   ListData(:,2)=[];
end