Figures-menu adaption for standalone applications

by

 

12 Nov 2012 (Updated )

Create dockable figures for standalones and disable or remove menu items from the figures container

Callback_DockingFcn
%% #######################     HEADER START    ############################
%
% Filename:				Callback_DockingFcn.m
%
% Author:				Alexander Mering
% Created:				08-Nov-2012
%
% Changed on:			XX-XX-XXXX  by NAME		SHORT CHANGE DESCRIPTION
%						XX-XX-XXXX  by NAME		SHORT CHANGE DESCRIPTION
%
%*************************************************************************
%
% Description:
%		Invoked if figure is docked (or a button in the document bar is pressed) to remove unwanted menu entries within the
%		"Figures" container
%
% #######################      HEADER END     ############################

%% #######################    FUNCTION START   ############################
function Callback_DockingFcn
% If a docking event is recognized, remove all unwanted menu entries from the figures container. Additionally, redefine the
% MousePressed Callbacks for the DTDocumentbar buttons since these are somehow redefined each time the figures are (un)docked
% or the buttons are pressed.

%% Undeployed part - nothing to do so far
if ~isdeployed % & 0  % OVERRIDE for testing purpose with matlab only
	return
end

%% Deployed part
% get figures Container and frame
matlab_instance = javaObjectEDT(com.mathworks.mde.desk.MLDesktop.getInstance);
figures_container = javaObjectEDT(matlab_instance.getGroupContainer('Figures'));

if isempty(figures_container)
	
	% since no container is open, create container and show it
	matlab_instance.addGroup('Figures')
	matlab_instance.showGroup('Figures', 1)

end

% update the event queue
drawnow; pause(0.05)

figures_container = javaObjectEDT(matlab_instance.getGroupContainer('Figures'));
figures_frame = javaObjectEDT(figures_container.getTopLevelAncestor);

%% DocumentButton Callback Definition
% Get document bar and the panel carrying the buttons
% Each time, the a button in the document bar is pressed, the menu is somehow recreated. So attach a MousePressed callback to
% the buttons which again invokes the DockingFcn

drawnow; pause(0.05)
DTDocumentBar = javaObjectEDT(figures_frame.getRootPane.getLayeredPane.getComponent(1).getComponent(1).getComponent(0).getComponent(0).getComponent(1).getComponent(0));
ContentPanel = javaObjectEDT(DTDocumentBar.getComponent(0).getComponent(0).getViewport.getView);

if ~isempty(ContentPanel.getComponents) % less than two documents are open and no DTDocumentbar exists
	
	drawnow; pause(0.05)
	
	GroupPanel = javaObjectEDT(ContentPanel.getComponent(0));
	GroupPanel_Elements = javaObjectEDT(GroupPanel.getComponents);
	
	% change the MousePressed Callback for each of the buttons to invoke the function which disables the menu
	for n = 1:GroupPanel.getComponentCount
		if isequal(char(GroupPanel_Elements(n).getClass.toString), 'class com.mathworks.widgets.desk.DTDocumentBar$DocumentButton')
			set(handle(GroupPanel_Elements(n), 'CallbackProperties'), 'MousePressedCallback', {@(source, event) Callback_DockingFcn})
		end
	end
	drawnow; pause(0.05)
end


%% Deactivate Menus
DesktopDebugMenuPos = [];

% get figures menu
drawnow; pause(0.05)
figures_menu = javaObjectEDT(figures_frame.getJMenuBar);

% run through toplevel menu items
for n = 1:figures_menu.getMenuCount
	% completely deactivate Debugging options
	if isequal(char(figures_menu.getMenu(n - 1).getName), 'DesktopDebugMenu')
		DesktopDebugMenuPos = n - 1;
	end
	
	% Remove some items from the Desktop menu
	if isequal(char(figures_menu.getMenu(n - 1).getName), 'DesktopMenu')
		desktop_menu = javaObjectEDT(figures_menu.getMenu(n - 1));
		
		DeletePos = [];
		
		for m = 1: desktop_menu.getMenuComponentCount
			if ismember({char(desktop_menu.getMenuComponent(m - 1).getName)}, {'ToggleFigure PaletteCheckBoxMenuItem', 'TogglePlot BrowserCheckBoxMenuItem', 'ToggleProperty EditorCheckBoxMenuItem'})
				DeletePos(end +1 ) = m - 1;
			end
		end
		
		for m = length(DeletePos):-1:1
			desktop_menu.remove(DeletePos(m))
		end
		
	end
	
end

% finally remove the "Debug" menu
if ~isempty(DesktopDebugMenuPos)
	figures_menu.remove(DesktopDebugMenuPos)
end

% #######################     FUNCTION END    ############################

Contact us