% PLOTHISTORY plot the history of the currently selected blocks.
%
% CALL: wb.plot
%
% M Hewitson 13-11-08
%
% $Id: plotHistory.m,v 1.9 2010/08/06 19:10:49 ingo Exp $
%
function plotHistory(varargin)
  
  wb = varargin{1};
  
  % Get all selected blocks
  sbs = awtinvoke(wb.mp, 'getSelectedBlocks');
  
  outvars = {};
  for ii=0:sbs.size()-1
    block = sbs.get(ii);
    
    % if this is a MElementWithPorts
    if isa(block, 'mpipeline.canvas.MElementWithPorts')
      
      % loop over all outputs
      outports = block.getOutputs();
      for kk=0:outports.size()-1
        op = outports.get(kk);
        % get variable name
        outvar = LTPDAworkbench.getWS_VarName(wb.UUID, block, op.getNumber);
        outvars = [outvars {outvar}];
      end
    end
  end
  
  % Check if each var exists in the workspace, and then launch in
  % dotview
  
  try
    successes = 0;
    for kk=1:numel(outvars)
      
      if evalin('base', ['isa(' outvars{kk} ', ''ltpda_uoh'')'])
        
        cmd = [outvars{kk} '.viewHistory()'];
        try
          evalin('base', cmd);
        catch err
          utils.helper.err(err.message);
          cmd = ['plot(' outvars{kk} '.hist)'];
          evalin('base', cmd);
        end
        successes = successes + 1;
      else
        cl = evalin('base', ['class(' outvars{kk} ')']);
        warning('Skipping selected variable %s; it is not an LTPDA User Object with history. It is of class [%s].', outvars{kk}, cl);
      end
    end
  catch
    utils.helper.errorDlg('Unable to plot history of selected objects. Has the pipeline been executed or do you have more than one object?', 'Plot history error');
  end
  
  if successes ~= numel(outvars)
    utils.helper.warnDlg('Not all outputs were processed. Check the MATLAB terminal for further warnings.', 'Plot history warning');
  end
end

