function exportsingleworkspace(h)
% -------------------------------------------------------------------------
%% load default position parameters for all the components
% some of the code in the function is reused by the resize function
% -------------------------------------------------------------------------
h.ScreenSize=get(0,'ScreenSize');
figureleftoffset=h.ScreenSize(3)*0.3;
figurebottomoffset=h.ScreenSize(4)*0.3;
figurewidth=h.ScreenSize(3)*0.4;
figureheight=h.ScreenSize(4)*0.4;

% -------------------------------------------------------------------------
%% initialize the main figure window
% -------------------------------------------------------------------------
h.Figure = figure('Units', 'Pixels', 'Toolbar', 'None', 'Menubar', 'None', ...
            'Position', [figureleftoffset figurebottomoffset ...
            figurewidth figureheight], ...
            'NumberTitle', 'off', 'Name', 'Export Timeseries To ...', ...
            'HandleVisibility', 'Callback', ...
            'Resize','off');
% get figure color and use it as the background color for all components
FigureDefaultColor=get(h.Figure,'Color');

% -------------------------------------------------------------------------
%% Build file panel
% -------------------------------------------------------------------------
filepnloffset=10;
filepnlwidth=figurewidth-2*filepnloffset;
filepnlheight=80;
filepnlbottomoffset=figureheight-filepnlheight-filepnloffset;
h.Handles.PNLfile = uibuttongroup('Parent',h.Figure,'Units','Pixels', ...
    'BackgroundColor',FigureDefaultColor,'Position', ...
    [filepnloffset filepnlbottomoffset filepnlwidth filepnlheight],...
    'Title','Select Export Destination','SelectionChangeFcn',{@localRadioFile,h});
% create file selection items
radiooffset=10;
radiofilebottomoffset=40;
radiofilewidth=50;
radioheight=18;
h.Handles.RADIOfile = uicontrol('style','radiobutton','String',' File :','Units',...
    'Pixels','Parent',h.Handles.PNLfile,'Position',...
    [radiooffset radiofilebottomoffset radiofilewidth radioheight],...
    'BackgroundColor',get(h.Handles.PNLfile,'BackgroundColor'));
buttonfilewidth=50;
h.Handles.BTNbrowse = uicontrol('String','Browse','Parent',h.Handles.PNLfile, ...
    'Units','Pixels','BackgroundColor',FigureDefaultColor,'Position',...
    [filepnlwidth-buttonfilewidth-radiooffset radiofilebottomoffset ...
    buttonfilewidth radioheight],'Callback',{@localBrowse h});
h.Handles.EDTfile = uicontrol('style','edit','Units','Pixels','Parent',h.Handles.PNLfile,...
    'Position', [2*radiooffset+radiofilewidth radiofilebottomoffset ...
    filepnlwidth-4*radiooffset-radiofilewidth-buttonfilewidth radioheight],...
    'String','','HorizontalAlignment','Left','BackgroundColor',[1 1 1]);
% create workspace selection
radioworkbottomoffset=10;
radioworkwidth=150;
h.Handles.RADIOworkspace = uicontrol('style','radiobutton','String',' Base Workspace','Units',...
    'Pixels','Parent',h.Handles.PNLfile,'Position',...
    [radiooffset radioworkbottomoffset radioworkwidth radioheight],...
    'BackgroundColor',get(h.Handles.PNLfile,'BackgroundColor'));
% initial selection
set(h.Handles.PNLfile,'SelectedObject',h.Handles.RADIOfile);

% -------------------------------------------------------------------------
%% Build buttons at the bottom of the figure window
% -------------------------------------------------------------------------
leftoffsetOKbtn=400;        
leftoffsetAPPLYbtn=300;
leftoffsetCANCELbtn=200;
leftoffsetHELPbtn=100;
bottomoffsetbtn=10;
widthbtn=69;
heightbtn=23;
h.Handles.BTNok = uicontrol('String','OK','Parent',h.Figure,'Units','Pixels','BackgroundColor',FigureDefaultColor,'Position',...
    [figurewidth-leftoffsetOKbtn bottomoffsetbtn widthbtn heightbtn],'Callback',{@localOK h});
