{"id":4,"date":"2012-12-07T10:54:43","date_gmt":"2012-12-07T08:54:43","guid":{"rendered":"http:\/\/www.descher.at\/descher-vu\/?p=4"},"modified":"2013-01-23T12:02:35","modified_gmt":"2013-01-23T10:02:35","slug":"dynamic-menu-contributions","status":"publish","type":"post","link":"http:\/\/www.descher.at\/descher-vu\/2012\/12\/dynamic-menu-contributions\/","title":{"rendered":"Dynamic menu contributions"},"content":{"rendered":"<p><a title=\"Eclipse Bug 389063\" href=\"https:\/\/bugs.eclipse.org\/bugs\/show_bug.cgi?id=389063\">Bug 389063<\/a> introduced dynamic menu items within the Eclipse 4 application model. Today Paul Webster pushed the <a href=\"http:\/\/git.eclipse.org\/c\/platform\/eclipse.platform.ui.git\/commit\/?id=27d838d28e86ab5d79d20d7eeda638e258666d70\">commit<\/a> to platform.ui so it is ready to show starting with tonights integration build or the upcoming Kepler M4.<\/p>\n<p>But what is it, and how to use it? During development I started a sample project on called <a id=\"ea49e76f05b3b7d2804839e0d670b5a3-212078d585ec0f43d9a4f1e0e2ffb56d2d26f2c7\" title=\"at.descher.eclipse.bug389063\" href=\"https:\/\/github.com\/col-panic\/generic-stuff\/tree\/master\/at.descher.eclipse.bug389063\">at.descher.eclipse.bug389063<\/a> that is the first to take usage of the new feature. It is accessible on my <a href=\"https:\/\/github.com\/col-panic\/generic-stuff\">github repository<\/a>.<\/p>\n<p>Generally, dynamic menu contributions are added to a menu just like other elements of the form <em>HandledMenuItem<\/em> or <em>DirectMenuItem<\/em>. But instead of representing only one element they can be instantiated dynamically by anything your code wants it to show. This is similar to the dynamic menu contribution one could add in Eclipse 3.x using the <em>org.eclipse.ui.menus<\/em> extension point.<\/p>\n<p>Corresponding to the Eclipse application model you now have to insert a respective <em>menu:DynamicMenuContribution<\/em> element, an example is shown in the following code snippet:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&lt;children xsi:type=&quot;menu:DynamicMenuContribution&quot;\r\n       xmi:id=&quot;_JTeEkAp4EeK8ULSwuUCzow&quot; elementId=&quot;at.descher.dmc.0&quot;\r\n       label=&quot;irrelevantLabelWillNotBeShown&quot;\r\n       contributionURI=&quot;bundleclass:\/\/at.descher.eclipse.bug389063\/\r\n        at.descher.eclipse.bug389063.dynamic.DynamicMainMenuContribution&quot; \/&gt;\r\n <\/pre>\n<p>Here one can see the reference to the contributionURI which is the class responsible to provide the Dynamic Menu Elements. There are two new annotations available which are evaluated within such contribution classes:<\/p>\n<ul>\n<li><em>@AboutToShow<\/em> the method carrying this annotation is called when the menu is about to open<\/li>\n<li><em>@AboutToHide<\/em> the method carrying this annotation is called when the menu is about to close<\/li>\n<\/ul>\n<p>In order to provide an example I take an excerpt of the respective methods from the class <em>DynamicMainMenuContribution<\/em> class:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@AboutToShow\r\npublic void aboutToShow(List&lt;MMenuElement&gt; items) {\r\n MDirectMenuItem dynamicItem = MMenuFactory.INSTANCE\r\n   .createDirectMenuItem();\r\n  dynamicItem.setLabel(&quot;Dynamic Menu Item (&quot; + new Date() + &quot;)&quot;);\r\n  dynamicItem\r\n    .setContributorURI(&quot;platform:\/plugin\/at.descher.eclipse.bug389063&quot;);\r\n  dynamicItem\r\n    .setContributionURI(&quot;bundleclass:\/\/at.descher.eclipse.bug389063\/\r\n         at.descher.eclipse.bug389063.dynamic.DirectMenuItemAHandler&quot;);\r\nitems.add(dynamicItem);\r\n<\/pre>\n<p>In <em>@AboutToShow<\/em> an empty list, namely <em>items<\/em> gets injected. It is to be populated by the developer. The above example shows the addition of a simple <em>MDirectMenuItem<\/em> which is handled by the <em>DirectMenuItemAHandler<\/em> class.<\/p>\n<p><span style=\"color: #ff0000;\">Caution<\/span> Do not put code with a long execution time within this method, as this directly blocks the opening process of the menu, and may look to the user as though the system crashed.<\/p>\n<p>Providing an <em>@AboutToHide<\/em> method is optional, if one wants to say so, if however one has a reason to use it, the framework takes care about injecting the list populated during <em>@AboutToShow<\/em> into this method. So the following example shows that the list is not empty, and uses the <em>@AboutToHide<\/em> annotated method to simply increase a counter.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@AboutToHide\r\npublic void aboutToHide(List&lt;MMenuElement&gt; items) {\r\n System.out.println(&quot;aboutToHide() items-size: &quot; + items.size());\r\n addSecond = !addSecond;\r\n}\r\n<\/pre>\n<p>There does not yet exist support for the Application Model Editor, but I am <a href=\"https:\/\/bugs.eclipse.org\/bugs\/show_bug.cgi?id=395982\">working<\/a> on it. As soon as there is news, I&#8217;ll present it here \ud83d\ude42<\/p>\n<p><span style=\"color: #ff0000;\"><strong>Remark<\/strong><\/span> Recently I found out about two problems using this dynamic contribution, I already filed the respective Eclipse Bugs in <a title=\"398866\" href=\"http:\/\/bugs.eclipse.org\/398866\">398866<\/a> and <a title=\"398865\" href=\"http:\/\/bugs.eclipse.org\/398865\">398865<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Bug 389063 introduced dynamic menu items within the Eclipse 4 application model. Today Paul Webster pushed the commit to platform.ui so it is ready to show starting with tonights integration build or the upcoming Kepler M4. But what is it, and how to use it? During development I started a sample project on called at.descher.eclipse.bug389063 &hellip; <a href=\"http:\/\/www.descher.at\/descher-vu\/2012\/12\/dynamic-menu-contributions\/\" class=\"more-link\"><span class=\"screen-reader-text\">Dynamic menu contributions<\/span> weiterlesen <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,4],"tags":[],"_links":{"self":[{"href":"http:\/\/www.descher.at\/descher-vu\/wp-json\/wp\/v2\/posts\/4"}],"collection":[{"href":"http:\/\/www.descher.at\/descher-vu\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.descher.at\/descher-vu\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.descher.at\/descher-vu\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.descher.at\/descher-vu\/wp-json\/wp\/v2\/comments?post=4"}],"version-history":[{"count":28,"href":"http:\/\/www.descher.at\/descher-vu\/wp-json\/wp\/v2\/posts\/4\/revisions"}],"predecessor-version":[{"id":53,"href":"http:\/\/www.descher.at\/descher-vu\/wp-json\/wp\/v2\/posts\/4\/revisions\/53"}],"wp:attachment":[{"href":"http:\/\/www.descher.at\/descher-vu\/wp-json\/wp\/v2\/media?parent=4"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.descher.at\/descher-vu\/wp-json\/wp\/v2\/categories?post=4"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.descher.at\/descher-vu\/wp-json\/wp\/v2\/tags?post=4"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}