function flag=initialize(h,filename)
% INITIALIZE is the function that tstool uses to display the import dialog
% given full path of the file in the 'filename' parameter.

% 1. the import dialog is resizable.
% 2. if the filename is the same as the last one, just display the import
%    dialog without reloading the file, otherwise, load the new file and
%    refresh the dialog.

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

% -------------------------------------------------------------------------
% load default position parameters for all the components
% -------------------------------------------------------------------------
h.defaultPositions;

% -------------------------------------------------------------------------
% if using ActiveX control for display, create webcomponent connection
% everytime 
% -------------------------------------------------------------------------
if ~isempty(h.Handles.ActiveX)
    try
        h.Handles.WebComponent = actxserver('Excel.Application');
        h.Handles.WebComponent.Visible = 0;
    catch
        % if error, use uitable for display
        h.Handles.ActiveX=[];
        h.Handles.WebComponent=[];
    end       
end


% -------------------------------------------------------------------------
%% check if the same file is to be opened 
% -------------------------------------------------------------------------
if strcmp(h.IOData.FileName,filename)
    % the input filename is the same as h.IOData.ExcelFileName
    if ~isempty(h.Handles.ActiveX)
        % activex web component should be available
        h.Handles.ActiveX.move([h.DefaultPos.Table_leftoffset ...
                                h.DefaultPos.Table_bottomoffset ...
                                h.DefaultPos.Table_width ...
                                h.DefaultPos.Table_height]);
        % we are going to reconnect to the excel file through the com
        % server since everytime the dialog is closed the connection is
        % lost. 
        try
            % open the file connection
            h.Handles.originalWorkbooks = h.Handles.WebComponent.Workbooks;
            invoke(h.Handles.originalWorkbooks, 'open', filename);
            h.Handles.originalSheets = h.Handles.WebComponent.ActiveWorkBook.Sheets;
        catch
            % activex web component should be available. however, if we have
            % the connection problem, use uitable instead
            errordlg('excel import dialog couldn''t connect to the excel workbook','Time Series Tools');
            flag=false;
            return
        end
    end
    if  ~isempty(h.Handles.tsTable)
       set(h.Handles.tsTable,'Position',...
           [h.DefaultPos.Table_leftoffset ...
            h.DefaultPos.Table_bottomoffset+28 ...
            h.DefaultPos.Table_width ...
            h.DefaultPos.Table_height-28]);
    end                            
    set(h.Handles.PNLdata,'Visible','on');
    set(h.Handles.PNLtime,'Visible','on');
    % update the dialog title
    set(h.Figure,'Name',strcat('Import Timeseries From an Excel Workbook (',filename,')'));
    flag=true;
    return
end

% update the filename
h.IOData.FileName=filename;

% -------------------------------------------------------------------------
%% Initialize the internal state variables
% -------------------------------------------------------------------------
% update the dialog title
set(h.Figure,'Name',strcat('Import Timeseries From an Excel Workbook (',filename,')'));
% initialize state variables
h.IOData.checkLimitColumn=20;
h.IOData.checkLimitRow=20;
% no selection
h.IOData.SelectedColumns=[];
h.IOData.SelectedRows=[];
% no format information
h.IOData.formatcell=struct();
h.IOData.formatcell.name='';
h.IOData.formatcell.columnIndex=0;
h.IOData.formatcell.rowIndex=0;
h.IOData.formatcell.matlabFormatString = ...
    {'dd-mmm-yyyy HH:MM:SS' 'dd-mmm-yyyy' 'mm/dd/yy' 'mm/dd' 'HH:MM:SS' ...
    'HH:MM:SS PM' 'HH:MM' 'HH:MM PM' 'mmm.dd,yyyy HH:MM:SS' 'mmm.dd,yyyy' 'mm/dd/yyyy'};
h.IOData.formatcell.matlabFormatIndex = [0 1 2 6 13 14 15 16 21 22 23];
h.IOData.formatcell.matlabUnitString={'Years','Weeks','Days','Hours','Mins','Secs'};

h.Handles.bar=waitbar(20/100,'Loading Excel Spreadsheet, Please Wait...');
% -------------------------------------------------------------------------
%% Build data panel
% -------------------------------------------------------------------------
flag=h.initializeDataPanel(filename);
if ~flag
    h.IOData.FileName=[];
    if ~isempty(h.Handles.WebComponent)
        try
            invoke(h.Handles.WebComponent, 'quit'); 
            invoke(h.Handles.WebComponent, 'delete'); 
        catch
            % failed to use the activex component for some reason unknown
            % errordlg('excel import dialog couldn''t connect to the excel comserver during exit','Time Series Tools');
        end
    end
    return
end
waitbar(90/100,h.Handle.bar);

% -------------------------------------------------------------------------
%% Build time panel
% -------------------------------------------------------------------------
h.initializeTimePanel;
waitbar(100/100,h.Handle.bar);
delete(h.Handle.bar);

% -------------------------------------------------------------------------
%% select all
% -------------------------------------------------------------------------
if ~isempty(h.Handles.ActiveX)
    tmpStr=strcat('A1:',...
            h.findcolumnletter(size(h.Handles.ActiveX.ActiveSheet.UsedRange.Value,2)),...
            num2str(size(h.Handles.ActiveX.ActiveSheet.UsedRange.Value,1)));
    h.Handles.ActiveX.ActiveSheet.Range(tmpStr).Select;
elseif ~isempty(h.Handles.tsTable)
    awtinvoke(h.Handles.tsTable.getTable,'selectAll');
end

% -------------------------------------------------------------------------
%% show table
% -------------------------------------------------------------------------
if ~isempty(h.Handles.ActiveX)
    h.Handles.ActiveX.move([h.DefaultPos.Table_leftoffset ...
                        h.DefaultPos.Table_bottomoffset ...
                        h.DefaultPos.Table_width ...
                        h.DefaultPos.Table_height]);
else
    set(h.Handles.tsTablePanel,'Position', ...
        [h.DefaultPos.Table_leftoffset ...
        h.DefaultPos.Table_bottomoffset+28 ...
        h.DefaultPos.Table_width ...
        h.DefaultPos.Table_height-28]);
end
set(h.Handles.PNLdata,'Visible','on');
set(h.Handles.PNLtime,'Visible','off');
set(h.Handles.PNLDisplayWorkspaceInfo,'Title','Selected Time Vector Information : ');
set(h.Handles.PNLtime,'Visible','on');

