% CB_TABLE show the outputs of the currently selected blocks in a table.
%
% CALL: LTPDAworkbench.cb_table
%
% M Hewitson 13-11-08
%
% $Id: cb_table.m,v 1.4 2010/08/06 19:10:49 ingo Exp $
%
function cb_table(varargin)
  
  wb = varargin{1};
  
  % Get all selected blocks
  sbs = awtinvoke(wb.mp, 'getSelectedBlocks');
  
  failed = false;
  skipped = 0;
  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);
        
        % Check if the user have execute the pipeline.
        if (evalin('base', ['exist(''' strtok(outvar, '.') ''')']) ~= 1) || ...
            isempty(evalin('base', strtok(outvar, '.')))
          utils.helper.errorDlg('Failed to show one or more of the selected objects in tables. Has the pipeline been executed?', 'Table block outputs error');
          return
        end
        
        if evalin('base', ['isa(' outvar ', ''ao'')'])
          try
            cmd = ['table(' outvar ')'];
            evalin('base', cmd);
          catch
            failed = true;
          end
        else
          warning('!!! Skipping block output [%s] since it''s not an AO', outvar);
          skipped = skipped + 1;
        end
      end
    end
  end
  
  if failed
    utils.helper.errorDlg('Failed to show one or more of the selected objects in tables. Has the pipeline been executed?', 'Table block outputs error');
  end
  
  if skipped > 0
    utils.helper.warnDlg('Not all selected blocks could be tabulated. Check the MATLAB terminal for further warnings.');
  end
  
end

