Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Undo operations with EString attributes
Undo operations with EString attributes [message #933658] Fri, 05 October 2012 08:05 Go to next message
Cedric Moonen is currently offline Cedric Moonen
Messages: 274
Registered: August 2009
Senior Member
The undo/redo mechanism in EMF is pretty powerful and very easy to implement (if you modify your model via emf edit commands, you basically get it for free).
However, there is a little point which is rather annoying: the editing of EString attributes via a swt Text or even a more complex widget (like a jface source viewer for instance).
In those cases, the undo/redo mechanism does what it is supposed to do: only "look" at the model. This means that all "UI" information is not kept in the commands. By this, I mean that the caret position, the selection, ... are not undone properly.

The problem I encounter now is that I am using a jface source viewer to edit a string attribute. When I have a change in the document, I create and execute a SetCommand to modify my EMF model and when my EMF model change I update the text in the viewer to reflect the model. This allows the undo/redo to be reflected in the viewer when the model is modified by an undo or redo.
The problem that I have is that I loose the caret position (and also the previous selection if any, but this is less annoying), which means that the caret position is reset to 0 at each undo/redo operation, which is really annoying for the user.

How would you solve a problem like that ? What I was thinking of is to create a specific command which is able to undo/redo caret positions and text selection in the viewer. Then, when there is a change in the viewer, I create a compound command with both the SetCommand for the model and my custom command for the viewer.

Is this the way to do it or is there a more generic approach to the problem ?
Re: Undo operations with EString attributes [message #933794 is a reply to message #933658] Fri, 05 October 2012 10:49 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 26138
Registered: July 2009
Senior Member
Cedric,

What you describe sounds like the right way.

On 05/10/2012 10:05 AM, Cedric Moonen wrote:
> The undo/redo mechanism in EMF is pretty powerful and very easy to
> implement (if you modify your model via emf edit commands, you
> basically get it for free).
> However, there is a little point which is rather annoying: the editing
> of EString attributes via a swt Text or even a more complex widget
> (like a jface source viewer for instance).
> In those cases, the undo/redo mechanism does what it is supposed to
> do: only "look" at the model. This means that all "UI" information is
> not kept in the commands. By this, I mean that the caret position, the
> selection, ... are not undone properly.
>
> The problem I encounter now is that I am using a jface source viewer
> to edit a string attribute. When I have a change in the document, I
> create and execute a SetCommand to modify my EMF model and when my EMF
> model change I update the text in the viewer to reflect the model.
> This allows the undo/redo to be reflected in the viewer when the model
> is modified by an undo or redo.
> The problem that I have is that I loose the caret position (and also
> the previous selection if any, but this is less annoying), which means
> that the caret position is reset to 0 at each undo/redo operation,
> which is really annoying for the user.
>
> How would you solve a problem like that ? What I was thinking of is to
> create a specific command which is able to undo/redo caret positions
> and text selection in the viewer. Then, when there is a change in the
> viewer, I create a compound command with both the SetCommand for the
> model and my custom command for the viewer.
> Is this the way to do it or is there a more generic approach to the
> problem ?
Re: Undo operations with EString attributes [message #933926 is a reply to message #933794] Fri, 05 October 2012 13:18 Go to previous messageGo to next message
Cedric Moonen is currently offline Cedric Moonen
Messages: 274
Registered: August 2009
Senior Member
Ed,

Thanks for the reply. I tried that solution but unfortunately there's another little problem: I need to set the caret position always after having set the text in the source viewer. The problem is that for a CompoundCommand, the commands will be called in reverse order for the undo. Which means I have either the undo which works fine or the redo which works fine (depending if I add first the set command in the list or my custom command).

A better approach (I think) would be instead to override CommandWrapper in which I override the undo and redo methods. And there I put my custom code to manage the viewer.
I think it also makes more sense because the widget manipulation should not really be a command (since it doesn't touch the model at all).
Re: Undo operations with EString attributes [message #934038 is a reply to message #933926] Fri, 05 October 2012 15:41 Go to previous messageGo to next message
Hallvard Traetteberg is currently offline Hallvard Traetteberg
Messages: 601
Registered: July 2009
Location: Trondheim, Norway
Senior Member
Cedric,

The general problem is that the model does not include (enough) UI
state, to properly handle undo/redo. Your solution is to add state to
the command stack, but this doesn't seem to work that well, since you
don't really control it.

There are a couple of possibilities:
- Model the UI state in a transient layer, and store the UI state there.
Commands must update both model and UI state.
- Create a Map<String,int> in the viewer that keeps track of the latest
n Strings and where the caret was when that String was set using a
command. When the viewer is updated, lookup the String value to retrieve
the caret position and set it accordingly.

Hallvard

On 05.10.12 06.18, Cedric Moonen wrote:
> Ed,
>
> Thanks for the reply. I tried that solution but unfortunately there's
> another little problem: I need to set the caret position always after
> having set the text in the source viewer. The problem is that for a
> CompoundCommand, the commands will be called in reverse order for the
> undo. Which means I have either the undo which works fine or the redo
> which works fine (depending if I add first the set command in the list
> or my custom command).
>
> A better approach (I think) would be instead to override CommandWrapper
> in which I override the undo and redo methods. And there I put my custom
> code to manage the viewer.
> I think it also makes more sense because the widget manipulation should
> not really be a command (since it doesn't touch the model at all).
Re: Undo operations with EString attributes [message #934788 is a reply to message #933926] Sat, 06 October 2012 09:44 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 26138
Registered: July 2009
Senior Member
Cedric,

The source viewer itself has an undo stack too... Perhaps during an
undo, you should be undoing that rather than updating the source from
the model...

On 05/10/2012 3:18 PM, Cedric Moonen wrote:
> Ed,
>
> Thanks for the reply. I tried that solution but unfortunately there's
> another little problem: I need to set the caret position always after
> having set the text in the source viewer. The problem is that for a
> CompoundCommand, the commands will be called in reverse order for the
> undo. Which means I have either the undo which works fine or the redo
> which works fine (depending if I add first the set command in the list
> or my custom command).
>
> A better approach (I think) would be instead to override
> CommandWrapper in which I override the undo and redo methods. And
> there I put my custom code to manage the viewer.
> I think it also makes more sense because the widget manipulation
> should not really be a command (since it doesn't touch the model at all).
Re: Undo operations with EString attributes [message #953412 is a reply to message #934788] Mon, 22 October 2012 08:19 Go to previous message
Cedric Moonen is currently offline Cedric Moonen
Messages: 274
Registered: August 2009
Senior Member
Ed,

Sorry for the late answer.

Yes indeed the source viewer has also an undo stack. But the problem is that my model is much larger than the part which is edited via the source viewer (and the rest of my model edition also need support for undo/redo). This means that I have to use a more general command stack which works on the complete model (the command stack managed by the editing domain).
Anyway, I have a working solution by overriding the CommandWrapper command and there add some information about the widget state. Maybe not the best solution but this seems to work fine.
Previous Topic:[CDO] Clone example strange behavior
Next Topic:xmi:id get lost when saving Resource
Goto Forum:
  


Current Time: Fri Oct 24 10:21:01 GMT 2014

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

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