From VisualWorks®, 7.6 of February 1, 2008 on February 1, 2008 at 12:47:38 pm (Dialog confirm: 'You are filing-in a Parcel source file!\\While this is possible it will not have\the same effect as loading the parcel.\None of the Parcel''s prerequisites will\be loaded and none of its load actions\will be performed.\\Are you sure you want to file-in?' withCRs) ifFalse: [self error: 'Parcel file-in abandoned. Choose terminate or close.'] <class> <name>MenuCommandExample</name> <environment>Examples</environment> <super>UI.ApplicationModel</super> <private>false</private> none files action <imports></imports> <category>Examples-Help</category> <attributes> <package>MenuCommand-Example</package> </attributes> </class> <comment> Examples.MenuCommandExample
This example class demonstrates how to implement a menu of commands (as opposed to a menu of values) in each of the three types of menu: menu bar, menu button and pop-up menu.
</comment> <methods> Examples.MenuCommandExample <category>aspects</category>
action ^action
files ^files
</methods> <methods> Examples.MenuCommandExample <category>menu messages</category>
addFile "Prompt for a filename and add it to the list." | name list selectionInList | name := Dialog requestFileName: 'File to add' default: '' version: #mustBeOld. name isEmpty ifFalse: [ name asFilename isDirectory ifTrue: [^Dialog warn: 'A directory cannot be added.' for: ScheduledControllers activeController view]. selectionInList := self files. list := selectionInList list. selectionInList selection isNil ifTrue: [list add: name] ifFalse: [list add: name before: selectionInList selection]. selectionInList list: list]
deleteFile "Remove the selected file from the list." | selectionInList list | selectionInList := self files. list := selectionInList list. list removeAtIndex: selectionInList selectionIndex. selectionInList list: list.
explainUsage "Tell how to use the example." Dialog warn: ' This example demonstrates a menu of commands that is accessible via a menu bar, a menu button or a pop-up menu. It can also be used to list your most-used text files for quick editing. '
openFile "Open a File Editor on the selected file." | selectedFile | selectedFile := self files selection. selectedFile asFilename edit.
</methods> <methods> Examples.MenuCommandExample <category>change messages</category>
configureMenu "Disable or enable the menu items depending on whether a file is selected." | menu submenu | menu := self builder menuAt: #fileMenu. submenu := (menu menuItemLabeled: 'File') submenu. self files selection isNil ifTrue: [ (submenu menuItemLabeled: 'Open') disable. (submenu menuItemLabeled: 'Delete') disable] ifFalse: [ (submenu menuItemLabeled: 'Open') enable. (submenu menuItemLabeled: 'Delete') enable]
performAction self perform: self action value.
</methods> <methods> Examples.MenuCommandExample <category>initialize-release</category>
initialize files := SelectionInList new. files selectionIndexHolder onChangeSend: #configureMenu to: self. action := nil asValue. action onChangeSend: #performAction to: self.
</methods> <methods> Examples.MenuCommandExample <category>interface opening</category>
postOpenWith: aBuilder "Disable the unusable menu items." self configureMenu.
</methods> <methods> Examples.MenuCommandExample class <category>resources</category>
fileMenu | mb menu submenu | mb := MenuBuilder new. mb beginSubMenuLabeled: 'File'; add: 'Open' -> #openFile; line; add: 'Add' -> #addFile; add: 'Delete' -> #deleteFile; endSubMenu. mb beginSubMenuLabeled: 'Help'; add: 'Usage' -> #explainUsage; endSubMenu. "Add shortcut keys." menu := mb menu. submenu := (menu menuItemLabeled: 'File') submenu. (submenu menuItemLabeled: 'Open') shortcutKeyCharacter: $O. (submenu menuItemLabeled: 'Add') shortcutKeyCharacter: $A. (submenu menuItemLabeled: 'Delete') shortcutKeyCharacter: $D. ^menu
</methods> <methods> Examples.MenuCommandExample class <category>interface specs</category>
windowSpec "UIPainter new openOnClass: self andSelector: #windowSpec" <resource: #canvas> ^#(#FullSpec #window: #(#WindowSpec #label: 'MenuCommandExample' #min: #(#Point 233 200 ) #bounds: #(#Rectangle 326 352 559 552 ) #flags: 4 #menu: #fileMenu ) #component: #(#SpecCollection #collection: #( #(#SequenceViewSpec #layout: #(#LayoutFrame 10 0 35 0 -10 1 -10 1 ) #model: #files #menu: #fileMenu ) #(#MenuButtonSpec #layout: #(#LayoutFrame -50 0.5 5 0 50 0.5 30 0 ) #model: #action #label: 'Actions' #menu: #fileMenu ) ) ) )
</methods> "Imported Classes:" self error: 'Attempting to file-in parcel imports. Choose terminate or close' <class> <name>ApplicationModel</name> <environment>UI</environment> <super>UI.Model</super> <private>false</private> none builder uiSession eventHandlers savedWindowInformation <imports></imports> <category>UIBuilder-Framework</category> <attributes> <package>UIBuilder-Framework</package> </attributes> </class>