function [p] = gui_elec_open(p,command,parent)

% gui_elec_open - Load an electrode data file into matlab workspace
% 
% Usage: [p] = gui_elec_open(p,[command],[parent])
%
% p is a structure, generated by 'eeg_toolbox_defaults'
% command is either 'init' or 'load'
% parent is a handle to the gui that calls this gui, useful
% for updating the UserData field of the parent from this gui.
% The p structure is returned to the parent when the parent 
% handle is given.
%

% $Revision: 1.1 $ $Date: 2009-04-28 22:13:56 $

% Licence:  GNU GPL, no express or implied warranties
% History:  01/02 Darren.Weber_at_radiology.ucsf.edu
% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


if ~exist('p','var'),
   [p] = eeg_toolbox_defaults;
elseif isempty(p),
   [p] = eeg_toolbox_defaults;
end

if ~exist('command','var'),
    command = 'init';
elseif isempty(command),
    command = 'init';
end

command = lower(command);

switch command,
case 'init',
    if exist('parent','var'),
        ELECOpen = INIT(p,parent);
    else
        ELECOpen = INIT(p,'');
    end
otherwise,
    ELECOpen = get(gcbf,'Userdata');
    set(ELECOpen.gui,'Pointer','watch');
end


switch command,
case 'plot',
    
    ELECOpen.p.elec.plot = 1;
    ELECOpen.p = elec_open(ELECOpen.p);
    
case 'save',
    
    fprintf('\nGUI_ELEC_OPEN: Save As not implemented yet.\n');
    
case 'return',
    
    ELECOpen.p.elec.plot = 0;
    ELECOpen.p = elec_open(ELECOpen.p);
    
case 'cancel',
    
    GUI.parent = ELECOpen.parent;
    gui_updateparent(GUI);
    close gcbf;
    
otherwise,
    
end


switch command,
case {'init','cancel'},
otherwise,
    set(ELECOpen.gui,'Pointer','arrow');
    set(ELECOpen.gui,'Userdata',ELECOpen);
    if ~isequal(get(ELECOpen.handles.Bhold,'Value'),0),
       [p] = gui_updateparent(ELECOpen,0);
    else
       [p] = gui_updateparent(ELECOpen);
        close gcbf;
    end
end



