WorkspaceApplication.st
changeset 3752 79378c1f0faf
parent 3736 687a0a8de66c
child 3812 fef8f2db9662
--- a/WorkspaceApplication.st	Wed Jul 24 21:19:44 2002 +0200
+++ b/WorkspaceApplication.st	Wed Jul 24 23:35:27 2002 +0200
@@ -3,7 +3,8 @@
 ToolApplicationModel subclass:#WorkspaceApplication
 	instanceVariableNames:'workspaces tabList selectedWorkspaceIndexHolder workspaceHolder
 		autoDefineWorkspaceVariables'
-	classVariableNames:'DefaultAutoDefineWorkspaceVariables'
+	classVariableNames:'DefaultAutoDefineWorkspaceVariables LastFilterBlockString
+		LastProcessingBlockString'
 	poolDictionaries:''
 	category:'Interface-Smalltalk'
 !
@@ -188,6 +189,19 @@
                   #translateLabel: true
                   #value: #paste
                 )
+               #(#MenuItem
+                  #label: '-'
+                )
+               #(#MenuItem
+                  #label: 'Filter Text...'
+                  #translateLabel: true
+                  #value: #filterText
+                )
+               #(#MenuItem
+                  #label: 'Process Text...'
+                  #translateLabel: true
+                  #value: #processText
+                )
                )
               nil
               nil
@@ -593,6 +607,38 @@
     Workspace addWorkspaceVariable:name.
 !
 
