% CB_IMPORTFROMOBJECT import pipelines from an object in the workspace.
%
% CALL: LTPDAworkbench.cb_importWBfromObjects
%
% M Hewitson 13-11-08
%
% $Id: cb_importWBfromObjects.m,v 1.4 2010/08/06 19:10:48 ingo Exp $
%
function cb_importWBfromObjects(varargin)
  
  wb = varargin{1};
  
  % Display an option dialog
  % - from workspace
  % - from file
  warning('off', 'MATLAB:JavaEDTAutoDelegation');
  cd = mpipeline.dialogs.FileOrWorkspaceDialog(wb.mp, true);
  cd.setVisible(true);
  warning('on', 'MATLAB:JavaEDTAutoDelegation');
  
  xml = '';
  if ~cd.isCancelled
    
    choice = cd.getChoice;
    
    if strcmp(choice, 'workspace')
      
      [objs, varnames] = getWorkspaceObjs();
      
      % check we have a least one object with an attached workbench
      haveWB = false;
      for kk=1:numel(objs)
        for ll=1:numel(objs{kk})
          if ~isempty(objs{kk}(ll).mdlfile)
            haveWB = true;
          end
        end
      end
      
      if ~haveWB
        utils.helper.errorDlg('The workspace contains no LTPDA objects with attached workbenches.', 'No workbench found');
      end
      
      % Add all objects to the workspace viewer that have an attached
      % workbench file.
      wd = mpipeline.dialogs.WorkspaceLister(wb.mp,true);
      for kk=1:numel(varnames)
        for ll=1:numel(objs{kk})
          objs{kk}(ll).mdlfile
          if ~isempty(objs{kk}(ll).mdlfile)
            wd.addObject(varnames(kk).name, varnames(kk).class, char(objs{kk}(ll)));
          end
        end
      end
      
      % launch the dialog
      warning('off', 'MATLAB:JavaEDTAutoDelegation');
      wd.setVisible(true);
      warning('on', 'MATLAB:JavaEDTAutoDelegation');
      
      % check what the user did
      if ~wd.isCancelled
        % get the selected object
        wsobj = wd.getSelection;
        obj = [];
        if ~isempty(wsobj)
          % look for the matching object in the MATLAB workspace
          for kk=1:numel(objs)
            if strcmp(varnames(kk).name, char(wsobj.getName)) && ...
                strcmp(varnames(kk).class, char(wsobj.getObjectclass))
              obj = objs{kk};
            end
          end
        end
        if ~isempty(obj)
          % get xml file
          xml = obj.mdlfile;
        end
      end
      
      
    elseif strcmp(choice, 'file')
      
      [filename, pathname] = uigetfile({'*.xml', 'Pick an XML object file'; '*.mat', 'Pick a MAT object file'});
      if isequal(filename,0) || isequal(pathname,0)
      else
        fname = fullfile(pathname, filename);
        try
          obj = ao(fname);
          xml = obj.mdlfile;
          if isempty(xml)
            error('### AO from file %s has no attached workspace.', fname);
          end
          
        catch
          lasterr
          error('### Failed to load an AO from the given file.');
        end
      end % End empty filename
    else
      error('### unknown choice');
    end
  end
  
  % Get the XML string and pass back to the workbench
  if ~isempty(xml)
    wb.mp.createPipelinesFromXMLstring(xml);
  end
  
end

%---------------- Get a list of LTPDA objects in the MATLAB workspace
function [objs, varnames] = getWorkspaceObjs()
  
  % get base workspace variables
  ws_vars = evalin('base','whos');
  
  objs = {};
  varnames = [];
  for j=1:length(ws_vars)
    cmd = sprintf('obj = evalin(''base'', ''%s'');', ws_vars(j).name);
    eval(cmd)
    if isa(obj, 'ltpda_uo')
      for kk=1:numel(obj)
        objs = [objs {obj(kk)}];
        varnames = [varnames ws_vars(j)];
      end
    end
  end
end

