%% Context Menu for Specific Component
% Create a context menu with actions appropriate to a tree component. Assign the context menu to the top-level tree nodes in a tree.
% In a UI figure, create a tree with four top-level nodes and a set of nested nodes.

fig = uifigure;

tree = uitree(fig,'Position',[20 200 175 100]);

category1 = uitreenode(tree,'Text','Runners');
r1 = uitreenode(category1,'Text','Joe');
r2 = uitreenode(category1,'Text','Linda');

category2 = uitreenode(tree,'Text','Cyclists');
c1 = uitreenode(category2,'Text','Rajeev');

category3 = uitreenode(tree,'Text','Hikers');
h1 = uitreenode(category3,'Text','Jack');

category4 = uitreenode(tree,'Text','Swimmers');
s1 = uitreenode(category4,'Text','Logan');

%
%
% Create a context menu with three menu items. For the last menu item, create four submenus.

cm = uicontextmenu(fig);
m1 = uimenu(cm,'Text','Expand All');
m2 = uimenu(cm,'Text','Collapse All');
m3 = uimenu(cm,'Text','Scroll To...');

sbm1 = uimenu(m3,'Text','Runners');
sbm2 = uimenu(m3,'Text','Cyclists');
sbm3 = uimenu(m3,'Text','Hikers');
sbm4 = uimenu(m3,'Text','Swimmers');

% Then, assign the context menu to the top-level tree nodes by setting the ContextMenu property of each node to the ContextMenu object.

category1.ContextMenu = cm;
category2.ContextMenu = cm;
category3.ContextMenu = cm;
category4.ContextMenu = cm;

% Right-click any of the top-level tree nodes to view the context menu.
%
% At this point, the code is still unfinished. The context menu appears when you right-click, but selecting menu items does not have any effect. To finish implementing the behavior of the menu items, create a MenuSelectedFcn callback function.
% Copyright 2019 The MathWorks, Inc.