code cleanup; more application code generation
authorClaus Gittinger <cg@exept.de>
Fri, 27 Oct 2006 11:13:31 +0200
changeset 7480 33930379ab27
parent 7479 efd5ed2b558d
child 7481 11c16a700dcf
code cleanup; more application code generation
CodeGeneratorTool.st
--- a/CodeGeneratorTool.st	Thu Oct 26 19:43:27 2006 +0200
+++ b/CodeGeneratorTool.st	Fri Oct 27 11:13:31 2006 +0200
@@ -477,7 +477,8 @@
 createApplicationCodeFor:aClass
     "create an empty application framework"
 
-    |nonMetaClass metaClass className txt isDialog categoryForMenuActionsMethods compileMenuAction|
+    |nonMetaClass metaClass className txt isDialog 
+     categoryForMenuActionsMethods compileMenuAction compileTemplateAction|
 
     self startCollectChanges.
 
@@ -504,6 +505,25 @@
             inCategory:'interface specs'.
     ].
 
+    compileTemplateAction := 
+        [:selector :category |
+            (nonMetaClass includesSelector:selector) ifFalse:[
+                |codeTemplateSelector|
+
+                codeTemplateSelector := ('codeFor_',(selector upTo:$:)) asSymbol.
+                txt := self perform:codeTemplateSelector.
+                self
+                    compile:txt
+                    forClass:nonMetaClass 
+                    inCategory:(category = '*' ifTrue:categoryForMenuActionsMethods ifFalse:category).
+            ]
+        ].
+
+    #(
+        #'postBuildWith:'   'initialization & release'
+        #'postOpenWith:'    'initialization & release'
+    ) pairWiseDo:compileTemplateAction.
+
     isDialog ifFalse:[
         "/ add a topMenu method 
 
@@ -514,184 +534,30 @@
                 forClass:metaClass 
                 inCategory:'menu specs'.
         ].
-    ].
 
-    (nonMetaClass includesSelector:#postBuildWith:) ifFalse:[
-        txt :=
-'postBuildWith:aBuilder
-    "This is a hook method generated by the Browser.
-     It will be invoked during the initialization of your app/dialog,
-     after all of the visual components have been built, 
-     but BEFORE the top window is made visible.
-     Add any app-specific actions here (reading files, setting up values etc.)
-     See also #postOpenWith:, which is invoked after opening."
-
-    "/ add any code here ...
-
-    ^ super postBuildWith:aBuilder
-'.
-        self
-            compile:txt
-            forClass:nonMetaClass 
-            inCategory:'initialization & release'.
-    ].
-
-    (nonMetaClass includesSelector:#postOpenWith:) ifFalse:[
-        txt :=
-'postOpenWith:aBuilder
-    "This is a hook method generated by the Browser.
-     It will be invoked right after the applications window has been opened.
-     Add any app-specific actions here (starting background processes etc.).
-     See also #postBuildWith:, which is invoked before opening."
-
-    "/ add any code here ...
-
-    ^ super postOpenWith:aBuilder
-'.
-        self
-            compile:txt
-            forClass:nonMetaClass 
-            inCategory:'initialization & release'.
-    ].
-
-    isDialog ifFalse:[
-        (nonMetaClass includesSelector:#closeRequest) ifFalse:[
-            txt :=
-'closeRequest
-    "This is a hook method generated by the Browser.
-     It will be invoked when your app/dialog-window is about to be
-     closed (this method has a chance to suppress the close).
-     See also #closeDownViews, which is invoked when the close is really done."
-
-    "/ change the code below as required ...
-    "/ Closing can be suppressed, by simply returning.
-    "/ The ''super closeRequest'' at the end will initiate the real closeDown
-
-    ("self hasUnsavedChanges" true) ifTrue:[
-        (self confirm:(resources string:''Close without saving ?'')) ifFalse:[
-            ^ self
-        ]
-    ].
-
-    ^ super closeRequest
-'.
-            self
-                compile:txt
-                forClass:nonMetaClass 
-                inCategory:'initialization & release'.
-        ].
-    ].
-
-    isDialog ifFalse:[
-        (nonMetaClass includesSelector:#closeDownViews) ifFalse:[
-            txt :=
-'closeDownViews
-    "This is a hook method generated by the Browser.
-     It will be invoked when your app/dialog-window is really closed.
-     See also #closeDownViews, which is invoked before and may suppress the close
-     or ask the user for confirmation."
-
-    "/ change the code below as required ...
-    "/ This should cleanup any leftover resources
-    "/ (for example, temporary files)
-    "/ super closeRequest will initiate the closeDown
-
-    "/ add your code here
-
-    "/ do not remove the one below ...
-    ^ super closeDownViews
-'.
-            self
-                compile:txt
-                forClass:nonMetaClass 
-                inCategory:'initialization & release'.
-        ].
+        #(
+            #'hasUnsavedChanges'        'private queries'
+            #'closeRequest'             'initialization & release'
+            #'closeDownViews'           'initialization & release'
+            #'menuSaveAs'               '*'
+            #'menuNew'                  '*'
+            #'menuOpen'                 '*'
+            #'menuSave'                 '*'
+            #'doSaveAs'                 '*'
+            #'openDocumentation'        '*'
+            #'openAboutThisApplication' '*'
+        ) pairWiseDo:compileTemplateAction.
     ].
 
     isDialog ifTrue:[
-        (nonMetaClass includesSelector:#accept) ifFalse:[
-            txt :=
-'closeAccept
-    "This is a hook method generated by the Browser.
-     It will be invoked when your dialog-window is closed with OK."
-
-    "/ add any actions as required here ...
-    Transcript showCR:''dialog accepted''.
-
-    "/ do not remove the one below ...
-    ^ super closeAccept
-'.
-            self
-                compile:txt
-                forClass:nonMetaClass 
-                inCategory:'user actions'.
-        ].
-    ].
-
-    isDialog ifFalse:[
-        compileMenuAction := 
-            [:selector :item |
-                (nonMetaClass includesSelector:selector) ifFalse:[
-                    txt :=
-selector,'
-    "This method was generated by the Browser.
-     It will be invoked when the menu-item ''',item,''' is selected."
-
-    "/ change below and add any actions as required here ...
-    self warn:''no action for ''''',item,''''' defined.''.
-'.
-                    self
-                        compile:txt
-                        forClass:nonMetaClass 
-                        inCategory:categoryForMenuActionsMethods.
-                ].
-            ].
-
-        compileMenuAction value:#menuNew value:'new'.
-        compileMenuAction value:#menuOpen value:'open'.
-        compileMenuAction value:#menuSave value:'save'.
-        compileMenuAction value:#menuSaveAs value:'saveAs'.
-
-        (nonMetaClass includesSelector:#openDocumentation) ifFalse:[
-            txt :=
-'openDocumentation
-    "This method was generated by the Browser.
-     It will be invoked when the menu-item ''help-documentation'' is selected."
-
-    "/ change below as required ...
-
-    "/ to open an HTML viewer on some document (under ''doc/online/<language>/'' ):
-    HTMLDocumentView openFullOnDocumentationFile:''TOP.html''.
-
-    "/ add application-specific help files under the ''doc/online/<language>/help/appName''
-    "/ directory, and open a viewer with:
-    "/ HTMLDocumentView openFullOnDocumentationFile:''help/<MyApplication>/TOP.html''.
-'.
-            self
-                compile:txt
-                forClass:nonMetaClass 
-                inCategory:categoryForMenuActionsMethods.
-        ].
-
-        (nonMetaClass includesSelector:#openAboutThisApplication) ifFalse:[
-            txt :=
-'openAboutThisApplication
-    "This method was generated by the Browser.
-     It will be invoked when the menu-item ''help-about'' is selected."
-
-    "/ could open a customized aboutBox here ...
-    super openAboutThisApplication
-'.
-            self
-                compile:txt
-                forClass:nonMetaClass 
-                inCategory:categoryForMenuActionsMethods.
-        ].
+        #(
+            #'closeAccept'      'user actions'
+        ) pairWiseDo:compileTemplateAction.
     ].
 
     self executeCollectedChangesNamed:('Add Application Code for ' , className).
 
