{"id":56,"date":"2013-02-12T09:52:12","date_gmt":"2013-02-12T07:52:12","guid":{"rendered":"http:\/\/www.descher.at\/descher-vu\/?p=56"},"modified":"2013-02-12T09:52:12","modified_gmt":"2013-02-12T07:52:12","slug":"synchronising-stateful-items-part1","status":"publish","type":"post","link":"http:\/\/www.descher.at\/descher-vu\/2013\/02\/synchronising-stateful-items-part1\/","title":{"rendered":"Synchronising stateful items &#8230; [part1]"},"content":{"rendered":"<p>I&#8217;ve been banging my head some time now (which can be seen in <a title=\"Bug 397249\" href=\"https:\/\/bugs.eclipse.org\/bugs\/show_bug.cgi?id=397249\">Bug 397249<\/a>) on the problem of sychronizing stateful Eclipse 4 application model items with the code.<\/p>\n<p>Wait, let me set the scene first: The Items I am talking about are either <em>Direct<\/em> or <em>Handled<\/em>, <em>Menu<\/em> or <em>Tool<\/em>, <em>Item<\/em>s added to either a <em>Menu<\/em> or a <em>Toolbar<\/em> in the application model. And by stateful I mean the requirements of Items of type <em>Check<\/em> or <em>Radio<\/em> to have their state synchronized with the state of the application.<\/p>\n<h4>Statefullness requires a Command connection<\/h4>\n<p>Overall stateful usage of items is bound to being connected with a Command, hence all <span style=\"text-decoration: underline;\">items of type <em>Direct<\/em> are generally not considered for stateful usage<\/span>. They should be used with check type only, for one shot execution of whatever you might deem necessary.<\/p>\n<p>I, however, see a feasible way to use them in <em>Type.Check<\/em> which might be handy to you. In order to update the state of a <em>Type.CHECK<\/em> Direct*Item, simply use the <em>@CanExecute<\/em> annotation for a method in your contribution_uri class like this:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@CanExecute\r\npublic boolean canExecute(MItem element) {\r\n  element.setSelected(false);\r\n  return true;\r\n}\r\n<\/pre>\n<p><em>@CanExecute<\/em> is called everytime your element is to be shown. It allows you to update the availability of your action according to your settings, and you may also use it to update the state.<\/p>\n<p>Using Direct*Items is good for smaller tasks, but one must not forget, that using this element the model has to access the implementing class to query the state. This may lead to lazy plug-ins being started just to gather the information about the availability resp. state.<\/p>\n<h4>Stateful Handled*Item<\/h4>\n<p>In order to use Handled*Items, a method (not in the code meaning but literal) is required to initialize the state of your <em>Type.CHECK<\/em> and <em>Type.RADIO<\/em> items at startup to the state of your code.<\/p>\n<p>Let&#8217;s consider this for both <em>Type.CHECK<\/em> and <em>Type.RADIO.<\/em> Lets fix an example of a viewer (yes, just like this SWT TableViewer <a href=\"http:\/\/eclipsesource.com\/blogs\/2012\/10\/26\/filtering-tables-in-swtjface\/\">example<\/a>) showing a set of Persons being either male or female. A person may also be married, single or widowed.<\/p>\n<p>The <em>Type.CHECK<\/em> items in the filter will be the boolean representation of <em>showOnlyMale <\/em>in Handler, which sets it into the state to show only male persons.<\/p>\n<p>The <em>Type.RADIO<\/em> items will allow us to show married, single or all persons. Here we have three contribution items which together form a radio group.<\/p>\n<p>It is important that the items allowing us to select the states will be present more than once but connected to the same command. The following image depicts this:<\/p>\n<p><a href=\"http:\/\/www.descher.at\/descher-vu\/wp-content\/uploads\/2013\/02\/filterCommand.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-91\" alt=\"filterCommand\" src=\"http:\/\/www.descher.at\/descher-vu\/wp-content\/uploads\/2013\/02\/filterCommand.png\" width=\"506\" height=\"163\" srcset=\"http:\/\/www.descher.at\/descher-vu\/wp-content\/uploads\/2013\/02\/filterCommand.png 599w, http:\/\/www.descher.at\/descher-vu\/wp-content\/uploads\/2013\/02\/filterCommand-300x96.png 300w\" sizes=\"(max-width: 506px) 100vw, 506px\" \/><\/a><\/p>\n<p>The code is implemented in the <em>Handler<\/em> part, while the model is being represented by all none code parts, that is the <em>Command<\/em> and the multiple <em>ContributionItems<\/em>.<\/p>\n<p>One may set the isSelected feature of the Contribution items within the application model, they are, however, not being synced to the state of the Handler (i.e. the Filter in our example) and vice versa (how should they, Eclipse does not know about the connection).<\/p>\n<p>So first the question arises, whether the model should synchronize to the code, or the code to the model for this cases?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been banging my head some time now (which can be seen in Bug 397249) on the problem of sychronizing stateful Eclipse 4 application model items with the code. Wait, let me set the scene first: The Items I am talking about are either Direct or Handled, Menu or Tool, Items added to either a &hellip; <a href=\"http:\/\/www.descher.at\/descher-vu\/2013\/02\/synchronising-stateful-items-part1\/\" class=\"more-link\"><span class=\"screen-reader-text\">Synchronising stateful items &#8230; [part1]<\/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":[],"_links":{"self":[{"href":"http:\/\/www.descher.at\/descher-vu\/wp-json\/wp\/v2\/posts\/56"}],"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=56"}],"version-history":[{"count":24,"href":"http:\/\/www.descher.at\/descher-vu\/wp-json\/wp\/v2\/posts\/56\/revisions"}],"predecessor-version":[{"id":94,"href":"http:\/\/www.descher.at\/descher-vu\/wp-json\/wp\/v2\/posts\/56\/revisions\/94"}],"wp:attachment":[{"href":"http:\/\/www.descher.at\/descher-vu\/wp-json\/wp\/v2\/media?parent=56"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.descher.at\/descher-vu\/wp-json\/wp\/v2\/categories?post=56"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.descher.at\/descher-vu\/wp-json\/wp\/v2\/tags?post=56"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}