function h = tsImportdlg

% import com.mathworks.toolbox.timeseries.*;
import javax.swing.*;

% persistent mytsimportdlg;
% if isempty(mytsimportdlg)
%     mytsimportdlg = tsguis.tsImportdlg;
%     h = mytsimportdlg; 
% else
%     h = mytsimportdlg;
%     h.OutputValue=[];    
%     return
% end

h = tsguis.tsImportdlg;
h.OutputValue=[];

%% Build HG components
% figure window
heightbtn=23;
heightradio=18;
heighttxt=18;    
heightedt=21;
heightcomb=18;
widthbtn=50;
separation=10;
leftratio=0.25;
bottomratio=0.3;
widthratio=0.5;
heightratio=0.4;
ScreenSize=get(0,'ScreenSize');
h.Figure=figure('Name', 'Select Time Series Object(s) from Matlab Workspace', ...
        'WindowStyle','modal', ...
        'HandleVisibility', 'Callback', ...
        'Units', 'pixels', ...
        'Toolbar', 'None', ...
        'Menubar', 'None', ...
        'NumberTitle', 'off', ...
        'Resize', 'off', ...
        'Visible','off', ...
        'Position',[ScreenSize(3)*leftratio ...
                    ScreenSize(4)*bottomratio ...
                    ScreenSize(3)*widthratio ...
                    ScreenSize(4)*heightratio], ...
        'CloseRequestFcn', {@localFigClose h} ...
        );
FigureDefaultColor=get(h.Figure,'Color');
% buttons
h.Handles.BTNrefresh = uicontrol('Parent', h.Figure, 'Position',[ScreenSize(3)*widthratio-4*widthbtn-4*separation, separation, widthbtn, heightbtn],...
    'Style', 'pushbutton','Callback', {@localRefresh h}, 'String','Refresh');
h.Handles.BTNselect = uicontrol('Parent', h.Figure, 'Position',[ScreenSize(3)*widthratio-3*widthbtn-3*separation, separation, widthbtn, heightbtn],...
    'Style', 'pushbutton','Callback', {@localSave h}, 'String','Import');
h.Handles.BTNcancel = uicontrol('Parent',h.Figure,'Position',[ScreenSize(3)*widthratio-2*widthbtn-2*separation, separation, widthbtn, heightbtn],...
    'Style', 'pushbutton','Callback', {@localCancel h},  'String','Cancel');
h.Handles.BTNhelp = uicontrol('Parent',h.Figure,'Position',[ScreenSize(3)*widthratio-widthbtn-separation, separation, widthbtn, heightbtn],...
    'Style', 'pushbutton','Callback', {@localHELP h}, 'String','Help');
% workspace browser
h.Handles.Browser=sharedlsimgui.varbrowser;
h.Handles.Browser.typesallowed={'tsdata.timeseries'};
h.Handles.Browser.open;
s=JScrollPane(h.Handles.Browser.javahandle);
[junk, h.Handles.jBrowser]=javacomponent(s,[separation, 40, ScreenSize(3)*widthratio-2*separation, ScreenSize(4)*heightratio-40-separation],h.Figure);
h.Handles.Browser.javahandle.setMultiSelection(1);
h.Handles.Browser.javahandle.setColumnCount(2);
set(h.Handles.Browser.javahandle.getColumnOptions,'Resizable','on');
% assign the copy callback
h.Handles.Browser.addlisteners(handle.listener(h.Handles.Browser,'listselect',...
    {@localWorkspaceSelect h}));
% Install default listeners
h.Listeners = [handle.listener(h,h.findprop('Visible'),'PropertyPostSet',...
       @(es,ed) set(get(h,'Figure'),'Visible',get(h,'Visible')))];
% checkbox for copy or handle
h.Handles.Checkbox = uicontrol('Parent', h.Figure, 'Position',[separation, separation, 250, heightbtn],...
    'Style', 'checkbox', 'String','  make a copy of this time series object',...
    'BackgroundColor',FigureDefaultColor,'Value',1);


function localRefresh(eventSrc, eventData, h)
% callback for refresh button
h.Handles.Browser.open;
h.Handles.Browser.javahandle.deselectAll;


function localWorkspaceSelect(eventSrc, eventData, h)
info=h.Handles.Browser.getSelectedVarInfo;


function localFigClose(eventSrc, eventData, h)

h.OutputValue=[];
h.Visible = 'off';
uiresume(h.Figure);


function localSave(eventSrc, eventData, h)

%info=h.Handles.Browser.getSelectedVarInfo;
selectedRows=h.Handles.Browser.javahandle.getSelectedRows;
for i=1:length(selectedRows)
    name=h.Handles.Browser.Variables(selectedRows(i)+1).name;
    if ~isempty(name)
        if get(h.Handles.Checkbox,'Value')==1
            tmp=evalin('base',[name ';']);
            h.OutputValue.(name)=tmp.copy;
%             if ~strcmp(h.OutputValue.(name).timeinfo.startdate,'') && strcmp(h.OutputValue.(name).timeinfo.format,'')
%                 h.OutputValue.(name).timeinfo.format='dd-mmm-yyyy HH:MM:SS';
%             end
        else
            h.OutputValue.(name)=evalin('base',[name ';']);
%             if ~strcmp(h.OutputValue.(name).timeinfo.startdate,'') && strcmp(h.OutputValue.(name).timeinfo.format,'')
%                 h.OutputValue.(name).timeinfo.format='dd-mmm-yyyy HH:MM:SS';
%             end
        end
        if strcmp(h.OutputValue.(name).name,'default') && ~strcmp(name,'default')
            h.OutputValue.(name).name=name;
        end
    end
end
h.Visible = 'off';
uiresume(h.Figure);


function localCancel(eventSrc, eventData, h)

h.OutputValue=[];
h.Visible = 'off';
uiresume(h.Figure);


function localHELP(eventSrc, eventData, h)
% callback for the HELP button

helpdlg(sprintf('%s\n\n%s',...
    'Import Time Series Object(s) from workspace :',...
    'Highlight the time series objects and click the Import button'),...
    'Help');


