function varargout = list_select(varargin)
% list_select: generates the analysis configuration GUI
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% LIST_SELECT Application M-file for list_select.fig
%    FIG = LIST_SELECT launch list_select GUI.
%    LIST_SELECT('callback_name', ...) invoke the named callback.

% Last Modified by GUIDE v2.0 14-Jan-2002 21:11:46

if nargin == 0
   err_text='No list select data was sent!';
   fault(err_text);
   varargout{1} = 'cancel';
   return;
elseif nargin == 1  % LAUNCH GUI
   
   fig = openfig(mfilename,'reuse');
   
   % Use system color scheme for figure:
   set(fig,'Color',get(0,'defaultUicontrolBackgroundColor'));
   
   % Generate a structure of handles to pass to callbacks, and store it. 
   handles = guihandles(fig);
   guidata(fig, handles);
   set(fig,'WindowStyle','normal');
   
   % load name items
   set(handles.figure1,'UserData',varargin{1});
   
   % set single axes current setting
   data=get(handles.figure1,'UserData');
   set(handles.checkbox1,'Value',data.config.single_axes);
   set(handles.checkbox1,'enable','on');
   set(handles.checkbox2,'Value',data.config.disp_position);
   set(handles.checkbox2,'enable','on');
   drawnow;
   
   % Populate the listbox
   split_list(handles)
   load_listbox(handles);
   
   if nargout > 0
      varargout{1} = fig;
   end
   
elseif ischar(varargin{1}) % INVOKE NAMED SUBFUNCTION OR CALLBACK
   try
      if (nargout)
         [varargout{1:nargout}] = feval(varargin{:}); % FEVAL switchyard
      else
         feval(varargin{:}); % FEVAL switchyard
      end
   catch
      %disp(lasterr);
   end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;

% --------------------------------------------------------------------
function varargout = listbox1_Callback(h, eventdata, handles, varargin)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if strcmp(get(handles.figure1,'SelectionType'),'open')
   index_selected = get(handles.listbox1,'Value');
   
   list01=get(handles.listbox1,'UserData');
   list02=get(handles.listbox2,'UserData');
   
   if (isempty(list01)==0)
      list=[];
      list02{end+1}=list01{index_selected};
      for (i=1:size(list01,2))
         if (i~=index_selected)
            list{end+1}=list01{i};
         end;
      end;
      
      set(handles.listbox1,'UserData',list);
      set(handles.listbox2,'UserData',list02);
   end;
   load_listbox(handles);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;

% --------------------------------------------------------------------
function varargout = listbox2_Callback(h, eventdata, handles, varargin)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if strcmp(get(handles.figure1,'SelectionType'),'open')
   index_selected = get(handles.listbox2,'Value');
   
   list01=get(handles.listbox1,'UserData');
   list02=get(handles.listbox2,'UserData');
   
   if (isempty(list02)==0)
      list=[];
      list01{end+1}=list02{index_selected};
      for (i=1:size(list02,2))
         if (i~=index_selected)
            list{end+1}=list02{i};
         end;
      end;
      
      set(handles.listbox1,'UserData',list01);
      set(handles.listbox2,'UserData',list);
   end;
   load_listbox(handles);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;

% --------------------------------------------------------------------
function varargout = pushbutton1_Callback(h, eventdata, handles, varargin)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
index_selected = get(handles.listbox1,'Value');

list01=get(handles.listbox1,'UserData');
list02=get(handles.listbox2,'UserData');

if (isempty(list01)==0)
   list=[];
   list02{end+1}=list01{index_selected};
   for (i=1:size(list01,2))
      if (i~=index_selected)
         list{end+1}=list01{i};
      end;
   end;
   
   set(handles.listbox1,'UserData',list);
   set(handles.listbox2,'UserData',list02);
end;
load_listbox(handles);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;

% --------------------------------------------------------------------
function varargout = pushbutton2_Callback(h, eventdata, handles, varargin)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
index_selected = get(handles.listbox2,'Value');

list01=get(handles.listbox1,'UserData');
list02=get(handles.listbox2,'UserData');

if (isempty(list02)==0)
   list=[];
   list01{end+1}=list02{index_selected};
   for (i=1:size(list02,2))
      if (i~=index_selected)
         list{end+1}=list02{i};
      end;
   end;
   
   set(handles.listbox1,'UserData',list01);
   set(handles.listbox2,'UserData',list);
end;
load_listbox(handles);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;

