function child = createChild(h,varargin)

child = [];
if nargin==1
    % Add from import dialog
    tmp = tsguis.tsImportdlg;
    tmp.open;
    if isempty(tmp.OutputValue)
        return
    else
        names = fieldnames(tmp.OutputValue);
        for i=1:length(names)
            ts = tmp.OutputValue.(names{i});
            child = localCreateChild(h,ts,i<length(names));
        end
    end
else % The timeseries has been supplied as an argument
    ts = varargin{1};
    child = localCreateChild(h,ts);
end    

function child = localCreateChild(h,ts,varargin)

%% Get the time series object
tsIcon = fullfile(matlabroot, 'toolbox', 'matlab', 'timeseries', ...
                           'data.gif');                      
child = [];
name = {};

%% No complex numbers
if ~isreal(ts.Data)
    errordlg('Cannot use complext-valued time series within Time Series Tools',...
        'Time Series Tools','modal')
    return
end

%% check the size of each sample and return error if the sample size is not
%% 1-by-1 or a vector
tmpsize=ts.getContainer('Data').Samplesize;
if ~(length(tmpsize)==2) || all(tmpsize>1)
    errordlg('Time series data within the Time Series Tool cannot exceed 2 dimensions.',...
        'Time Series Tools','modal')
    return
end

%% check gridfirst value and make it true if it is false
if ts.datainfo.gridfirst==0
    % make transpose
    tmp=fliplr(size(ts.data));
    ts.init(reshape(ts.data,tmp([1 3])),ts.time);
    ts.datainfo.gridfirst=1;
end

%% Time series object must have unique name in tstool
% check duplication
tsNode = h.getChildren('Label',ts.Name);
if isempty(tsNode)
    % no duplication
    %% Create a @tsnode and add it to the parent and listen to data changes
    tsNode = tsguis.tsnode(ts);
    tsNode.Tslistener = handle.listener(tsNode.Timeseries,'datachange', {@localsyncts tsNode h});
    child = h.addNode(tsNode);

    % Stamp the history for display in the @tsnode panel
    if ~isempty(name)
       tsNode.History = sprintf('%s %s','Imported from workspace object',name{1});  
    end
else
    % Duplicated, check if same handle handle 
    if handle(tsNode.Timeseries)==handle(ts)
        % same handle, give a warning
        warndlg(sprintf('%s\n\n%s\n\n%s',['A time series node is already linked to the selected object and you don''t need to import it again.'],...
            'However, if you want to make a copy of it and import the copy, make sure the checkbox in the previous import dialog is checked.',... 
            'And also prepare to give a new name different from the one in the tstool.'));
        return
    else
        % different but same name, ask if a name change is desired
        tmpname=ts.name;
        while 1
            % comparing the given new name with all the nodes in tstool
            answer=inputdlg(sprintf('%s ''%s'' %s\n\n%s\n','Time Series object ', ...
            tmpname,...
            ' is already defined',...
            'Please give a different name for the new object to be imported :'),'Name Confliction Dialog');
            if isempty(answer)
                return;
            else
                tmpname = strtrim(cell2mat(answer));
                tsNode = h.getChildren('Label',tmpname);
                if ~isempty(tsNode)
                    continue;
                else
                    if isvarname(tmpname)
                        ts.name=tmpname;
                        break;
                    else
                        errordlg('invalid time series object name.');
                        return;
                    end
                end
            end
        end
        % Create a @tsnode and add it to the parent and listen to data changes
        tsNode = tsguis.tsnode(ts);
        tsNode.Tslistener = handle.listener(tsNode.Timeseries,'datachange',{@localsyncts tsNode h});
        child = h.addNode(tsNode);

        % Stamp the history for display in the @tsnode panel
        if ~isempty(name)
           tsNode.History = sprintf('%s %s','Imported from workspace object',name{1});  
        end
    end
end

%% Add listener to timeseries deletion which will remove the corresponding
%% node. This is esseential to enable undo to remove time series
manager = h.getRoot.TsViewer.TreeManager;
child.addListeners(handle.listener(child.Timeseries,'ObjectBeingDestroyed',@(es,ed) remove(child,manager)))

%% Expand and select the new timeseries node
% The setSelectedNode method will ultimately be executed on the awt thread.
% Consequently if >1 child is being added its callback (getDialogInterface)
% may fire after the 2nd or higher node has been added. This
% would result in two or more node panels being visible at one.
if nargin<=2 || ~varargin{1}
    h.getRoot.Tsviewer.TreeManager.Tree.expand(h.getTreeNodeInterface);
    h.getRoot.Tsviewer.TreeManager.Tree.setSelectedNode(tsNode.getTreeNodeInterface);
end

function localsyncts(eventSrc,eventData,tsNode,tsparentnode)
    
%% tsnode panel update

%% Update the timeseries editing table
tsNode.synctable;

%% Send a timeserieschange event 
tsparentnode.send('timeserieschange');