function figmenus(this)

%% File menu
fileMenu = uimenu('Parent',this.TreeManager.Figure,'Label','File');

%% File - new submenu
newFileMenu = uimenu('Parent',fileMenu,'Label','New');
newFilePlotMenu = uimenu('Parent',newFileMenu,'Label','Plot');
newFileTimePlotMenu = uimenu('Parent',newFilePlotMenu,'Label','Time Plot', ...
    'Callback',@(es,ed) addPlot(this,'Time Plots'));
newFileSpecPlotMenu = uimenu('Parent',newFilePlotMenu,'Label','Spectral Plot', ...
    'Callback',@(es,ed) addPlot(this,'Spectral Plots'));
newFileXYPlotMenu = uimenu('Parent',newFilePlotMenu,'Label','XY Plot', ...
    'Callback',@(es,ed) addPlot(this,'XY Plots'));
newFileCorrPlotMenu = uimenu('Parent',newFilePlotMenu,'Label', ...
    'Correlation Plot','Callback',@(es,ed) addPlot(this,'Correlations'));
newFileHistPlotMenu = uimenu('Parent',newFilePlotMenu,'Label', ...
    'Histogram','Callback',@(es,ed) addPlot(this,'Histograms'));
newFileTimeseriesMenu = uimenu('Parent',newFileMenu,'Label', ...
    'Time Series','Callback',{@localAddTS this});

% -----------------------------------------------------------------------
% Import/Export 
% -----------------------------------------------------------------------
%% File - Import Menu
importMenu = uimenu('Parent',fileMenu,'Label','Import...','Separator','on','Callback',...
    {@localFileImport this});
%% File - Export submenu
exportMenu = uimenu('Parent',fileMenu,'Label','Export...');
exportFileMenu = uimenu('Parent',exportMenu,'Label', ...
    'Export to File','Callback',{@localFileExport this});
exportWorkspaceMenu = uimenu('Parent',exportMenu,'Label', ...
    'Export to Workspace','Callback',{@localWorkspaceExport this});

%% File - close submenu
closeMenu = uimenu('Parent',fileMenu,'Label','Close','Separator','on',...
    'Callback',@(es,ed) set(get(this,'TreeManager'),'Visible','off'));

%% Edit menu
editMenu = uimenu('Parent',this.TreeManager.Figure,'Label','Edit','Tag','ts.edit');

%% Data menu
dataMenu = uimenu('Parent',this.TreeManager.Figure,'Label','Data','Tag','ts.data');

%% Edit Undo and Redo
undoMenu = uimenu('Parent',editMenu,'Label','Undo','Callback', ...
    @(es,ed) undo(this),'Tag','ts.undo');
redoMenu = uimenu('Parent',editMenu,'Label','Redo','Callback',...
    @(es,ed) redo(this),'Tag','ts.redo');
r = tsguis.recorder;
L = [handle.listener(r,r.findprop('Undo'),'PropertyPostSet',...
       {@setUndoStatus undoMenu r});...
     handle.listener(r,r.findprop('Redo'),'PropertyPostSet',...
       {@setRedoStatus redoMenu r})];
this.Listeners = [this.Listeners; L];
setUndoStatus([],[],undoMenu,r)
setRedoStatus([],[],redoMenu,r)

%% Edit Cut/copy/paste/delete
copyMenu = uimenu('Parent',editMenu,'Label','Copy','Separator','on',...
    'Callback',{@localCopy this},'Tag','ts.copy');
pasteMenu = uimenu('Parent',editMenu,'Label','Paste','Callback',...
    {@localPaste this this.TreeManager},'Tag','ts.paste');
this.Listeners = [this.Listeners;...
     handle.listener(this,this.findprop('Clipboard'),'PropertyPostSet',...
     {@localClipBoardUpdate this pasteMenu})];
localClipBoardUpdate([],[],this,pasteMenu)

%% Edit - delete menu
deleteMenu = uimenu('Parent',editMenu,'Label','Delete','Separator','on',...
    'Callback',{@localDelete this},'Tag','ts.delete');