+askForFilterBlock:message template:template rememberIn:nameOfClassVar
+    |filterBlockString filterBlock dialog textHolder classVarValue|
+
+    classVarValue := self class classVarAt:nameOfClassVar ifAbsent:nil.
+    classVarValue isNil ifTrue:[
+        self class classVarAt:nameOfClassVar put:template. 
+        classVarValue := template.
+    ].
+
+    textHolder := ValueHolder new.
+    dialog := Dialog 
+                 forRequestText:(resources string:message)
+                 lines:25 
+                 columns:70
+                 initialAnswer:classVarValue
+                 model:textHolder.
+    dialog addButton:(Button label:'Template' action:[textHolder value:template. textHolder changed:#value.]).
+    dialog open.
+    dialog accepted ifFalse:[^ nil].
+
+    filterBlockString := textHolder value.
+    self class classVarAt:nameOfClassVar put:filterBlockString. 
+
+    filterBlock := Parser evaluate:filterBlockString.
+    filterBlock isBlock ifFalse:[
+        self halt:'oops - bad input'.
+        ^ nil
+    ].
+
+    ^ filterBlock
+!
+
 autoDefineWorkspaceVariablesChanged
     "does not work yet - needs more interaction with parser"
 
@@ -634,6 +680,70 @@
     self selectedWorkspacesTextView doIt
 !
 
+filterText
+    |template filterBlock newList oldList answer nDeleted deletedLines|
+
+    template :=
+'"/ general text filter;
+"/ the following block should evaluate to true for all lines
+"/ you want to keep - lines for which the block returns false will be removed.
+"/ Beginner warning: Smalltalk know-how is useful here.
+
+[:line |
+     "/ any condition on line.
+     "/ Notice, that line might be a Text object (i.e. non-string),
+     "/ so you may want to use line string.
+     "/ 
+     "/ Useful queries on the line are:
+     "/     - size                   the length of the line
+     "/     - hasChangeOfEmphasis    any bold, italic etc.
+     "/     - startsWith:someString
+     "/     - endsWith:someString
+
+     "/ example filter (removes all empty lines)
+     "/
+     "/ line size > 0
+
+     "/ example filter (removes all lines which do not end with some suffix)
+     "/
+     "/ (line asLowercase endsWith:''foo'') not
+
+     "/ dummy filter (keeps all lines)
+     "/
+     true
+]
+'.
+
+    filterBlock := self askForFilterBlock:'Filter block:'
+                        template:template
+                        rememberIn:#LastFilterBlockString.
+    filterBlock isNil ifTrue:[^ self].
+
+    oldList := self selectedWorkspacesTextView list.
+    oldList := oldList collect:[:lineOrNil | lineOrNil ? ''].
+    newList := oldList select:filterBlock.
+    newList := newList collect:[:line | (line isString and:[line size == 0]) ifTrue:[nil] ifFalse:[line]].
+    nDeleted := oldList size - newList size.
+    nDeleted == 0 ifTrue:[
+        self information:'No lines were deleted.'.
+        ^ self
+    ].
+
+    answer := Dialog confirmWithCancel:(resources 
+                        string:'%1 lines remain (%2 deleted). Change text ?'
+                        with:newList size
+                        with:nDeleted)
+                labels:#( 'Cancel' 'No, Show Deleted' 'Yes').
+    answer isNil ifTrue:[^ self].
+    answer ifFalse:[
+        deletedLines := oldList reject:filterBlock.
+        TextBox openOn:(deletedLines asStringCollection) title:'Filtered lines'.
+        ^ self.
+    ].
+
+    self selectedWorkspacesTextView list:newList.
+!
+
 inspectIt
     self inspectIt:false
 !
@@ -736,6 +846,69 @@
     self selectedWorkspacesTextView printIt
 !
 
+processText
+    |template filterBlock newList oldList answer nChanged changedLines flags|
+
+    template :=
+'"/ general text processor;
+"/ the following block should evaluate to a new line, 
+"/ given the original line as argument.
+"/ Beginner warning: Smalltalk know-how is useful here.
+
+[:line |
+     "/ any processing on line.
+     "/ Notice, that line might be a Text object (i.e. non-string),
+     "/ 
+     "/ Useful operations on the line are:
+     "/     - '' .... '' ,                      concatenation of any prefix/suffix
+     "/     - leftPaddedTo:size                 padding
+     "/     - rightPaddedTo:size                padding
+     "/     - copyTo:(size min:N)               
+     "/     - asUppercase 
+     "/     - asLowercase
+
+     "/ makes everything bold
+     "/
+     "/ line allBold
+
+     "/ dummy filter (keeps all lines as-is)
+     "/
+     line
+]
+'.
+    filterBlock := self askForFilterBlock:'Processing block:'
+                        template:template
+                        rememberIn:#LastProcessingBlockString.
+    filterBlock isNil ifTrue:[^ self].
+
+    oldList := self selectedWorkspacesTextView list.
+    oldList := oldList collect:[:lineOrNil | lineOrNil ? ''].
+    newList := oldList collect:filterBlock.
+    newList := newList collect:[:line | (line isString and:[line size == 0]) ifTrue:[nil] ifFalse:[line]].
+
+    flags := (1 to:oldList size) collect:[:i | (oldList at:i) ~= (newList at:i)].
+    flags := flags select:[:flag | flag].
+    nChanged := flags size.
+    nChanged == 0 ifTrue:[
+        self information:'No lines were changed.'.
+        ^ self
+    ].
+
+    answer := Dialog confirmWithCancel:(resources 
+                        string:'%1 lines changed. Change text ?'
+                        with:nChanged)
+                labels:#( 'Cancel' 'No, Show Changed' 'Yes').
+    answer isNil ifTrue:[^ self].
+    answer ifFalse:[
+        changedLines := (1 to:oldList size) select:[:i | (oldList at:i) ~= (newList at:i)].
+        changedLines := changedLines collect:[:i | (newList at:i)].
+        TextBox openOn:(changedLines asStringCollection) title:'Changed lines'.
+        ^ self.
+    ].
+
+    self selectedWorkspacesTextView list:newList.
+!
+
 removeAllWorkspaceVariables
     (Dialog confirm:'Do you really want to remove all Workspace Variables ?')
     ifTrue:[
@@ -890,5 +1063,5 @@
 !WorkspaceApplication class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libtool/WorkspaceApplication.st,v 1.33 2002-07-17 15:47:38 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libtool/WorkspaceApplication.st,v 1.34 2002-07-24 21:35:27 cg Exp $'
 ! !