%ERP_CREATE_MODIFY_UI user interface for erp_create_datamat &
%	erp_modify_datamat
%
%  USAGE: erp_create_modify_ui(ismodify)
%

%   Called by ERP_CREATE_DATAMAT, ERP_MODIFY_DATAMAT
%
%   I (session_file) - Matlab data file that contains a structure array
%			containing the session information for the study
%
%   Created on 30-DEC-2002 by Jimmy Shen
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function h01 = erp_create_modify_ui(ismodify)

   save_setting_status = 'on';
   erp_create_modify_pos = [];

   try
      load('pls_profile');
   catch
   end

   if ~isempty(erp_create_modify_pos) & strcmp(save_setting_status,'on')

      pos = erp_create_modify_pos;

   else

      w = 0.85;
      h = 0.58;
      x = (1-w)/2;
      y = (1-h)/2;

      pos = [x y w h];

   end

   h01 = figure('Units','normal', ...
   	'Position',pos, ...
        'Color',[0.8 0.8 0.8], ...
        'Name','Create Datamat', ...
        'MenuBar','none', ...
        'NumberTitle','off', ...
        'deletefcn','erp_modify_datamat(''delete_fig'');', ...
        'WindowStyle', 'normal', ...
   	'ToolBar','none');

   num_line = 8;
   factor_line = 1/(num_line+1);
   left_margin = 0.05;

   x = left_margin;
   y = (num_line-0) * factor_line;
   w = 0.2;
   h = 0.6 * factor_line;

   pos = [x y w h];

   h1 = uicontrol('Parent',h01, ...		% chan selection Label
        'Style','text', ...
        'Units','normal', ...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'FontUnits','normal', ...
        'FontSize',0.55, ...
        'HorizontalAlignment','center', ...
        'String','Channels', ...
        'Position',pos);

   y = (num_line-2)*factor_line;
   h = 2 * factor_line;

   pos = [x y w h];

   chan_lst_hdl = uicontrol('parent',h01, ...	% chan listbox
        'unit','normal', ...
        'style','listbox', ...
        'fontunit','normal', ...
        'fontsize',0.16, ...
        'max',2, ...
        'string',' ', ...
        'Callback','erp_modify_datamat(''select_channels'');', ...
        'position',pos);

   x = x+w+0.01;
   y = (num_line-0) * factor_line;
   w = 0.2;
   h = 0.6 * factor_line;

   pos = [x y w h];

   h1 = uicontrol('Parent',h01, ...		% subj selection Label
        'Style','text', ...
        'Units','normal', ...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'FontUnits','normal', ...
        'FontSize',0.55, ...
        'HorizontalAlignment','center', ...
        'String','Subjects', ...
        'Position',pos);

   y = (num_line-2)*factor_line;
   h = 2 * factor_line;

   pos = [x y w h];

   subj_lst_hdl = uicontrol('parent',h01, ...	% subj listbox
        'unit','normal', ...
        'style','listbox', ...
        'fontunit','normal', ...
        'fontsize',0.16, ...
        'max',2, ...
        'string',' ', ...
        'Callback','erp_modify_datamat(''select_subjects'');', ...
        'position',pos);

   x = x+w+0.01;
   y = (num_line-0) * factor_line;
   w = 0.23;
   h = 0.6 * factor_line;

   pos = [x y w h];

   h1 = uicontrol('Parent',h01, ...		% cond selection Label
        'Style','text', ...
        'Units','normal', ...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'FontUnits','normal', ...
        'FontSize',0.55, ...
        'HorizontalAlignment','center', ...
        'String','Conditions', ...
        'Position',pos);

   y = (num_line-2)*factor_line;
   h = 2 * factor_line;

   pos = [x y w h];

   cond_lst_hdl = uicontrol('parent',h01, ...	% cond listbox
        'unit','normal', ...
        'style','listbox', ...
        'fontunit','normal', ...
        'fontsize',0.16, ...
        'max',2, ...
        'string',' ', ...
        'Callback','erp_modify_datamat(''select_conditions'');', ...
        'position',pos);

   x = x+w+0.01;
   y = (num_line-0) * factor_line;
   w = 1 - x - left_margin;
   h = 0.6 * factor_line;

   pos = [x y w h];

   h1 = uicontrol('Parent',h01, ...		% behav selection Label
        'Style','text', ...
        'Units','normal', ...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'FontUnits','normal', ...
        'FontSize',0.55, ...
        'HorizontalAlignment','center', ...
        'String','Behaviors', ...
        'Position',pos);

   y = (num_line-2)*factor_line;
   h = 2 * factor_line;

   pos = [x y w h];

   behav_lst_hdl = uicontrol('parent',h01, ...	% behav listbox
        'unit','normal', ...
        'style','listbox', ...
        'fontunit','normal', ...
        'fontsize',0.16, ...
        'max',2, ...
        'string',' ', ...
        'Callback','erp_modify_datamat(''select_behav'');', ...
        'position',pos);

   x = left_margin;
   y = (num_line-3)*factor_line;
   w = 0.19;
   h = 0.6 * factor_line;

   pos = [x y w h];

   h1 = uicontrol('parent',h01, ... 		% select all channels
        'unit','normal', ...
        'style','push', ...
        'fontunit','normal', ...
        'fontsize',0.55, ...
        'string','Select All Channels', ...
        'callback', 'erp_modify_datamat(''select_all_chan'');', ...
        'position',pos);

   x = x+w+0.02;

   pos = [x y w h];

   h1 = uicontrol('parent',h01, ... 		% select all subjects
        'unit','normal', ...
        'style','push', ...
        'fontunit','normal', ...
        'fontsize',0.55, ...
        'string','Select All Subjects', ...
        'callback', 'erp_modify_datamat(''select_all_subj'');', ...
        'position',pos);

   x = x+w+0.02;

   pos = [x y w h];

   h1 = uicontrol('parent',h01, ... 		% select all conditions
        'unit','normal', ...
        'style','push', ...
        'fontunit','normal', ...
        'fontsize',0.55, ...
        'string','Select All Conditions', ...
        'callback', 'erp_modify_datamat(''select_all_cond'');', ...
        'position',pos);

   x = x+w+0.05;

   pos = [x y w h];

   h1 = uicontrol('parent',h01, ... 		% select all behav
        'unit','normal', ...
        'style','push', ...
        'fontunit','normal', ...
        'fontsize',0.55, ...
        'string','Select All Behaviors', ...
        'callback', 'erp_modify_datamat(''select_all_behav'');', ...
        'position',pos);

   x = left_margin;
   y = (num_line-4)*factor_line;
   h = 0.6 * factor_line;

   pos = [x y w h];

   h1 = uicontrol('parent',h01, ... 		% Edit Channel Order
        'unit','normal', ...
        'style','push', ...
        'fontunit','normal', ...
        'fontsize',0.55, ...
        'string','Edit Channel Order ...', ...
        'callback', 'erp_modify_datamat(''edit_chan_order'');', ...
        'position',pos);

   if ~ismodify

      x = x+w+0.02;
      x = x+w+0.02;

      pos = [x y w h];

      h1 = uicontrol('parent',h01, ... 		% Merge Conditions
        'unit','normal', ...
        'style','push', ...
        'fontunit','normal', ...
        'fontsize',0.55, ...
        'string','Merge Conditions ...', ...
        'callback', 'erp_create_datamat(''merge_conditions'');', ...
        'position',pos);

   end

   x = left_margin;
   y = (num_line-5)*factor_line;
   w = 0.16;
   h = 0.6 * factor_line;

   pos = [x y w h];

   h1 = uicontrol('Parent',h01, ...		% Prestim Label
        'Style','text', ...
        'Units','normal', ...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'FontUnits','normal', ...
        'FontSize',0.55, ...
        'HorizontalAlignment','left', ...
        'String','Prestim Baseline:', ...
        'Position',pos);

   x = x+w;
   w = 0.05;

   pos = [x y w h];

   prestim_hdl = uicontrol('Parent',h01, ...	% Prestim Edit
        'Style','text', ...
        'Units','normal', ...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'FontUnits','normal', ...
        'FontSize',0.55, ...
        'HorizontalAlignment','right', ...
        'String',' ', ...
        'Position',pos);

   x = x+w+0.01;

   pos = [x y w h];

   h1 = uicontrol('Parent',h01, ...		% (ms)
        'Style','text', ...
        'Units','normal', ...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'FontUnits','normal', ...
        'FontSize',0.55, ...
        'HorizontalAlignment','left', ...
        'String','(ms)', ...
        'Position',pos);

   x = x+w+0.07;
   w = 0.14;

   pos = [x y w h];

   h1 = uicontrol('Parent',h01, ...		% Sweep Label
        'Style','text', ...
        'Units','normal', ...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'FontUnits','normal', ...
        'FontSize',0.55, ...
        'HorizontalAlignment','left', ...
        'String','Sweep Length:', ...
        'Position',pos);

   x = x+w;
   w = 0.05;

   pos = [x y w h];

   sweep_hdl = uicontrol('Parent',h01, ...	% Sweep Edit
        'Style','text', ...
        'Units','normal', ...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'FontUnits','normal', ...
        'FontSize',0.55, ...
        'HorizontalAlignment','right', ...
        'String',' ', ...
        'Position',pos);

   x = x+w+0.01;

   pos = [x y w h];

   h1 = uicontrol('Parent',h01, ...		% (ms)
        'Style','text', ...
        'Units','normal', ...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'FontUnits','normal', ...
        'FontSize',0.55, ...
        'HorizontalAlignment','left', ...
        'String','(ms)', ...
        'Position',pos);

   x = x+w+0.07;
   w = 0.14;

   pos = [x y w h];

   h1 = uicontrol('Parent',h01, ...		% Epoch Label
        'Style','text', ...
        'Units','normal', ...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'FontUnits','normal', ...
        'FontSize',0.55, ...
        'HorizontalAlignment','left', ...
        'String','End of Epoch:', ...
        'Position',pos);

   x = x+w;
   w = 0.05;

   pos = [x y w h];

   epoch_hdl = uicontrol('Parent',h01, ...	% Epoch Edit 
        'Style','text', ...
        'Units','normal', ...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'FontUnits','normal', ...
        'FontSize',0.55, ...
        'HorizontalAlignment','right', ...
        'String',' ', ...
        'Position',pos);

   x = x+w+0.01;

   pos = [x y w h];

   h1 = uicontrol('Parent',h01, ...		% (ms)
        'Style','text', ...
        'Units','normal', ...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'FontUnits','normal', ...
        'FontSize',0.55, ...
        'HorizontalAlignment','left', ...
        'String','(ms)', ...
        'Position',pos);

   x = left_margin;
   y = (num_line-6)*factor_line;
   h = 0.6 * factor_line;
   w = 0.22;

   pos = [x y w h];

   h1 = uicontrol('Parent',h01, ...		% Start Time Lable
        'Style','text', ...
        'Units','normal', ...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'FontUnits','normal', ...
        'FontSize',0.55, ...
        'HorizontalAlignment','left', ...
        'String','Analysis Starting Time:', ...
        'Position',pos);

   x = x+w;
   h = 0.6 * factor_line;
   w = 0.05;

   pos = [x y w h];

   start_time_hdl = uicontrol('Parent',h01, ...	% Start Time Edit 
        'Style','edit', ...
        'Units','normal', ...
        'BackgroundColor',[1 1 1], ...
        'FontUnits','normal', ...
        'FontSize',0.55, ...
        'HorizontalAlignment','right', ...
        'String',' ', ...
        'Callback','erp_modify_datamat(''start_time_edit'');', ...
        'Position',pos);

   x = x+w+0.01;
   h = 0.6 * factor_line;

   pos = [x y w h];

   h1 = uicontrol('Parent',h01, ...		% (ms)
        'Style','text', ...
        'Units','normal', ...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'FontUnits','normal', ...
        'FontSize',0.55, ...
        'HorizontalAlignment','left', ...
        'String','(ms)', ...
        'Position',pos);

   x = (1-2*left_margin)*0.7;
   h = 0.6 * factor_line;
   w = 0.22;

   pos = [x y w h];

   h1 = uicontrol('Parent',h01, ...		% End Time Label
        'Style','text', ...
        'Units','normal', ...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'FontUnits','normal', ...
        'FontSize',0.55, ...
        'HorizontalAlignment','left', ...
        'String','Analysis Ending Time:', ...
        'Position',pos);

   x = x+w;
   h = 0.6 * factor_line;
   w = 0.05;

   pos = [x y w h];

   end_time_hdl = uicontrol('Parent',h01, ...	% End Time Edit 
        'Style','edit', ...
        'Units','normal', ...
        'BackgroundColor',[1 1 1], ...
        'FontUnits','normal', ...
        'FontSize',0.55, ...
        'HorizontalAlignment','right', ...
        'String',' ', ...
        'Callback','erp_modify_datamat(''end_time_edit'');', ...
        'Position',pos);

   x = x+w+0.01;
   h = 0.6 * factor_line;

   pos = [x y w h];

   h1 = uicontrol('Parent',h01, ...		% (ms)
        'Style','text', ...
        'Units','normal', ...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'FontUnits','normal', ...
        'FontSize',0.55, ...
        'HorizontalAlignment','left', ...
        'String','(ms)', ...
        'Position',pos);

   if ismodify

      x = left_margin;
      y = (num_line-7)*factor_line;
      w = 0.22;
      h = 0.6 * factor_line - 0.01 * factor_line;

      pos = [x y w h];

      h1 = uicontrol('Parent',h01, ...		% Save As Lable
        'Style','text', ...
        'Units','normal', ...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'FontUnits','normal', ...
        'FontSize',0.55, ...
        'HorizontalAlignment','left', ...
        'String','Save Datamat As:', ...
		'visible','on', ...
        'Position',pos);

      x = x+w;
      w = 0.25;
      h = 0.6 * factor_line;

      pos = [x y w h];

      filename_hdl = uicontrol('Parent',h01, ...	% Save As Edit
        'Style','edit', ...
        'Units','normal', ...
        'BackgroundColor',[1 1 1], ...
        'FontUnits','normal', ...
        'FontSize',0.55, ...
        'HorizontalAlignment','left', ...
        'String',' ', ...
        'Callback','erp_modify_datamat(''filename_edit'');', ...
		'visible','on', ...
        'Position',pos);

      x = left_margin + 0.54;
      y = (num_line-7)*factor_line;
      w = 0.15;
      h = 0.6 * factor_line;