%% Tools menu
toolsMenu = uimenu('Parent',this.TreeManager.Figure,'Label','Tools','Tag',...
    'ts.tools');

%% Tools - manipulation submenus
shiftMenu = uimenu('Parent',toolsMenu,'Label','Time shift...','Callback',...
    {@localTimeShift this},'Tag','ts.shift'); 
selectionMenu = uimenu('Parent',toolsMenu,'Label','Select data...','Callback',...
    {@localDataSelect this},'Tag','ts.select');
mergeMenu = uimenu('Parent',toolsMenu,'Label','Merge/resample...','Callback',...
    {@localMerge this},'Tag','ts.merge');
missingMenu = uimenu('Parent',dataMenu,'Label','Remove missing data...','Callback',...
    {@localPreproc this});
filterMenu = uimenu('Parent',dataMenu,'Label','Filter data...','Callback',...
    {@localPreproc this});
detrendMenu = uimenu('Parent',dataMenu,'Label','Detrend data...','Callback',...
    {@localPreproc this});
%% Tools - stats submenu
statsMenu = uimenu('Parent',toolsMenu,'Label','Descriptive statistics...',...
    'Callback',{@localOpenStats this.Tsnode this.TreeManager},...
    'Separator','on');
%% Tools - macro recorder
recorder = tsguis.recorder;
recorderMenu = uimenu('Parent',toolsMenu,'Label','Record M code...',...
    'Separator','on','Callback',@(es,ed) edit(recorder,this.TreeManager.Figure),...
    'Separator','on');

%% Tools - arithmatic
matlabMenu = uimenu('Parent',toolsMenu,'Label','MATLAB expression...','Callback',...
    {@localArith this},'Tag','ts.arith');

%% Plot menu
plotMenu = uimenu('Parent',this.TreeManager.Figure,'Label','Plot');
newViewLineStyleMenu = uimenu('Parent',plotMenu,'Label','Lines styles...',...
    'Callback',{@localLineStyle this});

%% Exclude undo/redo who's enabled status is set by listeners to the 
%% @recorder object
this.TreeManager.Menus = [fileMenu; copyMenu; deleteMenu;...
    toolsMenu; plotMenu; dataMenu;mergeMenu;...
    selectionMenu; shiftMenu; matlabMenu; statsMenu];

function localAddTS(es,ed,h)

h.TSnode.createChild

function localLineStyle(eventSrc,eventData,this)
%

StyleFrame = get(eventSrc,'UserData');
if isempty(StyleFrame)
   % Create style dialog
   StyleFrame = styledlg(this);
   set(eventSrc,'UserData',StyleFrame);
else
   % Bring it up front
   set(StyleFrame,'Visible','on');
end

function localCopy(eventSrc,eventData,this)

%% Copy the currently selected node to the @tsviewer clipboard if it is a
%% @tsnode or a @viewnode
selectednode = this.Treemanager.getselectednode;
if isa(selectednode,'tsguis.tsnode') || isa(selectednode,'tsguis.viewnode')   
     this.Clipboard = selectednode;
end

function localPaste(eventSrc,eventData,this,manager)

%% Paste the clipboard to the currently selected node in the @tsviewer
selectednode = this.Treemanager.getselectednode;
if ~isempty(selectednode)
    selectednode.paste(manager);
end

function localDelete(eventSrc,eventData,this)

thisnode = this.TreeManager.getselectednode;

%% Only @viewnodes and @tsnodes can be deleted
if (isa(thisnode,'tsguis.viewnode') || isa(thisnode,'tsguis.tsnode')) && ...
        ~isempty(thisnode.up)
    % Select the root so the deleted node is not selected
    this.TreeManager.Tree.setSelectedNode(thisnode.getRoot.down.getTreeNodeInterface);
    thisnode.up.removeNode(thisnode)
end

function localTimeShift(eventSrc,eventData,this)

%% Time shift menu callback to open the time shift dialog

%% Get the current node
thisnode = this.TreeManager.getselectednode;

%% Try calling the openshift on it
if ~isempty(thisnode)
    try
        thisnode.openshiftdlg(this.Treemanager)
    catch
        msg = sprintf('The time shift dialog cannot be opened for the node: %s',...
            thisnode.Label);
        errordlg(msg,'Time Series Tool','modal')
        return    
    end
