function initializeOptionPanel(h)
% INITIALIZEOPTIONPANEL initializes the 'Import Option' 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', ...
    'FontUnits','normalized',...
    'FontSize',9,...
    'FontWeight','bold', ...
    'BackgroundColor',h.DefaultPos.OptionPanelDefaultColor, ...
    'Title','3. Select Import Options', ...
    'SelectionChangeFcn',{@localRadio,h}, ...
    'Position',[h.DefaultPos.leftoffsetpnl ...
                h.DefaultPos.bottomoffsetOptionpnl ...
                h.DefaultPos.widthpnl ...
                h.DefaultPos.heightOptionpnl] ...
    );
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','Right', ...
    '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] ...
    );
%populate combobox with current timeseries objects in the base workspace
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
h.Handles.COMBsingleINSERT = uicontrol('Parent',h.Handles.PNLoption, ...
    'style','popupmenu', ...
    'Units','Pixels', ...
    'BackgroundColor',[1 1 1], ...
    'String', strCell, ...
    'Value',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] ...
);
% 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');


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');
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');
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');
end


function localSwitchMultiple(eventSrc, eventData, h)
if get(h.Handles.COMBmultipleNEW,'Value')==1
    % use a row or column as name
    if get(h.Handles.COMBdataSample,'Value')==1
        % a sample is a row
        set(h.Handles.TXTmultipleNEW,'String','Row Number ');
    else
        set(h.Handles.TXTmultipleNEW,'String','Column Letter ');
    end
else
    set(h.Handles.TXTmultipleNEW,'String','Common Name ');
end