function initializeOptionPanel(h)
% INITIALIZEOPTIONPANEL initializes the Step 3 panel

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


% -------------------------------------------------------------------------
%% Build timeseries naming option control and its contents
% -------------------------------------------------------------------------
h.DefaultPos.OptionPanelDefaultColor=h.DefaultPos.FigureDefaultColor;
h.Handles.PNLoption = uibuttongroup('Parent',h.Figure, ...
    'Units','Pixels', ...
    'BackgroundColor',h.DefaultPos.OptionPanelDefaultColor, ...
    'SelectionChangeFcn',{@localRadio,h}, ...
    'Position',[h.DefaultPos.leftoffsetDynamicPnl ...
                h.DefaultPos.buttomoffsetDynamicPnl ...
                h.DefaultPos.widthDynamicPnl ...
                h.DefaultPos.heightDynamicPnl], ...
    'Visible','off' ...
    );
h.Handles.RADIOsingleNEW = uicontrol('Parent',h.Handles.PNLoption, ...
    'style','radiobutton',...
    'Units','Pixels', ...
    'BackgroundColor',h.DefaultPos.OptionPanelDefaultColor, ...
    'String',' Create single Time Series object with name : ', ...
    'Position',[h.DefaultPos.RADIOoptionleftoffset ...
                h.DefaultPos.RADIOoptionbottomoffset_singleNew ...
                h.DefaultPos.RADIOoptionwidth ...
                h.DefaultPos.heightradio] ...
    );
h.Handles.EDTsingleNEW = uicontrol('Parent',h.Handles.PNLoption, ...
    'style','edit', ...
    'Units','Pixels', ...
    'BackgroundColor',[1 1 1], ...
    'String','', ...
    'TooltipString','Key in the name of the time series object to be imported.', ...
    'HorizontalAlignment','Left', ...
    'Position',[h.DefaultPos.EDToptionleftoffset ...
                h.DefaultPos.RADIOoptionbottomoffset_singleNew-2 ...
                h.DefaultPos.EDToptionwidth ...
                h.DefaultPos.heightedt] ...
    );
h.Handles.RADIOmultipleNEW = uicontrol('Parent',h.Handles.PNLoption, ...
    'style','radiobutton',...
    'Units','Pixels', ...
    'BackgroundColor',h.DefaultPos.OptionPanelDefaultColor, ...
    'String',' Create multiple Time Series objects : ', ...
    'Position',[h.DefaultPos.RADIOoptionleftoffset ...
                h.DefaultPos.RADIOoptionbottomoffset_multipleNew ...
                h.DefaultPos.RADIOoptionwidth ...
                h.DefaultPos.heightradio] ...
    );
h.Handles.COMBmultipleNEW = uicontrol('Parent',h.Handles.PNLoption, ...
    'style','popupmenu', ...
    'Units','Pixels', ...
    'BackgroundColor',[1 1 1],...
    'String', {'name them after the headings in a row/column' 'name them by using column/row index as suffix'}, ...
    'Value',1, ...
    'TooltipString',['If a row/column contains variable names and you want to use them for' ...
                    'the time series objects, select the first drop-down menu item, otherwise, select the second menu item.'], ...
    'Position',[h.DefaultPos.EDToptionleftoffset ...
                h.DefaultPos.RADIOoptionbottomoffset_multipleNew ...
                h.DefaultPos.EDToptionwidth ...
                h.DefaultPos.heighttxt], ...
    'Callback',{@localSwitchMultiple h} ...
    );
h.Handles.TXTmultipleNEW = uicontrol('Parent',h.Handles.PNLoption, ...
    'style','text',...
    'Units','Pixels', ...
    'BackgroundColor',h.DefaultPos.OptionPanelDefaultColor, ...
    'String',' Row Number : ', ...
    'HorizontalAlignment','Left', ...
    'Position',[h.DefaultPos.EDToptionleftoffset+h.DefaultPos.EDToptionwidth+35 ...
                h.DefaultPos.RADIOoptionbottomoffset_multipleNew-4 ...
                h.DefaultPos.TXToptionmultiplewidth ...
                h.DefaultPos.heighttxt] ...
    );
