Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » GEF » Event/Command handling design issue
Event/Command handling design issue [message #241397] Fri, 08 February 2008 13:51 Go to next message
Eclipse UserFriend
Originally posted by: johan.piculell.ericsson.com

Hi. Not really sure how to explain my problem but what I want to do is to
hook into the event/command handling to be able to undo actions depending
on user input.

Maybe an example is easier as an explanation.

When drawing a connection between two graphical objects in the edit area,
I want to ask the user for a name to add to the connector.
So in my model object I add a popup asking for a name when a source
connection is added to the model, which occurs when
ConnectionCommand.execute() is called.
Then I figured it would be nice with a cancel button, so the adding of the
connector can be aborted. Then the for me obvious choice would be to call
undo() on the command stack if user press cancel. But this does not work
since I'm "hooked into" the middle of the chain of events here, the
command is not fully executed, hence does not seem to exist on the stack.

Another example is that when make any changes in the graphical editor I
want to check if the file to save in is writable, and if not ask user for
changing write permissions (i.e. sourcesystem checkout). And if the choice
is no, then I want to undo the change. And The problem is essentially the
same here.

Does my example make any sense to anyone? Any design ideas to handle this
type of requirements?

thanks
/Johan
Re: Event/Command handling design issue [message #241399 is a reply to message #241397] Fri, 08 February 2008 16:32 Go to previous messageGo to next message
Neeraj Bhusare is currently offline Neeraj BhusareFriend
Messages: 177
Registered: July 2009
Location: Canada
Senior Member
Hello Johan,
Comments below.

Johan Piculell wrote:

> Hi. Not really sure how to explain my problem but what I want to do is to
> hook into the event/command handling to be able to undo actions depending
> on user input.

> Maybe an example is easier as an explanation.

> When drawing a connection between two graphical objects in the edit area,
> I want to ask the user for a name to add to the connector.
> So in my model object I add a popup asking for a name when a source
> connection is added to the model, which occurs when
> ConnectionCommand.execute() is called.
> Then I figured it would be nice with a cancel button, so the adding of the
> connector can be aborted. Then the for me obvious choice would be to call
> undo() on the command stack if user press cancel.

Generally the 'decision' to execute a command is implemented in the
canExecute() method of the command. So you just need to provide a proper
implementation for the same.
Well, if this doesn't help, you may like play around with the command
stack :).
This may not be a recommended approach.

> But this does not work since I'm "hooked into" the middle of the chain of
>events here, the command is not fully executed, hence does not seem to exist
>on the stack.

The command is placed on the undo stack after it is executed. Below code
explains it.

public class CommandStack{
...
public void execute(Command command) {

//code...
command.execute();

//code...
undoable.push(command);

//code...
}}

So you really cannot perform undo() before you complete execute(). What
you could think of is to restrict the execution of the command. By this I
mean asking for the input (dialog - ok/cancel) before the command is
executed. You could do this by overriding the execute() in the
org.eclipse.gef.commands.CommandStack.

XXCommandStack extends CommandStack{
public void execute(Command command) {
//Your dialog comes here.

//if the user presses 'ok'
super.execute(command)
}}

You also need to inform the EditDomain about the new Command stack.

XXEditor extends GraphicalEditorWithFlyoutPalette
{
public XXEditor() {

DefaultEditDomain defaultEditDomain = new DefaultEditDomain(this);
defaultEditDomain.setCommandStack(new XXCommandStack());
setEditDomain(defaultEditDomain);

}}

Well, the downside with this approach is that you wont be having the
flexibility of undo and redo in case the user abandons the command
execution.

Hope this helps.




> Another example is that when make any changes in the graphical editor I
> want to check if the file to save in is writable, and if not ask user for
> changing write permissions (i.e. sourcesystem checkout). And if the choice
> is no, then I want to undo the change. And The problem is essentially the
> same here.

> Does my example make any sense to anyone? Any design ideas to handle this
> type of requirements?

> thanks
> /Johan


Twitter : @NeerajBhusare
Blog : https://nbhusare.github.io/
Best regards, Neeraj
Re: Event/Command handling design issue [message #241409 is a reply to message #241399] Mon, 11 February 2008 06:27 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: johan.piculell.ericsson.com

Thanks Neeraj, I'm glad that you understood the problem :)
I will look into your suggestion, but does not feel very generic solution,
I mean this would bring up a question for all commands unless I add logic
for sorting out which commands should have a question or not in my new
stack class. Could be quite messy if many commands and several different
dialogs needed.

thanks
/Johan

Neeraj wrote:

> Hello Johan,
> Comments below.

> Johan Piculell wrote:

>> Hi. Not really sure how to explain my problem but what I want to do is to
>> hook into the event/command handling to be able to undo actions depending
>> on user input.

>> Maybe an example is easier as an explanation.

>> When drawing a connection between two graphical objects in the edit area,
>> I want to ask the user for a name to add to the connector.
>> So in my model object I add a popup asking for a name when a source
>> connection is added to the model, which occurs when
>> ConnectionCommand.execute() is called.
>> Then I figured it would be nice with a cancel button, so the adding of the
>> connector can be aborted. Then the for me obvious choice would be to call
>> undo() on the command stack if user press cancel.

> Generally the 'decision' to execute a command is implemented in the
> canExecute() method of the command. So you just need to provide a proper
> implementation for the same.
> Well, if this doesn't help, you may like play around with the command
> stack :).
> This may not be a recommended approach.

>> But this does not work since I'm "hooked into" the middle of the chain of
>>events here, the command is not fully executed, hence does not seem to exist
>>on the stack.

> The command is placed on the undo stack after it is executed. Below code
> explains it.

> public class CommandStack{
> ...
> public void execute(Command command) {

> //code...
> command.execute();

> //code...
> undoable.push(command);

> //code...
> }}

> So you really cannot perform undo() before you complete execute(). What
> you could think of is to restrict the execution of the command. By this I
> mean asking for the input (dialog - ok/cancel) before the command is
> executed. You could do this by overriding the execute() in the
> org.eclipse.gef.commands.CommandStack.

> XXCommandStack extends CommandStack{
> public void execute(Command command) {
> //Your dialog comes here.

> //if the user presses 'ok'
> super.execute(command)
> }}

> You also need to inform the EditDomain about the new Command stack.

> XXEditor extends GraphicalEditorWithFlyoutPalette
> {
> public XXEditor() {

> DefaultEditDomain defaultEditDomain = new DefaultEditDomain(this);
> defaultEditDomain.setCommandStack(new XXCommandStack());
> setEditDomain(defaultEditDomain);

> }}

> Well, the downside with this approach is that you wont be having the
> flexibility of undo and redo in case the user abandons the command
> execution.

> Hope this helps.




>> Another example is that when make any changes in the graphical editor I
>> want to check if the file to save in is writable, and if not ask user for
>> changing write permissions (i.e. sourcesystem checkout). And if the choice
>> is no, then I want to undo the change. And The problem is essentially the
>> same here.

>> Does my example make any sense to anyone? Any design ideas to handle this
>> type of requirements?

>> thanks
>> /Johan
Re: Event/Command handling design issue [message #241483 is a reply to message #241397] Wed, 13 February 2008 10:11 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: modica.cs.tu-berlin.deNOSPAM

Johan Piculell schrieb:
> Hi. Not really sure how to explain my problem but what I want to do is
> to hook into the event/command handling to be able to undo actions
> depending on user input.
>
> Maybe an example is easier as an explanation.
>
> When drawing a connection between two graphical objects in the edit
> area, I want to ask the user for a name to add to the connector.
> So in my model object I add a popup asking for a name when a source
> connection is added to the model, which occurs when
> ConnectionCommand.execute() is called.
> Then I figured it would be nice with a cancel button, so the adding of
> the connector can be aborted. Then the for me obvious choice would be to
> call undo() on the command stack if user press cancel. But this does not
> work since I'm "hooked into" the middle of the chain of events here, the
> command is not fully executed, hence does not seem to exist on the stack.

Hi,

we had the same problem that we didn't want to have these canceled
commands on the stack. But instead of messing with the commands we
extended our ConnectionCreationTools so that the actual command would
only be executed after all user input has been checked:

@Override
protected void executeCurrentCommand() {
Command command = getCurrentCommand();
if (command instanceof SomeSpecialCommand) {
// show dialogs, wizards etc. for SomeSpecialCommand
}
if (everythingFine)
super.executeCurrentCommand();
}

Hope this helps!

Cheers,
Tony

> Another example is that when make any changes in the graphical editor I
> want to check if the file to save in is writable, and if not ask user
> for changing write permissions (i.e. sourcesystem checkout). And if the
> choice is no, then I want to undo the change. And The problem is
> essentially the same here.
>
> Does my example make any sense to anyone? Any design ideas to handle
> this type of requirements?
>
> thanks
> /Johan
>
Re: Event/Command handling design issue [message #241502 is a reply to message #241483] Thu, 14 February 2008 12:06 Go to previous message
Eclipse UserFriend
Originally posted by: johan.piculell.ericsson.com

Thanks Tony, this looks promising, at least for some of my "Command design
issues".

/Johan
Previous Topic:GEF editor based on EMF model
Next Topic:Opening a Palette View when editor is open thwors Error
Goto Forum:
  


Current Time: Fri Apr 26 11:04:04 GMT 2024

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

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

Back to the top