'From Cuis 2.6 of 10 August 2010 [latest update: #540] on 2 September 2010 at 9:55:53 am'!
!classDefinition: #FileList category: #'Tools-FileList'!
StringHolder subclass: #FileList
	instanceVariableNames: 'fileName directory volList volListIndex list listIndex pattern sortMode brevityState sortAscending '
	classVariableNames: 'FileReaderRegistry RecentDirs '
	poolDictionaries: ''
	category: 'Tools-FileList'!

!FileList methodsFor: 'initialization' stamp: 'jmv 9/2/2010 09:40'!
directory: dir
	"Set the path of the volume to be displayed."

	self okToChange ifFalse: [^ self].

	self modelSleep.
	directory _ dir.
	self modelWakeUp.

	sortMode ifNil: [
		sortMode _ #date.
		sortAscending _ false ].
	volList _ ((Array with: '[]'), directory pathParts)  "Nesting suggestion from RvL"
			withIndexCollect: [:each :i | ( String new: i-1 withAll: $ ), each].
	volListIndex := volList size.
	self changed: #relabel.
	self changed: #volumeList.
	self pattern: pattern! !

!FileList methodsFor: 'own services' stamp: 'jmv 9/2/2010 09:49'!
serviceSortByDate
	"Answer a service for sorting by date"
	| buttonLabel |
	buttonLabel _ sortMode = #date
		ifTrue: [
			sortAscending
				ifTrue: [ '[^] - date' ]
				ifFalse: [ '[v] - date' ]]
		ifFalse: [ 'date' ].
	^  (SimpleServiceEntry new
			provider: self 
			label: 'by date' 
			selector: #sortByDate 
			description: 'sort entries by date')
		extraSelector: #sortingByDate;
		buttonLabel: buttonLabel! !

!FileList methodsFor: 'own services' stamp: 'jmv 9/2/2010 09:49'!
serviceSortByName
	"Answer a service for soring by name"
	| buttonLabel |
	buttonLabel _ sortMode = #name
		ifTrue: [
			sortAscending
				ifTrue: [ '[^] - name' ]
				ifFalse: [ '[v] - name' ]]
		ifFalse: [ 'name' ].
	^ (SimpleServiceEntry new
		provider: self label: 'by name' selector: #sortByName 
		description: 'sort entries by name')
		extraSelector: #sortingByName;
		buttonLabel: buttonLabel! !

!FileList methodsFor: 'own services' stamp: 'jmv 9/2/2010 09:49'!
serviceSortBySize
	"Answer a service for sorting by size"
	| buttonLabel |
	buttonLabel _ sortMode = #size
		ifTrue: [
			sortAscending
				ifTrue: [ '[^] - size' ]
				ifFalse: [ '[v] - size' ]]
		ifFalse: [ 'size' ].
	^ (SimpleServiceEntry
		provider: self
		label: 'by size'
		selector: #sortBySize
		description: 'sort entries by size')
		 extraSelector: #sortingBySize;
		 buttonLabel: buttonLabel.! !

!FileList methodsFor: 'private' stamp: 'jmv 9/2/2010 09:51'!
resort: newMode
	"Re-sort the list of files."

	| name |
	listIndex > 0
		ifTrue: [name _ self fileNameFromFormattedItem: (list at: listIndex)].
	sortAscending _ sortMode = newMode
		ifTrue: [ sortAscending not ]
		ifFalse: [ newMode = #name].
	sortMode _ newMode.
	self pattern: pattern.
	name ifNotNil: [
		fileName _ name.
		listIndex _ list findFirst: [:item | (self fileNameFromFormattedItem: item) = name. ].
		self changed: #fileListIndex].
	listIndex = 0 ifTrue: [self changed: #contents].
	self updateButtonRow
! !

!FileList methodsFor: 'private' stamp: 'jmv 9/2/2010 09:51'!
sortBlock
	"Answer block to decide what order to display the directory entries."

	^ [ :xx :yy | | x y |
		(xx isDirectory = yy isDirectory) 
			ifTrue: [  
				sortAscending
					ifTrue: [ x _ xx. y _ yy ]
					ifFalse: [ x _ yy. y _ xx ].
				"sort by user-specified criterion"
				sortMode = #name 
					ifTrue: [(x name compare: y name) <= 2]
					ifFalse: [ sortMode = #date
						ifTrue: [ x modificationTime = y modificationTime
								ifTrue: [ (x name compare: y name) <= 2 ]
								ifFalse: [ x modificationTime <= y modificationTime ] ]
						ifFalse: [ "size"
							x fileSize = y fileSize 
								ifTrue: [ (x name compare: y name) <= 2 ]
								ifFalse: [ x fileSize <= y fileSize ] ] ] ]
			ifFalse: [
				"directories always precede files"
				xx isDirectory ] ]! !


!FileList2 methodsFor: 'initialization' stamp: 'jmv 9/2/2010 09:40'!
directory: dir
	"Set the path of the volume to be displayed."

	self okToChange ifFalse: [^ self].

	self modelSleep.
	directory _ dir.
	self modelWakeUp.

	sortMode ifNil: [
		sortMode _ #date.
		sortAscending _ false ].
	volList _ Array with: '[]'.
	directory ifNotNil: [
		volList _ volList, directory pathParts.  "Nesting suggestion from RvL"
	].
	volList _ volList withIndexCollect: [:each :i | ( String new: i-1 withAll: $ ), each].
	self changed: #relabel.
	self changed: #volumeList.
	self pattern: pattern.
	directoryChangeBlock ifNotNil: [directoryChangeBlock value: directory].! !

!classDefinition: #FileList category: #'Tools-FileList'!
StringHolder subclass: #FileList
	instanceVariableNames: 'fileName directory volList volListIndex list listIndex pattern sortMode brevityState sortAscending'
	classVariableNames: 'FileReaderRegistry RecentDirs'
	poolDictionaries: ''
	category: 'Tools-FileList'!
