% TWO_VIEWERS
%
% Sets up two directory viewers side by side and two buttons.
% When pressed, the buttons display the selected files from
% the corresponding tree.
%
% Demonstrates how to use the directory_viewer tree
% control.
%
% Added Java Popups to one of the tree controls
function two_viewers import javax.swing.*;
% Set up figure
figure('units', 'normalized');
b1 = uicontrol( 'string','Selections', ...
'units' , 'normalized', ...
'position', [0 0.5 0.5 0.5], ...
'callback', @b1_cb ...
);
b2 = uicontrol( 'string','Selections', ...
'units', 'normalized', ...
'position', [0.5 0.5 0.5 0.5], ...
'callback', @b2_cb ...
);
t1 = directory_viewer;
set(t1, 'Units', 'normalized',...
'position', [0 0 0.5 0.5]);
t2 = directory_viewer;
set(t2, 'Units', 'normalized',...
'position', [0.5 0 0.5 0.5]);
function b1_cb(h, env) disp( t1.SelectedDirectories() ) end
function b2_cb(h, env) disp( t2.SelectedDirectories() ) end
% Define callbacks for context menu items
cb1 = @(src,ev)disp('x');
cb2 = @(src,ev)disp('y');
cb3 = @(src,ev)disp('z');
% Define the context menu items
item1 = JMenuItem('button1');
item2 = JMenuItem('button2');
item3 = JMenuItem('button3');
set(item1, 'ActionPerformedCallback', cb1);
set(item2, 'ActionPerformedCallback', cb1);
set(item3, 'ActionPerformedCallback', cb1);
popup = JPopupMenu;
popup.add(item1);
popup.add(item2);
popup.add(item3);
set(b2,'UIContextMenu', cmenu);
% Get the tree from the tree model
tree = t2.Tree;
% Add a mouse pressed callback from the model
set(tree, 'MousePressedCallback', @mouse_cb);
% Mouse Pressed Handler
function mouse_cb(h, ev) if ev.getModifiers()== ev.META_MASK popup.show(tree, ev.getX, ev.getY);
popup.repaint;
% If you want to get fancy here you
% can use the JTree.getPathForLocation
% method to find the actual node for the
% point clicked and then generate a custom
% popup based on the node.
end
end
end
|