return


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ELECOpen] = INIT(p,parent)

    % GUI General Parameters
    
    GUIwidth  = 500;
    GUIheight = 120;
    
    version = '$Revision: 1.1 $';
    name = sprintf('ELEC File Open [v %s]\n',version(11:15));
    
    GUI = figure('Name',name,'Tag','ELEC_OPEN',...
                 'NumberTitle','off','HandleVisibility','callback',...
                 'MenuBar','none','Position',[1 1 GUIwidth GUIheight]);
    movegui(GUI,'center');
    
    ELECOpen.gui = GUI;
    
    Font.FontName   = 'Helvetica';
    Font.FontUnits  = 'Pixels';
    Font.FontSize   = 12;
    Font.FontWeight = 'normal';
    Font.FontAngle  = 'normal';
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Voltage Data Selection and Parameters

    G.Title_data = uicontrol('Parent',GUI,'Style','text','Units','Normalized',Font, ...
        'Position',[.01 .75 .15 .2],...
        'String','Coordinates','HorizontalAlignment','left');
    
    switch p.elec.type
        case 'Cartesian',   elecType = 1;
        case 'Spherical1',  elecType = 2;
        case 'Spherical2',  elecType = 3;
        case 'scan3ddasc',  elecType = 4;
        case 'scantri',     elecType = 5;
        case 'brainstorm',  elecType = 6;
        case 'EMSE',        elecType = 7;
        otherwise,          elecType = 1;
    end
    G.PelectrodeType = uicontrol('Tag','PelecType','Parent',GUI,'Style','popupmenu',...
        'Units','Normalized',Font,  ...
        'Position',[.18 .75 .2 .2],...
        'String',{'Cartesian' 'Spherical1' 'Spherical2' 'scan3ddasc' 'scantri' 'brainstorm' 'EMSE'},'Value',elecType,...
        'TooltipString',strcat('Coordinate Type: ',...
                               ' ''Spherical1'' = theta and phi in degrees,',...
                               ' ''Spherical2'' = theta and phi in radians' ,...
                               ' ''scan3ddasc'' = scan 3Dspace ascii export',....
                               ' ''scantri''    = scan 3Dspace .tri file',....
                               ' ''brainstorm'' = brainstorm channel file',...
                               ' ''EMSE''       = EMSE .elp file'),...
        'Callback',strcat('ELECOpen = get(gcbf,''Userdata'');',...
                          'ELECOpen.p.elec.type = popupstr(ELECOpen.handles.PelectrodeType);',...
                          'set(gcbf,''Userdata'',ELECOpen); clear ELECOpen;'));
    
    G.Title_elecN = uicontrol('Tag','Title_elecN','Parent',GUI,'Style','text',...
        'Units','Normalized',Font,...
        'Position',[.58 .75 .1 .2],...
        'String','N =',...
        'TooltipString','Maximum number of consecutive electrodes to load from file.');
    G.EelectrodeN = uicontrol('Tag','EelecN','Parent',GUI,'Style','edit',...
        'Units','Normalized',Font,  ...
        'Position',[.68 .75 .1 .2],...
        'String',num2str(p.elec.n),'Value',p.elec.n,...
        'Callback',strcat('ELECOpen = get(gcbf,''Userdata'');',...
                          'ELECOpen.p.elec.n = str2num( get(ELECOpen.handles.EelectrodeN,''String'') );',...
                          'set(ELECOpen.handles.EelectrodeN,''Value'',ELECOpen.p.elec.n,''String'',num2str(ELECOpen.p.elec.n) );',...
                          'set(gcbf,''Userdata'',ELECOpen); clear ELECOpen;'));
    
    G.Title_path = uicontrol('Parent',GUI,'Style','text','Units','Normalized',Font, ...
        'Position',[.01 .50 .15 .2],...
        'String','Path','HorizontalAlignment','left');
    G.EelectrodePath = uicontrol('Parent',GUI,'Style','edit','Units','Normalized',Font,  ...
    	'Position',[.18 .50 .6 .2], 'String',p.elec.path,...
        'Callback',strcat('ELECOpen = get(gcbf,''Userdata'');',...
                          'ELECOpen.p.elec.path = get(ELECOpen.handles.EelectrodePath,''String'');',...
                          'set(gcbf,''Userdata'',ELECOpen); clear ELECOpen;'));
    
    G.Title_file = uicontrol('Parent',GUI,'Style','text','Units','Normalized',Font, ...
        'Position',[.01 .25 .15 .2],...
        'String','File','HorizontalAlignment','left');
    G.EelectrodeFile = uicontrol('Parent',GUI,'Style','edit','Units','Normalized',Font,  ...
    	'Position',[.18 .25 .6 .2], 'String',p.elec.file,...
        'Callback',strcat('ELECOpen = get(gcbf,''Userdata'');',...
                          'ELECOpen.p.elec.file = get(ELECOpen.handles.EelectrodeFile,''String'');',...
                          'set(gcbf,''Userdata'',ELECOpen); clear ELECOpen;'));
    
    Font.FontWeight = 'bold';
    
    % BROWSE: Look for the data
    browsecommand = strcat('ELECOpen = get(gcbf,''Userdata'');',...
        'cd(ELECOpen.p.elec.path);',...
        '[file, path] = uigetfile(',...
        '{''*.*'',   ''All Files (*.*)'';', ...
        ' ''*.mat'', ''MATLAB (BrainStorm) Files (*.mat)'';', ...
        ' ''*.elp'', ''EMSE Files (*.elp)'';', ...
        ' ''*.dat'', ''Scan 3Dspace Ascii Export Files (*.dat)'';', ...
        ' ''*.tri'', ''Scan 3Dspace Files (*.tri)'';', ...
        ' ''*.asc;*.txt'', ''ASCII Data Files (*.asc,*.txt)''},', ...
        '''Select Electrode Coordinate File'');',...
        'if ~isequal(path,0), ELECOpen.p.elec.path = path; end;',...
        'if ~isequal(file,0), ELECOpen.p.elec.file = file; end;',...
        'set(ELECOpen.handles.EelectrodePath,''String'',ELECOpen.p.elec.path);',...
        'set(ELECOpen.handles.EelectrodeFile,''String'',ELECOpen.p.elec.file);',...
        'if ~isempty(findstr(file,''.txt'')) | ~isempty(findstr(file,''.asc'')), ',...
            'set(ELECOpen.handles.PelectrodeType,''Value'',1); ',...
            'ELECOpen.p.elec.type = popupstr(ELECOpen.handles.PelectrodeType);',...
        'elseif ~isempty(findstr(file,''.dat'')), ',...
            'set(ELECOpen.handles.PelectrodeType,''Value'',4); ',...
            'ELECOpen.p.elec.type = popupstr(ELECOpen.handles.PelectrodeType);',...
        'elseif ~isempty(findstr(file,''.tri'')), ',...
            'set(ELECOpen.handles.PelectrodeType,''Value'',5); ',...
            'ELECOpen.p.elec.type = popupstr(ELECOpen.handles.PelectrodeType);',...
        'elseif ~isempty(findstr(file,''.mat'')), ',...
            'set(ELECOpen.handles.PelectrodeType,''Value'',6); ',...
            'ELECOpen.p.elec.type = popupstr(ELECOpen.handles.PelectrodeType);',...
        'elseif ~isempty(findstr(file,''.elp'')), ',...
            'set(ELECOpen.handles.PelectrodeType,''Value'',7); ',...
            'ELECOpen.p.elec.type = popupstr(ELECOpen.handles.PelectrodeType);',...
        'else, ',...
            'set(ELECOpen.handles.PelectrodeType,''Value'',1); ',...
            'ELECOpen.p.elec.type = popupstr(ELECOpen.handles.PelectrodeType);',...
        'end;',...
        'set(gcbf,''Userdata'',ELECOpen); clear ELECOpen;');
    G.BelectrodeFile = uicontrol('Parent',GUI,'Style','pushbutton','Units','Normalized',Font, ...
        'Position',[.01 .01 .18 .2], 'String','BROWSE',...
        'BackgroundColor',[0.8 0.8 0.0],...
        'ForegroundColor', [1 1 1], 'HorizontalAlignment', 'center',...
        'Callback', browsecommand );
    
    % PLOT: Load & plot the data!
    G.Bplot = uicontrol('Parent',GUI,'Style','pushbutton','Units','Normalized', Font, ...
        'Position',[.20 .01 .18 .2],...
        'String','PLOT','BusyAction','queue',...
        'TooltipString','Plot the data and return the file parameters.',...
        'BackgroundColor',[0.0 0.5 0.0],...
        'ForegroundColor', [1 1 1], 'HorizontalAlignment', 'center',...
        'Callback',strcat('ELECOpen = get(gcbf,''Userdata'');',...
                          'p = gui_elec_open(ELECOpen.p,''plot'');',...
                          'clear ELECOpen;'));
    
    % Save As
	G.Bsave = uicontrol('Parent',GUI,'Style','pushbutton','Units','Normalized', Font, ...
        'Position',[.40 .01 .18 .2],'HorizontalAlignment', 'center',...
        'String','SAVE AS','TooltipString','ELEC File Conversion Tool (not implemented yet)',...
        'BusyAction','queue',...
        'Visible','on',...
        'BackgroundColor',[0.0 0.0 0.75],...
        'ForegroundColor', [1 1 1], 'HorizontalAlignment', 'center',...
        'Callback',strcat('ELECOpen = get(gcbf,''Userdata'');',...
                          'p = gui_elec_open(ELECOpen.p,''save'');',...
                          'clear ELECOpen;'));

    % Return file parameters
    G.Breturn = uicontrol('Parent',GUI,'Style','pushbutton','Units','Normalized', Font, ...
        'Position',[.60 .01 .18 .2],...
        'String','RETURN','BusyAction','queue',...
        'TooltipString','Close and return file parameters',...
        'BackgroundColor',[0.75 0.0 0.0],...
        'ForegroundColor', [1 1 1], 'HorizontalAlignment', 'center',...
        'Callback',strcat('ELECOpen = get(gcbf,''Userdata'');',...
                          'p = gui_elec_open(ELECOpen.p,''return'');',...
                          'clear ELECOpen;'));
    
    % Cancel
    G.Bcancel = uicontrol('Parent',GUI,'Style','pushbutton','Units','Normalized', Font, ...
        'Position',[.80 .01 .18 .2],...
        'String','CANCEL','BusyAction','queue',...
        'TooltipString','Close, do not return parameters.',...
        'BackgroundColor',[0.75 0.0 0.0],...
        'ForegroundColor', [1 1 1], 'HorizontalAlignment', 'center',...
        'Callback',strcat('ELECOpen = get(gcbf,''Userdata'');',...
                          'p = gui_elec_open(ELECOpen.p,''cancel'');',...
                          'clear ELECOpen;'));
    
	% Help
	G.Bhelp = uicontrol('Parent',GUI,'Style','pushbutton','Units','Normalized', Font, ...
        'Position',[.80 .25 .18 .2],'String','Help','BusyAction','queue',...
        'BackgroundColor',[1 1 0],...
        'ForegroundColor',[0 0 0], 'HorizontalAlignment', 'center',...
        'Callback','doc elec_open;');
    
	% Hold GUI Open checkbox
	G.Bhold = uicontrol('Parent',GUI,'Style','checkbox','Units','Normalized', Font, ...
        'Position',[.80 .50 .18 .2],'String','Hold GUI','BusyAction','queue',...
        'TooltipString','ELEC File Load GUI remains open after ''Plot'' and ''Return'' commands.',...
        'Value',p.hold,'HorizontalAlignment', 'center');
    
    
    % Store userdata
    if exist('parent','var'), ELECOpen.parent.gui = parent; end
    ELECOpen.handles = G;
    ELECOpen.p = p;
    set(GUI,'Userdata',ELECOpen);

return