h.Handles.EDTmultipleNEW = uicontrol('Parent',h.Handles.PNLoption, ...
    'style','edit', ...
    'Units','Pixels', ...
    'BackgroundColor',[1 1 1], ...
    'String','', ...
    'TooltipString',['Key in the row number/column letter. Or specify the' ...
                    'the common part of the names for the time series objects'],...
    'HorizontalAlignment','Left', ...
    'Position',[h.DefaultPos.EDToptionleftoffset+h.DefaultPos.EDToptionwidth+40+h.DefaultPos.TXToptionmultiplewidth+h.DefaultPos.separation ...
                h.DefaultPos.RADIOoptionbottomoffset_multipleNew-2 ...
                h.DefaultPos.EDToptionmultiplewidth ...
                h.DefaultPos.heightedt] ...
    );
h.Handles.RADIOsingleINSERT = uicontrol('Parent',h.Handles.PNLoption, ...
    'style','radiobutton',...
    'Units','Pixels', ...
    'BackgroundColor',h.DefaultPos.OptionPanelDefaultColor, ...
    'String',' Import into an existing Time Series object : ', ...
    'Position',[h.DefaultPos.RADIOoptionleftoffset ...
                h.DefaultPos.RADIOoptionbottomoffset_singleINSERT ...
                h.DefaultPos.RADIOoptionwidth ...
                h.DefaultPos.heightradio] ...
    );
h.Handles.COMBsingleINSERT = uicontrol('Parent',h.Handles.PNLoption, ...
    'style','popupmenu', ...
    'Units','Pixels', ...
    'BackgroundColor',[1 1 1], ...
    'TooltipString','Select a time series object from the drop-down list and insert selected data samples',...
    'Position',[h.DefaultPos.EDToptionleftoffset ...
                h.DefaultPos.RADIOoptionbottomoffset_singleINSERT ...
                h.DefaultPos.EDToptionwidth ...
                h.DefaultPos.heighttxt] ...
);
%populate combobox with current timeseries objects in the base workspace
localRefresh([], [], h);
h.Handles.BTNRefresh = uicontrol('Parent',h.Handles.PNLoption, ...
    'style','pushbutton', ...
    'Units','Pixels', ...
    'BackgroundColor',h.DefaultPos.OptionPanelDefaultColor,...
    'String','Refresh',...
    'Position',[h.DefaultPos.EDToptionleftoffset+h.DefaultPos.EDToptionwidth+35 ...
                h.DefaultPos.RADIOoptionbottomoffset_singleINSERT-5 ...
                80 ...
                h.DefaultPos.heightbtn], ...
    'Callback',{@localRefresh h} ...
    );
% initialize radiobuttons
set(h.Handles.PNLoption,'SelectedObject',h.Handles.RADIOsingleNEW);
set(h.Handles.COMBmultipleNEW,'Enable','off');
set(h.Handles.EDTmultipleNEW,'Enable','off','BackgroundColor',h.DefaultPos.OptionPanelDefaultColor);
set(h.Handles.COMBsingleINSERT,'Enable','off');
set(h.Handles.BTNRefresh,'Enable','off');


function localRefresh(eventSrc, eventData, h)
tsgui=tstool;
alltsobjects = get(tsgui.Tsnode.getChildren,{'Timeseries'});
strCell={};
for i=1:length(alltsobjects)
    strCell=[strCell {alltsobjects{i}.Name}];
end
if isempty(strCell)
    strCell={''};
end
set(h.Handles.COMBsingleINSERT,'String', strCell,'Value',1);


