FileSorter.st
author Claus Gittinger <cg@exept.de>
Wed, 18 Jul 2007 14:16:40 +0200
changeset 1891 af37298a4b38
parent 1522 7a569eca4d68
child 1928 32553a766637
permissions -rw-r--r--
printing/storing; ms handling

"
 COPYRIGHT (c) 2004 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"

"{ Package: 'stx:libbasic2' }"

Object subclass:#FileSorter
	instanceVariableNames:'directoriesBeforeFiles selector sortCaseless sortReverse'
	classVariableNames:''
	poolDictionaries:''
	category:'Interface-Tools-File'
!

!FileSorter class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 2004 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

documentation
"
    sort files by selector
    care for directoriesBeforeFiles sortCaseless sortReverse

    [author:]
        Christian Penk

    [instance variables:]
    directoriesBeforeFiles: if true all directories and files will be sorted 
                            independently by the selector and the directories 
                            are before files 
    sortCaseless:           if true and the selector is #baseName ignore upper
                            and lowercase in sort
    sortReverse:            if true starts with highest value of the sort

    [class variables:]

    [see also:]

"
! !

!FileSorter class methodsFor:'instance creation'!

directoriesBeforeFiles:arg1 selector:arg2 sortCaseless:arg3 sortReverse:arg4
    ^ self new directoriesBeforeFiles:arg1 selector:arg2 sortCaseless:arg3 sortReverse:arg4
! !

!FileSorter methodsFor:'accessing'!

directoriesBeforeFiles
    ^ directoriesBeforeFiles ? false
!

directoriesBeforeFiles:something
    directoriesBeforeFiles := something.
!

directoriesBeforeFiles:directoriesBeforeFilesArg selector:selectorArg sortCaseless:sortCaselessArg sortReverse:sortReverseArg 
    "set instance variables (automatically generated)"

    directoriesBeforeFiles := directoriesBeforeFilesArg.
    selector := selectorArg.
    sortCaseless := sortCaselessArg.
    sortReverse := sortReverseArg.
!

selector
    ^ selector
!

selector:something
    selector := something.
!

sortCaseless
    ^ sortCaseless ? false
!

sortCaseless:something
    sortCaseless := something.
!

sortReverse
    ^ sortReverse ? false
!

sortReverse:something
    sortReverse := something.
! !

!FileSorter methodsFor:'action'!

sortItemList:aList

    |selectorSymbol instanceSortBlock cmpOp sortBlock locSortReverse|

    selectorSymbol := selector asSymbol.
    sortReverse ifTrue:[
        cmpOp := #'>'
    ] ifFalse:[
        cmpOp := #'<'
    ].
    locSortReverse := self sortReverse.
    instanceSortBlock := [:a :b | 
        |entry1 entry2|

        entry1 := (a perform:selectorSymbol).
        entry2 := (b perform:selectorSymbol).
        ((entry1 isNil) or:[entry2 isNil]) ifTrue:[
            ((entry1 isNil) and:[entry2 isNil]) ifTrue:[
                true
            ] ifFalse:[
                ((entry1 notNil) and:[entry2 isNil]) ifTrue:[
                    locSortReverse
                ] ifFalse:[
                    locSortReverse not
                ]
            ]
        ] ifFalse:[
            ((selector = #baseName) and:[sortCaseless]) ifTrue:[
                entry1 := entry1 asLowercase.
                entry2 := entry2 asLowercase.
            ].
            entry1 perform:cmpOp with:entry2
        ]
    ].
    directoriesBeforeFiles ifTrue:[
        sortBlock := [:a :b|
            |aIsDir bIsDir res|

            aIsDir := a isDirectory.
            bIsDir := b isDirectory.
            (aIsDir ~~ bIsDir) ifTrue:[
                res := aIsDir 
            ] ifFalse:[
                res := instanceSortBlock value:a value:b.
            ].
            res
          ].
    ] ifFalse:[
        sortBlock := instanceSortBlock.
    ].

    aList sort:sortBlock.
    ^ aList
! !

!FileSorter class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic2/FileSorter.st,v 1.4 2005-02-02 11:03:47 cg Exp $'
! !