function new_session_info = rri_changepath_se(varargin)
%
% usage: rri_changepath_se(old_session_info, title)
%

   if nargin > 0 & isstruct(varargin{1}) 	% create figure

      old_session_info = '';
      tit_nam = 'Change PLS Session Path';

      if (nargin >= 1), old_session_info = varargin{1}; end;
      if (nargin >= 2), tit_nam = varargin{2}; end;

      init(old_session_info, tit_nam);
      uiwait;

      new_session_info = getappdata(gcf,'new_session_info');

      close(gcf);
      return;
   elseif nargin == 0 | ~iscell(varargin{1})
      msg = 'Invalid Session Profile';
      msgbox (msg, 'Error');

      return;
   end;

   %  clear the message line,
   %
   h = findobj(gcf,'Tag','MessageLine');
   set(h,'String','');

   action = upper(varargin{1}{1});

   if strcmp(action,'BROWSE1_BUTTON_PRESSED'),
      new_path1_edit_hdl = findobj(gcf,'tag','NewPath1Edit');
      newpath1 = get(new_path1_edit_hdl,'string');
      newpath1 = rri_getdirectory({newpath1});
      if ~isempty(newpath1), set(new_path1_edit_hdl,'string',newpath1); end;
   elseif strcmp(action,'BROWSE2_BUTTON_PRESSED'),
      new_path2_edit_hdl = findobj(gcf,'tag','NewPath2Edit');
      newpath2 = get(new_path2_edit_hdl,'string');
      newpath2 = rri_getdirectory({newpath2});
      if ~isempty(newpath2), set(new_path2_edit_hdl,'string',newpath2); end;
   elseif strcmp(action,'DONE_BUTTON_PRESSED'),
      change_path;
      uiresume;
   elseif strcmp(action,'CANCEL_BUTTON_PRESSED'),
      setappdata(gcf,'new_session_info',getappdata(gcf,'old_session_info'));
      uiresume;
   elseif strcmp(action,'DELETE_FIG')
      delete_fig;
   end;

   return;


