function addTs(h,ts,varargin)

%% If necessary build the specplot
if isempty(h.Plot)
    % Hide status text
    set(h.Handles.InitTXT,'Visible','off')
    
    %% Create @specplot. All @timeplot style managers are defined by the
    %% tsviewer
    %TO DO after g235628 is resolved: viewpanel =
    viewpanel = tsguis.uitspanel(h.Figure,'Time series periodogram');
    h.Plot = tsguis.specplot(viewpanel,1,'StyleManager',...
        h.getRoot.TsViewer.StyleManager);   
    
    h.Plot.Parent = h; %  Install parent
    
    %% Enable drag and drop after creating the property editor. The
    %% property editor must be created first because otherwize the
    %% plottools will overwrite the AxisCanval drop adaptor.
    propedit(h.Plot.AxesGrid.Parent);
    h.setDropAdaptor(h.DropAdaptor);
    
    % Drop out of plotedit mode. This must be done here or when the 
    % axes are copied by the resize method the callbacks will be in the 
    % wrong state.
    plotedit(h.Figure,'off')
    
    %% Add node listeners which require a plot to be present
    h.addPlotListeners
    
    % Lock out selection mode toolbar when normalized.
    % The reason is that the current mechanism for drawing normalized
    % plots introduces a flicker during mouse actions in special modes. The
    % reason is that when in normalization mode the view draw method sets the
    % XData and YData of all the lines to empty, leaving it to the view
    % adjustview method to reset them to non-empty values. The result is a
    % flicker during data selection actions.
    h.Plot.addlisteners(handle.listener(h.Plot.AxesGrid,h.Plot.AxesGrid.findprop('YNormalization'),...
       'PropertyPostSet',{@localToolbarEnableToggle h}));

    % Hack to refresh the figure window so that the Property Editor will
    % not appear overlapping the axes
    jf = get(h.Figure,'javaframe');
    mdi = javax.swing.SwingUtilities.getWindowAncestor(jf.getAxisComponent);
    s = mdi.getSize;
    s.width = s.getWidth+1;
    mdi.setSize(s);   
end

%% Is the time series already there?
existingTs = h.Plot.getTimeSeries;
if ~isempty(h.Plot.getTimeSeries(ts.Name))
    return 
end

%% If this is the first time series to be added sets the freq units in the
%% @specplot to the current timeseries units/format
if length(h.Plot.waves)==0 
   set(h.Plot.AxesGrid,'XUnits',sprintf('cyc/%s',ts.TimeInfo.Units(1:end-1)));
end

%% Update table and plot
if nargin==2
    h.Plot.addTs(ts);
else
    h.Plot.addTs(ts,varargin{1});
end

%% Fire tschanged event to announce the change
h.send('tschanged',handle.EventData(h,'tschange'));

%% Refresh
%%%%%%%%%%%%%%%%%%%%% PLOTTOOL INTEGRATION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%h.axestable
%%%%%%%%%%%%%%%%%%%%% PLOTTOOL INTEGRATION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
h.Plot.Visible = 'on';
    
function localToolbarEnableToggle(eventSrc,eventData,h)

%% Callback to the specplot axesgrid YNormalization listener which disables
%% the tsspecnode toolbar buttons when the specplot is in normalized mode
if strcmp(h.Plot.Axesgrid.YNormalization,'on')
    set(h.Handles.ToolbarBtns,'Enable','off')
else
    set(h.Handles.ToolbarBtns,'Enable','on')
end
