home *** CD-ROM | disk | FTP | other *** search
- ViewManager subclass: #PhoneBook
- instanceVariableNames: 'people selectedPerson '
- classVariableNames: ''
- poolDictionaries: ''
-
- Object subclass: #Person
- instanceVariableNames: 'name phoneNumber '
- classVariableNames: ''
- poolDictionaries: ''
-
- PhoneBook methods
-
- names: listBox
- "Set the contents of the names listBox"
-
- listBox contents: (self alphabetizedArrayOfNames)
-
- open
- "Open the receiver's window, and begin
- processing user input. Double-clicking a
- name will print that person's name and
- number"
-
- self label: 'Phone Book'.
-
- self addSubpane:
- ((ListBox new)
- owner: self;
- when: #getContents perform: #names: ;
- when: #getMenu perform: #namesMenu: ;
- when: #doubleClickSelect perform: #setSelectedPerson: ;
- framingRatio:
- (Rectangle leftTopUnit extentFromLeftTop: 1@(3/4))).
-
- self addSubpane:
- ((StaticText new)
- centered;
- owner: self;
- when: #getContents perform: #information: ;
- framingRatio:
- ((Rectangle leftTopUnit rightAndDown: 0@(3/4))
- extentFromLeftTop: 1@(1/4))).
-
- self openWindow.
-
- selectedPerson
- "Get the value of selectedPerson."
-
- ^selectedPerson
-
- selectedPerson: aPerson
- "Set the value of selectedPerson."
-
- ^selectedPerson := aPerson.
-
- printAlphabetically
- "Print alphabeticlly all the people in the receiver's book
- on the Transcript"
-
- | names |
-
- Transcript nextPutAll: 'My Phonebook:'; cr; cr.
-
- names := self alphabetizedArrayOfNames.
- names do: [:aName |
- (self people at: aName) printOn: Transcript.
- Transcript cr
- ].
-
- information: infoPane
- "Set the contents of the infoPane to be the printString
- of the selectedPerson."
-
- selectedPerson isNil ifTrue: [
- infoPane contents: ''
- ] ifFalse: [
- infoPane contents: self selectedPerson printString
- ].
-
- setSelectedPerson: namesPane
- "Private - set the selectedPerson to be
- the person who's name was double-clicked"
-
- selectedPerson := (self people at: namesPane selectedItem).
- self changed: #information:
-
- removePerson
- "remove the selectedPerson from the phoneBook"
-
- self selectedPerson isNil ifTrue: [
- MessageBox message: 'You must select a person to remove'.
- ] ifFalse: [
- self people removeKey: self selectedPerson name.
- self selectedPerson: nil;
- changed: #names: ;
- changed: #information:
- ].
-
- namesMenu: namesPane
- "Set the namesPane menu"
-
- namesPane setMenu:
- ((Menu new)
- title: '&PhoneBook';
- owner: self;
- appendItem: '&Add Person...' selector: #addPerson ;
- appendItem:
- '&Print alphabetically' selector: #printAlphabetically)
-
- initialize
- "Initialize the receiver to be a default phoneBook.
- Don't forget to call super initialize, to initialize
- the ViewManager representation we're inheriting"
-
- super initialize.
- people := Dictionary new.
-
- alphabetizedArrayOfNames
- "Return an alphabetized array of all the names
- in the receiver's book"
-
- ^self people keys asSortedCollection asArray
-
- addPerson
- "Add a person to the receiver's phone book"
-
- | aPerson |
-
- aPerson := (Person new) fillFromKeyboard.
-
- aPerson notNil ifTrue: [
- self people at: aPerson name put: aPerson.
- self changed: #names:
- ].
-
- people
- "Get the value of people."
-
- ^people
-
-
-
- Person methods
-
- phoneNumber: aSymbol
- "Private - Set the value of phoneNumber."
-
- ^phoneNumber := aSymbol
-
- fillFromKeyboard
- "Fill the receiver's fields from the keyboard.
- Return nil if the user pressed cancel for any
- field; return self otherwise"
-
- | aName aPhoneNumber |
-
- aName := Prompter prompt: 'Name?' default: self name.
- aName isNil ifTrue: [
- ^nil
- ].
-
- aPhoneNumber := Prompter prompt: 'Phone number?' default: self phoneNumber.
- aPhoneNumber isNil ifTrue: [
- ^nil
- ].
-
- ^self
- name: aName asSymbol;
- phoneNumber: aPhoneNumber asSymbol;
- yourself.
-
- name: aSymbol
- "Private - Set the value of name."
-
- ^name := aSymbol
-
- phoneNumber
- "Get the value of phoneNumber."
-
- ^phoneNumber
-
- initialize
- "Initialize the receiver to be a default Person"
-
- self
- name: 'a Person' asSymbol;
- phoneNumber: '411' asSymbol.
-
- printOn: aStream
- "Print the receiver's representation on aStream
- like Jon Doe, (514) 555-1212"
-
- self name printOn: aStream.
- aStream nextPut: $, ;
- nextPut: $ .
- self phoneNumber printOn: aStream.
-
- name
- "Get the value of name."
-
- ^name