<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Alexander Mering &#8211; Undocumented Matlab</title>
	<atom:link href="https://undocumentedmatlab.com/articles/tag/alexander-mering/feed" rel="self" type="application/rss+xml" />
	<link>https://undocumentedmatlab.com</link>
	<description>Professional Matlab consulting, development and training</description>
	<lastBuildDate>Wed, 14 Nov 2012 18:00:46 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.2</generator>
	<item>
		<title>Disabling menu entries in deployed docked figures</title>
		<link>https://undocumentedmatlab.com/articles/disabling-menu-entries-in-deployed-docked-figures?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=disabling-menu-entries-in-deployed-docked-figures</link>
					<comments>https://undocumentedmatlab.com/articles/disabling-menu-entries-in-deployed-docked-figures#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 14 Nov 2012 18:00:46 +0000</pubDate>
				<category><![CDATA[Figure window]]></category>
		<category><![CDATA[Guest bloggers]]></category>
		<category><![CDATA[High risk of breaking in future versions]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Listeners]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[Alexander Mering]]></category>
		<category><![CDATA[Callbacks]]></category>
		<category><![CDATA[Compiler]]></category>
		<category><![CDATA[Desktop]]></category>
		<category><![CDATA[Docking]]></category>
		<category><![CDATA[FindJObj]]></category>
		<category><![CDATA[JavaFrame]]></category>
		<category><![CDATA[Listener]]></category>
		<category><![CDATA[Menubar]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=3344</guid>

					<description><![CDATA[<p>Matlab's standard menu items can and should be removed from deployed  docked figures. This article explains how. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/disabling-menu-entries-in-deployed-docked-figures">Disabling menu entries in deployed docked figures</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/docking-figures-in-compiled-applications" rel="bookmark" title="Docking figures in compiled applications">Docking figures in compiled applications </a> <small>Figures in compiled applications cannot officially be docked since R2008a, but this can be done using a simple undocumented trick....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/using-pure-java-gui-in-deployed-matlab-apps" rel="bookmark" title="Using pure Java GUI in deployed Matlab apps">Using pure Java GUI in deployed Matlab apps </a> <small>Using pure-Java GUI in deployed Matlab apps requires a special yet simple adaptation. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-9-popup-figures" rel="bookmark" title="Matlab toolstrip – part 9 (popup figures)">Matlab toolstrip – part 9 (popup figures) </a> <small>Custom popup figures can be attached to Matlab GUI toolstrip controls. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/jgraph-in-matlab-figures" rel="bookmark" title="JGraph in Matlab figures">JGraph in Matlab figures </a> <small>JGraph is a powerful open-source Java library that can easily be integrated in Matlab figures. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p><i>Last week I presented an <a target="_blank" href="/articles/removing-user-preferences-from-deployed-apps/">article</a> explaining how to solve an issue with deployed (compiled) Matlab applications. Today I&#8217;d like to welcome guest blogger <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/authors/208302">Alexander Mering</a>, who will explain how to disable standard Matlab menu items in deployed docked Matlab figures. Alexander has been an avid follower of this blog, and from his <a target="_blank" rel="nofollow" href="https://www.mathworks.com/matlabcentral/newsreader/search_results?dur=all&#038;search_string=authorid%3A146471">CSSM posts</a> we can tell that he&#8217;s been heavily using advanced GUI features presented in this blog. His article today nicely shows how we can use different building-blocks presented in different articles in this blog, to achieve something new and useful.</i><br />
As Yair pointed out in many occasions, the power of Matlab could be greatly enhanced using the underlying Java mechanism. To me, while developing a larger standalone tool for technical calculations, these hints are worth a mint (as I guess for many of you).<br />
One of these very useful hints is the ability to <a target="_blank" href="/articles/docking-figures-in-compiled-applications/">dock figure windows in standalone applications</a>. This perfectly fits to my understanding of a &#8220;clean desktop&#8221;, i.e., having as less as possible separate windows. Since in many calculations dozens of figures are generated, the desktop gets up crowded very fast &#8211; if these are not grouped. So docking is essential (at least for me). Unfortunately there seems to be a serious problem with the resulting menu entries (at least in R2011b on Win XP), leading to a crash of the standalone application. Based on the posts by Yair, I will sketch a possible route to avoid this issue.</p>
<h3 id="symptom">The symptom</h3>
<p>In the compiled application, docking could be accomplished by accessing the figure frame&#8217;s underlying Java level:</p>
<pre lang='matlab'>
% get java frame for sophisticated modifications
jframe = get(handle(figure_handle), 'JavaFrame');
% allow docking
jframe.fHG1Client.setClientDockable(true)
</pre>
<p>Using this modification, the user is now allowed to dock and undock the figures manually. For initial docking of the figure,</p>
<pre lang='matlab'>javaFrame.fHG1Client.setClientWindowStyle(true,false)</pre>
<p>could be used during figure creation. Unfortunately, there are menu entries in the Figures container which are either unwanted (since not usable) or even directly crash the standalone applications:<br />
<span id="more-3344"></span><br />
<center><figure style="width: 320px" class="wp-caption aligncenter"><img fetchpriority="high" decoding="async" alt="Useless Debug menu items in deployed applications" src="https://undocumentedmatlab.com/images/Useless_debug_menu.png" title="Useless Debug menu items in deployed applications" width="320" height="267" /><figcaption class="wp-caption-text">Useless Debug menu items in deployed applications</figcaption></figure></center><br />
<center><figure style="width: 504px" class="wp-caption aligncenter"><img decoding="async" alt="Menu items crashing deployed applications" src="https://undocumentedmatlab.com/images/Buggy_desktop_menu.png" title="Menu items crashing deployed applications" width="504" height="241" /><figcaption class="wp-caption-text">Menu items crashing deployed applications</figcaption></figure></center><br />
Since crashing menu entries will be found and used by end-users (though these are somehow hidden), these prohibit the usage of the docking feature as long as these could be invoked. So how can we disable / remove these menu items?</p>
<h3 id="unsuccessful">The unsuccessful solution</h3>
<p>Unfortunately, the straight forward solution of getting the handle to the Figures containers&#8217; menu bar and remove the unwanted items does not work. The reason for this is the (to me unexpected behavior) that the menu bar seems to be rebuilt whenever a figure is docked/undocked.<br />
This is actually the same behavior that automatically rebuilds the Editor&#8217;s menu bar whenever an editor file is added/removed. The Editor container is basically the same docking container as the Figures container, as shown by Yair&#8217;s <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/16650-setfigdockgroup"><i><b>setFigDockGroup</b></i> utility</a>:<br />
<center><figure style="width: 504px" class="wp-caption aligncenter"><img decoding="async" alt="Docking a figure in the Editor container (group)" src="https://undocumentedmatlab.com/images/setFigDockGroup.png" title="Docking a figure in the Editor container (group)" width="504" height="551" /><figcaption class="wp-caption-text">Docking a figure in the Editor container (group)</figcaption></figure></center><br />
Therefore, removing unwanted menu items only helps until the next figure docking/undocking. To make it even worse: also pressing any of the buttons within the document bar (if having more than one figure) somehow rebuilds the entire menu structure, reverting our changes. So the solution becomes a bit more complex.</p>
<h3 id="solution">The working solution</h3>
<p>For the working solution, many pieces presented by Yair should be put together. The first piece results from the question how to detect a dock/undock event. Since no such callback is defined, we need to use a property listener as Yair showed in his post about the <a target="_blank" href="/articles/continuous-slider-callback/">continuous slider callback</a>:</p>
<pre lang='matlab'>
% listen to the WindowStyle property to detect docking / undocking events
hProp = findprop(handle(figure_handle),'WindowStyle');  % a schema.prop object
% if the event occurs, invoke the callback
hlistener = handle.listener(handle(figure_handle), hProp, 'PropertyPostSet',{@(source, event) Callback_DockingFcn});
% attach listener to the GUI since it needs to be known (as long as the figure exists)
setappdata(figure_handle, 'Handle_Listener', hlistener);
</pre>
<p>Now, whenever the figure&#8217;s <b>WindowStyle</b> property (which controls the docking state) is changed, our docking callback is invoked.<br />
The next piece of the puzzle takes care of the menu rebuild whenever any document bar button is pressed. To overcome this behavior, the idea is to define the <b>MousePressed</b> callback of theses buttons to (again) invoke the docking callback. This is necessary for two reasons: First, pressing the button (i.e., changing the current figure) rebuilds the menu, overwriting our changed menu entries. Secondly, all other buttons are also somehow rebuilt and the callbacks are removed if a new figure is docked.<br />
The handles to the document bar buttons could be found using Yair&#8217;s <a target="_blank" href="/articles/findjobj-find-underlying-java-object/"><i><b>findjobj</b></i> utility</a>. We have already seen that the Editor container is analogous to the Figures docking container. So let&#8217;s use the method described by Yair for <a target="_blank" href="/articles/accessing-the-matlab-editor/">accessing the Editor container</a>, to access the Figures container:</p>
<pre lang='matlab'>
figures_container = javaObjectEDT(matlab_instance.getGroupContainer('Figures'));
figures_frame = javaObjectEDT(figures_container.getTopLevelAncestor);
</pre>
<p>Once we get the Java Frame for the Figures container, the buttons could be found by digging through its children. This finally allows to set the callback using</p>
<pre lang='matlab'>
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
        thisElement = GroupPanel_Elements(n);
        if isequal(char(thisElement.getClass.toString), 'class com.mathworks.widgets.desk.DTDocumentBar$DocumentButton')
            set(handle(thisElement, 'CallbackProperties'), 'MousePressedCallback', {@(source, event) Cbrake_Callback_Diagrams_DockingFcn})
        end
    end
    drawnow; pause(0.05)
end
</pre>
<p>where the loop runs through the current buttons in the document bar.<br />
As the last step of our procedure, we finally remove (or disable) the menu entries which are unwanted. This is achieved by extracting the handle to the Figures menu by:</p>
<pre lang='matlab'>figures_menu = javaObjectEDT(figures_frame.getJMenuBar);</pre>
<p>Running through the menu items, searching for the unwanted entries (as long as they have pre-defined <a target="_blank" href="/articles/modifying-default-toolbar-menubar-actions/">menu-item names</a>) at the end sets us into the position to take care of the menu items:</p>
<pre lang='matlab'>
% run through top-level 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
</pre>
<p>Since this callback is invoked whenever a figure is docked/undocked, or the currently shown figure is changed (by pressing the document bar button), all unwanted menu items within the Figures menu could be removed.<br />
As a result, the new Figures container menu looks like:<br />
<center><figure style="width: 319px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Deployed menu without unwanted items" src="https://undocumentedmatlab.com/images/Reduced_figures_menu.png" title="Deployed menu without unwanted items" width="319" height="205" /><figcaption class="wp-caption-text">Deployed menu without unwanted items</figcaption></figure></center></p>
<h3 id="remarks">Remarks</h3>
<p>I must admit that above solution is still imperfect. For instance, sometimes there is a larger delay between the docking (or button press event) and the removing of the menu item. Nevertheless, this solution allows me to distribute my standalone with docked figures without having menu items directly leading to a fatal error.<br />
Obviously, the solution has some positive side effects:</p>
<ul>
<li>As could be seen from the screen shot, the Matlab desktop becomes available also within your compiled applications. This might be wanted. If not, it could be removed the same way as the other menu items. One drawback of making the desktop available should be mentioned: In my tests, the standalone Matlab desktop shows the whole list of recent files I have in the Matlab editor at compile time. This is somehow ugly but not that problematic.</li>
<li>Additional menu items could be added, giving more possibilities for modifications.</li>
</ul>
<p>I have uploaded a first version of the docking and creation functions, together with a small test project, to the <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/39026-figures-menu-adaption-for-standalone-applications">Matlab file Exchange</a>. Readers are welcome to download the code and send me improvement suggestions. Or you could simply <a href="/articles/disabling-menu-entries-in-deployed-docked-figures#respond">leave a comment</a> below.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/disabling-menu-entries-in-deployed-docked-figures">Disabling menu entries in deployed docked figures</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/docking-figures-in-compiled-applications" rel="bookmark" title="Docking figures in compiled applications">Docking figures in compiled applications </a> <small>Figures in compiled applications cannot officially be docked since R2008a, but this can be done using a simple undocumented trick....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/using-pure-java-gui-in-deployed-matlab-apps" rel="bookmark" title="Using pure Java GUI in deployed Matlab apps">Using pure Java GUI in deployed Matlab apps </a> <small>Using pure-Java GUI in deployed Matlab apps requires a special yet simple adaptation. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-9-popup-figures" rel="bookmark" title="Matlab toolstrip – part 9 (popup figures)">Matlab toolstrip – part 9 (popup figures) </a> <small>Custom popup figures can be attached to Matlab GUI toolstrip controls. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/jgraph-in-matlab-figures" rel="bookmark" title="JGraph in Matlab figures">JGraph in Matlab figures </a> <small>JGraph is a powerful open-source Java library that can easily be integrated in Matlab figures. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/disabling-menu-entries-in-deployed-docked-figures/feed</wfw:commentRss>
			<slash:comments>14</slash:comments>
		
		
			</item>
	</channel>
</rss>
