Monday, October 10, 2005

Re: Re: Dolphin's Command Framework

I got the following comment from Günther regarding this previous discussion:
Blaine,

I tried to get this to work with Menus and ContextMenus in the ViewComposer, without success.

Could you give us a for dummies description how to do that?

Thanks,

Günther, guenni

OK,
I've been writing a little utility for myself to manage my Mp3s in Dolphin and here's the code that I use to add commands to a context menu (when you right-click on a model object in a tree view):
setContextMenuFor: aModel 
| menu |
menu := aModel isNil
ifTrue: [nil]
ifFalse:
[| contextMenu |
contextMenu := Menu description: 'IRiver'.
aModel actionItemDescriptions do:
[:each | | command description |
command := each key.
description := each value.
contextMenu
addCommand: command
description: description].
contextMenu].
self tree view contextMenu: menu

The interesting bit of this method happens in #addCommand:description:. The command argument can be anything that understands the protocol #queryCommand: and #value. Symbol and MessageSend both do as well as my CommandMessageSend that I discussed earlier (since it is a subclass of MessageSend). Here's some examples of #actionItemDescriptions implementations that are called on the model in the above code:
Mp3Respository>>actionItemDescriptions
| result |
result := super actionItemDescriptions.
result
add: (CommandMessageSend receiver: self selector: #syncBackup) -> 'Sync Backup'.
^result

Mp3Root>>actionItemDescriptions
| result |
result := super actionItemDescriptions.
result
add: (MessageSend receiver: self selector: #save) -> 'Save'.
^result

I tried doing it through the ViewComposer and it can be done, but it's more trouble than it's worth. I decided to generate the code dynamically if I needed it. It's just as easy to add it through the code as it is to add it via the ViewComposer. I hope this explanation helps. I was trying out to see how I liked the model answering the things that it could do without knowing about the GUI. This was a cut at what it would feel like. Feel free to email me! =) I'll try to get on the IRC discussion more often.

1 comment:

Stefan Schmiedl said...

This is great stuff. I came upon MessageSend a few days ago and brooded on how to use it. This makes it perfectly clear and elegant.

One memento, however: Add those can... methods to the category must not strip or you will spend quite some time on trying to figure out why it does not work in deployed applications.