function refresh(ViewerObj)
%REFRESH  Refresh systems in LTI Viewer

%   Note: User will be prompted before any changes are made
%
%   Author(s): A. DiVergilio
%   Copyright 1986-2000 The MathWorks, Inc. 
%   $Revision: 1.3 $  $Date: 2000/06/14 20:03:15 $

status(ViewerObj,'Refreshing systems...');

%---Watch on
set(ViewerObj.Handle,'Pointer','watch');

Cancel = 0;

SystemNames = ViewerObj.SystemNames;
Systems = ViewerObj.Systems;
PlotStyles = ViewerObj.PlotStyles;

%---Valid system size
if ~isempty(Systems)
   [numout,numin] = size(Systems{1});
else
   numout = 0;
   numin = 0;
end

%---Check status of systems vs workspace
[NewSystems,SystemsStatus] = systemstat(SystemNames,numout,numin);
IsDeleted  = find(SystemsStatus==-1);
IsDiffSize = find(SystemsStatus==0);
IsSameSize = find(SystemsStatus==1);
for n=1:length(IsSameSize)
   if ~isequal(Systems{IsSameSize(n)},NewSystems{IsSameSize(n)})
      SystemsStatus(IsSameSize(n)) = 2;
   end
end
IsChanged = find(SystemsStatus==2);

%---Notify user that systems of different size will not be updated (default 'continue')
if ~Cancel & length(IsDiffSize)>0
   Str = cat(1,...
         {'The following systems have a different number of';...
          'inputs and/or outputs than those currently displayed:';...
          ''},...
          strcat({'     '},SystemNames(IsDiffSize,:)),...
         {'';...
          'These systems will not be imported.';...
          ''});
   switch questdlg(Str,'Updating the LTI Viewer','Continue','Cancel','Continue')
   case 'Cancel'
      Cancel = 1;
   end
end

%---Ask user if old systems should be removed (default 'no')
if ~Cancel & length(IsDeleted)>0
   Str = cat(1,...
         {'The following systems are no longer in the workspace:';...
          ''},...
          strcat({'     '},SystemNames(IsDeleted,:)),...
         {'';...
          'Do you want to remove these systems from the LTI Viewer?';...
          ''});
   switch questdlg(Str,'Updating the LTI Viewer','No')
   case 'Yes'
      status(ViewerObj,'Removing old systems...');
      deletesys(ViewerObj,IsDeleted);
      status(ViewerObj,'Refreshing systems...');
   case 'Cancel'
      Cancel = 1;
   end
end

%---Confirm update to systems which have been altered (default 'yes')
if ~Cancel & length(IsChanged)>0
   Str = cat(1,...
         {'The following systems have changed in the workspace:';...
          ''},...
          strcat({'     '},SystemNames(IsChanged,:)),...
         {'';...
          'Do you want to reflect these changes in the LTI Viewer?';...
          ''});
   switch questdlg(Str,'Updating the LTI Viewer')
   case 'Yes'
      SystemData = struct('Names',{SystemNames(IsChanged,:)},...
         'Systems',{NewSystems(IsChanged,:)},...
         'PlotStyles',{PlotStyles(IsChanged,:)});
      rguifcn('addsystems',ViewerObj.Handle,SystemData,0)
   case 'Cancel'
      Cancel = 1;
   end
end

if Cancel
   status(ViewerObj,'Refresh canceled.',1);
else
   status(ViewerObj,'All systems now reflect the current Workspace values.',1);
end

%---Watch off
set(ViewerObj.Handle,'Pointer','arrow');
