home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / BURKS / LANGUAGE / SMALTALK / TEXTBOOK / AP11.ST (.txt) < prev    next >
Text File  |  1997-04-22  |  13KB  |  499 lines

  1.  
  2. 'Smalltalk Textbook Appendix 11'!
  3.  
  4.  
  5.  
  6.  
  7.  
  8. FileBrowser subclass: #EngiFileChoiceModel
  9.     instanceVariableNames: ''
  10.     classVariableNames: ''
  11.     poolDictionaries: ''
  12.     category: 'Engi-Choice'!
  13. EngiFileChoiceModel comment:
  14. '
  15.  
  16. Engi 0.04 (8 February 1994)
  17. Copyright (C) 1994 by Atsushi Aoki
  18.  
  19. '!
  20.  
  21.  
  22. !EngiFileChoiceModel methodsFor: 'initialize-release'!
  23.  
  24. initialize
  25.     autoRead := ValueHolder with: false! !
  26.  
  27. !EngiFileChoiceModel methodsFor: 'pattern'!
  28.  
  29. pattern: aString 
  30.     myPattern := aString asText.
  31.     self changed: #fileNameList.
  32.     self changed: #pattern! !
  33.  
  34. !EngiFileChoiceModel methodsFor: 'file name list'!
  35.  
  36. fileListMenu
  37.     fileName == nil ifTrue: [^PopUpMenu
  38.             labels: 'make directory...' withCRs
  39.             lines: #()
  40.             values: #(#makeDir )].
  41.     selectionState = #directory ifTrue: [^PopUpMenu
  42.             labels: 'new pattern' withCRs
  43.             lines: #()
  44.             values: #(#directoryPattern )].
  45.     ^PopUpMenu
  46.         labels: 'spawn' withCRs
  47.         lines: #()
  48.         values: #(#spawnFileList )!
  49.  
  50. fileListMenuToPopUp
  51.     fileName == nil ifTrue: [^PopUpMenu
  52.             labels: 'make directory...' withCRs
  53.             lines: #()
  54.             values: #(#makeDir )].
  55.     selectionState = #directory ifTrue: [^PopUpMenu
  56.             labels: 'new pattern' withCRs
  57.             lines: #()
  58.             values: #(#directoryPattern )].
  59.     ^nil!
  60.  
  61. fileName: selection 
  62.     super fileName: selection.
  63.     self changed: #fileName!
  64.  
  65. makeDir
  66.     | continue newName |
  67.     continue := true.
  68.     [continue]
  69.         whileTrue: 
  70.             [newName := EngiTextModel request: 'Name for new directory?' default: ''.
  71.             newName isNil ifTrue: [^nil].
  72.             newName isEmpty ifTrue: [^nil].
  73.             ((self isLegalFileName: newName)
  74.                 and: [newName asFilename exists not])
  75.                 ifTrue: [continue := false]
  76.                 ifFalse: [(DialogView confirm: 'File exists or bad file name. Try again?')
  77.                         ifFalse: [^nil]]].
  78.     (Filename named: newName) makeDirectory.
  79.     list == nil ifTrue: [self list: SortedCollection new].
  80.     list add: newName.
  81.     self changed: #fileNameList! !
  82.  
  83. !EngiFileChoiceModel methodsFor: 'viewing'!
  84.  
  85. open
  86.     | patternHeight topView patternView listView |
  87.     patternHeight := TextAttributes defaultLineGrid + 8.
  88.     topView := EngiTopView
  89.                 model: nil
  90.                 label: 'File Choice'
  91.                 minimumSize: 250 @ 180.
  92.     patternView := TextView
  93.                 on: self
  94.                 aspect: #pattern
  95.                 change: #acceptPattern:from:
  96.                 menu: #patternMenu
  97.                 initialSelection: nil.
  98.     patternView controller dispatchOn: Character cr to: #alwaysAcceptKey:.
  99.     patternView controller dispatchOn: #Enter to: #alwaysAcceptKey:.
  100.     patternView := LookPreferences edgeDecorator on: patternView.
  101.     patternView noMenuBar.
  102.     patternView noVerticalScrollBar.
  103.     patternView noHorizontalScrollBar.
  104.     listView := SelectionInListView
  105.                 on: self
  106.                 aspect: #fileNameList
  107.                 change: #fileName:
  108.                 list: #fileNameList
  109.                 menu: #fileListMenu
  110.                 initialSelection: #fileName.
  111.     listView := LookPreferences edgeDecorator on: listView.
  112.     listView noMenuBar.
  113.     listView useVerticalScrollBar.
  114.     listView useHorizontalScrollBar.
  115.     topView add: patternView in: (topView frameFraction: (0 @ 0 corner: 1 @ 0)
  116.             offset: (0 @ 0 corner: 0 @ patternHeight)).
  117.     topView add: listView in: (topView frameFraction: (0 @ 0 corner: 1 @ 1)
  118.             offset: (0 @ patternHeight corner: 0 @ 0)).
  119.     topView open! !
  120. "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
  121.  
  122. EngiFileChoiceModel class
  123.     instanceVariableNames: ''!
  124.  
  125.  
  126. !EngiFileChoiceModel class methodsFor: 'instance creation'!
  127.  
  128. pattern: aPattern 
  129.     | choiceModel aCollection |
  130.     choiceModel := self new.
  131.     choiceModel pattern: aPattern.
  132.     aPattern = ''
  133.         ifFalse: 
  134.             [aCollection := choiceModel filesMatching: aPattern.
  135.             aCollection := aCollection asSortedCollection.
  136.             choiceModel list: aCollection].
  137.     ^choiceModel! !
  138.  
  139. !EngiFileChoiceModel class methodsFor: 'utilities'!
  140.  
  141. request: messageVisual 
  142.     "EngiFileChoiceModel request: 'message'."
  143.  
  144.     ^self request: messageVisual default: String new!
  145.  
  146. request: messageVisual default: aPattern 
  147.     "EngiFileChoiceModel request: 'message' default: '*'."
  148.  
  149.     | visualComponent margin height width half patternHeight fileChoice topView patternView listView aBoolean |
  150.     ((messageVisual respondsTo: #bounds)
  151.         and: [messageVisual respondsTo: #displayOn:at:])
  152.         ifTrue: [visualComponent := messageVisual]
  153.         ifFalse: [((messageVisual isKindOf: String)
  154.                 or: [messageVisual isKindOf: Text])
  155.                 ifTrue: [visualComponent := messageVisual asComposedText]
  156.                 ifFalse: [visualComponent := messageVisual printString asComposedText]].
  157.     margin := 8.
  158.     height := margin + visualComponent bounds height + margin.
  159.     width := margin + visualComponent bounds width + margin.
  160.     half := visualComponent bounds width // 2 + 1.
  161.     patternHeight := TextAttributes defaultLineGrid + 8.
  162.     fileChoice := self pattern: aPattern.
  163.     topView := EngiTopView new.
  164.     topView minimumSize: width @ (height + 100 + 40).
  165.     patternView := TextView
  166.                 on: fileChoice
  167.                 aspect: #pattern
  168.                 change: #acceptPattern:from:
  169.                 menu: #patternMenu
  170.                 initialSelection: nil.
  171.     patternView controller dispatchOn: Character cr to: #alwaysAcceptKey:.
  172.     patternView controller dispatchOn: #Enter to: #alwaysAcceptKey:.
  173.     patternView := LookPreferences edgeDecorator on: patternView.
  174.     patternView noMenuBar.
  175.     patternView noVerticalScrollBar.
  176.     patternView noHorizontalScrollBar.
  177.     listView := SelectionInListView
  178.                 on: fileChoice
  179.                 aspect: #fileNameList
  180.                 change: #fileName:
  181.                 list: #fileNameList
  182.                 menu: #fileListMenuToPopUp
  183.                 initialSelection: #fileName.
  184.     listView := LookPreferences edgeDecorator on: listView.
  185.     listView noMenuBar.
  186.     listView useVerticalScrollBar.
  187.     listView useHorizontalScrollBar.
  188.     topView add: visualComponent in: (topView frameFraction: (0.5 @ 0 corner: 0.5 @ 0)
  189.             offset: (half negated @ margin corner: half @ height)).
  190.     topView add: patternView in: (topView frameFraction: (0 @ 0 corner: 1 @ 0)
  191.             offset: (0 @ height corner: 0 @ (height + patternHeight))).
  192.     topView add: listView in: (topView frameFraction: (0 @ 0 corner: 1 @ 1)
  193.             offset: (0 @ (height + patternHeight) corner: 0 @ 0)).
  194.     aBoolean := topView popUp.
  195.     aBoolean = true
  196.         ifTrue: [^fileChoice fileName]
  197.         ifFalse: [^nil]! !
  198.  
  199. !EngiFileChoiceModel class methodsFor: 'examples'!
  200.  
  201. example1
  202.     "EngiFileChoiceModel example1."
  203.  
  204.     | choiceModel |
  205.     choiceModel := EngiFileChoiceModel pattern: '*'.
  206.     choiceModel open.
  207.     choiceModel open.
  208.     ^choiceModel!
  209.  
  210. example2
  211.     "EngiFileChoiceModel example2."
  212.  
  213.     ^EngiFileChoiceModel request: 'Please select a file.' default: '*'! !
  214.  
  215.  
  216.  
  217.  
  218.  
  219. EngiVariable subclass: #EngiMultipleChoiceModel
  220.     instanceVariableNames: 'list '
  221.     classVariableNames: ''
  222.     poolDictionaries: ''
  223.     category: 'Engi-Choice'!
  224. EngiMultipleChoiceModel comment:
  225. '
  226.  
  227. Engi 0.04 (8 February 1994)
  228. Copyright (C) 1994 by Atsushi Aoki
  229.  
  230. '!
  231.  
  232.  
  233. !EngiMultipleChoiceModel methodsFor: 'accessing'!
  234.  
  235. list
  236.     ^list!
  237.  
  238. list: aCollection 
  239.     | string |
  240.     list := OrderedCollection new.
  241.     aCollection do: [:each | ((each isKindOf: String)
  242.             or: [each isKindOf: Text])
  243.             ifTrue: [list add: each]
  244.             ifFalse: [(each isKindOf: ComposedText)
  245.                     ifTrue: [list add: each asText]
  246.                     ifFalse: 
  247.                         [string := each printString copyUpTo: Character cr.
  248.                         list add: string asText]]].
  249.     self changed: #item! !
  250.  
  251. !EngiMultipleChoiceModel methodsFor: 'adaptor'!
  252.  
  253. item
  254.     ^self value!
  255.  
  256. item: selection 
  257.     | aSet |
  258.     aSet := Set new.
  259.     selection do: [:each | aSet add: ((each isKindOf: Number)
  260.                 ifTrue: [each]
  261.                 ifFalse: [self list indexOf: each])].
  262.     self value: aSet.
  263.     self changed: #itemSelection!
  264.  
  265. itemList
  266.     ^self list!
  267.  
  268. itemMenu
  269.     ^nil!
  270.  
  271. itemSelection
  272.     ^self item!
  273.  
  274. itemView
  275.     | itemView edgeDecorator |
  276.     itemView := self defaultViewClass
  277.                 on: self
  278.                 printItems: false
  279.                 oneItem: false
  280.                 aspect: #item
  281.                 change: #item:
  282.                 list: #itemList
  283.                 menu: #itemMenu
  284.                 initialSelection: #itemSelection
  285.                 useIndex: true.
  286.     edgeDecorator := LookPreferences edgeDecorator on: itemView.
  287.     edgeDecorator useVerticalScrollBar.
  288.     edgeDecorator useHorizontalScrollBar.
  289.     ^edgeDecorator! !
  290.  
  291. !EngiMultipleChoiceModel methodsFor: 'viewing'!
  292.  
  293. open
  294.     | topWindow |
  295.     topWindow := EngiTopView
  296.                 model: nil
  297.                 label: self defaultWindowLabel
  298.                 minimumSize: 250 @ 180.
  299.     topWindow add: self itemView in: (0 @ 0 corner: 1 @ 1).
  300.     topWindow open! !
  301.  
  302. !EngiMultipleChoiceModel methodsFor: 'defaults'!
  303.  
  304. defaultViewClass
  305.     ^SelectionSetInListView!
  306.  
  307. defaultWindowLabel
  308.     ^'Multiple Choice'! !
  309. "-- -- -- -