Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Model persistence - transient?
Model persistence - transient? [message #1693887] Wed, 29 April 2015 07:22 Go to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
Hi,

I am trying to use the DynamicMenuContribution to implement an E4 recent file mechanism. But I come across an issue with the model persistence.

In the @AboutToShow annotated method, I create MHandledMenuItems that are added to the list of MMenuElement. This works fine so far. The issue is related to the persistence. If I don't specify -clearPersistedState, the menu items are doubled on each startup. Of course this is because of the model persistence, as the created menu items are added to the model and therefore got persisted. And on starting the application the items are already there and with no additional check my DynamicMenuContribution adds the items again.

Now I'm wondering if there is an easy way to configure a model element to be "transient". Is it possible somehow (e.g. via tag) to specify that a model element shouldn't be persisted?

I was searching for an answer and going through the code for a while but didn't find anything.

I currently can think of the following workarounds for this issue:

1. Add a check to the DynamicMenuContribution to see whether the elements are already there. Fine for this scenario, but I'm not sure if this is a solution for the issue in general.

2. On @AboutToHide the items should be removed again. But I don't think that is a suitable solution regarding performance.

3. Implement and register a custom IModelResourceHandler as explained here: http://www.vogella.com/tutorials/Eclipse4ModelPersistence/article.html
But I personally think that is a huge workaround for a quite common use case.

4. Add a custom tag to the model element and implement and register a lifecycle handler that inspects the model elements for the custom tag and removes it in a method annotated with @PreSave

I personally prefer the last one, since it is the most flexible and for a user easiest way and should only affect the performance on closing the application. Although implementing and registering the lifecycle handler is a bit nasty for a user, so it would be nice if that would be a platform feature.

I just wonder if this mechanism doesn't already exist somehow and I am not able to find it. Maybe EMF itself provides such a feature?

Greez,
Dirk
Re: Model persistence - transient? [message #1693888 is a reply to message #1693887] Wed, 29 April 2015 07:30 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
I just noticed that

Quote:
1. Add a check to the DynamicMenuContribution to see whether the elements are already there.


seem to not work which might be related to how the model persistence works and what the DynamicMenuContribution does.
Re: Model persistence - transient? [message #1693912 is a reply to message #1693887] Wed, 29 April 2015 09:18 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Can you show me the @AboutToShow handler addition and removal should be
handled by the renderer system itself - at least in e(fx)clipse renderer
we are doing it and I think we behave the same as SWT.

Tom

On 29.04.15 09:22, Dirk Fauth wrote:
> Hi,
>
> I am trying to use the DynamicMenuContribution to implement an E4 recent
> file mechanism. But I come across an issue with the model persistence.
>
> In the @AboutToShow annotated method, I create MHandledMenuItems that
> are added to the list of MMenuElement. This works fine so far. The issue
> is related to the persistence. If I don't specify -clearPersistedState,
> the menu items are doubled on each startup. Of course this is because of
> the model persistence, as the created menu items are added to the model
> and therefore got persisted. And on starting the application the items
> are already there and with no additional check my
> DynamicMenuContribution adds the items again.
>
> Now I'm wondering if there is an easy way to configure a model element
> to be "transient". Is it possible somehow (e.g. via tag) to specify that
> a model element shouldn't be persisted?
>
> I was searching for an answer and going through the code for a while but
> didn't find anything.
> I currently can think of the following workarounds for this issue:
>
> 1. Add a check to the DynamicMenuContribution to see whether the
> elements are already there. Fine for this scenario, but I'm not sure if
> this is a solution for the issue in general.
>
> 2. On @AboutToHide the items should be removed again. But I don't think
> that is a suitable solution regarding performance.
>
> 3. Implement and register a custom IModelResourceHandler as explained
> here:
> http://www.vogella.com/tutorials/Eclipse4ModelPersistence/article.html
> But I personally think that is a huge workaround for a quite common use
> case.
>
> 4. Add a custom tag to the model element and implement and register a
> lifecycle handler that inspects the model elements for the custom tag
> and removes it in a method annotated with @PreSave
>
> I personally prefer the last one, since it is the most flexible and for
> a user easiest way and should only affect the performance on closing the
> application. Although implementing and registering the lifecycle handler
> is a bit nasty for a user, so it would be nice if that would be a
> platform feature.
>
> I just wonder if this mechanism doesn't already exist somehow and I am
> not able to find it. Maybe EMF itself provides such a feature?
>
> Greez,
> Dirk
Re: Model persistence - transient? [message #1693925 is a reply to message #1693912] Wed, 29 April 2015 10:10 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
The file history is stored via preferences and loaded/persisted in a lifecycle handler.

public class RecentFileItems {
	
	@AboutToShow
	public void aboutToShow(List<MMenuElement> items, EModelService modelService, MApplication application,
			@Named(ContextKeys.FILE_HISTORY) Deque<String> fileHistory) {
		
		int counter = 0;
		for (String file : fileHistory) {
			List<MCommand> commands = modelService.findElements(application, "de.codecentric.eclipse.tutorial.app.command.open", MCommand.class, null);
			
			MHandledMenuItem handledItem = modelService.createModelElement(MHandledMenuItem.class);
			handledItem.setLabel(++counter + " " + file.substring(file.lastIndexOf(File.separator)+1));
			handledItem.setCommand(commands.get(0));
			
			//set parameter
			MParameter parameter = modelService.createModelElement(MParameter.class);
			parameter.setName("de.codecentric.eclipse.tutorial.app.commandparameter.fileToOpen");
			parameter.setValue(file);
			handledItem.getParameters().add(parameter);
			
			items.add(handledItem);
		}
		
		if (fileHistory.size() > 0) {
			items.add(modelService.createModelElement(MMenuSeparator.class));
		}
	}
		
}
Re: Model persistence - transient? [message #1693926 is a reply to message #1693925] Wed, 29 April 2015 10:21 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Have you tried putting a break point to
MenuManagerHideProcessor#processDynamicElements this where SWT remove
the items.

Tom

On 29.04.15 12:10, Dirk Fauth wrote:
> The file history is stored via preferences and loaded/persisted in a
> lifecycle handler.
>
>
> public class RecentFileItems {
>
> @AboutToShow
> public void aboutToShow(List<MMenuElement> items, EModelService
> modelService, MApplication application,
> @Named(ContextKeys.FILE_HISTORY) Deque<String> fileHistory) {
>
> int counter = 0;
> for (String file : fileHistory) {
> List<MCommand> commands =
> modelService.findElements(application,
> "de.codecentric.eclipse.tutorial.app.command.open", MCommand.class, null);
>
> MHandledMenuItem handledItem =
> modelService.createModelElement(MHandledMenuItem.class);
> handledItem.setLabel(++counter + " " +
> file.substring(file.lastIndexOf(File.separator)+1));
> handledItem.setCommand(commands.get(0));
>
> //set parameter
> MParameter parameter =
> modelService.createModelElement(MParameter.class);
>
> parameter.setName("de.codecentric.eclipse.tutorial.app.commandparameter.fileToOpen");
>
> parameter.setValue(file);
> handledItem.getParameters().add(parameter);
>
> items.add(handledItem);
> }
>
> if (fileHistory.size() > 0) {
>
> items.add(modelService.createModelElement(MMenuSeparator.class));
> }
> }
>
> }
Re: Model persistence - transient? [message #1693930 is a reply to message #1693926] Wed, 29 April 2015 10:46 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
I just did and there is no code that removes the items from the application model automatically. It just calls @AboutToHide. Is it necessary to implement the removal of dynamically created items manually?
Re: Model persistence - transient? [message #1693940 is a reply to message #1693930] Wed, 29 April 2015 12:05 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
On 29.04.15 12:46, Dirk Fauth wrote:
> I just did and there is no code that removes the items from the
> application model automatically. It just calls @AboutToHide. Is it
> necessary to implement the removal of dynamically created items manually?

MenuManagerHideProcessor at line 118 =>
renderer.removeDynamicMenuContributions(menuManager, menuModel, mel);

MenuManagerRenderer at line 1126 => removeMenuContributions(menuModel,
dump);

MenuManagerRenderer at line 604 => remove elements from model

Tom
Re: Model persistence - transient? [message #1693942 is a reply to message #1693940] Wed, 29 April 2015 12:14 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
Looks like you are referring to a newer codebase. I tested here at the customers site with Luna, and in that codebase the lines you are referring to do not exist.

I will test with Mars.
Re: Model persistence - transient? [message #1693949 is a reply to message #1693942] Wed, 29 April 2015 12:44 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
OK, with Mars the dynamic menu contributions are removed and rebuild correctly.

But now I have the issue that the MHandledMenuItems are not executed anymore. The handler for the command is not called. This worked before, so what has changed with Mars?
Re: Model persistence - transient? [message #1693991 is a reply to message #1693949] Wed, 29 April 2015 20:49 Go to previous messageGo to next message
Eclipse UserFriend
Dirk Fauth wrote on Wed, 29 April 2015 08:44
OK, with Mars the dynamic menu contributions are removed and rebuild correctly.

But now I have the issue that the MHandledMenuItems are not executed anymore. The handler for the command is not called. This worked before, so what has changed with Mars?


Are these items in a top-level menu?
Re: Model persistence - transient? [message #1694007 is a reply to message #1693991] Thu, 30 April 2015 04:11 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
They are added to the file menu.
Re: Model persistence - transient? [message #1694083 is a reply to message #1694007] Thu, 30 April 2015 12:27 Go to previous message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
I created a ticket, because it looks like a regression issue: https://bugs.eclipse.org/bugs/show_bug.cgi?id=465930
Previous Topic:IEclipseContext.get() does not return a value for @Creatable
Next Topic:Closed view displayed again after restarting application
Goto Forum:
  


Current Time: Tue Mar 19 06:24:37 GMT 2024

Powered by FUDForum. Page generated in 0.02895 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top