| [EMF] [CommandStack] Best Practices to use ChangeCommand around UI Dialog [message #900231] |
Sun, 05 August 2012 18:08  |
Paco Blanco Messages: 28 Registered: June 2012 |
Junior Member |
|
|
I try to do this:
- To change an EMF model object by an UI Dialog (jface TitleAreaDialog)
- To register the changes in the CommandStack only if user does a click in the Ok Button of the Dialog
- To undo the possible changes in the dialog if user does a click in the Cancel Button (not register the changes in the CommandStack)
To register the changes, I use ChangeCommand.
My Code is more or less the next:
Collection<Notifier> objectsToRecord = new ArrayList<Notifier>();
objectsToRecord.add(objectToRegisterChanges);
cancel = false;
ChangeCommand changeCommand = new ChangeCommand(objectsToRecord) {
@Override
protected void doExecute() {
CustomDialog editorDialog = new CustomDialog(
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.NONE,
objectToRegisterChanges);
editorDialog.create();
if (editorDialog.open() != Window.OK) {
cancel = true;
}
}
};
getCommandStack().execute(command);
if(cancel) {
getCommandStack().undo();
}
objectToRegisterChanges is an EMF Object
CustomDialog extends JFace TitleAreaDialog where attributes and list belong to objectToRegisterChanges are changed
getCommandStack() returns my own BasicCommandStack
Even if user does a click in Cancel Button, the changes in the objectToRegisterChanges are added to CommandStack by the ChangeCommand. My code logically does the undo in this case but the command is on the top of the CommandStack and it can be redo
Does someone say to me the best solution for my situation or guide me??
|
|
|
|
|
|
|
|
|
| Re: [EMF] [CommandStack] Best Practices to use ChangeCommand around UI Dialog [message #900358 is a reply to message #900347] |
Mon, 06 August 2012 11:33   |
Paco Blanco Messages: 28 Registered: June 2012 |
Junior Member |
|
|
Step by step:
- Christoph / John. You are right that changes in the model are automatically reflected in other views/editors with databinding. In my case, I use CDO to manage model modifications so other clients/views/editors have their own CDO instance that are only refreshed when a commit is executed.
1) My model object has a list, not more than 10 items inside. But do you considerer the copy of the object is the better way if you have a list with 1000 items?
- Thomas, savePoint could give a solution but
2) where is the commandStack to undo/redo the changes? Because if user hits Ok, we want to give him the possibility of undo/redo these changes.
My new code that works 
Collection<Notifier> objectsToRecord = new ArrayList<Notifier>();
objectsToRecord.add(objectToRegisterChanges);
ChangeRecorder changeRecorder = new ChangeRecorder();
changeRecorder.beginRecording(objectsToRecord);
CustomDialog editorDialog = new CustomDialog(..., objectToRegisterChanges);
editorDialog .create();
int respond = editorDialog.open();
MyChangeCommand changeCommand = new MyChangeCommand(changeRecorder, objectsToRecord) {
@Override
protected void doExecute() {}
};
if (respond == Window.OK) {
getCommandStack().execute(changeCommand);
} else {
changeCommand.execute();
changeCommand.undo();
}
....
abstract class MyChangeCommand extends ChangeCommand {
public MyChangeCommand(ChangeRecorder changeRecorder, Collection<Notifier> notifiers) {
super(notifiers);
this.changeRecorder = changeRecorder;
}
@Override
public void execute() {
ChangeRecorder changeRecorder = getChangeRecorder();
if (changeRecorder == null) {
changeRecorder = createChangeRecorder();
changeRecorder.beginRecording(notifier != null ? Collections.singleton(notifier) : notifiers);
}
try {
doExecute();
} finally {
setChangeDescription(changeRecorder.endRecording());
disposeChangeRecorder(changeRecorder);
}
}
}
What is your opinion?
[Updated on: Mon, 06 August 2012 11:41] Report message to a moderator
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.05897 seconds