function addTs(h,ts,varargin)

%% If necessary build the timeplot

if isempty(h.Plot)
    
    %% Hide status text
    set(h.Handles.InitTXT,'Visible','off')
    
    %% Create @timeplot. All @timeplot style managers are defined by the
    %% tsviewer
    viewpanel = tsguis.uitspanel(h.Figure,'Time series plot');
    h.Plot = tsguis.timeplot(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.
    %send(handle(ancestor(h.Plot.AxesGrid.Parent,'figure')),'resizeevent')   
   % eventviewer(ancestor(h.Figure,'figure'))
    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 listener to keep char checkboxes and domain panel updated
%%%%%%%%%%%%%%%%%%%%% PLOTTOOL INTEGRATION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%     h.addlisteners(handle.listener(h.Plot.AxesGrid,'ViewChanged', ...
%         @(es,ed) refresh(h)));
%%%%%%%%%%%%%%%%%%%%% PLOTTOOL INTEGRATION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
    %% 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

% Initial wave is abs time vector so start with an absolute time vector
if length(h.Plot.waves)==0 && ~isempty(ts.TimeInfo.StartDate)
    h.Plot.Absolutetime = 'on';
    h.Plot.StartDate = ts.TimeInfo.StartDate;
    h.Plot.TimeUnits = 'days';
    if ~isempty(ts.TimeInfo.Format)
        h.Plot.TimeFormat = ts.TimeInfo.Format;
    end
    h.Plot.settimemode(h.Plot.PropEditor,'absolute');
    %h.settimemode('absolute');
% Relative time vector to absolute plot      
elseif strcmp(h.Plot.Absolutetime,'on') && isempty(ts.TimeInfo.StartDate)
    msg = 'You are attempting to add a time series with a relative time vector to a view displaying an absolute time vector. Proceding will remove any refence to absolute time in the plot.';
    ButtonName = questdlg(msg, 'Time Vector Reference Mismatch', ...
                       'Convert View to Relative Time','Continue','Abort','Abort');
    if strcmp(ButtonName,'Abort')
        return
    end
    if strcmp(ButtonName,'Convert View to Relative Time')
        h.Plot.Absolutetime = 'off';
        h.Plot.settimemode(h.Plot.PropEditor,'relative');
        %h.settimemode('relative');
    else
        %h.settimemode('absolute');
        h.Plot.settimemode(h.Plot.PropEditor,'absolute');
    end
elseif strcmp(h.Plot.Absolutetime,'off') && ~isempty(ts.TimeInfo.StartDate)
    msg = 'You are attempting to add a time series with an absolute time vector to a view which uses a relative time vector so the absolute time cannot be displayed in the view';
    ButtonName = questdlg(msg, 'Time Vector Reference Mismatch', ...
                       'Continue','Abort','Abort');
    if strcmp(ButtonName,'Abort')
        return
    end
    %h.settimemode('relative');
    h.Plot.settimemode(h.Plot.PropEditor,'relative');
elseif length(h.Plot.waves)==0
    %h.settimemode('relative');
    h.Plot.settimemode(h.Plot.PropEditor,'relative');
end

%% If this is the first time series to be added sets the units/format in the
%% @timeplot to the current timeseries units/format
if length(h.Plot.waves)==0 
   set(h.Plot,'TimeUnits',ts.TimeInfo.Units,'TimeFormat',ts.TimeInfo.Format)
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 the axestable
h.axestable
h.Plot.Visible = 'on';



function localToolbarEnableToggle(eventSrc,eventData,h)

%% Callback to the timeplot axesgrid YNormalization listener which disables
%% the tsseriesview toolbar buttons when the timeplot is in normalized mode
dataselectmenu = h.Plot.AxesGrid.findMenu('dataselect');
timeselectmenu = h.Plot.AxesGrid.findMenu('timeselect');
if strcmp(h.Plot.Axesgrid.YNormalization,'on')
    set(h.Handles.ToolbarBtns,'Enable','off')
    if ~isempty(dataselectmenu)
        set(dataselectmenu,'Enable','off');
    end
    if ~isempty(timeselectmenu)
        set(timeselectmenu,'Enable','off');
    end    
else
    set(h.Handles.ToolbarBtns,'Enable','on')
    if ~isempty(dataselectmenu)
        set(dataselectmenu,'Enable','on');
    end
    if ~isempty(timeselectmenu)
        set(timeselectmenu,'Enable','on');
    end   
end