{"id":204,"date":"2014-07-10T09:45:51","date_gmt":"2014-07-10T07:45:51","guid":{"rendered":"http:\/\/www.descher.at\/descher-vu\/?p=204"},"modified":"2014-07-10T09:45:51","modified_gmt":"2014-07-10T07:45:51","slug":"e4-updating-ui-contributions-on-context-switch","status":"publish","type":"post","link":"http:\/\/www.descher.at\/descher-vu\/2014\/07\/e4-updating-ui-contributions-on-context-switch\/","title":{"rendered":"e4: Updating UI contributions on context switch"},"content":{"rendered":"<p>During the implementation of a new project, I want to harness the capabilities of the Eclipse application model when it comes to changes in the Eclipse context, or more accurate the de-\/activation of context specific handlers.<\/p>\n<p>What does that mean? Lets have a look at the following application model:<\/p>\n<p><a href=\"http:\/\/www.descher.at\/descher-vu\/wp-content\/uploads\/2014\/07\/contextswitchmodel.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-205\" src=\"http:\/\/www.descher.at\/descher-vu\/wp-content\/uploads\/2014\/07\/contextswitchmodel.png\" alt=\"contextswitchmodel\" width=\"470\" height=\"556\" srcset=\"http:\/\/www.descher.at\/descher-vu\/wp-content\/uploads\/2014\/07\/contextswitchmodel.png 470w, http:\/\/www.descher.at\/descher-vu\/wp-content\/uploads\/2014\/07\/contextswitchmodel-253x300.png 253w\" sizes=\"(max-width: 470px) 100vw, 470px\" \/><\/a><strong>1<\/strong> is the <em>global<\/em> definition of our <span style=\"color: #3366ff;\">testCommand<\/span>, <strong>2<\/strong> and <strong>3<\/strong> are perspectives in our application where for the given command a separate handler gets activated due to the context switch from <span style=\"color: #3366ff;\">perspectiveA<\/span> to <span style=\"color: #3366ff;\">perspectiveB<\/span> and vice-versa. <strong>4<\/strong> is the UI contribution of the command visible to the user in the top window trim.<\/p>\n<p>The resulting application looks like <a href=\"http:\/\/www.descher.at\/descher-vu\/wp-content\/uploads\/2014\/07\/testApp.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-208\" src=\"http:\/\/www.descher.at\/descher-vu\/wp-content\/uploads\/2014\/07\/testApp.png\" alt=\"testApp\" width=\"275\" height=\"117\" \/><\/a>shown on the left hand side. Where the UIContribution <strong>4<\/strong> is marked with a red arrow. Now the task of the handlers is to toggle between the perspectives and update the UI contribution to either show perspectiveA or perspectiveB.<\/p>\n<p>As described in <a href=\"https:\/\/stackoverflow.com\/questions\/24599870\/updating-ui-contributions-on-handler-switch-in-e4-application-model\">stackoverflow <\/a>and the <a href=\"https:\/\/www.eclipse.org\/forums\/index.php\/t\/789832\/\">Eclipse forum<\/a> I did not, at first, find an obvious approach to this (remark in Eclipse 3.x this could be implemented by using an <a href=\"http:\/\/help.eclipse.org\/indigo\/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fui%2Fcommands%2FIElementUpdater.html\">IElementUpdater<\/a>). After some more analysis I found a way to do so. The source for the following example is certainly <a href=\"http:\/\/www.descher.at\/descher-vu\/wp-content\/uploads\/2014\/07\/handlerSwitchUiElementUpdater.zip\">available<\/a>.<\/p>\n<p>Lets fix the example by having a look at <span style=\"color: #3366ff;\">HandlerA<\/span><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\r\npublic class HandlerA {\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0@Execute\r\n\u00a0\u00a0 \u00a0public void execute(EPartService partService,\r\n             EModelService modelService, MApplication application) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0MPerspective perspective = (MPerspective) modelService.find(\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&quot;perspectiveB&quot;, application);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0partService.switchPerspective(perspective);\r\n\u00a0\u00a0 \u00a0}\r\n\r\n\u00a0\u00a0 \u00a0@Inject\r\n\u00a0\u00a0 \u00a0public void bla(\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0@Optional @Active @Named(&quot;handler::testCommand&quot;) Object value,\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0EModelService modelService, MApplication application) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0if (value == null)\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0List&lt;MHandledItem&gt; findElements = modelService.findElements(\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0application, null, MHandledItem.class, null);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0for (MHandledItem mHandledItem : findElements) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0mHandledItem.setLabel(&quot;perspectiveA&quot;);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0mHandledItem.setTooltip(&quot;perspectiveA now active&quot;);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0}\r\n}\r\n\r\n<\/pre>\n<p>The <span style=\"color: #3366ff;\">@Execute<\/span> method does the simple task of toggling the perspective. The updating itself happens in lines 12-23. This method does listen to property changes of <em>handler::testCommand<\/em> which is the global command (see 1 in the above app model). Each time a context switch happens we get injected the current handler through Object value. So we may act accordingly. Here we act by using the <span style=\"color: #3366ff;\">modelService<\/span> to find all contribution elements subsequently setting a new Label and Tooltip.<\/p>\n<p>If more commands and handlers are in your application you should do a more intelligent search and\/or filter on the value to act correct.<\/p>\n<p>This approach, however, is not perfect, <em>as you have to implement this method in both handlers in order to always be able to act upon the change<\/em>. There are other feasible approaches like creating an <span style=\"color: #3366ff;\">Addon<\/span> where you put the @Inject part, you would have to do this for each command separately, however.<\/p>\n<p>I would prefer to be able to define a class within the <span style=\"color: #3366ff;\">MCommand<\/span> element to put the respective code into which would always be notified in any change that happens related to it. So imagine that a <span style=\"color: #3366ff;\">MCommand<\/span> has an option <span style=\"color: #3366ff;\">Class Uri<\/span> and the respective referenced class could look like this<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class TestCommand {\r\n\r\n\u00a0\u00a0 \u00a0@Inject\r\n\u00a0\u00a0 \u00a0EModelService modelService;\r\n\r\n\u00a0\u00a0 \u00a0@Inject\r\n\u00a0\u00a0 \u00a0MApplication application;\r\n\r\n\u00a0\u00a0 \u00a0@Inject\r\n\u00a0\u00a0 \u00a0public void bla(@Optional @Active MHandler activeHandler,\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0@Active MCommand command) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0if (activeHandler == null)\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0List&lt;MHandledItem&gt; findElements =\r\n                modelService.findElements(\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0application, null, MHandledItem.class, null);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0for (MHandledItem mHandledItem : findElements) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0mHandledItem.setLabel(&quot;perspectiveA&quot;);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0mHandledItem.setTooltip(&quot;perspectiveA now active&quot;);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0}\r\n}\r\n<\/pre>\n<p>Where the injection will happen on change of the activeHandler and I also get the <span style=\"color: #3366ff;\">MCommand<\/span> itself injected. This way one could also efficiently store the given UiContribution elements and limit the searches &#8230;.<\/p>\n<p>What do you think? Eager to hear your opinion \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>During the implementation of a new project, I want to harness the capabilities of the Eclipse application model when it comes to changes in the Eclipse context, or more accurate the de-\/activation of context specific handlers. What does that mean? Lets have a look at the following application model: 1 is the global definition of &hellip; <a href=\"http:\/\/www.descher.at\/descher-vu\/2014\/07\/e4-updating-ui-contributions-on-context-switch\/\" class=\"more-link\"><span class=\"screen-reader-text\">e4: Updating UI contributions on context switch<\/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":[4],"tags":[8,9,10],"_links":{"self":[{"href":"http:\/\/www.descher.at\/descher-vu\/wp-json\/wp\/v2\/posts\/204"}],"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=204"}],"version-history":[{"count":17,"href":"http:\/\/www.descher.at\/descher-vu\/wp-json\/wp\/v2\/posts\/204\/revisions"}],"predecessor-version":[{"id":224,"href":"http:\/\/www.descher.at\/descher-vu\/wp-json\/wp\/v2\/posts\/204\/revisions\/224"}],"wp:attachment":[{"href":"http:\/\/www.descher.at\/descher-vu\/wp-json\/wp\/v2\/media?parent=204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.descher.at\/descher-vu\/wp-json\/wp\/v2\/categories?post=204"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.descher.at\/descher-vu\/wp-json\/wp\/v2\/tags?post=204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}