% cb_executePlan execute the execution plan
%
% M Hewitson 27-05-09
%
% $Id: cb_executePlan.m,v 1.4 2011/03/30 07:59:30 hewitson Exp $
%
function cb_executePlan(varargin)
  
  disp('*** executing plan...');
  
  wb = varargin{1};
  
  % Get all selected blocks
  execPlan = awtinvoke(wb.mp, 'getExecPlan');
  steps = execPlan.getSteps();
  
  wb.mp.setExecuting(true);
  
  try
    % Go through each step
    for kk=1:steps.size
      % Get the execution step
      step = steps.get(kk-1);
      if step.isActive
        % Get diagram
        bd = step.getDiag;
        disp('===================================');
        disp(['== Executing ' char(bd.getName) ]);
        disp('===================================');
        % set to active diagram
        bd.setVisible(true);
        bd.setSelected(true);
        % run this diagram
        res = wb.runPipeline;
        if ~res
          bname = questdlg(sprintf('Execution of pipeline "%s" failed. Do you want to continue?', char(bd.getName)), ...
            'Pipeline Execution Failure', ...
            'Continue', 'Abort', 'Abort');
          
          switch lower(bname)
            case 'continue'
            case 'abort'
              break;
          end
          
        end
      end
    end
    
  catch
    wb.mp.setExecuting(false);
  end
  
  wb.mp.setExecuting(false);
  
end
