% TABLE display the data from the AO in a table.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: TABLE display the data from the AO in a table.
%
% CALL:        table(ao)
% 
% NOTE: this does not support xyzdata objects.
%
% <a href="matlab:utils.helper.displayMethodInfo('ao', 'table')">Parameters Description</a>
%
% VERSION:     $Id: table.m,v 1.12 2011/04/08 08:56:18 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = table(varargin)
  
  % Check if this is a call for parameters
  if utils.helper.isinfocall(varargin{:})
    varargout{1} = getInfo(varargin{3});
    return
  end
  
  import utils.const.*
  utils.helper.msg(msg.PROC3, 'running %s/%s', mfilename('class'), mfilename);
  
  % Collect all AOs
  as = utils.helper.collect_objects(varargin(:), 'ao');
  pl = utils.helper.collect_objects(varargin(:), 'plist');
  
  pl = combine(pl, getDefaultPlist());
  
  singleTable = pl.find('singleTable');
  
  x = java.util.ArrayList();
  y = java.util.ArrayList();
  xt = java.util.ArrayList();
  yt = java.util.ArrayList();
  
  % Loop over AOs
  for jj = 1:numel(as)
    
    if isa(as(jj).data, 'xyzdata')
      % The table doesn'T work for xyzdata
      error('### The table doesn''t work for AOs with xyz-data');
    elseif isa(as(jj).data, 'cdata')
      for kk = 1:size(as(jj).y, 2)
        data = as(jj).y;
        y.add(data(:,kk));
        x.add([]);
        yt.add('const');
        xt.add([]);
      end
    else
      x.add(as(jj).x);
      y.add(as(jj).y);
      xt.add(['x ' char(as(jj).xunits)]);
      yt.add(['y ' char(as(jj).yunits)]);
    end
    
    if ~singleTable
      d = datatable.TableForm(x,y,xt,yt);
      d.setVisible(true);
      d.setTitle(as(jj).name);
      x.clear();
      y.clear();
      xt.clear();
      yt.clear();
    end
  end
  if singleTable
    d = datatable.TableForm(x,y,xt,yt);
    d.setVisible(true);
  end
end

%--------------------------------------------------------------------------
% Get Info Object
%--------------------------------------------------------------------------
function ii = getInfo(varargin)
  if nargin == 1 && strcmpi(varargin{1}, 'None')
    sets = {};
    pl   = [];
  else
    sets = {'Default'};
    pl   = getDefaultPlist;
  end
  % Build info object
  ii = minfo(mfilename, 'ao', 'ltpda', utils.const.categories.output, '$Id: table.m,v 1.12 2011/04/08 08:56:18 hewitson Exp $', sets, pl);
  ii.setModifier(false);
  ii.setOutmin(0);
end

%--------------------------------------------------------------------------
% Get Default Plist
%--------------------------------------------------------------------------
function plout = getDefaultPlist()
  persistent pl;  
  if exist('pl', 'var')==0 || isempty(pl)
    pl = buildplist();
  end
  plout = pl;  
end

function pl = buildplist()
  pl = plist();
  
  p = param({'singleTable','Display the AOs in a single talbe.'}, paramValue.TRUE_FALSE);
  pl.append(p);
  
end


