added:14 methods
authorStefan Vogel <sv@exept.de>
Mon, 09 May 2011 15:59:06 +0200
changeset 13364 ca7da07b8bb2
parent 13363 070b1c3087bc
child 13365 352699e52383
added:14 methods comment/format in: #asString changed: #documentation #lookupInMessageCatalog category of: #expandMacros #expandMacrosWith:
UserMessage.st
--- a/UserMessage.st	Mon May 09 10:22:54 2011 +0200
+++ b/UserMessage.st	Mon May 09 15:59:06 2011 +0200
@@ -20,8 +20,46 @@
     the resource mechanism, if the given catalogID is the name of a class;
     i.e.
         (YesNoBox classResources string:'continue')
+
     can now also be written as:
-        (#continue << #YesNoBox) 
+
+        (#continue << YesNoBox) asString
+        (#continue << #YesNoBox >> 'Continue really') asString
+"
+!
+
+examples
+"
+Use a class as catalog:
+                                                                [exBegin]
+        self warn:(#continue << YesNoBox) asString
+                                                                [exEnd]
+Use a class name as catalog:
+                                                                [exBegin]
+        self warn:(#continue << #YesNoBox) asString
+                                                                [exEnd]
+Some default text if the symbol is not present in the catalog:
+                                                                [exBegin]
+        self warn:(#continue << #YesNoBox >> 'Default for continue') asString.
+        self warn:(#continueRRRRRRR << #YesNoBox >> 'Default for continue') asString
+                                                                [exEnd]
+Here we inherit from the top catalog:
+                                                                [exBegin]
+        self information:(Time now printStringFormat:(#TIMEFORMAT << self >> 'Resolved via default %h:%m:%s') asString)
+                                                                [exEnd]
+
+Can also use a package's catalog:
+                                                                [exBegin]
+        self information:((#'WARN_RENAME' << #'stx:libtool' ) 
+                          withCRs bindWith:'ARG1' with:'ARG2')
+                                                                [exEnd]
+
+Lazy resolving the catalog in a block
+(if you generate the messages at startup and want to recognize language changes):
+                                                                [exBegin]
+        self information:((#'WARN_RENAME' << [ Tools::NewSystemBrowser classResources ] ) 
+                          withCRs bindWith:'ARG1' with:'ARG2')
+                                                                [exEnd]
 "
 ! !
 
@@ -35,6 +73,107 @@
      ^ self new key:aKeySymbol defaultString:aString
 ! !
 
+!UserMessage methodsFor:'Compatibility-V''Age'!
+
+bindWith:aString
+    "return a copy of the receiver, where a '%1' escape is
+     replaced by aString.
+     This has been added for VisualAge compatibility."
+
+    ^ self expandPlaceholdersWith:(Array with:aString)
+
+    "
+     'do you like %1 ?' bindWith:'smalltalk'
+    "
+!
+
+bindWith:string1 with:string2
+    "return a copy of the receiver, where a '%1' escape is
+     replaced by string1 and '%2' is replaced by string2.
+     This has been added for VisualAge compatibility."
+
+    ^ self expandPlaceholdersWith:(Array with:string1 with:string2)
+
+    "
+     'do you prefer %1 or rather %2 ?'
+	bindWith:'smalltalk' with:'c++'
+    "
+!
+
+bindWith:str1 with:str2 with:str3
+    "return a copy of the receiver, where a '%1', '%2' and '%3' escapes
+     are replaced by str1, str2 and str3 respectively.
+     This has been added for VisualAge compatibility."
+
+    ^ self expandPlaceholdersWith:(Array with:str1 with:str2 with:str3)
+
+    "
+     'do you prefer %1 or rather %2 (not talking about %3) ?'
+	bindWith:'smalltalk' with:'c++' with:'c'
+    "
+!
+
+bindWith:str1 with:str2 with:str3 with:str4
+    "return a copy of the receiver, where a '%1', '%2', '%3' and '%4' escapes
+     are replaced by str1, str2, str3 and str4 respectively.
+     This has been added for VisualAge compatibility."
+
+    ^ self expandPlaceholdersWith:(Array with:str1 with:str2 with:str3 with:str4)
+
+    "
+     'do you prefer %1 or rather %2 (not talking about %3 or even %4) ?'
+	bindWith:'smalltalk' with:'c++' with:'c' with:'assembler'
+    "
+!
+
+bindWith:str1 with:str2 with:str3 with:str4 with:str5
+    "return a copy of the receiver, where a '%1' .. '%5' escapes
+     are replaced by str1 .. str5 respectively.
+     This has been added for VisualAge compatibility."
+
+    ^ self expandPlaceholdersWith:(Array with:str1 with:str2 with:str3 with:str4 with:str5)
+
+    "Created: 31.1.1997 / 16:25:42 / cg"
+!
+
+bindWith:str1 with:str2 with:str3 with:str4 with:str5 with:str6
+    "return a copy of the receiver, where a '%1' .. '%6' escapes
+     are replaced by str1 .. str6 respectively.
+     This has been added for VisualAge compatibility."
+
+    ^ self expandPlaceholdersWith:(Array with:str1 with:str2
+					 with:str3 with:str4
+					 with:str5 with:str6)
+!
+
+bindWith:str1 with:str2 with:str3 with:str4 with:str5 with:str6 with:str7
+    "return a copy of the receiver, where a '%1' .. '%7' escapes
+     are replaced by str1 .. str7 respectively.
+     This has been added for VisualAge compatibility."
+
+    ^ self expandPlaceholdersWith:(Array with:str1 with:str2
+					 with:str3 with:str4
+					 with:str5 with:str6
+					 with:str7)
+!
+
+bindWithArguments:anArrayOfStrings
+    "return a copy of the receiver, where a '%i' escape
+     is replaced by the coresponding string from the argument array.
+     'i' may be between 1 and 9 (i.e. a maximum of 9 placeholders is allowed).
+     This has been added for VisualAge compatibility."
+
+    ^ self expandPlaceholdersWith:anArrayOfStrings
+
+    "
+     'do you prefer %1 or rather %2 (not talking about %3) ?'
+        bindWithArguments:#('smalltalk' 'c++' 'c')
+
+     'do you %(what) ?'
+        bindWithArguments:(Dictionary new at:#'what' put:'understand'; yourself)
+    "
+! !
+
 !UserMessage methodsFor:'accessing'!
 
 catalogID
@@ -74,7 +213,9 @@
 !UserMessage methodsFor:'converting'!
 
 asString
-    "for now: return the defaultString, ignoring the catalogID."
+    "convert the user message to a string.
+     If there us no mapping for the user message -
+        for now: return the defaultString, ignoring the catalogID."
 
     |str|
 
@@ -86,6 +227,35 @@
     ^ key asString
 !
 
+string
+    ^ self asString
+! !
+
+!UserMessage methodsFor:'printing & storing'!
+
+displayOn:aStream
+    (aStream isStream not and:[aStream ~~ Transcript]) ifTrue:[
+        "/ what a kludge - Dolphin and Squeak mean: printOn: a stream;
+        "/ ST/X (and some old ST80's) mean: draw-yourself on a GC.
+        self asString displayOn:aStream.
+        ^ self
+    ].
+
+    key storeOn:aStream.
+    aStream nextPutAll:' << '.
+    catalogID storeOn:aStream.
+    defaultString notNil ifTrue:[
+        aStream nextPutAll:' >> '.
+        defaultString storeOn:aStream.
+    ].
+!
+
+printOn:aStream
+    aStream nextPutAll:self asString
+! !
+
+!UserMessage methodsFor:'special string converting'!
+
 expandMacros
     ^ self asString expandMacros
 
@@ -96,26 +266,16 @@
 
 !
 
-string
-    ^ self asString
-! !
-
-!UserMessage methodsFor:'printing & storing'!
-
-displayString
-
-    |stream|
+expandPlaceholdersWith:argArrayOrDictionary
+    ^ self asString expandPlaceholdersWith:argArrayOrDictionary
+!
 
-    stream := '' writeStream.
+expandPlaceholdersWith:argArrayOrDictionary on:aStream
+    ^ self asString expandPlaceholdersWith:argArrayOrDictionary on:aStream
+!
 
-    key storeOn:stream.
-    stream nextPutAll:' << '.
-    catalogID storeOn:stream.
-    defaultString notNil ifTrue:[
-        stream nextPutAll:' >> '.
-        defaultString storeOn:stream.
-    ].
-    ^ stream contents.
+withCRs
+   ^ self asString withCRs
 ! !
 
 !UserMessage methodsFor:'utilities'!
@@ -137,42 +297,50 @@
 !
 
 lookupInMessageCatalog
-    |messageCatalog|
+    |messageCatalog resolvedMessageCatalog|
 
-    "/ for now - handle the case that the catalogID is
-    "/ a classes name; in that case, ask its resourcePack.
+    "catalogID may be a block"
+    messageCatalog := resolvedMessageCatalog :=catalogID value.
 
-    catalogID isSymbol ifTrue:[
-        messageCatalog := Smalltalk at:catalogID.
-    ] ifFalse:[
-        messageCatalog := catalogID.
+    "for now - handle the case that the catalogID is
+     a classes name; in that case, ask its resourcePack."
+    messageCatalog isSymbol ifTrue:[
+        resolvedMessageCatalog := Smalltalk at:messageCatalog.
+        resolvedMessageCatalog isNil ifTrue:[
+            "no class, so it is a packageID"
+            resolvedMessageCatalog := ResourcePack forPackage:messageCatalog cached:true.
+        ].
     ].
-    messageCatalog isBehavior ifTrue:[
-        messageCatalog := messageCatalog classResources.
+    resolvedMessageCatalog isBehavior ifTrue:[
+        resolvedMessageCatalog := resolvedMessageCatalog classResources.
     ].
-    messageCatalog isNil ifTrue:[
+    (resolvedMessageCatalog isNil) ifTrue:[
         defaultString isNil ifTrue:[
-            ^ 'Non-existant message (%1<<%2)' bindWith:key with:catalogID printString.
+            ^ 'Non-existant message: (%1<<%2)' bindWith:key with:catalogID printString.
         ].
         ^ defaultString.
     ].
 
-    ^ messageCatalog at:key ifAbsent:defaultString.
+    ^ resolvedMessageCatalog at:key ifAbsent:nil.
 
     "
-     (#'WARN_RENAME' << #BrowserView >> 'A class named %1 already exists (in ''%2'')\\that class will no longer be visible (i.e. removed) if you continue.\\Continue ?' )
-     (#'WARN_RENAME' << #BrowserView)
-     (#'WARN_RENAME' << BrowserView classResources)
+     (#'WARN_RENAME' << #BrowserView >> 'A class named %1 already exists (in ''%2'')\\that class will no longer be visible (i.e. removed) if you continue.\\Continue ?' ) asString
+     (#'WARN_RENAME' << #BrowserView) asString
+     (#'WARN_RENAME' << BrowserView classResources) asString
+     (#'WARN_RENAME' << [ BrowserView classResources] ) asString
+     (#'WARN_RENAME' << #'stx:libtool' ) asString
      (#dontKnow << #nonExistantMessageCatalog) asString
+     (#dontKnow << [YesNoBox]) asString
+     (#dontKnow << #nonExistantMessageCatalog >> 'Default text') asString
     "
 ! !
 
 !UserMessage class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/UserMessage.st,v 1.6 2009-10-02 00:08:10 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/UserMessage.st,v 1.7 2011-05-09 13:59:06 stefan Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic/UserMessage.st,v 1.6 2009-10-02 00:08:10 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/UserMessage.st,v 1.7 2011-05-09 13:59:06 stefan Exp $'
 ! !