% MPL2JPL converts a MATALB plist object into a java plist object.
%
% CALL:  jpl = LTPDAworkbench.mpl2jpl(mpl)
%
% M Hewitson 13-11-08
%
% $Id: mpl2jpl.m,v 1.15 2011/04/18 16:57:17 ingo Exp $
%
function jpl = mpl2jpl(mpl)
  % build java plist
  if isempty(mpl)
    jpl = mpipeline.plisttable.JPlist('');
  else
    jpl = mpipeline.plisttable.JPlist(mpl.name);
    for aa=1:numel(mpl.params)
      pv = mpipeline.plisttable.JParamValue;
      if isa(mpl.params(aa).val, 'paramValue')
        options = mpl.params(aa).val.getOptions;
        for ll=1:numel(options)
          pval = convertVal(options{ll});
          type = class(pval);
          pv.addOption(pval, type);
        end
        pv.setValIndex(mpl.params(aa).val.valIndex-1);
        pv.setSelection(mpl.params(aa).val.selection);
      else
        pval = convertVal(mpl.params(aa).val);
        pv.addOption(pval, class(pval));
      end
      p = mpipeline.plisttable.JParam(mpl.params(aa).key, pv, mpl.params(aa).desc);
      jpl.add(p);
    end
  end
end

function jval = convertVal(val)
  
  if ischar(val)
    jval = val;
  elseif isnumeric(val)
    if numel(val) == 1
      jval = val;
    else
      jval = utils.helper.mat2str(val);
    end
  elseif iscell(val)
    jval = utils.prog.mcell2str(val);
  elseif islogical(val)
    if val
      jval = true;
    else
      jval = false;
    end
  elseif isa(val, 'ltpda_nuo')
    jval = string(val);
  elseif isa(val, 'ltpda_uo')
    try
      jval = string(val);
    catch
      % The 'string' method will fail when the object have more than one
      % history step. In this case build we a PLIST which constructs the
      % object from the properties.
      pl = val.generateConstructorPlist();
      jval = string(pl);
    end
  else
    jval = char(val);
  end
  
end