end
    
function localDataSelect(eventSrc,eventData,this)

%% Data select menu callback to open the date select dialog

%% Get the current node
thisnode = this.TreeManager.getselectednode;

%% Try calling the openselectdlg on it
if ~isempty(thisnode)
    try
        thisnode.openselectdlg(this.Treemanager)
    catch        
        msg = sprintf('The data selection dialog cannot be opened for the node: %s',...
            thisnode.Label);
        errordlg(msg,'Time Series Tool','modal')
        return    
    end
end

function localMerge(eventSrc,eventData,this)

%% Merge menu callback to open the merge dialog

%% Get the current node
thisnode = this.TreeManager.getselectednode;

%% Try calling the openselectdlg on it
if ~isempty(thisnode)
    try
        thisnode.openmergedlg(this.Treemanager)
    catch
        msg = sprintf('The merge/resample dialog cannot be opened for the node: %s',...
            thisnode.Label);
        errordlg(msg,'Time Series Tool','modal')
        return        
    end
end

function localPreproc(eventSrc,eventData,this)

%% Preprocessing callback to open the preproc dialog

%% Get the current node
thisnode = this.TreeManager.getselectednode;

%% Try calling the openselectdlg on it
if ~isempty(thisnode)
    try
        thisnode.openpreprocdlg(this.Treemanager)
    catch
        msg = sprintf('The preprocessing dialog cannot be opened for the node: %s',...
            thisnode.Label);
        errordlg(msg,'Time Series Tool','modal')
        return
    end
end

function localArith(eventSrc,eventData,this)

%% MATLAB expression callback to open the arithmetic dialog

%% Get the current node
thisnode = this.TreeManager.getselectednode;

%% Try calling the openarithdlg on it
if ~isempty(thisnode)
    try
        thisnode.openarithdlg(this.TreeManager)
    catch
        msg = sprintf('The MATLAB expression dialog cannot be opened for the node: %s',...
            thisnode.Label);
        errordlg(msg,'Time Series Tool','modal')
        return
    end
end


% -----------------------------------------------------------------------
% for importing
% -----------------------------------------------------------------------
function localFileImport(es,ed,this)
importWizard = tsguis.ImportWizard(this);


% -----------------------------------------------------------------------
% for exporting
% -----------------------------------------------------------------------
function localFileExport(es,ed,this)
dlg = tsguis.allExportdlg;
%% Open export for selected time series node
node = this.TreeManager.getselectednode;
if isa(node,'tsguis.tsnode')
    dlg.initialize('file',this.TreeManager.Figure,node.Timeseries);
end


function localWorkspaceExport(es,ed,this)
%% export this timeseries object to workspace
node = this.TreeManager.getselectednode;
if ~isa(node,'tsguis.tsnode')
    return
end
list=evalin('base','whos;');
flag=false;
for i=1:length(list)
    if strcmp(list(i).name,node.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',node.Timeseries.name,node.Timeseries.copy);
        case 'Abort'
            return
    end
else
    assignin('base',node.Timeseries.name,node.Timeseries.copy);
end


function localOpenStats(es,ed,h,manager)

dlg = tsguis.statsdlg(h,manager);
dlg.Visible = 'on';

function setUndoStatus(es,ed,undoMenu,r)

%% Recorder Undo stack listener callback
if isempty(r.Undo)
    set(undoMenu,'Enable','off')
else
    set(undoMenu,'Enable','on')
end

function setRedoStatus(es,ed,redoMenu,r)

%% Recorder Redo stack listener callback
if isempty(r.Redo)
    set(redoMenu,'Enable','off')
else
    set(redoMenu,'Enable','on')
end

function localClipBoardUpdate(es,ed,this,pasteMenu)

%% Callback to clipboard listener
if ~isempty(this.Clipboard) && ...
        isa(this.Clipboard,class(this.TreeManager.getselectednode))
    set(pasteMenu,'Enable','on')
else
    set(pasteMenu,'Enable','off')
end
        
