function initializeSourcePanel(h)
% INITIALIZESOURCEPANEL initializes the 'Step 1' panel

% Author: Rong Chen 
% Revised: 
% Copyright 1986-2004 The MathWorks, Inc.


% -------------------------------------------------------------------------
%% Build timeseries naming option control and its contents
% -------------------------------------------------------------------------
h.DefaultPos.SourcePanelDefaultColor=h.DefaultPos.FigureDefaultColor;
h.Handles.PNLsource = uipanel('Parent',h.Figure, ...
    'Units','Pixels', ...
    'BackgroundColor',h.DefaultPos.SourcePanelDefaultColor, ...
    'Position',[h.DefaultPos.leftoffsetDynamicPnl ...
                h.DefaultPos.buttomoffsetDynamicPnl ...
                h.DefaultPos.widthDynamicPnl ...
                h.DefaultPos.heightDynamicPnl] ...
    );
h.Handles.TXTsource = uicontrol('Parent',h.Handles.PNLsource, ...
    'style','text',...
    'Units','Pixels', ...
    'BackgroundColor',h.DefaultPos.SourcePanelDefaultColor, ...
    'String',' Import Time Series From : ', ...
    'HorizontalAlignment','Left', ...
    'Position',[20 ...
                h.DefaultPos.heightDynamicPnl-40 ...
                200 ...
                h.DefaultPos.heighttxt] ...
    );
h.Handles.COMBsource = uicontrol('Parent',h.Handles.PNLsource, ...
    'style','popupmenu', ...
    'Units','Pixels', ...
    'BackgroundColor',[1 1 1],...
    'String', {'an Excel Workbook (.xls)' 'a MAT File (.mat)' 'MATLAB Workspace'}, ...
    'Value',1, ...
    'TooltipString',['Select a source where the data is stored.'], ...
    'Position',[20+200 ...
                h.DefaultPos.heightDynamicPnl-36 ...
                250 ...
                h.DefaultPos.heightcomb], ...
    'Callback',{@localSwitch h} ...
    );
h.Handles.TXTbrowser = uicontrol('Parent',h.Handles.PNLsource, ...
    'style','text',...
    'Units','Pixels', ...
    'BackgroundColor',h.DefaultPos.SourcePanelDefaultColor, ...
    'String',' File Location : ', ...
    'HorizontalAlignment','Left', ...
    'Position',[20 ...
                h.DefaultPos.heightDynamicPnl-75 ...
                200 ...
                h.DefaultPos.heighttxt] ...
    );
h.Handles.EDTbrowser = uicontrol('Parent',h.Handles.PNLsource, ...
    'style','edit',...
    'Units','Pixels', ...
    'BackgroundColor',[1 1 1], ...
    'HorizontalAlignment','Left', ...
    'Position',[20+200 ...
                h.DefaultPos.heightDynamicPnl-75 ...
                350 ...
                h.DefaultPos.heightedt] ...
    );
h.Handles.BTNbrowser = uicontrol('Parent',h.Handles.PNLsource, ...
    'style','pushbutton', ...
    'Units','Pixels', ...
    'BackgroundColor',h.DefaultPos.FigureDefaultColor,...
    'String','Browse ...',...
    'Position',[20+200+350+20 ...
                h.DefaultPos.heightDynamicPnl-75 ...
                h.DefaultPos.widthbtn ...
                h.DefaultPos.heightbtn],...
    'Callback',{@localBrowser h} ...
    );


function localSwitch(eventSrc, eventData, h)
choice=get(h.Handles.COMBsource,'Value');
switch choice
    case 1
        set(h.Handles.TXTbrowser,'Visible','on');
        set(h.Handles.EDTbrowser,'Visible','on');
        set(h.Handles.EDTbrowser,'String','');
        set(h.Handles.BTNbrowser,'Visible','on');
    case 2
        set(h.Handles.TXTbrowser,'Visible','on');
        set(h.Handles.EDTbrowser,'Visible','on');
        set(h.Handles.EDTbrowser,'String','');
        set(h.Handles.BTNbrowser,'Visible','on');
    case 3
        set(h.Handles.TXTbrowser,'Visible','off');
        set(h.Handles.EDTbrowser,'Visible','off');
        set(h.Handles.BTNbrowser,'Visible','off');
end

function localBrowser(eventSrc, eventData, h)
% switch to the last path that is used in loading a file
choice=get(h.Handles.COMBsource,'Value');
if isfield(h.DefaultPos,'lastloadedfilepath')
    tmpPath=eval('pwd');
    eval(['cd ' h.DefaultPos.lastloadedfilepath]);    
    switch choice
        case 1
            [filename, pathname, filterindex] = uigetfile( ...
            {'*.xls', 'Excel Workbooks (*.xls)'}, ...
            'Pick an import file');
            h.DefaultPos.lastloadedfilepath=pathname;
        case 2
            [filename, pathname, filterindex] = uigetfile( ...
            {'*.mat', 'MATLAB Saved Files (*.mat)'}, ...
            'Pick an import file');
            h.DefaultPos.lastloadedfilepath=pathname;
        case 3
            ;
    end
    eval(['cd ' tmpPath]);    
else
    switch choice
        case 1
            [filename, pathname, filterindex] = uigetfile( ...
            {'*.xls', 'Excel Workbooks (*.xls)'}, ...
            'Pick an import file');
            h.DefaultPos.lastloadedfilepath=pathname;
        case 2
            [filename, pathname, filterindex] = uigetfile( ...
            {'*.mat', 'MATLAB Saved Files (*.mat)'}, ...
            'Pick an import file');
            h.DefaultPos.lastloadedfilepath=pathname;
        case 3
            ;
    end
    h.DefaultPos.lastloadedfilepath=pathname;
end
if isequal(filename,0) | isequal(pathname,0)
    % User pressed cancel
    return
else
    set(h.Handles.EDTbrowser,'String',fullfile(pathname,filename));
end

% % call import dialog
% if isequal(filename,0) | isequal(pathname,0)
%     % User pressed cancel
%     return
% else
%     % User selected a file and show the (singleton) import dialog
%     % get the file extension
%     [filePath,fileName,fileExtension,fileVersion] = fileparts(filename);
%     if ~isempty(fileExtension)
%         if strcmpi(fileExtension,'.xls')
%             exceldlg = tsguis.excelImportdlg;
%             exceldlg.initialize(fullfile(pathname,filename));
%         elseif strcmpi(fileExtension,'.mat')
%             matdlg = tsguis.matImportdlg;
%             matdlg.initialize(fullfile(pathname,filename));
%         elseif strcmpi(fileExtension,'.txt')
%             %asciidlg.initialize(fullfile(pathname,filename));
%         elseif strcmpi(fileExtension,'.csv')            
%             %csvdlg.initialize(fullfile(pathname,filename));
%         else
%             %asciidlg.initialize(fullfile(pathname,filename));
%         end
%     else
%         %asciidlg.initialize(fullfile(pathname,filename));
%     end
% end
% 