% --------------------------------------------------------------------
function init(old_session_info, tit_nam)

   save_setting_status = 'on';
   rri_changepath_pos = [];

   try
      load('pls_profile');
   catch
   end

   if ~isempty(rri_changepath_pos) & strcmp(save_setting_status,'on')

      pos = rri_changepath_pos;

   else

      w = 0.7;
      h = 0.4;
      x = (1-w)/2;
      y = (1-h)/2;

      pos = [x y w h];

   end

   h0 = figure('Color',[0.8 0.8 0.8], ...
        'Units','normal', ...
        'Name',tit_nam, ...
        'NumberTitle','off', ...
        'MenuBar','none', ...
        'Position',pos, ...
        'DeleteFcn','rri_changepath_se({''DELETE_FIG''});', ...
        'WindowStyle', 'normal', ...
        'Tag','GetFilesFigure', ...
        'ToolBar','none');

   left_margin = 0.05;
   text_height = 0.1;

   fnt = 12;

   x = left_margin;
   y = 0.8;
   w = 0.3;
   h = text_height;

   pos = [x y w h];

   h1 = uicontrol('Parent',h0, ...            % old path1 label
        'Style','text', ...
        'Units','normal', ...
        'BackgroundColor',[0.8 0.8 0.8], ...
	'fontunit','point', ...
   	'FontSize',fnt, ...
        'HorizontalAlignment','left', ...
        'ListboxTop',0, ...
        'Position',pos, ...
        'String','Old session path:', ...
        'Tag','OldPath1Label');

   x = 0.53;

   pos = [x y w h];

   h1 = uicontrol('Parent',h0, ...            % old path2 label
        'Style','text', ...
        'Units','normal', ...
        'BackgroundColor',[0.8 0.8 0.8], ...
	'fontunit','point', ...
   	'FontSize',fnt, ...
        'HorizontalAlignment','left', ...
        'ListboxTop',0, ...
        'Position',pos, ...
        'String','Old data path:', ...
        'Tag','OldPath2Label');

   x = left_margin;
   y = 0.7;

   pos = [x y w h];

   h1 = uicontrol('Parent',h0, ...            % old path1 edit
        'Style','edit', ...
        'Units','normal', ...
        'BackgroundColor',[0.9 0.9 0.9], ...
	'fontunit','point', ...
   	'FontSize',fnt, ...
        'HorizontalAlignment','left', ...
        'ListboxTop',0, ...
        'Position',pos, ...
        'String','N/A', ...
        'Tag','OldPath1Edit');

   x = 0.53;

   pos = [x y w h];

   h1 = uicontrol('Parent',h0, ...            % old path2 edit
        'Style','edit', ...
        'Units','normal', ...
        'BackgroundColor',[0.9 0.9 0.9], ...
	'fontunit','point', ...
   	'FontSize',fnt, ...
        'HorizontalAlignment','left', ...
        'ListboxTop',0, ...
        'Position',pos, ...
        'String','N/A', ...
        'Tag','OldPath2Edit');

   x = left_margin;
   y = 0.5;

   pos = [x y w h];

   h1 = uicontrol('Parent',h0, ...            % new path1 label
        'Style','text', ...
        'Units','normal', ...
        'BackgroundColor',[0.8 0.8 0.8], ...
	'fontunit','point', ...
   	'FontSize',fnt, ...
        'HorizontalAlignment','left', ...
        'ListboxTop',0, ...
        'Position',pos, ...
        'String','New session path:', ...
        'Tag','NewPath1Label');

   x = 0.53;

   pos = [x y w h];

   h1 = uicontrol('Parent',h0, ...            % new path2 label
        'Style','text', ...
        'Units','normal', ...
        'BackgroundColor',[0.8 0.8 0.8], ...
	'fontunit','point', ...
   	'FontSize',fnt, ...
        'HorizontalAlignment','left', ...
        'ListboxTop',0, ...
        'Position',pos, ...
        'String','New data path:', ...
        'Tag','NewPath2Label');

   x = left_margin;
   y = 0.4;

   pos = [x y w h];

   h1 = uicontrol('Parent',h0, ...            % new path1 edit
        'Style','edit', ...
        'Units','normal', ...
        'BackgroundColor',[1 1 1], ...
	'fontunit','point', ...
   	'FontSize',fnt, ...
        'HorizontalAlignment','left', ...
        'ListboxTop',0, ...
        'Position',pos, ...
        'String','', ...
        'Tag','NewPath1Edit');

   x = x + w;
   w = 0.12;

   pos = [x y w h];

   h1 = uicontrol('Parent',h0, ...            % new path1 browse
        'Units','normal', ...
	'fontunit','point', ...
   	'FontSize',fnt, ...
        'ListboxTop',0, ...
        'Position',pos, ...
        'HorizontalAlignment','center', ...
        'String','Browse', ...
        'Callback','rri_changepath_se({''BROWSE1_BUTTON_PRESSED''});', ...
        'Tag','NewPath1Browse');

   x = 0.53;
   w = 0.3;

   pos = [x y w h];

   h1 = uicontrol('Parent',h0, ...            % new path2 edit
        'Style','edit', ...
        'Units','normal', ...
        'BackgroundColor',[1 1 1], ...
	'fontunit','point', ...
   	'FontSize',fnt, ...
        'HorizontalAlignment','left', ...
        'ListboxTop',0, ...
        'Position',pos, ...
        'String','', ...
        'Tag','NewPath2Edit');

   x = x + w;
   w = 0.12;

   pos = [x y w h];

   h1 = uicontrol('Parent',h0, ...            % new path2 browse
        'Units','normal', ...
	'fontunit','point', ...
   	'FontSize',fnt, ...
        'ListboxTop',0, ...
        'Position',pos, ...
        'HorizontalAlignment','center', ...
        'String','Browse', ...
        'Callback','rri_changepath_se({''BROWSE2_BUTTON_PRESSED''});', ...
        'Tag','newPath2Browse');

   x = left_margin;
   y = 0.15;
   w = 0.15;

   pos = [x y w h];

   h1 = uicontrol('Parent',h0, ...                      % DONE
        'Units','normal', ...
	'fontunit','point', ...
   	'FontSize',fnt, ...
        'ListboxTop',0, ...
        'Position',pos, ...
        'HorizontalAlignment','center', ...
        'String','Change', ...
        'Callback','rri_changepath_se({''DONE_BUTTON_PRESSED''});', ...
        'Tag','DONEButton');

   x = x+w+0.05;

   pos = [x y w h];

   h1 = uicontrol('Parent',h0, ...                      % CANCEL
        'Units','normal', ...
	'fontunit','point', ...
   	'FontSize',fnt, ...
        'ListboxTop',0, ...
        'Position',pos, ...
        'HorizontalAlignment','center', ...
        'String','Close', ...
        'Callback','rri_changepath_se({''CANCEL_BUTTON_PRESSED''});', ...
        'Tag','CANCELButton');

   x = x+w+0.06;
   y = 0.15;
   w = 1-left_margin-x;

   pos = [x y w h];

   h1 = uicontrol('Parent',h0, ...            % notes
        'Style','text', ...
        'Units','normal', ...
        'BackgroundColor',[0.8 0.8 0.8], ...
	'fontunit','point', ...
   	'FontSize',fnt, ...
        'HorizontalAlignment','left', ...
        'ListboxTop',0, ...
        'Position',pos, ...
        'String','Notes: Do not include subject or run sub-folder in data path', ...
        'Tag','noteslabel');

   x = .01;
   y = 0;
   w = 1;

   pos = [x y w h];

   c = uicontrol('Parent',h0, ...		% Message Line
   	'Style','text', ...
   	'Units','normal', ...
   	'BackgroundColor',[0.8 0.8 0.8], ...
   	'ForegroundColor',[0.8 0.0 0.0], ...
	'fontunit','point', ...
   	'FontSize',fnt, ...
   	'HorizontalAlignment','left', ...
   	'Position',pos, ...
   	'String','', ...
   	'Tag','MessageLine');

   setappdata(gcf,'old_session_info',old_session_info);
   setappdata(gcf,'new_session_info',old_session_info);

   set_field;

   return;					% init