function localRadio(eventSrc, eventData, h)
% callback for the radiobutton group
if get(eventSrc,'SelectedObject')==h.Handles.RADIOsingleNEW
    % single new
    set(h.Handles.EDTsingleNEW,'Enable','on','BackgroundColor',[1 1 1]);
    set(h.Handles.COMBmultipleNEW,'Enable','off');
    set(h.Handles.EDTmultipleNEW,'Enable','off','BackgroundColor',h.DefaultPos.OptionPanelDefaultColor);
    set(h.Handles.COMBsingleINSERT,'Enable','off');
    set(h.Handles.BTNRefresh,'Enable','off');
elseif get(eventSrc,'SelectedObject')==h.Handles.RADIOmultipleNEW
    set(h.Handles.EDTsingleNEW,'Enable','off','BackgroundColor',h.DefaultPos.OptionPanelDefaultColor);
    set(h.Handles.COMBmultipleNEW,'Enable','on');
    set(h.Handles.EDTmultipleNEW,'Enable','on','BackgroundColor',[1 1 1]);
    set(h.Handles.COMBsingleINSERT,'Enable','off');
    set(h.Handles.BTNRefresh,'Enable','off');
else
    set(h.Handles.EDTsingleNEW,'Enable','off','BackgroundColor',h.DefaultPos.OptionPanelDefaultColor);
    set(h.Handles.COMBmultipleNEW,'Enable','off');
    set(h.Handles.EDTmultipleNEW,'Enable','off','BackgroundColor',h.DefaultPos.OptionPanelDefaultColor);
    set(h.Handles.COMBsingleINSERT,'Enable','on');
    set(h.Handles.BTNRefresh,'Enable','on');
end


function localSwitchMultiple(eventSrc, eventData, h)
source=get(h.Handles.COMBsource,'Value');
if get(h.Handles.COMBmultipleNEW,'Value')==1
    % use a row or column as name
    if source==1
        % excel
        if get(h.Handles.exceldlg.Handles.COMBdataSample,'Value')==1
            % a sample is a row
            set(h.Handles.TXTmultipleNEW,'String','Row Number ');
            set(h.Handles.EDTmultipleNEW,'String','1');
        else
            set(h.Handles.TXTmultipleNEW,'String','Column Letter ');
            set(h.Handles.EDTmultipleNEW,'String','A');
        end
    elseif source==2
        % mat
        if get(h.Handles.matdlg.Handles.COMBdataSample,'Value')==1
            % a sample is a row
            set(h.Handles.TXTmultipleNEW,'String','Row Number ');
            set(h.Handles.EDTmultipleNEW,'String','1');
        else
            set(h.Handles.TXTmultipleNEW,'String','Column Number ');
            set(h.Handles.EDTmultipleNEW,'String','1');
        end
    elseif source==3
        % workspace
        if get(h.Handles.workspacedlg.Handles.COMBdataSample,'Value')==1
            % a sample is a row
            set(h.Handles.TXTmultipleNEW,'String','Row Number ');
            set(h.Handles.EDTmultipleNEW,'String','1');
        else
            set(h.Handles.TXTmultipleNEW,'String','Column Number ');
            set(h.Handles.EDTmultipleNEW,'String','1');
        end
    end
else
    set(h.Handles.TXTmultipleNEW,'String','Common Name ');
    if source==1
        % excel
        if ~isempty(h.Handles.exceldlg.Handles.ActiveX)
            set(h.Handles.EDTmultipleNEW,'String',h.Handles.exceldlg.Handles.ActiveX.ActiveSheet.Name);
        else
            strCell{get(h.Handles.COMBdataSheet,'Value')}
        end
    elseif source==2
        % mat
        set(h.Handles.EDTmultipleNEW,'String',h.Handles.matdlg.IOData.SelectedVariableInfo.name);
    elseif source==3
        % workspace
        set(h.Handles.EDTmultipleNEW,'String',h.Handles.workspacedlg.IOData.SelectedVariableInfo.name);
    end
end