Undo operations with EString attributes [message #933658] |
Fri, 05 October 2012 08:05 |
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 |
Ed Merks Messages: 33218 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 ?
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
|
Re: Undo operations with EString attributes [message #934038 is a reply to message #933926] |
Fri, 05 October 2012 15:41 |
Hallvard Traetteberg Messages: 673 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).
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.09377 seconds