function initializeDataPanel(h)
% INITIALIZEDATAPANEL initializes the 'Select Data to Import' panel, which
% contains either an activex control or a uitable for spreadsheet display,
% as well as a few other uicontrols

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

% -------------------------------------------------------------------------
%% Build data panel
%
% Note that: currently we only support single block data selection from the
% excel sheet.  The reason is that: 1. the mouse operation provided by the
% activex control only allows to select a single block; 2. the absolute
% time format results in a very complicated parser for the edit boxes if
% multuiple blocks are allowed.  So, if user has discontinuous blocks, he
% can either merge them in excel before importing or create multiple
% timeseries during the import stage and then merge them later in tstool.
% -------------------------------------------------------------------------
h.DefaultPos.DataPanelDefaultColor=h.DefaultPos.FigureDefaultColor;
h.Handles.PNLdata = uipanel('Parent',h.Figure, ...
    'Units','Pixels', ...
    'FontUnits','normalized',...
    'FontSize',9,...
    'FontWeight','bold', ...
    'BackgroundColor',h.DefaultPos.DataPanelDefaultColor,...
    'Position', [h.DefaultPos.leftoffsetpnl h.DefaultPos.bottomoffsetDatapnl h.DefaultPos.widthpnl h.DefaultPos.heightDatapnl], ...
    'Title','1. Select Data To Import');

% -------------------------------------------------------------------------
%% create all the other ui controls in the data panel
% -------------------------------------------------------------------------
% add column and row edit boxes, static texts and radio buttons
h.Handles.TXTdataSample = uicontrol('Parent',h.Handles.PNLdata, ...
    'style','text', ...
    'Units','Pixels', ...
    'BackgroundColor',h.DefaultPos.DataPanelDefaultColor, ...
    'String','Time vector is a : ', ...
    'HorizontalAlignment','Left', ...
    'Position',[h.DefaultPos.TXTdataSampleleftoffset ...
                h.DefaultPos.TXTdataSamplebottomoffset ...
                h.DefaultPos.TXTdataSamplewidth ...
                h.DefaultPos.heighttxt] ...
    );
h.Handles.COMBdataSample = uicontrol('Parent',h.Handles.PNLdata, ...
    'style','popupmenu', ...
    'Units','Pixels', ...
    'BackgroundColor',[1 1 1], ...
    'String',[{'Column'},{'Row'}], ...
    'Value',1,...
    'TooltipString','If data in the current variable is organized by column, select ''Column'', otherwise select ''Row''.', ...
    'Position',[h.DefaultPos.COMBdataSampleleftoffset ...
                h.DefaultPos.TXTdataSamplebottomoffset+4 ...
                h.DefaultPos.COMBdataSamplewidth ...
                h.DefaultPos.heightcomb], ...
    'Callback',{@localSwitchSample h} ...
    );
h.Handles.TXTFROM = uicontrol('Parent',h.Handles.PNLdata, ...
    'style','text', ...
    'Units','Pixels', ...
    'BackgroundColor',h.DefaultPos.DataPanelDefaultColor, ...
    'String','Selected Rows : ', ...
    'HorizontalAlignment','Left', ...
    'Position',[h.DefaultPos.TXTfromleftoffset ...
                h.DefaultPos.TXTdatabottomoffset ...
                h.DefaultPos.TXTfromwidth ...
                h.DefaultPos.heighttxt] ...
    );
h.Handles.EDTFROM = uicontrol('Parent',h.Handles.PNLdata, ...
    'style','edit', ...
    'Units','Pixels', ...
    'BackgroundColor',[1 1 1],...
    'String','', ...
    'TooltipString','Key in the row indices following the Matlab supported format', ...
    'HorizontalAlignment','Left', ...
    'Position',[h.DefaultPos.EDTfromleftoffset ...
                h.DefaultPos.TXTdatabottomoffset+2 ...
                h.DefaultPos.EDTfromwidth ...
                h.DefaultPos.heightedt],...
    'Callback',{@localChangeBlock h 'FROM'} ...
    );
h.Handles.TXTTO = uicontrol('Parent',h.Handles.PNLdata, ...
    'style','text', ...
    'Units','Pixels', ...
    'BackgroundColor',h.DefaultPos.DataPanelDefaultColor, ...
    'String','Selected Columns : ', ...
    'HorizontalAlignment','Left', ...
    'Position',[h.DefaultPos.TXTtoleftoffset ...
                h.DefaultPos.TXTdatabottomoffset ...
                h.DefaultPos.TXTtowidth ...
                h.DefaultPos.heighttxt] ...
    );