h.Handles.BTNapply = uicontrol('String','Apply','Parent',h.Figure,'Units','Pixels','BackgroundColor',FigureDefaultColor,'Position',...
    [figurewidth-leftoffsetAPPLYbtn bottomoffsetbtn widthbtn heightbtn],'Callback',{@localAPPLY h});
h.Handles.BTNcancel = uicontrol('String','Cancel','Parent',h.Figure,'Units','Pixels','BackgroundColor',FigureDefaultColor,'Position',...
    [figurewidth-leftoffsetCANCELbtn bottomoffsetbtn widthbtn heightbtn],'Callback',{@localCANCEL h});
h.Handles.BTNhelp = uicontrol('String','Help','Parent',h.Figure,'Units','Pixels','BackgroundColor',FigureDefaultColor,'Position',...
    [figurewidth-leftoffsetHELPbtn bottomoffsetbtn widthbtn heightbtn],'Callback',{@localHELP h});

% -------------------------------------------------------------------------
%% Build timeseries panel
% -------------------------------------------------------------------------
tspnloffset=10;
tspnlbottomoffset=bottomoffsetbtn+heightbtn+10;
tspnlwidth=figurewidth-2*filepnloffset;
tspnlheight=figureheight-filepnlheight-heightbtn-4*tspnloffset;
h.Handles.PNLts = uipanel('Parent',h.Figure,'Units','Pixels', ...
    'BackgroundColor',FigureDefaultColor,'Position', ...
    [tspnloffset tspnlbottomoffset tspnlwidth tspnlheight],...
    'Title','Select Timeseries Objects to be Exported');
% populate the table with timeseries objects
vars = evalin('base','who');
data={};
j=1;
for i=1:size(vars,1)
    if isa(evalin('base',vars{i}),'tsdata.timeseries')
        data(j,:)={false vars{i} evalin('base',strcat(vars{i},'.Name'))};
        j=j+1;
    end
end
% create uitable
if ~isempty(data)
    [h.Handles.tsTable, h.Handles.tsTablePanel] = uitable(h.Figure,'data',data);
    set(h.Handles.tsTablePanel,'Units','Pixels','Parent',h.Handles.PNLts,'Position', ...
        [10 10 tspnlwidth-20 tspnlheight-30], ...
        'BackgroundColor',FigureDefaultColor);
    set(h.Handles.tsTable.TableScrollPane,'Background',FigureDefaultColor);
    % prevent reordering the column
    h.Handles.tsTable.getTable.getTableHeader().setReorderingAllowed(false);
    h.Handles.tsTable.getTable.setRowSelectionAllowed(false);
    h.Handles.tsTable.getTable.setColumnSelectionAllowed(false);
    set(h.Handles.tsTable,'ColumnNames',{'Select' 'Timeseries Object' 'Description' 'Export Name(Optional)'});
end
%% set resize function handle
% set(h.Figure,'CloseRequestFcn',{@localCloseReq, h})

return


function localRadioFile(eventSrc, eventData, h)
% callback for the radiobutton group in the time panel
if get(eventSrc,'SelectedObject')==h.Handles.RADIOfile
    set(h.Handles.BTNbrowse,'Enable','on');
    set(h.Handles.EDTfile,'Enable','on','BackgroundColor',[1 1 1]);
else
    set(h.Handles.BTNbrowse,'Enable','off');
    set(h.Handles.EDTfile,'Enable','off','BackgroundColor',get(h.Figure,'Color'));
end


function localBrowse(eventSrc, eventData, h)
[filename, pathname, filterindex] = uigetfile( ...
   {'*.xls','Excel Spreadsheet Files(*.xls)'; ...
    '*.txt','ASCII Files (*.txt)'; ...
    '*.mat','MAT-Files (*.mat)'; ...
    '*.csv','Comma Separated Value Files(*.csv)'; ...
    '*.*',  'All Files (*.*)'});