%      x = left_margin + 0.49;
 %     y = (num_line-7)*factor_line;
  %    w = 0.15;
   %   h = 0.6 * factor_line;

      pos = [x y w h];

      h1 = uicontrol('Parent',h01, ...             % Modify
        'Units','normal', ...
        'FontUnits','normal', ...
        'FontSize',0.55, ...
        'ListboxTop',0, ...
        'String','Modify', ...
        'Callback','erp_modify_datamat(''click_modify'');', ...
        'Position',pos);

%      x = x+w+0.1;
      x = x+w+0.05;

      pos = [x y w h];

      h1 = uicontrol('Parent',h01, ...             % Cancel
        'Units','normal', ...
        'FontUnits','normal', ...
        'FontSize',0.55, ...
        'ListboxTop',0, ...
        'String','Cancel', ...
        'Callback','close(gcf);', ...
        'Position',pos);

   else

      x = left_margin;
      y = (num_line-7)*factor_line;
      w = 0.22;
      h = 0.6 * factor_line - 0.01 * factor_line;

      pos = [x y w h];

      h1 = uicontrol('Parent',h01, ...		% Save As Lable
        'Style','text', ...
        'Units','normal', ...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'FontUnits','normal', ...
        'FontSize',0.55, ...
        'HorizontalAlignment','left', ...
        'String','Save Datamat As:', ...
		'visible','off', ...
        'Position',pos);

      x = x+w;
      w = 0.25;
      h = 0.6 * factor_line;

      pos = [x y w h];

      filename_hdl = uicontrol('Parent',h01, ...	% Save As Edit
        'Style','edit', ...
        'Units','normal', ...
        'BackgroundColor',[1 1 1], ...
        'FontUnits','normal', ...
        'FontSize',0.55, ...
        'HorizontalAlignment','left', ...
        'String',' ', ...
        'Callback','erp_create_datamat(''filename_edit'');', ...
		'visible','off', ...
        'Position',pos);

      x = left_margin + 0.54;
      y = (num_line-7)*factor_line;
      w = 0.15;
      h = 0.6 * factor_line;

      pos = [x y w h];

      h1 = uicontrol('Parent',h01, ...		% Create
	'Units','normal', ...
   	'FontUnits','normal', ...
   	'FontSize',0.55, ...
	'ListboxTop',0, ...
	'String','Create', ...
        'Callback','erp_create_datamat(''create_bn_pressed'');', ...
	'Position',pos);

      x = x+w+0.05;

      pos = [x y w h];

      h1 = uicontrol('Parent',h01, ...		% Cancel
	'Units','normal', ...
   	'FontUnits','normal', ...
   	'FontSize',0.55, ...
	'ListboxTop',0, ...
	'String','Cancel', ...
        'Callback','erp_create_datamat(''close_bn_pressed'');', ...
	'Position',pos);

   end

   w = 1;
   h = 0.6 * factor_line;
   x = 0.01;
   y = 0;

   pos = [x y w h];

   h1 = uicontrol('Parent',h01, ...		% Message Line Label
        'Style','text', ...
        'Units','normal', ...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'ForegroundColor',[0.8 0.0 0.0], ...
        'FontUnits','normal', ...
        'FontSize',0.55, ...
        'HorizontalAlignment','left', ...
        'Position',pos, ...
        'String','', ...
        'Tag','MessageLine');

   setappdata(h01,'chan_lst_hdl', chan_lst_hdl);
   setappdata(h01,'subj_lst_hdl', subj_lst_hdl);
   setappdata(h01,'cond_lst_hdl', cond_lst_hdl);
   setappdata(h01,'behav_lst_hdl', behav_lst_hdl);

   setappdata(h01,'prestim_hdl', prestim_hdl);
   setappdata(h01,'sweep_hdl', sweep_hdl);
   setappdata(h01,'epoch_hdl', epoch_hdl);
   setappdata(h01,'start_time_hdl', start_time_hdl);
   setappdata(h01,'end_time_hdl', end_time_hdl);
   setappdata(h01,'filename_hdl', filename_hdl);

   return;


