#FEATURE by cg
authorClaus Gittinger <cg@exept.de>
Thu, 02 May 2019 21:27:01 +0200
changeset 18750 6be368350a8f
parent 18749 78d06a49dc41
child 18751 387323a6f3b4
#FEATURE by cg class: AbstractDirectoryBrowser added: #findAndSelectNextFileMatching: #findFilesMatching:in: #findNextFileMatching:startingAt: changed: #findFilesMatching:
AbstractDirectoryBrowser.st
--- a/AbstractDirectoryBrowser.st	Thu May 02 21:26:47 2019 +0200
+++ b/AbstractDirectoryBrowser.st	Thu May 02 21:27:01 2019 +0200
@@ -1,3 +1,5 @@
+"{ Encoding: utf8 }"
+
 "
  COPYRIGHT (c) 2003 by eXept Software AG
               All Rights Reserved
@@ -70,17 +72,95 @@
 !AbstractDirectoryBrowser methodsFor:'actions'!
 
 findFilesMatching:aGLOBPattern
+    "search files which match aGLOBPattern.
+     If a folder is selected, the search starts there;
+     otherwise in the top folder"
+     
+    |selItem searchDir|
+
+    (selItem := self theSingleSelectedItemOrNil) notNil ifTrue:[
+        selItem isDirectory ifTrue:[
+            searchDir := selItem pathName
+        ] ifFalse:[
+            searchDir := selItem pathName asFilename directory
+        ].    
+    ] ifFalse:[   
+        searchDir := self directory 
+    ].  
+    ^ self findFilesMatching:aGLOBPattern in:searchDir
+
+    "Created: / 17-07-2018 / 12:53:32 / Claus Gittinger"
+    "Modified: / 02-05-2019 / 20:41:57 / Claus Gittinger"
+!
+
+findFilesMatching:aGLOBPattern in:searchDir
+    "search files which match aGLOBPattern."
+     
     |matching|
 
     matching := OrderedCollection new.
-    self directory recursiveDirectoryContentsAsFilenamesDo:[:each |
+    searchDir asFilename recursiveDirectoryContentsAsFilenamesDo:[:each |
         (aGLOBPattern match:each baseName) ifTrue:[
             matching add:each
         ].    
     ].
     ^ matching
 
-    "Created: / 17-07-2018 / 12:53:32 / Claus Gittinger"
+    "Created: / 02-05-2019 / 18:58:30 / Claus Gittinger"
+!
+
+findNextFileMatching:aGLOBPattern startingAt:anItemOrNil
+    "search files which match aGLOBPattern."
+     
+    |searchFolder searchIndex stack|
+
+    stack := OrderedCollection new.
+    anItemOrNil isRootItem ifFalse:[
+        |i|
+        
+        i := anItemOrNil.
+        [i isRootItem] whileFalse:[
+            stack addFirst:(i parent -> (i parent children indexOf:i)).
+            i := i parent.
+        ].    
+    ].
+    
+    anItemOrNil isDirectory ifTrue:[
+        searchFolder := anItemOrNil.
+        searchIndex := 0.
+    ] ifFalse:[
+        searchFolder := anItemOrNil parent.
+        searchIndex := anItemOrNil parent children indexOf:anItemOrNil.
+    ].
+    stack add:(searchFolder -> searchIndex).
+    
+    [stack notEmpty] whileTrue:[
+        |work children|
+
+        work := stack removeLast.
+        searchFolder := work key.
+        searchIndex := work value.
+        
+        children := searchFolder children.
+        searchIndex+1 to:(children size) doWithExit:[:childIndex :exit|
+            |child|
+            
+            child := children at:childIndex.
+Transcript showCR:'search %1' with:child pathName.
+            child isDirectory ifFalse:[
+                (aGLOBPattern match:child baseName) ifTrue:[
+                    ^ child pathName asFilename
+                ].
+            ] ifTrue:[
+                stack add:(searchFolder -> childIndex).
+                stack add:(child -> 0).
+                exit value:nil.
+            ]
+        ].
+    ].  
+    ^ nil
+
+    "Created: / 02-05-2019 / 20:39:58 / Claus Gittinger"
 ! !
 
 !AbstractDirectoryBrowser methodsFor:'drag & drop'!
@@ -215,6 +295,18 @@
     self selectFiles:(self findFilesMatching:aGLOBPattern).
 
     "Created: / 17-07-2018 / 12:48:23 / Claus Gittinger"
+!
+
+findAndSelectNextFileMatching:aGLOBPattern
+    |fileOrNil|
+
+    fileOrNil := self findNextFileMatching:aGLOBPattern 
+                      startingAt:(self theSingleSelectedItemOrNil ? self fileList root).
+    self selectFiles:(fileOrNil isNil 
+                        ifTrue:[#()]
+                        ifFalse:[{ fileOrNil }])
+
+    "Created: / 02-05-2019 / 20:43:41 / Claus Gittinger"
 ! !
 
 !AbstractDirectoryBrowser methodsFor:'selection'!