h.Handles.EDTTO = uicontrol('Parent',h.Handles.PNLdata, ...
    'style','edit', ...
    'Units','Pixels', ...
    'BackgroundColor',[1 1 1],...
    'String','',...
    'TooltipString','Key in the column indices following the Matlab supported format).', ...
    'HorizontalAlignment','Left',...
    'Position',[h.DefaultPos.EDTtoleftoffset ...
                h.DefaultPos.TXTdatabottomoffset+2 ...
                h.DefaultPos.EDTtowidth ...
                h.DefaultPos.heightedt],...
    'Callback',{@localChangeBlock h 'TO'} ...
    );
h.Handles.BTNrefresh = uicontrol('Parent',h.Handles.PNLdata, ...
    'style','pushbutton', ...
    'Units','Pixels', ...
    'BackgroundColor',h.DefaultPos.DataPanelDefaultColor,...
    'String','Refresh',...
    'Position',[h.DefaultPos.widthpnl-70-20 ...
                h.DefaultPos.TXTdataSamplebottomoffset+4 ...
                70 ...
                h.DefaultPos.heightbtn],...
    'Callback',{@localRefresh h} ...
    );


% -------------------------------------------------------------------------
%% common callbacks
% -------------------------------------------------------------------------
function localRefresh(eventSrc, eventData, h)
% callback for refresh button
h.Handles.Browser.open;
h.Handles.Browser.javahandle.deselectAll;
h.ClearEditBoxes;
h.IOData.SelectedVariableInfo=[];


function localSwitchSample(eventSrc, eventData, h)
% callback for COMBdataSample (Switch : a sample is a row or a column)
% change some disaply
if get(h.Handles.COMBdataSample,'Value')==1
    localSetHEAD(h,'row');    
    % time vector is stored as a column
    if ~isempty(h.IOData.SelectedRows)
        h.updateStartEndTime(h.IOData.SelectedRows(1),h.IOData.SelectedRows(end));
    end
else
    localSetHEAD(h,'column');    
    % time vector is stored as a row
    if ~isempty(h.IOData.SelectedColumns)
        h.updateStartEndTime(h.IOData.SelectedColumns(1),h.IOData.SelectedColumns(end));
    end
end


function localChangeBlock(eventSrc, eventData, h, token)
% callback for the editbox in the data panel
% 'token' parameter accepts 'FROM' and 'TO'

% get string from the FROM editbox
tmpStr=strtrim(get(h.Handles.EDTFROM,'String'));
if isempty(tmpStr)
    return
end
try
    row=eval(tmpStr);
catch
    msgbox('Please use the standard matlab index, e.g. [1:10]','Time Series Tools');
    return;
end
tmpStr=strtrim(get(h.Handles.EDTTO,'String'));
if isempty(tmpStr)
    return
end
try
    column=eval(tmpStr);
catch
    msgbox('Please use the standard matlab index, e.g. [1:10]','Time Series Tools');
    return;
end
row=unique(sort(row));
column=unique(sort(column));
% get block size and check if it is a valid block    
if ~isempty(h.IOData.SelectedVariableInfo)
    h.IOData.SelectedRows=row(row>=1&row<=h.IOData.SelectedVariableInfo.size(1));
    h.IOData.SelectedColumns=column(column>=1&column<=h.IOData.SelectedVariableInfo.size(2));
end
if get(h.Handles.COMBdataSample,'Value')==1
    % time vector is stored as a column
    if ~isempty(h.IOData.SelectedRows)
        h.updateStartEndTime(h.IOData.SelectedRows(1),h.IOData.SelectedRows(end));
    else
        set(h.Handles.EDTtimeSheetStart,'String','');
        set(h.Handles.EDTtimeSheetEnd,'String','');
    end
else
    % time vector is stored as a row
    if ~isempty(h.IOData.SelectedColumns)
        h.updateStartEndTime(h.IOData.SelectedColumns(1),h.IOData.SelectedColumns(end));
    else
        set(h.Handles.EDTtimeSheetStart,'String','');
        set(h.Handles.EDTtimeSheetEnd,'String','');
    end
end


function localSetHEAD(h,str) 
% change combobox contents in the tab panel
if strcmpi(str,'row')
    % a sample is a row
    set(h.Parent.Handles.TXTmultipleNEW,'String','Row Number : ');
    set(h.Parent.Handles.EDTmultipleNEW,'String','1');
    h.TimePanelUpdate('column');
else
    % a sample is a column
    set(h.Parent.Handles.TXTmultipleNEW,'String','Column Number : ');
    set(h.Parent.Handles.EDTmultipleNEW,'String','1');
    h.TimePanelUpdate('row');
end