-    "Modified: / 05-09-2006 / 16:34:18 / cg"
+    "Modified: / 27-10-2006 / 10:21:58 / cg"
 !
 
 createClassResponsibleProtocolFor:aClass
@@ -1849,6 +1715,205 @@
     ].
 ! !
 
+!CodeGeneratorTool methodsFor:'code templates'!
+
+codeFor_closeAccept
+    ^
+'closeAccept
+    "This is a hook method generated by the Browser.
+     It will be invoked when your dialog-window is closed with OK."
+
+    "/ add any actions as required here ...
+    Transcript showCR:''dialog accepted''.
+
+    "/ do not remove the one below (otherwise, the dialog will not close itself)...
+    ^ super closeAccept
+'.
+
+    "Created: / 27-10-2006 / 10:03:31 / cg"
+!
+
+codeFor_closeDownViews
+    ^
+'closeDownViews
+    "This is a hook method generated by the Browser.
+     It will be invoked when your app/dialog-window is really closed.
+     See also #closeDownViews, which is invoked before and may suppress the close
+     or ask the user for confirmation."
+
+    "/ change the code below as required ...
+    "/ This should cleanup any leftover resources
+    "/ (for example, temporary files)
+    "/ super closeRequest will initiate the closeDown
+
+    "/ add your code here
+
+    "/ do not remove the one below ...
+    ^ super closeDownViews
+'.
+
+    "Created: / 27-10-2006 / 10:01:32 / cg"
+!
+
+codeFor_closeRequest
+    ^
+'closeRequest
+    "This is a hook method generated by the Browser.
+     It will be invoked when your app/dialog-window is about to be
+     closed (this method has a chance to suppress the close).
+     See also #closeDownViews, which is invoked when the close is really done."
+
+    "/ change the code below as required ...
+    "/ Closing can be suppressed, by simply returning.
+    "/ The ''super closeRequest'' at the end will initiate the real closeDown
+
+    self hasUnsavedChanges ifTrue:[
+        (self confirm:(resources string:''Close without saving ?'')) ifFalse:[
+            ^ self
+        ]
+    ].
+
+    ^ super closeRequest
+'.
+
+    "Created: / 27-10-2006 / 10:01:06 / cg"
+!
+
+codeFor_doSaveAs
+    ^ self codeFor_emptyMenuActionCodeFor:#doSaveAs menuItem:'save/saveAs'
+
+    "Created: / 27-10-2006 / 10:22:06 / cg"
+!
+
+codeFor_emptyMenuActionCodeFor:selector menuItem:item
+    ^
+selector,'
+    "This method was generated by the Browser.
+     It will be invoked when the menu-item ''',item,''' is selected."
+
+    "/ change below and add any actions as required here ...
+    self warn:''no action for ''''',item,''''' defined.''.
+'.
+
+    "Created: / 27-10-2006 / 10:16:43 / cg"
+!
+
+codeFor_hasUnsavedChanges
+    ^
+'hasUnsavedChanges
+    "Return true, if any unsaved changes are present 
+     (i.e. the contents needs to be saved or else will be lost)"
+
+    "/ add real code as required (or remove the halt and always return false)...
+    self halt:''check this code''.
+    ^ false.
+'.
+
+    "Created: / 27-10-2006 / 10:00:36 / cg"
+!
+
+codeFor_menuNew
+    ^ self codeFor_emptyMenuActionCodeFor:#menuNew menuItem:'new'
+
+    "Created: / 27-10-2006 / 10:17:08 / cg"
+!
+
+codeFor_menuOpen
+    ^ self codeFor_emptyMenuActionCodeFor:#menuOpen menuItem:'open'
+
+    "Created: / 27-10-2006 / 10:17:18 / cg"
+!
+
+codeFor_menuSave
+    ^ self codeFor_emptyMenuActionCodeFor:#menuSave menuItem:'save'
+
+    "Created: / 27-10-2006 / 10:17:25 / cg"
+!
+
+codeFor_menuSaveAs
+    ^
+'menuSaveAs
+    "This method was generated by the Browser.
+     It will be invoked when the menu-item ''saveAs'' is selected."
+
+    "/ change below as required... (see examples in Dialog class for more options)
+    Dialog
+        requestSaveFileName:(resources string:''Save'') 
+        default:''foo.txt'' 
+        fromDirectory:nil 
+        action:[:fileName | self doSaveAs:fileName] 
+        appendAction:nil.
+'.
+
+    "Created: / 27-10-2006 / 10:01:57 / cg"
+!
+
+codeFor_openAboutThisApplication
+    ^
+'openAboutThisApplication
+    "This method was generated by the Browser.
+     It will be invoked when the menu-item ''help-about'' is selected."
+
+    "/ could open a customized aboutBox here ...
+    super openAboutThisApplication
+'.
+
+    "Created: / 27-10-2006 / 10:03:13 / cg"
+!
+
+codeFor_openDocumentation
+    ^
+'openDocumentation
+    "This method was generated by the Browser.
+     It will be invoked when the menu-item ''help-documentation'' is selected."
+
+    "/ change below as required ...
+
+    "/ to open an HTML viewer on some document (under ''doc/online/<language>/'' ):
+    HTMLDocumentView openFullOnDocumentationFile:''TOP.html''.
+
+    "/ add application-specific help files under the ''doc/online/<language>/help/appName''
+    "/ directory, and open a viewer with:
+    "/ HTMLDocumentView openFullOnDocumentationFile:''help/<MyApplication>/TOP.html''.
+'.
+
+    "Created: / 27-10-2006 / 10:02:55 / cg"
+!
+
+codeFor_postBuildWith
+    ^
+'postBuildWith:aBuilder
+    "This is a hook method generated by the Browser.
+     It will be invoked during the initialization of your app/dialog,
+     after all of the visual components have been built, 
+     but BEFORE the top window is made visible.
+     Add any app-specific actions here (reading files, setting up values etc.)
+     See also #postOpenWith:, which is invoked after opening."
+
+    "/ add any code here ...
+
+    ^ super postBuildWith:aBuilder
+'.
+
+    "Created: / 27-10-2006 / 09:59:33 / cg"
+!
+
+codeFor_postOpenWith
+    ^
+'postOpenWith:aBuilder
+    "This is a hook method generated by the Browser.
+     It will be invoked right after the applications window has been opened.
+     Add any app-specific actions here (starting background processes etc.).
+     See also #postBuildWith:, which is invoked before opening."
+
+    "/ add any code here ...
+
+    ^ super postOpenWith:aBuilder
+'.
+
+    "Created: / 27-10-2006 / 09:59:56 / cg"
+! !
+
 !CodeGeneratorTool methodsFor:'compilation'!
 
 compile:theCode forClass:aClass inCategory:cat 
@@ -1962,5 +2027,5 @@
 !CodeGeneratorTool class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libtool/CodeGeneratorTool.st,v 1.50 2006-10-25 07:33:28 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libtool/CodeGeneratorTool.st,v 1.51 2006-10-27 09:13:31 cg Exp $'
 ! !