% --------------------------------------------------------------------
function varargout = pushbutton3_Callback(h, eventdata, handles, varargin)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
list01=get(handles.listbox1,'UserData');
list02=get(handles.listbox2,'UserData');

list=[];
list02a=[list02,list01];

set(handles.listbox1,'UserData',list);
set(handles.listbox2,'UserData',list02a);
load_listbox(handles);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;

% --------------------------------------------------------------------
function varargout = pushbutton4_Callback(h, eventdata, handles, varargin)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

list01=get(handles.listbox1,'UserData');
list02=get(handles.listbox2,'UserData');

list=[];
list01a=[list01,list02];

set(handles.listbox1,'UserData',list01a);
set(handles.listbox2,'UserData',list);
load_listbox(handles);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;

% --------------------------------------------------------------------
function varargout = checkbox1_Callback(h, eventdata, handles, varargin)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data=get(handles.figure1,'UserData');
data.config.single_axes=get(handles.checkbox1,'Value');
set(handles.figure1,'UserData',data);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;

% --------------------------------------------------------------------
function varargout = checkbox2_Callback(h, eventdata, handles, varargin)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data=get(handles.figure1,'UserData');
data.config.disp_position=get(handles.checkbox2,'Value');
set(handles.figure1,'UserData',data);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;

% --------------------------------------------------------------------
function varargout = pushbutton5_Callback(h, eventdata, handles, varargin)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
restore_list(handles);
uiresume(handles.figure1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;

% --------------------------------------------------------------------
function varargout = pushbutton6_Callback(h, eventdata, handles, varargin)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
set(handles.figure1,'UserData','');
uiresume(handles.figure1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;

% --------------------------------------------------------------------
function varargout = CloseReq_Callback(h, eventdata, handles, varargin)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
pushbutton6_Callback(h, eventdata, handles, varargin);
%closereq;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;

function load_listbox(handles)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data=get(handles.figure1,'UserData');
list01=get(handles.listbox1,'UserData');
list02=get(handles.listbox2,'UserData');

list01a=[];
for (i=1:size(data.config.field_names,1))
   for (j=1:size(list01,2))
      if (strcmpi(data.config.field_names{i},list01{j})==1)
         list01a{end+1}=list01{j};
      end;
   end;
end;

list02a=list02;
% list02a=[];
% for (i=1:size(data.config.field_names,1))
%    for (j=1:size(list02,2))
%       if (strcmpi(data.config.field_names{i},list02{j})==1)
%          list02a{end+1}=list02{j};
%       end;
%    end;
% end;

set(handles.listbox1,'UserData',list01a);
set(handles.listbox2,'UserData',list02a);
set(handles.listbox1,'String',list01a,'Value',1);
set(handles.listbox2,'String',list02a,'Value',1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;

function split_list(handles)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data=get(handles.figure1,'UserData');

% split list into used & unused variables
list01=[];
list02=[];
for (i=1:size(data.config.field_names,1))
   if (data.config.field_ranges(i,8)==0)
      if (data.config.field_ranges(i,5)==1)
         list01{end+1}=data.config.field_names{i};
      end;
   elseif (data.config.field_ranges(i,8)~=0)
      if (data.config.field_ranges(i,5)==0)
         data.config.field_ranges(i,8)=0;
      elseif (data.config.field_ranges(i,5)==1)
         list02{data.config.field_ranges(i,8)}=data.config.field_names{i};
      end;
   end;
end;

% sort the used variables
list02a=[];
for (i=1:size(list02,2))
   if (isempty(list02{i})==0)
      data.config.field_ranges(i,8)=i;
      list02a{end+1}=list02{i};
   end;
end;

set(handles.figure1,'UserData',data);
set(handles.listbox1,'UserData',list01);
set(handles.listbox2,'UserData',list02a);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;

function restore_list(handles)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data=get(handles.figure1,'UserData');
list01=get(handles.listbox1,'UserData');
list02=get(handles.listbox2,'UserData');

data.config.field_ranges(:,8)=zeros(size(data.config.field_names,1),1);
for (i=1:size(data.config.field_names,1))
   for (j=1:size(list01,2))
      if (strcmpi(data.config.field_names{i},list01{j})==1)
         data.config.field_ranges(i,8)=0;
      end;
   end;
   for (k=1:size(list02,2))
      if (strcmpi(data.config.field_names{i},list02{k})==1)
         data.config.field_ranges(i,8)=k;
      end;
   end;
end;

set(handles.figure1,'UserData',data);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;
