function h = addplot(this,manager,varargin)

%% Add an empty plot (table) to the MDI

set(manager.figure,'Pointer','watch')

%% Build figures
f = figure('Visible','off', 'NumberTitle','off','HandleVisibility','callback',...
    'WindowButtonMotionFcn',@hoverfig);
jf = get(f, 'JavaFrame');
drawnow % Figure must be fully built before setting the frame 
jf.setGroupName('Time Series Viewer');

%% Create view objects
switch this.Label
    case 'Time Plots'
        h  = tsguis.tsseriesview;
        namePrefix = 'Time plot: ';
        thishelpfile = 'timeplot.htm';
    case 'Spectral Plots'
        h  = tsguis.tsspecnode;
        namePrefix = 'Spectral plot: ';
        thishelpfile = 'specplot.htm';
    case 'XY Plots'
        h  = tsguis.tsxynode;
        namePrefix = 'XY plot: ';
        thishelpfile = 'xyplot.htm';
    case 'Correlations'
        h  = tsguis.tscorrnode;
        namePrefix = 'Correlation plot: ';   
        thishelpfile = 'corrplot.htm';
    case 'Histograms'
        h  = tsguis.tshistnode;
        namePrefix = 'Histogram: ';    
        thishelpfile = 'histplot.htm';
end
h.HelpFile = fullfile(matlabroot, 'toolbox', 'matlab', 'timeseries', ...
                         '@tsguis', '@viewnode',thishelpfile);
                     
%% Leaf node designation
h.AllowsChildren = false;

%% Build the view and set drop adaptors
if nargin>=3
    h.initialize(manager,f,varargin{1});
    set(h.Figure,'Name',[namePrefix, varargin{1}]);
else
    ct = 1;
    views = this.up.find('-depth',2);
    while ~isempty(views) && any(strcmp(sprintf('View%d',ct),get(views,{'Label'})))
        ct = ct+1;
    end      
    h.initialize(manager,f,sprintf('View%d',ct));
    set(h.Figure,'Name',[namePrefix sprintf('View%d',ct)]);
end

%% Does the new view have the right class
if ~strcmp(this.ChildClass,class(h))
    error('Invalid child class')
end

%% Add the node
newView = this.addNode(h);

%% Customize toolbar
h.Handles.Toolbar = findall(f,'Type','uitoolbar');
switch this.Label
    case 'Time Plots'  
        modenames = {'DataSelect','TimeseriesTranslate','TimeseriesScale',...
            'TimeSelect'};
        gifnames = {'tsselect.gif','tstrans.gif','tsscale.gif','tstimeselect.gif'};
        tipstr = {'Selects data','Move time series','Rescale time series',...
            'Select time interval(s)'};
    case 'Spectral Plots'
        modenames = {'IntervalSelect'};
        gifnames = {'tstimeselect.gif'};
        tipstr = {'Select frequency interval(s)'};
    case 'Histograms'
        modenames = {'IntervalSelect'};
        gifnames = {'tsrangeselect.gif'};
        tipstr = {'Select Y range interval'};
    case 'XY Plots'
        modenames = {'DataSelect'};
        gifnames = {'tsselect.gif'};        
        tipstr = {'Select points'};
    otherwise
        modenames = {};
        gifnames = {};        
        tipstr = {};
end

%% Assign on/off callbacks
for k=1:length(modenames)
    h.Handles.ToolbarBtns(k) = uitoggletool('Parent',h.Handles.Toolbar,...
               'Tag',modenames{k},'CData',localGetIcon(gifnames{k}));
    set(h.Handles.ToolbarBtns(k),'OnCallback',...
        {@localSetSelectmode h modenames{k}},'offcallback', ...
        {@localClearSelectmode h},'TooltipString',tipstr{k})
end

%% HG Toolbar buttons de-activate custom toolbar
hgbuttons = [uigettoolbar(f,'Exploration.ZoomIn');...
             uigettoolbar(f,'Exploration.ZoomOut');...
             uigettoolbar(f,'Exploration.Pan');...
             uigettoolbar(f,'Standard.EditPlot')];
set(hgbuttons,'OnCallback',{@localCustomTBoff h})
    
%% Expand and select the new view node
%drawnow
manager.Tree.expand(this.getTreeNodeInterface);
manager.Tree.expand(newView.getTreeNodeInterface);

%% Show fig
set(f,'Visible','on','WindowStyle', 'docked')

tsparent = manager.Root.find('-class','tsguis.tsparentnode');
newView.addlisteners(handle.listener(tsparent,'ObjectChildRemoved',...
    {@localRemoveTs newView}));

set(manager.figure,'Pointer','arrow')

function cdata = localGetIcon(thispath)

%% Get the icon for the data selection toolbar button
[cdata,map] = imread(thispath);

% Set all white (1,1,1) colors to be transparent (nan)
ind = find(map(:,1)+map(:,2)+map(:,3)==3);
map(ind) = nan;
cdata = ind2rgb(cdata,map);

function localSetSelectmode(eventSrc,eventData,h,type)

% onCallback to select mode toobar button which sets the select mode
% of the view

% Check that the Plot had been created
if ~isempty(h.Plot) && ishandle(h.Plot)
   h.Plot.setselectmode(type);
end

function localClearSelectmode(eventSrc,eventData,h)

% offCallback to select mode toobar button which sets the select mode
% of the view to None

if all(strcmp(get(h.Handles.ToolbarBtns,'State'),'off'))
    % Check that the Plot had been created and is not locked
    if ~isempty(h.Plot) && ishandle(h.Plot)
       h.Plot.setselectmode('None');
    end
end

    
function localRemoveTs(eventSrc,eventData,view)

%% Listener callback for @tsnodes being removed which removes the
%% corresponding waveform from any listenening views
if ~isempty(view.Plot)
    view.removets(eventData.Child.Timeseries);
end


function localCustomTBoff(es,ed,h)

%% HG Toolbarbutton OnCallback which clears special selection modes and
%% unpops their toolbar buttons
if ~isempty(h.Plot)
    h.Plot.setselectmode('None')
end