function menu = getPopupSchema(this,manager,varargin)
% GETPOPUPSCHEMA Constructs the default popup menu

% Author(s): James G. Owen
% Revised: 
% Copyright 1986-2002 The MathWorks, Inc.
% $Revision: 1.1.6.1 $ $Date: 2004/12/26 21:42:40 $

%% Create menus
menu  = com.mathworks.mwswing.MJPopupMenu('Default Menu');
menuDelete = com.mathworks.mwswing.MJMenuItem('Remove timeseries');
menuCopy = com.mathworks.mwswing.MJMenuItem('Copy timeseries');
menuPaste = com.mathworks.mwswing.MJMenuItem('Paste timeseries');
menuRename = com.mathworks.mwswing.MJMenuItem('Rename timeseries');
menuPreproc = com.mathworks.mwswing.MJMenuItem('Preprocess...');
menuExportFile = com.mathworks.mwswing.MJMenuItem('Export to file...');
menuExportWorkspace = com.mathworks.mwswing.MJMenuItem('Export to workspace...');
menuResample = com.mathworks.mwswing.MJMenuItem('Resample...');
menuExpression = com.mathworks.mwswing.MJMenuItem('MATLAB expression...');
ViewsMenu = com.mathworks.mwswing.MJMenu('Add to Plot');

%% Add them
menu.add(menuCopy);
menu.add(menuPaste);
menu.addSeparator;
menu.add(menuDelete);
menu.add(menuRename);
menu.addSeparator;
menu.add(menuExportFile);
menu.add(menuExportWorkspace);
menu.addSeparator;
menu.add(menuPreproc);
menu.add(menuResample);
menu.addSeparator;
%menu.add(menuArithmetic);
menu.add(menuExpression);
menu.addSeparator;
menu.add(ViewsMenu);

%% Add view type menus and their children comprising menus for "new view"
%% and all existing views
viewsNode = manager.Root.Tsviewer.ViewsNode;
viewTypeNodes = viewsNode.getChildren;
for k=1:length(viewTypeNodes)
    % Create the view type menu
    viewTypeMenu = com.mathworks.mwswing.MJMenu(viewTypeNodes(k).Label);
    ViewsMenu.add(viewTypeMenu);
    
    % Find the existing view nodes for this view type, if necessary
    % exclusing nodes that are being removed (varargin{1})
    viewNodes = viewTypeNodes(k).find('-class',viewTypeNodes(k).ChildClass);
    if nargin==3
        viewNodes = setdiff(viewNodes,varargin{1});
    end
    % Add the "new view" menu
    newViewMenu = com.mathworks.mwswing.MJMenuItem('New view...');
    set(handle(newViewMenu,'Callbackproperties'),'ActionPerformedCallback', ...
        {@localAddNewView,viewTypeNodes(k),manager,this.Timeseries})
    viewTypeMenu.add(newViewMenu);
    
    % Add menus for each existing view
    for j=1:length(viewNodes)
        viewMenu = com.mathworks.mwswing.MJMenuItem(viewNodes(j).Label);
        viewTypeMenu.add(viewMenu);
        set(handle(viewMenu,'CallbackProperties'),'ActionPerformedCallback',...
            @(es,ed) addTs(viewNodes(j),get(this,'Timeseries')),'MouseClickedCallback',...
            @(es,ed) addTs(viewNodes(j),get(this,'Timeseries')));
    end
end

%% Assign menu callbacks
set(handle(menuDelete,'callbackproperties'),'ActionPerformedCallback',...
    @(es,ed) remove(this,manager))
set(handle(menuCopy,'callbackproperties'),'ActionPerformedCallback',...
    @(es,ed) copynode(this,manager))
set(handle(menuPaste,'callbackproperties'),'ActionPerformedCallback',...
    @(es,ed) pastenode(this,manager))
set(handle(menuRename,'callbackproperties'),'ActionPerformedCallback',...
    {@LocalRename,this,manager}); 
set(handle(menuExportFile,'callbackproperties'),'ActionPerformedCallback',...
    @(es,ed) openExportdlg(this,manager))  
set(handle(menuExportWorkspace,'callbackproperties'),'ActionPerformedCallback',...
    {@LocalExportWorkspace,this,manager});
set(handle(menuPreproc,'callbackproperties'),'ActionPerformedCallback',...
    @(es,ed) openpreprocdlg(this,manager))
set(handle(menuResample,'callbackproperties'),'ActionPerformedCallback',...
    @(es,ed) openmergedlg(this,manager))
% set(handle(menuDataselect,'callbackproperties'),'ActionPerformedCallback',...
%     @(es,ed) openselectdlg(this,manager))
set(handle(menuExpression,'callbackproperties'),'ActionPerformedCallback',...
    @(es,ed) openarithdlg(this,manager,get(this,'Label')));

%% Add listener to update the enabled state of the paste menu depending on
%% the contents of the viewer clipboard
this.addListeners(handle.listener(manager.Root.Tsviewer,...
    manager.Root.Tsviewer.findprop('Clipboard'),'PropertyPostSet',...
    {@localSetPasteMenu manager.Root.Tsviewer menuPaste}));
localSetPasteMenu([],[],manager.Root.Tsviewer,menuPaste) % Exercise it

%% Listeners which force the context menus to be rebuilt if the view nodes
%% change
delete(this.Viewlisteners)
this.Viewlisteners = [handle.listener(viewTypeNodes,'ObjectChildAdded', ...
                           @(es,ed) clearmenus(this,manager)); ...
                      handle.listener(viewTypeNodes,'ObjectChildRemoved', ...
                           @(es,ed) clearmenus(this,manager,ed.Child))];


% --------------------------------------------------------------------------- %
function LocalRename(eventSrc, eventData, this, manager)

newname = inputdlg('New node name','Time Series Toolbox');
if ~isempty(newname)
    this.Timeseries.Name = newname{1};
    this.Label = newname{1};
    this.up.send('timeserieschange')
end

function LocalAddTs(eventSrc,eventData,tsnode,manager)

viewNodes = manager.Root.find('-class','tsguis.tsseriesview');
figure(double(viewNodes(1).Figure))
viewNodes(1).addTs(tsnode.Timeseries);

function localSetPasteMenu(eventSrc,eventData,viewer,MenuPaste)

%% Callback to tsviewer clipboard listener which sets the enabled state of
%% the paste menu
MenuPaste.setEnabled(isa(viewer.ClipBoard,'tsguis.tsnode'));

function localAddNewView(eventSrc,eventdata,thisviewnode,manager,ts)

%% Callback to the "new view" menu which creates a new view for the 
%% parent node thisviewnode and adds the currently selected time series to
%% it
newviewnode = thisviewnode.addplot(manager);
newviewnode.addTs(ts);

function localOpenDlg(eventSrc,eventData,this,manager,dlgmethod)

opendlg(this,manager,dlgmethod)


function LocalExportWorkspace(eventSrc, eventData, this, manager)

%% export this timeseries object to workspace
list=evalin('base','whos;');
flag=false;
for i=1:length(list)
    if strcmp(list(i).name,this.Timeseries.name)
        flag=true;
        break;
    end
end
if flag
    ButtonName=questdlg('A variable with the same name as the timeseries object already exists in the workspace.  Do you want to overwrite the existing variable or abort this operation?',...
        'Duplicated Variable Detected','Overwrite','Abort','Overwrite');
    switch ButtonName,
        case 'Overwrite'
            assignin('base',this.Timeseries.name,this.Timeseries);
        case 'Abort'
            return
    end
else
    assignin('base',this.Timeseries.name,this.Timeseries);
end