function fig = importdeletegui(this,action,class_str,size_str,SystemNames);
% IMPORTDELETEGUI Opens an Import/Delete Dialog for the LTI Viewer
  
%  Author(s): Kamesh Subbarao
%  Copyright 1986-2002 The MathWorks, Inc.

AllNames = SystemNames{:};
MaxName = size(AllNames,2);
NameBlanks = repmat(max(0,blanks(15-MaxName)),size(AllNames,1),1);
AllSize = strvcat(size_str{:});
MaxSize = size(AllSize,2);
SizeBlanks = repmat(max(0,blanks(13-MaxSize )),size(AllSize,1),1);
AllClass = strvcat(class_str{:});
ListStr = [AllNames,char(NameBlanks),AllSize,char(SizeBlanks),AllClass];

switch action
case 'import'
   TitleStr = 'Select the systems to import';
   HelpTag  =  'viewer_import';
   fig = LocalCreateDlg(this,action,ListStr,AllNames,TitleStr,HelpTag);
case 'delete'
   TitleStr = 'Select the systems to delete';
   HelpTag = 'viewer_delete';
   fig = LocalCreateDlg(this,action,ListStr,AllNames,TitleStr,HelpTag);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function fig = LocalCreateDlg(this,action,ListStr,AllNames,TitleStr,HelpTag);
% Creates the Import/Delete System Dialog
PointsToPixels = 72/get(0,'ScreenPixelsPerInch');
StdUnit = 'points';
UIColor = get(0,'DefaultUIControlBackground');
ud = struct('Parent',this.Figure,'SystemList',{AllNames});

h0 = figure('Color',UIColor,...
   'IntegerHandle','off',...
   'MenuBar','none',...
   'Name',xlate('LTI Browser'),...
   'NumberTitle','off',...
   'PaperPosition',PointsToPixels*[18 180 576 432],...
   'PaperUnits','points',...
   'Resize','off',...
   'Position',[106 135 320 267],...
   'Tag','LTIBrowserFigure',...
   'Visible','off',...
   'CloseRequestFcn',@LocalClose,...
   'WindowStyle','modal');

%---Position figure within ParentFig bounds
centerfig(h0,this.Figure);

h1 = uicontrol('Parent',h0, ...
   'Unit',StdUnit,...
   'BackgroundColor',UIColor, ...
   'Position',PointsToPixels*[16 243 290 20], ...
   'String',TitleStr, ...
   'Style','text');
h1 = uicontrol('Parent',h0, ...
   'Unit',StdUnit,...
   'BackgroundColor',UIColor, ...
   'Position',PointsToPixels*[21 222 63 20], ...
   'String','Name', ...
   'Style','text');
h1 = uicontrol('Parent',h0, ...
   'Unit',StdUnit,...
   'BackgroundColor',UIColor, ...
   'Position',PointsToPixels*[128.5 222 63 20], ...
   'String','Size', ...
   'Style','text');
h1 = uicontrol('Parent',h0, ...
   'Unit',StdUnit,...
   'BackgroundColor',UIColor, ...
   'Position',PointsToPixels*[236 221.5 63 20], ...
   'String','Class', ...
   'Style','text');

ud.SystemList = uicontrol('Parent',h0, ...
   'Unit',StdUnit,...
   'BackgroundColor',[1 1 1], ...
   'FontName','courier',...
   'Max',2,...
   'Position',PointsToPixels*[20 45 285 176], ...
   'String',ListStr, ...
   'Style','listbox', ...
   'Tag','SystemList', ...
   'UserData',AllNames, ...
   'Value',[]);
h1 = uicontrol('Parent',h0, ...
   'CallBack',{@LocalOK this,action,h0,ListStr,AllNames},...
   'Unit',StdUnit,...
   'BackgroundColor',UIColor, ...
   'UserData',TitleStr(end-5:end),...
   'Position',PointsToPixels*[20 10 55 25], ...
   'String','OK', ...
   'Tag','OkButton');
h1 = uicontrol('Parent',h0, ...
   'Callback',@LocalCancel,...
   'Unit',StdUnit,...
   'BackgroundColor',UIColor, ...
   'Position',PointsToPixels*[97 10 55 25], ...
   'String','Cancel', ...
   'Tag','CancelButton');
h1 = uicontrol('Parent',h0, ...
   'Callback',@LocalHelp,...
   'Unit',StdUnit,...
   'BackgroundColor',UIColor, ...
   'Position',PointsToPixels*[174 10 55 25], ...
   'String','Help', ...
   'Tag',HelpTag);
h1 = uicontrol('Parent',h0, ...
   'CallBack',{@LocalApply this,action,h0,ListStr,AllNames},...
   'Unit',StdUnit,...
   'BackgroundColor',UIColor, ...
   'UserData',TitleStr(end-5:end),...
   'Position',PointsToPixels*[250 10 55 25], ...
   'String','Apply', ...
   'Tag','ApplyButton');

set(h0,'UserData',ud,'Visible','on');

if nargout > 0, fig = h0; end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% LocalOK
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function LocalOK(eventSrc,eventData,this,action,h0,ListStr,AllNames)
set(h0,'pointer','watch');
LocalApply(eventSrc,eventData,this,action,h0,ListStr,AllNames);
set(h0,'pointer','arrow');
LocalClose(eventSrc,eventData);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% LocalApply
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function LocalApply(eventSrc,eventData,this,action,h0,ListStr,AllNames)

ud = get(h0,'UserData');
Vals = get(ud.SystemList,'Value');
if isempty(Vals)
    errordlg('No system selected for import/delete.','Systems Import/Delete','modal');
    return;
end
set(ud.SystemList,'Value',[]);
AllNames = cellstr(get(ud.SystemList,'UserData'));
if length(AllNames) & ~isempty(AllNames{1})
   SelectedNames = AllNames(Vals);
end

%---Names and values of systems to import/delete
SystemNames = cat(1,SelectedNames(:))';
sysValues   = cell(0,1);
switch action
case 'import'
    %---add systems from viewer
    for ct = 1:length(SystemNames)
        try
            sysValues(ct) = {evalin('base',SystemNames{ct})};
        catch
            lasterr;
        end
    end
    importsys(this,SystemNames,sysValues);
    
case 'delete'
    %---remove systems from viewer
    SystemStr = get(ud.SystemList,'String');
    SystemStr(Vals,:)=[];
    AllNames(Vals)=[]; 
    if isempty(AllNames),
        AllNames={''};
    end
    set(ud.SystemList,'String',SystemStr,'UserData',AllNames);
    deletesys(this,SystemNames);
    
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% LocalCancel
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function LocalCancel(eventSrc,eventData)
dlg = get(eventSrc,'Parent');
delete(dlg);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% LocalHelp
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function LocalHelp(eventSrc,eventData)
ctrlguihelp(get(gcbo,'Tag'));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% LocalCloseDlg
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function LocalClose(eventSrc,eventData)
dlg = get(eventSrc,'Parent');
delete(dlg);