%----------------------------------------------------------------------------
function set_field

   session_info = getappdata(gcf,'old_session_info');
   str1 = session_info.pls_data_path;

   if isfield(session_info,'subject')
      str2 = rri_fileparts(session_info.subject{1});
   elseif isfield(session_info,'run')
      str2 = rri_fileparts(session_info.run(1).data_path);
   else
      str2 = '';
   end

   old_path1_edit_hdl = findobj(gcf,'tag','OldPath1Edit');
   old_path2_edit_hdl = findobj(gcf,'tag','OldPath2Edit');
   new_path1_edit_hdl = findobj(gcf,'tag','NewPath1Edit');
   new_path2_edit_hdl = findobj(gcf,'tag','NewPath2Edit');

   set(old_path1_edit_hdl,'string',str1);
   set(old_path2_edit_hdl,'string',str2);
   set(new_path1_edit_hdl,'string',str1);
   set(new_path2_edit_hdl,'string',str2);

   return;					% set_field


%----------------------------------------------------------------------------
function delete_fig

    try
       load('pls_profile');
       pls_profile = which('pls_profile.mat');

       rri_changepath_pos = get(gcbf,'position');

       save(pls_profile, '-append', 'rri_changepath_pos');
    catch
    end

   return;					% delete_fig


%----------------------------------------------------------------------------
function change_path

   session_info = getappdata(gcf,'new_session_info');
   session_info.pls_data_path = get(findobj(gcf,'tag','NewPath1Edit'),'string');

   new_str = get(findobj(gcf,'tag','NewPath2Edit'),'string');

   if isfield(session_info,'subject')
      for i = 1:length(session_info.subject)
         [p f] = rri_fileparts(session_info.subject{i});
         session_info.subject{i} = fullfile(new_str,f);
      end
   elseif isfield(session_info,'run')
      for i = 1:length(session_info.run)
         [p f] = rri_fileparts(session_info.run(i).data_path);
         session_info.run(i).data_path = fullfile(new_str,f);
      end
   end

   setappdata(gcf,'new_session_info',session_info);

   return;					% change_path

