author | Claus Gittinger <cg@exept.de> |
Mon, 11 Sep 2017 09:11:40 +0200 | |
changeset 17687 | 01e713d1af75 |
parent 17557 | 97a1ae3f0b02 |
child 18069 | d4a380bc5227 |
permissions | -rw-r--r-- |
9706 | 1 |
" |
2 |
COPYRIGHT (c) 2002 by eXept Software AG |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
"{ Package: 'stx:libtool' }" |
|
13 |
||
15515
895b2312db3e
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15069
diff
changeset
|
14 |
"{ NameSpace: Smalltalk }" |
895b2312db3e
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15069
diff
changeset
|
15 |
|
9706 | 16 |
CodeGeneratorTool subclass:#SmalltalkCodeGeneratorTool |
17 |
instanceVariableNames:'' |
|
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'Interface-Browsers' |
|
21 |
! |
|
22 |
||
23 |
!SmalltalkCodeGeneratorTool class methodsFor:'documentation'! |
|
24 |
||
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 2002 by eXept Software AG |
|
28 |
All Rights Reserved |
|
29 |
||
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
38 |
||
39 |
documentation |
|
40 |
" |
|
41 |
This utility class contains various code generation facilites; |
|
42 |
these were extracted from the old and newBrowser. |
|
43 |
There is probably more to come... |
|
44 |
||
45 |
[author:] |
|
46 |
Claus Gittiner |
|
47 |
" |
|
48 |
! ! |
|
49 |
||
50 |
!SmalltalkCodeGeneratorTool class methodsFor:'code generation'! |
|
51 |
||
52 |
initialMenuSpecMethodSourceForApplications |
|
53 |
"return code for a menuSpec with typical stuff in it" |
|
54 |
||
55 |
^ |
|
56 |
'mainMenu |
|
57 |
"This resource specification was automatically generated by the CodeGeneratorTool." |
|
58 |
||
59 |
"Do not manually edit this!! If it is corrupted, |
|
60 |
the MenuEditor may not be able to read the specification." |
|
61 |
||
62 |
" |
|
63 |
MenuEditor new openOnClass:%1 andSelector:#mainMenu |
|
64 |
" |
|
65 |
||
66 |
<resource: #menu> |
|
67 |
||
68 |
^ ',(self initialMenuSpecForApplications decodeAsLiteralArray literalArrayEncoding storeString),' |
|
69 |
'. |
|
70 |
! |
|
71 |
||
72 |
initialPageMenuSpecMethodSourceForWebApplications |
|
73 |
"return code for a menuSpec with typical stuff in it" |
|
74 |
||
75 |
^ |
|
76 |
'mainMenu |
|
77 |
"This resource specification was automatically generated by the CodeGeneratorTool." |
|
78 |
||
79 |
"Do not manually edit this!! If it is corrupted, |
|
80 |
the MenuEditor may not be able to read the specification." |
|
81 |
||
82 |
" |
|
83 |
MenuEditor new openOnClass:%1 andSelector:#mainMenu |
|
84 |
" |
|
85 |
||
86 |
<resource: #menu> |
|
87 |
||
88 |
^ ',(self initialPageMenuSpecForWebApplications decodeAsLiteralArray literalArrayEncoding storeString),' |
|
89 |
'. |
|
90 |
||
91 |
" |
|
92 |
self initialPageMenuSpecMethodSourceForWebApplications |
|
93 |
" |
|
94 |
! |
|
95 |
||
96 |
initialPageSpecMethodSourceForWebApplications |
|
97 |
"return an empty pageSpec" |
|
98 |
||
99 |
^ |
|
100 |
'pageSpec |
|
101 |
"This resource specification was automatically generated by the CodeGeneratorTool." |
|
102 |
||
103 |
"Do not manually edit this!! If it is corrupted, |
|
104 |
the UIPainter may not be able to read the specification." |
|
105 |
||
106 |
" |
|
107 |
UIPainter new openOnClass:%1 andSelector:#pageSpec |
|
108 |
" |
|
109 |
||
110 |
<resource: #canvas> |
|
111 |
||
112 |
^ ',(self initialPageSpecForWebApplications decodeAsLiteralArray literalArrayEncoding storeString),' |
|
113 |
'. |
|
114 |
! |
|
115 |
||
116 |
initialToolbarMenuSpecMethodSource |
|
117 |
"return a menuSpec with typical stuff in it" |
|
118 |
||
119 |
^ |
|
120 |
'toolbarMenu |
|
121 |
"This resource specification was automatically generated by the CodeGeneratorTool." |
|
122 |
||
123 |
"Do not manually edit this!! If it is corrupted, |
|
124 |
the MenuEditor may not be able to read the specification." |
|
125 |
||
126 |
" |
|
127 |
MenuEditor new openOnClass:%1 andSelector:#toolbarMenu |
|
128 |
" |
|
129 |
||
130 |
<resource: #menu> |
|
131 |
||
132 |
^ ',(self initialToolbarMenuSpec decodeAsLiteralArray literalArrayEncoding storeString),' |
|
133 |
'. |
|
134 |
||
135 |
" |
|
136 |
self initialToolbarMenuSpecMethodSource |
|
137 |
" |
|
138 |
! |
|
139 |
||
140 |
initialWindowSpecMethodSourceForApplications |
|
141 |
"return an empty windowSpec with an initial menubar in it" |
|
142 |
||
143 |
^ |
|
144 |
'windowSpec |
|
145 |
"This resource specification was automatically generated by the CodeGeneratorTool." |
|
146 |
||
147 |
"Do not manually edit this!! If it is corrupted, |
|
148 |
the UIPainter may not be able to read the specification." |
|
149 |
||
150 |
" |
|
151 |
UIPainter new openOnClass:%1 andSelector:#windowSpec |
|
152 |
" |
|
153 |
||
154 |
<resource: #canvas> |
|
155 |
||
156 |
^ ', |
|
16779 | 157 |
self initialWindowSpecForApplications2 decodeAsLiteralArray prettyPrintString |
9706 | 158 |
. |
159 |
||
160 |
" |
|
161 |
self initialWindowSpecMethodSourceForApplications |
|
162 |
" |
|
163 |
! |
|
164 |
||
165 |
initialWindowSpecMethodSourceForDialogs |
|
166 |
"return an empty windowSpec for dialogs" |
|
167 |
||
168 |
^ |
|
169 |
'windowSpec |
|
170 |
"This resource specification was automatically generated by the CodeGeneratorTool." |
|
171 |
||
172 |
"Do not manually edit this!! If it is corrupted, |
|
173 |
the UIPainter may not be able to read the specification." |
|
174 |
||
175 |
" |
|
176 |
UIPainter new openOnClass:%1 andSelector:#windowSpec |
|
177 |
" |
|
178 |
||
179 |
<resource: #canvas> |
|
180 |
||
181 |
^ ', |
|
16779 | 182 |
self initialWindowSpecForDialogs decodeAsLiteralArray prettyPrintString |
9706 | 183 |
! ! |
184 |
||
185 |
!SmalltalkCodeGeneratorTool class methodsFor:'code generation-menus'! |
|
186 |
||
187 |
createActionMethodFor:aSelector in:aClass category:aCategory redefine:redefine |
|
188 |
|alreadyInSuperclass method code| |
|
189 |
||
190 |
(aClass includesSelector:aSelector) ifTrue:[ |
|
191 |
^ nil |
|
192 |
]. |
|
193 |
||
194 |
alreadyInSuperclass := aClass superclass canUnderstand:aSelector. |
|
195 |
(alreadyInSuperclass and:[redefine not]) ifTrue:[ |
|
196 |
^ nil |
|
197 |
]. |
|
198 |
||
199 |
method := self methodNameTemplateFor:aSelector. |
|
200 |
||
201 |
code := '%1 |
|
16627 | 202 |
<resource: #uiCallback> |
203 |
||
9706 | 204 |
"automatically generated by UIEditor ..." |
205 |
||
206 |
"*** the code below performs no action" |
|
207 |
"*** (except for some feedback on the Transcript)" |
|
208 |
"*** Please change as required and accept in the browser." |
|
209 |
"*** (and replace this comment by something more useful ;-)" |
|
210 |
||
211 |
"action to be added ..." |
|
212 |
||
16627 | 213 |
Logger info:''action for #%2 ...''. |
9706 | 214 |
' bindWith:method with:aSelector. |
215 |
||
216 |
alreadyInSuperclass ifTrue:[ |
|
217 |
code := code, (('\ super %1\' bindWith:method) withCRs). |
|
218 |
]. |
|
11523 | 219 |
self compile:code forClass:aClass inCategory:(aCategory ? #actions). |
9706 | 220 |
^ code |
221 |
! |
|
222 |
||
223 |
createAspectMethodFor:aSelector in:aClass category:aCategory redefine:redefine |
|
224 |
|alreadyInSuperclass method code text| |
|
225 |
||
226 |
(aClass includesSelector:aSelector) ifTrue:[ |
|
227 |
^ nil |
|
228 |
]. |
|
229 |
||
230 |
alreadyInSuperclass := aClass superclass canUnderstand:aSelector. |
|
231 |
(alreadyInSuperclass and:[redefine not]) ifTrue:[ |
|
232 |
^ nil |
|
233 |
]. |
|
234 |
||
235 |
method := self methodNameTemplateFor:aSelector. |
|
236 |
||
237 |
code := '%1 |
|
238 |
"automatically generated by UIEditor ..." |
|
239 |
||
240 |
"*** the code below creates a default model when invoked" |
|
241 |
"*** (which may not be the one you wanted)" |
|
242 |
"*** Please change as required and accept in the browser." |
|
243 |
"*** (and replace this comment by something more useful ;-)" |
|
244 |
||
245 |
"aspect to be added ..." |
|
246 |
||
16627 | 247 |
Logger info:''aspect for #%2 ...''. |
9706 | 248 |
|
249 |
' bindWith:method with:aSelector. |
|
250 |
||
251 |
alreadyInSuperclass ifTrue:[ |
|
252 |
text := ' ^ super %1\' bindWith:method. |
|
253 |
] ifFalse:[ |
|
254 |
text := ' ^ builder valueAspectFor:#''%1'' initialValue:true\' bindWith:aSelector. |
|
255 |
]. |
|
256 |
code := code, (text withCRs). |
|
11523 | 257 |
self compile:code forClass:aClass inCategory:(aCategory ? #actions). |
9706 | 258 |
^ code |
259 |
! ! |
|
260 |
||
14696
e74967e679d4
Added more methods to make it polymorphic with Compiler to some extent.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14572
diff
changeset
|
261 |
!SmalltalkCodeGeneratorTool class methodsFor:'compilation'! |
e74967e679d4
Added more methods to make it polymorphic with Compiler to some extent.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14572
diff
changeset
|
262 |
|
e74967e679d4
Added more methods to make it polymorphic with Compiler to some extent.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14572
diff
changeset
|
263 |
compile:theCode forClass:aClass inCategory:cat |
16779 | 264 |
"install some code for a class. |
16428 | 265 |
Return the new method or nil. |
17509 | 266 |
If refactory browser stuff is available, the refactory tools are used to support undo" |
14696
e74967e679d4
Added more methods to make it polymorphic with Compiler to some extent.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14572
diff
changeset
|
267 |
|
16393 | 268 |
| parser selector | |
14696
e74967e679d4
Added more methods to make it polymorphic with Compiler to some extent.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14572
diff
changeset
|
269 |
|
e74967e679d4
Added more methods to make it polymorphic with Compiler to some extent.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14572
diff
changeset
|
270 |
super compile:theCode forClass:aClass inCategory:cat. |
16393 | 271 |
|
14696
e74967e679d4
Added more methods to make it polymorphic with Compiler to some extent.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14572
diff
changeset
|
272 |
parser := Parser parseMethodSpecification: theCode in: aClass ignoreErrors: true ignoreWarnings: true. |
16428 | 273 |
(selector := parser selector) notNil ifTrue:[ |
16393 | 274 |
^ aClass compiledMethodAt: selector. |
14696
e74967e679d4
Added more methods to make it polymorphic with Compiler to some extent.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14572
diff
changeset
|
275 |
]. |
16393 | 276 |
^ nil |
14696
e74967e679d4
Added more methods to make it polymorphic with Compiler to some extent.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14572
diff
changeset
|
277 |
|
e74967e679d4
Added more methods to make it polymorphic with Compiler to some extent.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14572
diff
changeset
|
278 |
"Created: / 05-08-2014 / 16:14:55 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
17509 | 279 |
"Modified (comment): / 17-05-2017 / 16:58:44 / mawalch" |
14696
e74967e679d4
Added more methods to make it polymorphic with Compiler to some extent.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14572
diff
changeset
|
280 |
! |
e74967e679d4
Added more methods to make it polymorphic with Compiler to some extent.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14572
diff
changeset
|
281 |
|
e74967e679d4
Added more methods to make it polymorphic with Compiler to some extent.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14572
diff
changeset
|
282 |
compile:theCode forClass:aClass inCategory:cat notifying: requestor |
17509 | 283 |
"install some code for a class. |
16428 | 284 |
Return the new method or nil. |
17509 | 285 |
If refactory browser stuff is available, the refactory tools are used to support undo" |
14696
e74967e679d4
Added more methods to make it polymorphic with Compiler to some extent.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14572
diff
changeset
|
286 |
|
16326 | 287 |
CompilationErrorHandlerQuery |
288 |
answer:requestor |
|
289 |
do:[ |
|
290 |
^ self compile:theCode forClass:aClass inCategory:cat |
|
17509 | 291 |
] |
14696
e74967e679d4
Added more methods to make it polymorphic with Compiler to some extent.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14572
diff
changeset
|
292 |
|
e74967e679d4
Added more methods to make it polymorphic with Compiler to some extent.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14572
diff
changeset
|
293 |
"Created: / 05-08-2014 / 16:04:02 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
17509 | 294 |
"Modified (comment): / 17-05-2017 / 16:59:07 / mawalch" |
14696
e74967e679d4
Added more methods to make it polymorphic with Compiler to some extent.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14572
diff
changeset
|
295 |
! ! |
e74967e679d4
Added more methods to make it polymorphic with Compiler to some extent.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14572
diff
changeset
|
296 |
|
9706 | 297 |
!SmalltalkCodeGeneratorTool class methodsFor:'private'! |
298 |
||
299 |
methodNameTemplateFor:aSelector |
|
300 |
|numArgs method| |
|
301 |
||
302 |
numArgs := aSelector numArgs. |
|
303 |
numArgs == 1 ifTrue:[ |
|
304 |
method := aSelector, 'anArgument'. |
|
305 |
] ifFalse:[ |
|
306 |
numArgs == 0 ifTrue:[ |
|
307 |
method := aSelector |
|
308 |
] ifFalse:[ |
|
309 |
method := ''. |
|
310 |
aSelector keywords keysAndValuesDo:[:i :key| |
|
311 |
method := method, key, 'arg', i printString, ' '. |
|
312 |
]. |
|
313 |
] |
|
314 |
]. |
|
315 |
^ method |
|
316 |
! ! |
|
317 |
||
16355 | 318 |
!SmalltalkCodeGeneratorTool class methodsFor:'utilities - source code'! |
319 |
||
320 |
methodTemplate |
|
321 |
"return a method definition template string" |
|
322 |
||
323 |
|s| |
|
324 |
||
325 |
s := (TextStream ? WriteStream) on:''. |
|
326 |
s nextPutAll: |
|
327 |
'message "selector and argument names" |
|
328 |
"comment stating purpose of this message" |
|
329 |
||
330 |
|temporaries| |
|
331 |
||
332 |
"statement." |
|
333 |
"statement." |
|
334 |
||
335 |
" |
|
336 |
optional: comment giving example use |
|
337 |
" |
|
338 |
'. |
|
339 |
s cr. |
|
340 |
s emphasis:(UserPreferences current commentEmphasisAndColor). |
|
341 |
s nextPutAll: |
|
342 |
'" |
|
17075 | 343 |
This is an autogenerated method template. |
344 |
||
345 |
If you don''t like this method template to appear, |
|
346 |
disable it either via the global or browser''s settings dialog, |
|
347 |
or by evaluating: |
|
348 |
UserPreferences current showMethodTemplate:false |
|
349 |
||
350 |
Change the above template into real code; |
|
16355 | 351 |
remove this comment. |
16779 | 352 |
Then `accept'' either via the menu |
16355 | 353 |
or via the keyboard (usually CMD-A). |
354 |
||
355 |
You do not need this template; you can also |
|
356 |
select any existing methods code, change it, |
|
357 |
and finally `accept''. The method will then be |
|
358 |
installed under the selector as defined in the |
|
359 |
actual text - no matter which method is selected |
|
360 |
in the browser. |
|
361 |
||
362 |
Or clear this text, type in the method from scratch |
|
363 |
and install it with `accept''. |
|
364 |
" |
|
365 |
'. |
|
366 |
^ s contents |
|
17075 | 367 |
|
368 |
"Modified: / 18-11-2016 / 01:34:20 / cg" |
|
16355 | 369 |
! |
370 |
||
371 |
methodTemplateForDocumentation |
|
17080 | 372 |
"return a documentation method definition template string" |
16355 | 373 |
|
374 |
|s| |
|
375 |
||
376 |
s := (TextStream ? WriteStream) on:''. |
|
377 |
s nextPutAll: |
|
378 |
'documentation |
|
379 |
'. |
|
380 |
s emphasis:(UserPreferences current commentEmphasisAndColor). |
|
381 |
s nextPutAll:( |
|
382 |
'" |
|
17075 | 383 |
This is an autogenerated documentation template from ',Timestamp now printString,'. |
384 |
You can disable the generation of this template via the settings or by evaluating: |
|
385 |
UserPreferences current showMethodTemplate:false |
|
386 |
||
16355 | 387 |
comment describing this class. |
388 |
||
389 |
[instance variables:] |
|
390 |
describe instance variables |
|
391 |
[class variables:] |
|
392 |
describe class variables |
|
393 |
[see also:] |
|
16779 | 394 |
|
16355 | 395 |
[author:] |
396 |
%1 |
|
397 |
" |
|
398 |
' bindWith:(OperatingSystem getFullUserName)). |
|
399 |
^ s contents |
|
17075 | 400 |
|
401 |
"Modified: / 18-11-2016 / 01:32:06 / cg" |
|
17080 | 402 |
"Modified (comment): / 18-11-2016 / 10:52:32 / cg" |
403 |
! |
|
404 |
||
405 |
methodTemplateForPackageDocumentation |
|
406 |
"return a documentation-method definition template string for packages" |
|
407 |
||
408 |
|s| |
|
409 |
||
410 |
s := (TextStream ? WriteStream) on:''. |
|
411 |
s nextPutAll: |
|
412 |
'documentation |
|
413 |
'. |
|
414 |
s emphasis:(UserPreferences current commentEmphasisAndColor). |
|
415 |
s nextPutAll:( |
|
416 |
'" |
|
417 |
This is an autogenerated documentation template from ',Timestamp now printString,'. |
|
418 |
You can disable the generation of this template via the settings or by evaluating: |
|
419 |
UserPreferences current showMethodTemplate:false |
|
420 |
||
421 |
Package Documentation |
|
422 |
||
423 |
[see also:] |
|
424 |
||
425 |
[author:] |
|
426 |
%1 |
|
427 |
||
428 |
[primary maintainer:] |
|
429 |
%1 |
|
430 |
" |
|
431 |
' bindWith:(OperatingSystem getFullUserName)). |
|
432 |
^ s contents |
|
433 |
||
434 |
"Created: / 18-11-2016 / 10:49:08 / cg" |
|
16355 | 435 |
! |
436 |
||
437 |
methodTemplateForVersionMethodCVS |
|
438 |
"careful to avoid expansion by cvs here!!" |
|
439 |
||
440 |
^ ('version_CVS\ ^ ''$' , 'Header$''') withCRs |
|
441 |
||
442 |
"Created: / 21-08-2012 / 11:52:27 / cg" |
|
443 |
! ! |
|
444 |
||
9706 | 445 |
!SmalltalkCodeGeneratorTool methodsFor:'code generation'! |
446 |
||
447 |
createClassInitializeMethodIn:aClass |
|
448 |
"create a #initialize method on the class side (I'm tired of typing)" |
|
449 |
||
450 |
|nonMetaClass metaClass className code initializer bindings| |
|
451 |
||
452 |
nonMetaClass := aClass theNonMetaclass. |
|
453 |
metaClass := aClass theMetaclass. |
|
454 |
className := nonMetaClass name. |
|
455 |
||
456 |
self startCollectChanges. |
|
457 |
||
458 |
(metaClass includesSelector:#'initialize') ifFalse:[ |
|
459 |
bindings := Dictionary new. |
|
460 |
bindings at:'INIT_CLASSINSTVARS' put:( |
|
461 |
String streamContents:[:s | |
|
462 |
metaClass instVarNames do:[:eachClassInstVar | |
|
463 |
initializer := 'nil'. "/ need more intelligence here (try to guess class from messages sent to it) ... |
|
464 |
s nextPutLine:(' "/ %1 := %2.' bindWith:eachClassInstVar with:initializer). |
|
465 |
] |
|
466 |
]). |
|
467 |
||
468 |
bindings at:'INIT_CLASSVARS' put:( |
|
469 |
String streamContents:[:s | |
|
470 |
nonMetaClass classVarNames do:[:eachClassVar | |
|
471 |
initializer := 'nil'. "/ need more intelligence here (try to guess class from messages sent to it) ... |
|
472 |
s nextPutLine:(' "/ %1 := %2.' bindWith:eachClassVar with:initializer). |
|
473 |
] |
|
474 |
]). |
|
475 |
||
476 |
code := (self codeFor_classInitialize) expandPlaceholdersWith:bindings. |
|
477 |
||
478 |
self |
|
479 |
compile:code |
|
480 |
forClass:metaClass |
|
11523 | 481 |
inCategory:#initialization. |
9706 | 482 |
]. |
483 |
||
484 |
self executeCollectedChangesNamed:('Add Class Initializer to ' , className). |
|
485 |
! |
|
486 |
||
487 |
createClassTypeTestMethodsIn:aClass forClasses:subClasses |
|
488 |
"create a #isXXX test methods (I'm tired of typing)" |
|
489 |
||
490 |
| code| |
|
491 |
||
492 |
self startCollectChanges. |
|
493 |
||
494 |
subClasses do:[:eachSubClass | |
|
495 |
|nm selector| |
|
496 |
||
497 |
nm := eachSubClass nameWithoutPrefix. |
|
498 |
selector := 'is' , nm. |
|
499 |
(aClass includesSelector:selector) ifFalse:[ |
|
500 |
code := (selector , '\ ^ false') withCRs. |
|
501 |
self |
|
502 |
compile:code |
|
503 |
forClass:aClass |
|
11523 | 504 |
inCategory:#testing. |
9706 | 505 |
]. |
506 |
(eachSubClass includesSelector:selector) ifFalse:[ |
|
507 |
code := (selector , '\ ^ true') withCRs. |
|
508 |
self |
|
509 |
compile:code |
|
510 |
forClass:eachSubClass |
|
11523 | 511 |
inCategory:#testing. |
9706 | 512 |
]. |
513 |
]. |
|
514 |
||
515 |
self executeCollectedChangesNamed:'Add ClassType Tests' |
|
516 |
! |
|
517 |
||
518 |
createEnumTypeCodeFor:aClass |
|
519 |
|nonMetaClass metaClass className enumValues code initCode runValue maxValue| |
|
520 |
||
521 |
self startCollectChanges. |
|
522 |
||
523 |
nonMetaClass := aClass theNonMetaclass. |
|
524 |
metaClass := aClass theMetaclass. |
|
525 |
className := nonMetaClass name. |
|
526 |
||
527 |
enumValues := nonMetaClass classVarNames. |
|
528 |
enumValues do:[:eachVariableName | |
|
529 |
self |
|
530 |
createAccessMethodsFor:(Array with:eachVariableName) |
|
531 |
in:metaClass |
|
532 |
withChange:false |
|
533 |
asValueHolder:false |
|
534 |
readersOnly:true |
|
535 |
writersOnly:false |
|
536 |
]. |
|
537 |
||
538 |
maxValue := enumValues |
|
539 |
inject:0 |
|
540 |
into:[:maxSoFar :eachVariableName | |
|
541 |
|oldVal val| |
|
542 |
oldVal := nonMetaClass classVarAt:eachVariableName. |
|
543 |
oldVal notNil ifTrue:[ val := oldVal numericValue ]. |
|
544 |
(val ? maxSoFar) max:maxSoFar |
|
545 |
]. |
|
546 |
||
16878 | 547 |
initCode := WriteStream on:''. |
9706 | 548 |
initCode nextPutLine:'initialize'. |
549 |
runValue := maxValue + 1. |
|
550 |
enumValues keysAndValuesDo:[:idx :eachVariableName | |
|
551 |
|oldValue thisValue| |
|
552 |
||
553 |
oldValue := nonMetaClass classVarAt:eachVariableName. |
|
554 |
oldValue notNil ifTrue:[ |
|
555 |
thisValue := oldValue numericValue. |
|
556 |
] ifFalse:[ |
|
557 |
thisValue := runValue. |
|
558 |
runValue := runValue + 1. |
|
559 |
]. |
|
560 |
initCode |
|
561 |
nextPutAll:' '; |
|
562 |
nextPutAll:eachVariableName; |
|
563 |
nextPutAll:' := self basicNew'. |
|
564 |
(aClass canUnderstand:#'setNumericValue:') ifTrue:[ |
|
565 |
initCode nextPutAll:' setNumericValue: ',thisValue printString. |
|
566 |
]. |
|
567 |
(aClass canUnderstand:#'setCssClassString:') ifTrue:[ |
|
568 |
initCode nextPutAll:('; setCssClassString: ''' , nonMetaClass nameWithoutPrefix asLowercaseFirst , eachVariableName , ''''). |
|
569 |
]. |
|
570 |
(aClass canUnderstand:#'setName:') ifTrue:[ |
|
571 |
initCode nextPutAll:('; setName: ''' , eachVariableName asLowercaseFirst , ''''). |
|
572 |
]. |
|
573 |
initCode nextPutLine:'.'. |
|
574 |
]. |
|
575 |
initCode cr. |
|
576 |
initCode nextPutLine:' "'. |
|
577 |
initCode nextPutLine:' ',className, ' initialize'. |
|
578 |
initCode nextPutLine:' "'. |
|
579 |
||
580 |
self |
|
581 |
compile:(initCode contents) |
|
582 |
forClass:metaClass |
|
11523 | 583 |
inCategory:#'class initialization'. |
9706 | 584 |
|
585 |
||
586 |
code := 'allStateNames\ ^ #( ' , |
|
587 |
((enumValues collect:[:each | '#''',each asLowercaseFirst,'''']) asStringWith:' ') , ')', |
|
588 |
'\\ "\' , |
|
589 |
' ',className, ' allStateNames\' , |
|
590 |
' "\'. |
|
591 |
self |
|
592 |
compile:code withCRs |
|
593 |
forClass:metaClass |
|
11523 | 594 |
inCategory:#queries. |
9706 | 595 |
|
596 |
self executeCollectedChangesNamed:('Generate EnumType Code for ' , className). |
|
597 |
||
598 |
aClass initialize. |
|
599 |
||
600 |
"Modified: / 1.2.1998 / 16:10:03 / cg" |
|
601 |
! |
|
602 |
||
603 |
createExamplesMethodForViewClass:aClass |
|
604 |
"create an examples method" |
|
605 |
||
606 |
|nonMetaClass metaClass className code| |
|
607 |
||
608 |
nonMetaClass := aClass theNonMetaclass. |
|
609 |
metaClass := aClass theMetaclass. |
|
610 |
className := nonMetaClass name. |
|
611 |
||
612 |
self startCollectChanges. |
|
613 |
||
614 |
(metaClass includesSelector:#examples) ifFalse:[ |
|
16779 | 615 |
code := |
9706 | 616 |
'examples |
617 |
" |
|
618 |
Notice that everything between [exBegin] and [exEnd] is extracted by the html-doc generator |
|
619 |
to create nicely formatted and clickable executable examples in the generated html-doc. |
|
620 |
(see the browsers class-documentation menu items for more) |
|
621 |
||
622 |
trying the widget as standAlone view: |
|
623 |
[exBegin] |
|
624 |
%1 new open |
|
625 |
[exEnd] |
|
626 |
||
627 |
embedded in another view: |
|
628 |
[exBegin] |
|
629 |
|top v| |
|
630 |
||
631 |
top := StandardSystemView new. |
|
632 |
top extent:300@300. |
|
633 |
v := %1 new. |
|
634 |
v origin:10@10 corner:150@150. |
|
635 |
top add:v. |
|
636 |
top open |
|
637 |
[exEnd] |
|
638 |
" |
|
16779 | 639 |
' bindWith:className. |
640 |
||
641 |
self |
|
9706 | 642 |
compile:code |
16779 | 643 |
forClass:metaClass |
11523 | 644 |
inCategory:#documentation. |
9706 | 645 |
]. |
646 |
||
647 |
self executeCollectedChangesNamed:('Add Example to ' , className). |
|
648 |
! |
|
649 |
||
650 |
createInitializationMethodIn:aClass |
|
651 |
"create a #initialize methods (I'm tired of typing)" |
|
652 |
||
653 |
|nonMetaClass metaClass className code initializer m| |
|
654 |
||
655 |
nonMetaClass := aClass theNonMetaclass. |
|
656 |
metaClass := aClass theMetaclass. |
|
657 |
className := nonMetaClass name. |
|
658 |
||
659 |
self startCollectChanges. |
|
660 |
||
11523 | 661 |
(nonMetaClass includesSelector:#initialize) ifFalse:[ |
9706 | 662 |
code := |
663 |
'initialize |
|
664 |
"Invoked when a new instance is created." |
|
665 |
||
666 |
"/ please change as required (and remove this comment) |
|
667 |
'. |
|
668 |
||
669 |
nonMetaClass instVarNames do:[:eachInstVar | |
|
670 |
initializer := 'nil'. "/ need more intelligence here (try to guess class from messages sent to it) ... |
|
671 |
code := code , (' "/ ' , eachInstVar , ' := ' , initializer , '.' , Character cr). |
|
672 |
]. |
|
673 |
||
674 |
m := nonMetaClass responseTo:#initialize. |
|
675 |
(m notNil and:[m messagesSent size == 0]) ifTrue:[ |
|
676 |
"/ inherits an empty initialize. |
|
677 |
||
678 |
code := code , ' |
|
679 |
"/ super initialize. -- commented since inherited method does nothing |
|
680 |
'. |
|
681 |
] ifFalse:[ |
|
682 |
code := code , ' |
|
683 |
super initialize. |
|
684 |
'. |
|
685 |
]. |
|
686 |
||
687 |
self |
|
688 |
compile:code |
|
689 |
forClass:nonMetaClass |
|
11523 | 690 |
inCategory:#initialization. |
9706 | 691 |
]. |
692 |
||
693 |
self executeCollectedChangesNamed:('Add Initialized Instance Creation to ' , className). |
|
694 |
! |
|
695 |
||
696 |
createInitializedInstanceCreationMethodsIn:aClass |
|
697 |
"create a #new and #initialize methods (I'm tired of typing)" |
|
698 |
||
699 |
|nonMetaClass metaClass className code m| |
|
700 |
||
701 |
nonMetaClass := aClass theNonMetaclass. |
|
702 |
metaClass := aClass theMetaclass. |
|
703 |
className := nonMetaClass name. |
|
704 |
||
705 |
self startCollectChanges. |
|
706 |
||
707 |
self createInitializationMethodIn:aClass. |
|
708 |
||
11523 | 709 |
(metaClass includesSelector:#new) ifFalse:[ |
9706 | 710 |
m := metaClass responseTo:#new. |
711 |
(m isNil |
|
17330 | 712 |
or:[ (m sendsSelector:#initialize) not |
9706 | 713 |
or:[ |
714 |
(Dialog |
|
9720
1ea182385871
changed: #createInitializedInstanceCreationMethodsIn:
Claus Gittinger <cg@exept.de>
parents:
9706
diff
changeset
|
715 |
confirmWithCancel:('The inherited #new method already seems to invoke #initialize.\Redefine ?' withCRs) |
1ea182385871
changed: #createInitializedInstanceCreationMethodsIn:
Claus Gittinger <cg@exept.de>
parents:
9706
diff
changeset
|
716 |
default:false |
1ea182385871
changed: #createInitializedInstanceCreationMethodsIn:
Claus Gittinger <cg@exept.de>
parents:
9706
diff
changeset
|
717 |
onCancel:[^ self] ) |
1ea182385871
changed: #createInitializedInstanceCreationMethodsIn:
Claus Gittinger <cg@exept.de>
parents:
9706
diff
changeset
|
718 |
]]) ifTrue:[ |
9706 | 719 |
code := |
720 |
'new |
|
721 |
"return an initialized instance" |
|
722 |
||
723 |
^ self basicNew initialize. |
|
724 |
'. |
|
725 |
self |
|
726 |
compile:code |
|
727 |
forClass:metaClass |
|
11523 | 728 |
inCategory:#'instance creation'. |
9706 | 729 |
]. |
730 |
]. |
|
731 |
||
732 |
self executeCollectedChangesNamed:('Add Initialized Instance Creation to ' , className). |
|
733 |
||
9720
1ea182385871
changed: #createInitializedInstanceCreationMethodsIn:
Claus Gittinger <cg@exept.de>
parents:
9706
diff
changeset
|
734 |
"Created: / 11-10-2001 / 22:18:55 / cg" |
17330 | 735 |
"Modified: / 05-02-2017 / 01:25:06 / cg" |
9706 | 736 |
! |
737 |
||
13628
a718ccbff353
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
13591
diff
changeset
|
738 |
createIsAbstractMethodIn:aClass |
a718ccbff353
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
13591
diff
changeset
|
739 |
"create a #isAbstract query method (I'm tired of typing)" |
a718ccbff353
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
13591
diff
changeset
|
740 |
|
a718ccbff353
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
13591
diff
changeset
|
741 |
|metaClass className code| |
a718ccbff353
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
13591
diff
changeset
|
742 |
|
a718ccbff353
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
13591
diff
changeset
|
743 |
metaClass := aClass theMetaclass. |
a718ccbff353
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
13591
diff
changeset
|
744 |
className := aClass theNonMetaclass name. |
a718ccbff353
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
13591
diff
changeset
|
745 |
|
a718ccbff353
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
13591
diff
changeset
|
746 |
self startCollectChanges. |
a718ccbff353
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
13591
diff
changeset
|
747 |
|
a718ccbff353
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
13591
diff
changeset
|
748 |
(metaClass includesSelector:#isAbstract) ifFalse:[ |
a718ccbff353
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
13591
diff
changeset
|
749 |
code := |
a718ccbff353
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
13591
diff
changeset
|
750 |
'isAbstract |
a718ccbff353
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
13591
diff
changeset
|
751 |
"Return if this class is an abstract class. |
a718ccbff353
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
13591
diff
changeset
|
752 |
True is returned here for myself only; false for subclasses. |
16195 | 753 |
Abstract subclasses must redefine this again." |
13628
a718ccbff353
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
13591
diff
changeset
|
754 |
|
a718ccbff353
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
13591
diff
changeset
|
755 |
^ self == ',className,'. |
a718ccbff353
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
13591
diff
changeset
|
756 |
'. |
a718ccbff353
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
13591
diff
changeset
|
757 |
self |
a718ccbff353
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
13591
diff
changeset
|
758 |
compile:code |
a718ccbff353
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
13591
diff
changeset
|
759 |
forClass:metaClass |
a718ccbff353
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
13591
diff
changeset
|
760 |
inCategory:#'queries'. |
a718ccbff353
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
13591
diff
changeset
|
761 |
]. |
a718ccbff353
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
13591
diff
changeset
|
762 |
self executeCollectedChangesNamed:('Make ',className,' abstract'). |
a718ccbff353
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
13591
diff
changeset
|
763 |
! |
a718ccbff353
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
13591
diff
changeset
|
764 |
|
9706 | 765 |
createParametrizedInstanceCreationMethodsNamed:selector in:aClass |
766 |
"create a #selector instance creation method (I'm tired of typing)" |
|
767 |
||
768 |
|nonMetaClass metaClass className code initializer m dfn| |
|
769 |
||
770 |
dfn := Method methodDefinitionTemplateForSelector:selector. |
|
771 |
||
772 |
nonMetaClass := aClass theNonMetaclass. |
|
773 |
metaClass := aClass theMetaclass. |
|
774 |
className := nonMetaClass name. |
|
775 |
||
776 |
self startCollectChanges. |
|
777 |
||
778 |
(nonMetaClass includesSelector:selector asSymbol) ifFalse:[ |
|
779 |
code := |
|
780 |
'initialize',dfn asUppercaseFirst,' |
|
781 |
"Invoked when a new instance is created for arg." |
|
782 |
||
783 |
"/ please change as required (and remove these comments) |
|
784 |
"/ do something with arg here (instVar-foo := arg) |
|
785 |
'. |
|
786 |
nonMetaClass instVarNames do:[:eachInstVar | |
|
787 |
initializer := 'nil'. "/ need more intelligence here (try to guess class from messages sent to it) ... |
|
788 |
code := code , (' "/ ' , eachInstVar , ' := ' , initializer , '.' , Character cr). |
|
789 |
]. |
|
790 |
||
791 |
m := nonMetaClass responseTo:#initialize. |
|
792 |
(m notNil and:[ m messagesSent size == 0 ]) ifTrue:[ |
|
793 |
"/ inherits an empty initialize. |
|
794 |
||
795 |
code := code , ' |
|
796 |
"/ super initialize. -- commented since inherited method does nothing |
|
797 |
'. |
|
798 |
] ifFalse:[ |
|
799 |
code := code , ' |
|
800 |
super initialize. |
|
801 |
'. |
|
802 |
]. |
|
803 |
||
804 |
self |
|
805 |
compile:code |
|
806 |
forClass:nonMetaClass |
|
11523 | 807 |
inCategory:#initialization. |
9706 | 808 |
]. |
809 |
||
810 |
(metaClass includesSelector:selector) ifFalse:[ |
|
811 |
m := metaClass responseTo:selector. |
|
812 |
(m isNil |
|
813 |
or:[ (Dialog confirmWithCancel:'The ',selector,'- method is already inherited. Redefine ?' onCancel:[^ self]) ]) |
|
814 |
ifTrue:[ |
|
815 |
code := |
|
816 |
dfn,' |
|
817 |
"Create & return a new instance for arg." |
|
818 |
||
819 |
^ self basicNew initialize',dfn asUppercaseFirst,' |
|
820 |
'. |
|
821 |
self |
|
822 |
compile:code |
|
823 |
forClass:metaClass |
|
11523 | 824 |
inCategory:#'instance creation'. |
9706 | 825 |
]. |
826 |
]. |
|
827 |
||
828 |
self executeCollectedChangesNamed:('Add Parametrized Instance Creation to ' , className). |
|
829 |
! |
|
830 |
||
831 |
createPoolInitializationCodeFor:aClass |
|
11523 | 832 |
|nonMetaClass metaClass className poolVars initCode| |
9706 | 833 |
|
834 |
self startCollectChanges. |
|
835 |
||
836 |
nonMetaClass := aClass theNonMetaclass. |
|
837 |
metaClass := aClass theMetaclass. |
|
838 |
className := nonMetaClass name. |
|
839 |
||
840 |
poolVars := nonMetaClass classVarNames. |
|
841 |
||
16878 | 842 |
initCode := WriteStream on:''. |
9706 | 843 |
initCode nextPutLine:'initialize'. |
844 |
||
845 |
poolVars do:[:eachVariableName | |
|
846 |
|oldValue thisValue| |
|
847 |
||
848 |
oldValue := nonMetaClass classVarAt:eachVariableName. |
|
849 |
oldValue notNil ifTrue:[ |
|
850 |
thisValue := oldValue. |
|
851 |
] ifFalse:[ |
|
852 |
thisValue := nil. |
|
853 |
]. |
|
854 |
initCode |
|
855 |
nextPutAll:' '; |
|
856 |
nextPutAll:eachVariableName; |
|
857 |
nextPutAll:' := '; |
|
858 |
nextPutAll:thisValue storeString; |
|
859 |
nextPutLine:'.'. |
|
860 |
]. |
|
861 |
initCode cr. |
|
862 |
initCode nextPutLine:' "'. |
|
863 |
initCode nextPutLine:' ',className, ' initialize'. |
|
864 |
initCode nextPutLine:' "'. |
|
865 |
||
866 |
self |
|
867 |
compile:(initCode contents) |
|
868 |
forClass:metaClass |
|
11523 | 869 |
inCategory:#'class initialization'. |
9706 | 870 |
|
871 |
self executeCollectedChangesNamed:('Generate Pool Initialization Code for ' , className). |
|
872 |
||
873 |
aClass initialize. |
|
874 |
||
875 |
"Created: / 25-10-2006 / 09:28:40 / cg" |
|
876 |
! |
|
877 |
||
878 |
createRedefinedInstanceCreationMethodsIn:aClass |
|
879 |
"create a redefined #new methods" |
|
880 |
||
881 |
|nonMetaClass metaClass className code| |
|
882 |
||
883 |
nonMetaClass := aClass theNonMetaclass. |
|
884 |
metaClass := aClass theMetaclass. |
|
885 |
className := nonMetaClass name. |
|
886 |
||
887 |
self startCollectChanges. |
|
888 |
||
889 |
(metaClass includesSelector:#'new') ifFalse:[ |
|
890 |
code := |
|
891 |
'new |
|
892 |
^ super new. |
|
893 |
'. |
|
894 |
self |
|
895 |
compile:code |
|
896 |
forClass:metaClass |
|
11523 | 897 |
inCategory:#'redefined instance creation'. |
9706 | 898 |
]. |
899 |
(metaClass includesSelector:#'new:') ifFalse:[ |
|
900 |
code := |
|
901 |
'new:n |
|
902 |
^ super new:n. |
|
903 |
'. |
|
904 |
self |
|
905 |
compile:code |
|
906 |
forClass:metaClass |
|
11523 | 907 |
inCategory:#'redefined instance creation'. |
9706 | 908 |
]. |
909 |
||
910 |
self executeCollectedChangesNamed:('Redefined Instance Creation to ' , className). |
|
911 |
! |
|
912 |
||
9756
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
913 |
createSingletonPatternInstanceCreationMethodsIn:aClass usingVariable:varName |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
914 |
"create redefined #new methods for singleton pattern. |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
915 |
Uses varName (typically a class-instvar) to keep the singleton instance." |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
916 |
|
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
917 |
|nonMetaClass metaClass className code| |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
918 |
|
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
919 |
nonMetaClass := aClass theNonMetaclass. |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
920 |
metaClass := aClass theMetaclass. |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
921 |
className := nonMetaClass name. |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
922 |
|
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
923 |
self startCollectChanges. |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
924 |
|
11523 | 925 |
(metaClass includesSelector:#theOneAndOnlyInstance) ifFalse:[ |
9756
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
926 |
code := |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
927 |
'theOneAndOnlyInstance |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
928 |
"returns a singleton" |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
929 |
|
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
930 |
%1 isNil ifTrue:[ |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
931 |
%1 := self basicNew initialize. |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
932 |
]. |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
933 |
^ %1. |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
934 |
'. |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
935 |
self |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
936 |
compile:(code bindWith:varName) |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
937 |
forClass:metaClass |
11523 | 938 |
inCategory:#'instance creation'. |
9756
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
939 |
]. |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
940 |
|
11523 | 941 |
(metaClass includesSelector:#new) ifFalse:[ |
9756
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
942 |
code := |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
943 |
'new |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
944 |
"returns a singleton" |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
945 |
|
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
946 |
^ self theOneAndOnlyInstance. |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
947 |
'. |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
948 |
self |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
949 |
compile:(code bindWith:varName) |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
950 |
forClass:metaClass |
11523 | 951 |
inCategory:#'instance creation'. |
9756
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
952 |
]. |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
953 |
|
11523 | 954 |
(metaClass includesSelector:#flushSingleton) ifFalse:[ |
9756
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
955 |
code := |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
956 |
'flushSingleton |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
957 |
"flushes the cached singleton" |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
958 |
|
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
959 |
%1 := nil |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
960 |
|
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
961 |
" |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
962 |
self flushSingleton |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
963 |
" |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
964 |
'. |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
965 |
self |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
966 |
compile:(code bindWith:varName) |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
967 |
forClass:metaClass |
11523 | 968 |
inCategory:#'instance creation'. |
9756
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
969 |
]. |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
970 |
|
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
971 |
self executeCollectedChangesNamed:('Singleton Pattern for ' , className). |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
972 |
|
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
973 |
"Created: / 10-02-2011 / 16:32:48 / cg" |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
974 |
! |
5a1787bbf85e
added: #classMenuGenerateSingletonPatternInstanceCreationMethods
Claus Gittinger <cg@exept.de>
parents:
9720
diff
changeset
|
975 |
|
9706 | 976 |
createStandardPrintOnMethodIn:aClass |
977 |
"create a #printOn: method (I'm tired of typing)" |
|
978 |
||
979 |
|code nonMetaClass| |
|
980 |
||
981 |
nonMetaClass := aClass theNonMetaclass. |
|
982 |
||
983 |
self startCollectChanges. |
|
984 |
||
11523 | 985 |
(nonMetaClass includesSelector:#printOn:) ifFalse:[ |
9706 | 986 |
code := |
987 |
'printOn:aStream |
|
16786 | 988 |
"append a printed representation of the receiver to the argument, aStream" |
9706 | 989 |
|
990 |
super printOn:aStream. |
|
991 |
'. |
|
992 |
nonMetaClass instVarNames do:[:eachInstVarName | |
|
993 |
code := code , ' '. |
|
994 |
code := code , 'aStream nextPutAll:'''. |
|
995 |
code := code , eachInstVarName. |
|
996 |
code := code , ': ''.' , Character cr. |
|
997 |
code := code , ' '. |
|
998 |
code := code , eachInstVarName. |
|
999 |
code := code , ' printOn:aStream.' , Character cr. |
|
16786 | 1000 |
|
9706 | 1001 |
]. |
1002 |
||
16786 | 1003 |
self |
9706 | 1004 |
compile:code |
16786 | 1005 |
forClass:nonMetaClass |
11523 | 1006 |
inCategory:#'printing & storing'. |
9706 | 1007 |
]. |
1008 |
||
1009 |
||
1010 |
self executeCollectedChangesNamed:('Add #printOn: to ' , nonMetaClass name). |
|
1011 |
||
1012 |
"Created: / 11.10.2001 / 22:18:55 / cg" |
|
1013 |
! |
|
1014 |
||
1015 |
createStartupCodeFor:aClass forStartOf:anApplicationClassOrNil |
|
1016 |
"create startup code (main)" |
|
1017 |
||
1018 |
|nonMetaClass metaClass className source |
|
1019 |
hasAplicationClass anApplicationClassNameOrStartupClassName| |
|
1020 |
||
1021 |
self startCollectChanges. |
|
1022 |
||
1023 |
hasAplicationClass := anApplicationClassOrNil notNil. |
|
1024 |
nonMetaClass := aClass theNonMetaclass. |
|
1025 |
metaClass := aClass theMetaclass. |
|
1026 |
className := nonMetaClass name. |
|
1027 |
||
1028 |
(metaClass includesSelector:#main:) ifFalse:[ |
|
1029 |
||
1030 |
source := String streamContents:[:stream | |
|
1031 |
stream nextPutAll: |
|
1032 |
'main:argv |
|
1033 |
self verboseInfo:''starting %1''. |
|
1034 |
||
1035 |
'. |
|
1036 |
hasAplicationClass ifTrue: [ |
|
1037 |
stream nextPutAll: |
|
1038 |
' Smalltalk openDisplay. |
|
1039 |
Display notNil ifTrue:[ |
|
1040 |
Display exitOnLastClose:true. |
|
1041 |
]. |
|
1042 |
%1 open. |
|
1043 |
'. |
|
12458
ea3f2adb406f
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12440
diff
changeset
|
1044 |
] ifFalse:[ |
ea3f2adb406f
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12440
diff
changeset
|
1045 |
stream nextPutAll: |
ea3f2adb406f
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12440
diff
changeset
|
1046 |
' "/ replace by real code |
ea3f2adb406f
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12440
diff
changeset
|
1047 |
Stdout nextPutLine:''Hello World''. |
ea3f2adb406f
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12440
diff
changeset
|
1048 |
'. |
9706 | 1049 |
]. |
1050 |
]. |
|
1051 |
||
1052 |
anApplicationClassNameOrStartupClassName := hasAplicationClass |
|
1053 |
ifTrue: [anApplicationClassOrNil name] |
|
1054 |
ifFalse: [className.]. |
|
1055 |
self |
|
1056 |
compile:(source bindWith:anApplicationClassNameOrStartupClassName) |
|
1057 |
forClass:metaClass |
|
11523 | 1058 |
inCategory:#startup. |
9706 | 1059 |
]. |
1060 |
self executeCollectedChangesNamed:('Add Startup Code to ' , className). |
|
1061 |
! |
|
1062 |
||
1063 |
createTestCaseSampleCodeFor:aClass |
|
1064 |
"create an (almost) empty testCase class" |
|
1065 |
||
1066 |
|nonMetaClass metaClass| |
|
1067 |
||
1068 |
nonMetaClass := aClass theNonMetaclass. |
|
1069 |
metaClass := aClass theMetaclass. |
|
1070 |
"/ className := nonMetaClass name. |
|
1071 |
||
1072 |
( nonMetaClass includesSelector:#test1 ) ifFalse:[ |
|
1073 |
self |
|
1074 |
compile: |
|
1075 |
'test1 |
|
1076 |
"This is a demonstration testCase - it is meant to be removed eventually. |
|
1077 |
This testCase will PASS. |
|
1078 |
Double click on the TestCase class or open a TestRunner to see me checking... |
|
1079 |
- please add more methods like this..." |
|
1080 |
||
1081 |
|o| |
|
1082 |
||
1083 |
o := Array new:2. |
|
1084 |
self assert: ( o size == 2 ). |
|
1085 |
self should: [ o at:0 ] raise:Error. |
|
1086 |
self shouldnt: [ o at:1 ] raise:Error. |
|
1087 |
||
1088 |
" |
|
1089 |
self run:#test1 |
|
1090 |
self new test1 |
|
1091 |
" |
|
1092 |
' |
|
1093 |
forClass:nonMetaClass |
|
11523 | 1094 |
inCategory:#tests. |
9706 | 1095 |
]. |
1096 |
||
1097 |
( nonMetaClass includesSelector:#test2 ) ifFalse:[ |
|
1098 |
self |
|
1099 |
compile: |
|
1100 |
'test2 |
|
1101 |
"This is a demonstration testCase - it is meant to be removed eventually.. |
|
1102 |
This testCase WILL FAIL. |
|
1103 |
Double click on the TestCase class or open a TestRunner to see me checking... |
|
1104 |
- please add more methods like this..." |
|
1105 |
||
1106 |
|o| |
|
1107 |
||
1108 |
o := Array new:2. |
|
1109 |
self assert: ( o size == 3 ). |
|
1110 |
||
1111 |
" |
|
1112 |
self run:#test2 |
|
1113 |
self new test2 |
|
1114 |
" |
|
1115 |
' |
|
1116 |
forClass:nonMetaClass |
|
11523 | 1117 |
inCategory:#tests. |
9706 | 1118 |
]. |
1119 |
||
1120 |
( nonMetaClass includesSelector:#test3 ) ifFalse:[ |
|
1121 |
self |
|
1122 |
compile: |
|
1123 |
'test3 |
|
1124 |
"This is a demonstration testCase - it is meant to be removed eventually.. |
|
1125 |
This testCase WILL generate an ERROR. |
|
1126 |
Double click on the TestCase class or open a TestRunner to see me checking... |
|
1127 |
- please add more methods like this..." |
|
1128 |
||
1129 |
|o| |
|
1130 |
||
1131 |
o := Array new:2. |
|
1132 |
self assert: ( o foo ). |
|
1133 |
||
1134 |
" |
|
1135 |
self run:#test3 |
|
1136 |
self new test3 |
|
1137 |
" |
|
1138 |
' |
|
1139 |
forClass:nonMetaClass |
|
11523 | 1140 |
inCategory:#tests. |
9706 | 1141 |
]. |
1142 |
||
1143 |
( nonMetaClass includesSelector:#setUp ) ifFalse:[ |
|
1144 |
self |
|
1145 |
compile: |
|
1146 |
'setUp |
|
1147 |
"common setup - invoked before testing." |
|
1148 |
||
1149 |
super setUp |
|
1150 |
' |
|
1151 |
forClass:nonMetaClass |
|
11523 | 1152 |
inCategory:#'initialize / release'. |
9706 | 1153 |
]. |
1154 |
||
1155 |
( nonMetaClass includesSelector:#tearDown ) ifFalse:[ |
|
1156 |
self |
|
1157 |
compile: |
|
1158 |
'tearDown |
|
1159 |
"common cleanup - invoked after testing." |
|
1160 |
||
1161 |
super tearDown |
|
1162 |
' |
|
1163 |
forClass:nonMetaClass |
|
11523 | 1164 |
inCategory:#'initialize / release'. |
9706 | 1165 |
] |
1166 |
! |
|
1167 |
||
1168 |
createVisitorMethodsIn:visitedClass andVisitorClass:visitorClass |
|
1169 |
"create acceptVisitor: in visitedClass and acceptXXX in visitorClass. (I'm tired of typing)" |
|
1170 |
||
1171 |
|sel| |
|
1172 |
||
1173 |
self assert:( visitedClass isMeta not ). |
|
1174 |
self assert:( visitorClass isMeta not ). |
|
1175 |
||
1176 |
self startCollectChanges. |
|
1177 |
||
1178 |
sel := ('visit' , visitedClass nameWithoutPrefix , ':'). |
|
1179 |
self createAcceptVisitorMethod:sel in:visitedClass. |
|
1180 |
||
1181 |
(visitorClass includesSelector:sel) ifFalse:[ |
|
16779 | 1182 |
self |
9706 | 1183 |
compile: |
16779 | 1184 |
(('%1anObject |
9706 | 1185 |
"dispatched back from the visited %2-object (visitor pattern)" |
1186 |
||
1187 |
"fall back to general object-case - please change as required" |
|
1188 |
||
1189 |
^ self visitObject:anObject |
|
1190 |
') bindWith:sel with:visitedClass nameWithoutPrefix asLowercaseFirst) |
|
16779 | 1191 |
forClass:visitorClass |
11523 | 1192 |
inCategory:#visiting. |
9706 | 1193 |
]. |
1194 |
||
11523 | 1195 |
(visitorClass includesSelector:#visitObject:) ifFalse:[ |
16779 | 1196 |
self |
9706 | 1197 |
compile: |
16779 | 1198 |
('visitObject:anObject |
9706 | 1199 |
"dispatched back from the visited objects (visitor pattern)" |
1200 |
||
1201 |
"general fallBack - please change as required" |
|
1202 |
||
1203 |
self halt:''not yet implemented'' |
|
1204 |
') |
|
16779 | 1205 |
forClass:visitorClass |
11523 | 1206 |
inCategory:#visiting. |
9706 | 1207 |
]. |
1208 |
||
11523 | 1209 |
(visitorClass includesSelector:#visit:) ifFalse:[ |
16779 | 1210 |
self |
9706 | 1211 |
compile: |
16779 | 1212 |
('visit:anObject |
9706 | 1213 |
"visit anObject (visitor pattern). |
1214 |
The object should call back one of my visitXXXX methods." |
|
1215 |
||
1216 |
^ anObject acceptVisitor:self |
|
1217 |
') |
|
16779 | 1218 |
forClass:visitorClass |
11523 | 1219 |
inCategory:#visiting. |
9706 | 1220 |
]. |
1221 |
||
1222 |
self executeCollectedChangesNamed:('Add Visitor Pattern'). |
|
1223 |
! |
|
1224 |
||
13811 | 1225 |
createVisitorMethodsIn:visitedClass andVisitorClass:visitorClass withParameter:withParameter withSuper:withSuper |
1226 |
" |
|
1227 |
This is much like createVisitorMethodsIn:andVisitorClass:, |
|
1228 |
but: |
|
1229 |
- if withParameter is true, generates |
|
1230 |
#acceptVisitor:with: |
|
1231 |
and |
|
1232 |
#visit<CLASNAME>:with: |
|
1233 |
- if withSuper is true, generates |
|
1234 |
self visit<SUPERCLASSNAME>:[with:] in the visitXXX methods body" |
|
1235 |
||
1236 |
| sel superSel template | |
|
1237 |
||
1238 |
self assert:(visitedClass isMeta not). |
|
1239 |
self assert:(visitorClass isMeta not). |
|
1240 |
self startCollectChanges. |
|
1241 |
sel := ('visit' , visitedClass nameWithoutPrefix , ':'). |
|
1242 |
superSel := ('visit' , visitedClass superclass nameWithoutPrefix , ':'). |
|
1243 |
template := '`@sel: object %1 |
|
1244 |
"Dispatched back from the visited object (visitor pattern)" |
|
1245 |
||
1246 |
^ %2 |
|
1247 |
' |
|
1248 |
bindWith:(withParameter ifTrue:['with: param'] ifFalse:['']) |
|
1249 |
with:(withSuper |
|
1250 |
ifTrue:[ |
|
1251 |
'self `@superSel: anObject' |
|
1252 |
, (withParameter ifTrue:[' with: param'] ifFalse:['']) |
|
1253 |
] |
|
1254 |
ifFalse:['self subclassResponsibility']). |
|
1255 |
self createAcceptVisitorMethod:sel in:visitedClass withParameter: withParameter. |
|
1256 |
(visitorClass includesSelector:sel) ifFalse:[ |
|
1257 |
self addChange:((CodeGenerator new) |
|
1258 |
class:visitorClass; |
|
1259 |
protocol:'visiting'; |
|
1260 |
source:template; |
|
1261 |
replace:'`@sel:' with:sel asSymbol; |
|
1262 |
replace:'`@superSel:' with:superSel asSymbol; |
|
1263 |
change) |
|
1264 |
]. |
|
1265 |
(visitorClass includesSelector:#visit:) ifFalse:[ |
|
1266 |
self |
|
1267 |
compile:('visit:object %1 |
|
1268 |
"visit anObject (visitor pattern). |
|
1269 |
The object should call back one of my visitXXXX methods." |
|
1270 |
||
1271 |
^ object acceptVisitor:self %1 |
|
1272 |
' |
|
1273 |
bindWith:(withParameter ifTrue:['with: parameter'] ifFalse:[''])) |
|
1274 |
forClass:visitorClass |
|
1275 |
inCategory:'visiting'. |
|
1276 |
]. |
|
1277 |
self executeCollectedChangesNamed:('Add Visitor Pattern'). |
|
1278 |
||
1279 |
"Created: / 05-08-2013 / 13:13:09 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
1280 |
! |
|
1281 |
||
9706 | 1282 |
createWebApplicationCodeFor:aClass |
1283 |
"create an empty webApplication framework" |
|
1284 |
||
1285 |
|nonMetaClass metaClass className txt| |
|
1286 |
||
1287 |
self startCollectChanges. |
|
1288 |
||
1289 |
nonMetaClass := aClass theNonMetaclass. |
|
1290 |
metaClass := aClass theMetaclass. |
|
1291 |
className := nonMetaClass name. |
|
1292 |
||
1293 |
(metaClass includesSelector:#pageSpec) ifFalse:[ |
|
1294 |
txt := self class initialPageSpecMethodSourceForWebApplications. |
|
1295 |
self |
|
1296 |
compile:(txt bindWith:className) |
|
1297 |
forClass:metaClass |
|
11523 | 1298 |
inCategory:#'page specs'. |
9706 | 1299 |
]. |
1300 |
||
1301 |
self executeCollectedChangesNamed:('Add WebApplication Code for ' , className). |
|
1302 |
||
1303 |
"Modified: / 1.2.1998 / 16:10:03 / cg" |
|
1304 |
! |
|
1305 |
||
1306 |
createWebServiceCodeFor:aClass |
|
1307 |
"create an empty webService framework" |
|
1308 |
||
1309 |
|nonMetaClass metaClass className txt| |
|
1310 |
||
1311 |
self startCollectChanges. |
|
1312 |
||
1313 |
nonMetaClass := aClass theNonMetaclass. |
|
1314 |
metaClass := aClass theMetaclass. |
|
1315 |
className := nonMetaClass name. |
|
1316 |
||
9934
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1317 |
(nonMetaClass includesSelector:#page1:) ifFalse:[ |
9706 | 1318 |
txt := |
9934
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1319 |
'page1:aRequest |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1320 |
"This is a sample page-generation method. |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1321 |
This uses the simplest possible way to generate html: generating plain HTML. |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1322 |
See page2 for a more structured example." |
9706 | 1323 |
|
15003
95e259bc509f
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
14700
diff
changeset
|
1324 |
<resource: #PAGE> |
95e259bc509f
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
14700
diff
changeset
|
1325 |
|
9706 | 1326 |
|response| |
1327 |
||
1328 |
response := aRequest response. |
|
1329 |
response nextPutLine:''<HTML>''. |
|
1330 |
response nextPutLine:'' <HEAD>''. |
|
1331 |
response nextPutLine:'' <TITLE>Hello</TITLE>''. |
|
1332 |
response nextPutLine:'' </HEAD>''. |
|
1333 |
response nextPutLine:'' <BODY>''. |
|
1334 |
response nextPutLine:'' <H1>Hello World !!</H1>''. |
|
1335 |
response nextPutLine:'' </BODY>''. |
|
1336 |
response nextPutLine:''</HTML>''. |
|
1337 |
'. |
|
1338 |
self |
|
1339 |
compile:txt |
|
1340 |
forClass:nonMetaClass |
|
11523 | 1341 |
inCategory:#'response generation - pages'. |
9934
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1342 |
]. |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1343 |
|
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1344 |
(nonMetaClass includesSelector:#page2:) ifFalse:[ |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1345 |
txt := |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1346 |
'page2:aRequest |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1347 |
"This is a sample page-generation method. |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1348 |
This uses a slightly more convenient way to generate html: using an HTML tree builder. |
15006
51c4a44295be
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15005
diff
changeset
|
1349 |
Even better examples to follow..." |
9934
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1350 |
|
15003
95e259bc509f
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
14700
diff
changeset
|
1351 |
<resource: #PAGE> |
95e259bc509f
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
14700
diff
changeset
|
1352 |
|
9934
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1353 |
|page| |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1354 |
|
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1355 |
page := HTML::TreeBuilder newDocument. |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1356 |
page |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1357 |
head; |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1358 |
title:''Hello''; |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1359 |
headEnd; |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1360 |
body; |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1361 |
h1:''Hello World''; |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1362 |
div; |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1363 |
nlsText:''here is some text and ''; |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1364 |
a; href:''page3''; |
15007
1214b261a086
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15006
diff
changeset
|
1365 |
text:''an anchor to page3''; |
9934
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1366 |
aEnd; |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1367 |
text:'' and an image: ''; |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1368 |
img:(httpServer graphicLinkIdFor:(ToolbarIconLibrary smiley_cool) expirationTimeDelta:30 seconds); |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1369 |
divEnd; |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1370 |
bodyEnd. |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1371 |
|
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1372 |
aRequest reply:(page htmlString). |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1373 |
'. |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1374 |
self |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1375 |
compile:txt |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1376 |
forClass:nonMetaClass |
11523 | 1377 |
inCategory:#'response generation - pages'. |
9934
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1378 |
]. |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1379 |
|
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1380 |
(nonMetaClass includesSelector:#page3:) ifFalse:[ |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1381 |
txt := |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1382 |
'page3:aRequest |
15005
99f17b72f95d
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15004
diff
changeset
|
1383 |
"This is a sample page which generates plain text (i.e. not html)." |
9934
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1384 |
|
15003
95e259bc509f
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
14700
diff
changeset
|
1385 |
<resource: #PAGE> |
95e259bc509f
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
14700
diff
changeset
|
1386 |
|
9934
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1387 |
aRequest response |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1388 |
contentType:''text/plain''; |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1389 |
data:''This is some plain text, |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1390 |
without any html formatting.'' |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1391 |
'. |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1392 |
self |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1393 |
compile:txt |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1394 |
forClass:nonMetaClass |
11523 | 1395 |
inCategory:#'response generation - pages'. |
9934
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1396 |
]. |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1397 |
|
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1398 |
(nonMetaClass includesSelector:#process:) ifFalse:[ |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1399 |
txt := |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1400 |
'process:aRequest |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1401 |
"This is the web services main processing method. |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1402 |
It will be invoked for every incoming webBrowser-request. |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1403 |
The argument, aRequest contains the parameters (url, fields, parameters etc.); browse HTTPRequest for details. |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1404 |
Its response object collects any returned html or other contents. Browse HTTPResponse for more info. |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1405 |
|
15004
0df5dfbfbb1d
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15003
diff
changeset
|
1406 |
Here, a simple dipatch method is used: a hardcoded switch on the URL... |
0df5dfbfbb1d
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15003
diff
changeset
|
1407 |
It uses the page-name directly as selector for the page-generating method. |
0df5dfbfbb1d
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15003
diff
changeset
|
1408 |
Because that would open a possible security hole (incoming url being #halt, for example), |
0df5dfbfbb1d
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15003
diff
changeset
|
1409 |
we only call methods which have been annotated with PAGE (see sample pages)" |
9935
5a0685d2ee90
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9934
diff
changeset
|
1410 |
|
5a0685d2ee90
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9934
diff
changeset
|
1411 |
|whichPage methodName method| |
9934
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1412 |
|
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1413 |
whichPage := aRequest pathRelativeToService. |
9935
5a0685d2ee90
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9934
diff
changeset
|
1414 |
whichPage isEmpty ifTrue:[ whichPage := ''page1'' ]. |
5a0685d2ee90
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9934
diff
changeset
|
1415 |
|
5a0685d2ee90
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9934
diff
changeset
|
1416 |
"/ to prevent intruders from calling any method, check if it one |
5a0685d2ee90
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9934
diff
changeset
|
1417 |
"/ of my own methods, which is marked as a PAGE-method. |
5a0685d2ee90
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9934
diff
changeset
|
1418 |
|
5a0685d2ee90
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9934
diff
changeset
|
1419 |
methodName := (whichPage,'':'') asSymbol. |
5a0685d2ee90
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9934
diff
changeset
|
1420 |
method := self class compiledMethodAt:methodName. |
5a0685d2ee90
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9934
diff
changeset
|
1421 |
"/ is there such a method ? |
5a0685d2ee90
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9934
diff
changeset
|
1422 |
method notNil ifTrue:[ |
5a0685d2ee90
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9934
diff
changeset
|
1423 |
"/ and it is marked as being a WebPage-generator ? |
5a0685d2ee90
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9934
diff
changeset
|
1424 |
(method hasResource:#''PAGE'') ifTrue:[ |
5a0685d2ee90
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9934
diff
changeset
|
1425 |
self perform:methodName with:aRequest. |
5a0685d2ee90
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9934
diff
changeset
|
1426 |
^ self |
5a0685d2ee90
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9934
diff
changeset
|
1427 |
]. |
9934
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1428 |
]. |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1429 |
aRequest reportNotFound:''No such page in this service''. |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1430 |
'. |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1431 |
self |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1432 |
compile:txt |
e8885f059bea
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9756
diff
changeset
|
1433 |
forClass:nonMetaClass |
11523 | 1434 |
inCategory:#'response generation'. |
9706 | 1435 |
]. |
1436 |
||
1437 |
(metaClass includesSelector:#linkName) ifFalse:[ |
|
1438 |
txt := |
|
1439 |
'linkName |
|
1440 |
"return the default linkName path (with slash)." |
|
1441 |
||
1442 |
^ ''/NewService'' |
|
1443 |
'. |
|
1444 |
self |
|
1445 |
compile:txt |
|
1446 |
forClass:metaClass |
|
11523 | 1447 |
inCategory:#defaults. |
9706 | 1448 |
]. |
1449 |
||
1450 |
(metaClass includesSelector:#settingsApplicationClass) ifFalse:[ |
|
1451 |
txt := |
|
1452 |
'settingsApplicationClass |
|
1453 |
"a SettingsApplication class - or nil (used in the settings dialog if non-nil)." |
|
1454 |
||
1455 |
^ nil |
|
1456 |
'. |
|
1457 |
self |
|
1458 |
compile:txt |
|
1459 |
forClass:metaClass |
|
11523 | 1460 |
inCategory:#defaults. |
9706 | 1461 |
]. |
1462 |
||
15067
dce943cf284f
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15007
diff
changeset
|
1463 |
(nonMetaClass includesSelector:#addRequiredForeignServicesTo:) ifFalse:[ |
dce943cf284f
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15007
diff
changeset
|
1464 |
txt := |
dce943cf284f
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15007
diff
changeset
|
1465 |
'addRequiredForeignServicesTo:aServer |
dce943cf284f
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15007
diff
changeset
|
1466 |
"add any additional (usually file-) services" |
dce943cf284f
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15007
diff
changeset
|
1467 |
|
dce943cf284f
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15007
diff
changeset
|
1468 |
"/ uncomment to install additional standard file services |
dce943cf284f
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15007
diff
changeset
|
1469 |
"/ self addStandardFileServicesTo:aServer. |
dce943cf284f
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15007
diff
changeset
|
1470 |
|
dce943cf284f
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15007
diff
changeset
|
1471 |
"/ uncomment and add an instance variable named ''tentativeObjectService'' if you need |
dce943cf284f
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15007
diff
changeset
|
1472 |
"/ a tentative object service |
dce943cf284f
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15007
diff
changeset
|
1473 |
"/ tentativeObjectService := aServer tentativeObjectService. |
dce943cf284f
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15007
diff
changeset
|
1474 |
|
dce943cf284f
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15007
diff
changeset
|
1475 |
"/ uncomment to install a file service for my data files (in the ''data'' folder of my project directory |
dce943cf284f
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15007
diff
changeset
|
1476 |
"/ (aServer serviceForLink:(self linkName,''/data'') ifAbsent:nil) isNil ifTrue:[ |
15069
ca54ea5a83f9
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15067
diff
changeset
|
1477 |
"/ aServer addFileServiceWithRootLinkName:(self linkName,''/data'') |
15067
dce943cf284f
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15007
diff
changeset
|
1478 |
"/ rootDirectory:(self class projectDirectory / ''data'') |
dce943cf284f
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15007
diff
changeset
|
1479 |
"/ defaultFileName:nil. |
dce943cf284f
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15007
diff
changeset
|
1480 |
"/ ]. |
dce943cf284f
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15007
diff
changeset
|
1481 |
'. |
dce943cf284f
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15007
diff
changeset
|
1482 |
self |
dce943cf284f
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15007
diff
changeset
|
1483 |
compile:txt |
dce943cf284f
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15007
diff
changeset
|
1484 |
forClass:nonMetaClass |
dce943cf284f
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15007
diff
changeset
|
1485 |
inCategory:#initialization. |
dce943cf284f
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15007
diff
changeset
|
1486 |
]. |
9706 | 1487 |
|
1488 |
self executeCollectedChangesNamed:('Add WebService Code for ' , className). |
|
1489 |
||
9935
5a0685d2ee90
changed: #createWebServiceCodeFor:
Claus Gittinger <cg@exept.de>
parents:
9934
diff
changeset
|
1490 |
"Modified: / 02-06-2011 / 12:51:15 / cg" |
9706 | 1491 |
! |
1492 |
||
1493 |
createWidgetCodeFor:aClass |
|
1494 |
"create usually required widget code (redraw, model update, event handling)" |
|
1495 |
||
1496 |
|nonMetaClass metaClass className compileTemplateAction| |
|
1497 |
||
1498 |
self startCollectChanges. |
|
1499 |
||
1500 |
nonMetaClass := aClass theNonMetaclass. |
|
1501 |
metaClass := aClass theMetaclass. |
|
1502 |
className := nonMetaClass name. |
|
1503 |
||
16779 | 1504 |
compileTemplateAction := |
9706 | 1505 |
[:selector :templateSelector :category | |
1506 |
(nonMetaClass includesSelector:selector) ifFalse:[ |
|
1507 |
|txt| |
|
1508 |
||
1509 |
txt := self perform:templateSelector. |
|
1510 |
self |
|
1511 |
compile:txt |
|
16779 | 1512 |
forClass:nonMetaClass |
9706 | 1513 |
inCategory:category. |
1514 |
] |
|
1515 |
]. |
|
1516 |
||
1517 |
#( |
|
11523 | 1518 |
initialize code_forWidget_initialize #'initialization & release' |
1519 |
update:with:from: code_forWidget_update #'change & update' |
|
1520 |
redrawX:y:width:height: code_forWidget_redraw #'drawing' |
|
1521 |
buttonPress:x:y: code_forWidget_buttonPress #'event handling' |
|
1522 |
keyPress:x:y: code_forWidget_keyPress #'event handling' |
|
1523 |
sizeChanged: code_forWidget_sizeChanged #'event handling' |
|
9706 | 1524 |
) inGroupsOf:3 do:compileTemplateAction. |
1525 |
||
1526 |
self executeCollectedChangesNamed:('Add Widget Code for ' , className). |
|
1527 |
! ! |
|
1528 |
||
1529 |
!SmalltalkCodeGeneratorTool methodsFor:'code generation-basic'! |
|
1530 |
||
1531 |
createAccessMethodsFor:aCollectionOfVarNames in:aClass withChange:withChange asValueHolder:asValueHolder readersOnly:readersOnly writersOnly:writersOnly lazyInitialization:lazyInitialization |
|
1532 |
"workhorse for creating access methods for instvars." |
|
1533 |
||
1534 |
|classesClassVars generateCommentsForSetters generateCommentsForGetters| |
|
1535 |
||
1536 |
self startCollectChanges. |
|
1537 |
||
1538 |
generateCommentsForSetters := userPreferences generateCommentsForSetters. |
|
1539 |
generateCommentsForGetters := userPreferences generateCommentsForGetters. |
|
1540 |
||
1541 |
classesClassVars := aClass theNonMetaclass allClassVarNames. |
|
1542 |
||
1543 |
aCollectionOfVarNames do:[:name | |
|
1544 |
|source varType methodName defaultMethodName argName| |
|
1545 |
||
1546 |
varType := (classesClassVars includes:name) |
|
1547 |
ifTrue:['static'] |
|
1548 |
ifFalse:[ |
|
1549 |
(aClass isMeta ifTrue:['classInstVar'] ifFalse:['instance'])]. |
|
1550 |
||
1551 |
methodName := name. |
|
1552 |
name isUppercaseFirst ifTrue:[ |
|
10371
c35aa5f6abf6
changed: #createAccessMethodsFor:in:withChange:asValueHolder:readersOnly:writersOnly:lazyInitialization:
Claus Gittinger <cg@exept.de>
parents:
9960
diff
changeset
|
1553 |
(name conform:[:ch | ch isLetter not or:[ch isUppercase]]) ifFalse:[ "/ allow all-uppercase for class-vars |
c35aa5f6abf6
changed: #createAccessMethodsFor:in:withChange:asValueHolder:readersOnly:writersOnly:lazyInitialization:
Claus Gittinger <cg@exept.de>
parents:
9960
diff
changeset
|
1554 |
methodName := methodName asLowercaseFirst. |
c35aa5f6abf6
changed: #createAccessMethodsFor:in:withChange:asValueHolder:readersOnly:writersOnly:lazyInitialization:
Claus Gittinger <cg@exept.de>
parents:
9960
diff
changeset
|
1555 |
] |
9706 | 1556 |
]. |
1557 |
argName := 'something'. |
|
1558 |
||
1559 |
"/ the GETTER |
|
1560 |
writersOnly ifFalse:[ |
|
1561 |
lazyInitialization ifTrue:[ |
|
1562 |
defaultMethodName := 'default' , name asUppercaseFirst. |
|
1563 |
]. |
|
1564 |
||
1565 |
"check, if method is not already present" |
|
1566 |
(aClass includesSelector:(methodName asSymbol)) ifFalse:[ |
|
1567 |
asValueHolder ifTrue:[ |
|
1568 |
source := methodName , '\'. |
|
1569 |
generateComments ifTrue:[ |
|
1570 |
source := source , ' "return/create the ''%2'' value holder (automatically generated)"\\'. |
|
1571 |
]. |
|
1572 |
source := source , ' %2 isNil ifTrue:[\'. |
|
1573 |
lazyInitialization ifTrue:[ |
|
1574 |
source := source |
|
1575 |
, ' %2 := self class %3 asValue.\'. |
|
1576 |
] ifFalse:[ |
|
1577 |
source := source |
|
1578 |
, ' %2 := ValueHolder new.\'. |
|
1579 |
]. |
|
1580 |
||
1581 |
withChange ifTrue:[ |
|
1582 |
source := source |
|
1583 |
, ' %2 addDependent:self.\'. |
|
1584 |
]. |
|
1585 |
source := source |
|
1586 |
, ' ].\' |
|
1587 |
, ' ^ %2'. |
|
1588 |
] ifFalse:[ |
|
1589 |
source := methodName , '\'. |
|
1590 |
lazyInitialization ifTrue:[ |
|
1591 |
generateCommentsForGetters ifTrue:[ |
|
1592 |
source := source , ' "return the %1 instance variable ''%2'' with lazy instance creation (automatically generated)"\\'. |
|
1593 |
]. |
|
1594 |
source := source |
|
1595 |
, ' %2 isNil ifTrue:[\' |
|
1596 |
, ' %2 := self class %3.\' |
|
1597 |
, ' ].\' |
|
1598 |
, ' ^ %2'. |
|
1599 |
] ifFalse:[ |
|
1600 |
generateCommentsForGetters ifTrue:[ |
|
1601 |
source := source , ' "return the %1 instance variable ''%2'' (automatically generated)"\\'. |
|
1602 |
]. |
|
1603 |
source := source |
|
1604 |
, ' ^ %2'. |
|
1605 |
]. |
|
1606 |
]. |
|
1607 |
source := (source bindWith:varType with:name with:defaultMethodName) withCRs. |
|
11523 | 1608 |
self compile:source forClass:aClass inCategory:(asValueHolder ifTrue:[#aspects] ifFalse:[#accessing]). |
9706 | 1609 |
] ifTrue:[ |
1610 |
Transcript showCR:'method ''', methodName , ''' already present' |
|
1611 |
]. |
|
1612 |
||
1613 |
"/ default for lazy on class side |
|
1614 |
lazyInitialization ifTrue:[ |
|
1615 |
(aClass theMetaclass includesSelector:(defaultMethodName asSymbol)) ifFalse:[ |
|
1616 |
source := defaultMethodName , '\'. |
|
1617 |
generateComments ifTrue:[ |
|
1618 |
source := source , ' "default value for the ''%2'' instance variable (automatically generated)"\\'. |
|
1619 |
]. |
|
1620 |
source := source |
|
1621 |
, ' self shouldImplement.\' |
|
1622 |
, ' ^ nil.'. |
|
1623 |
source := (source bindWith:varType with:name) withCRs. |
|
11523 | 1624 |
self compile:source forClass:aClass theMetaclass inCategory:#defaults. |
9706 | 1625 |
]. |
1626 |
]. |
|
1627 |
]. |
|
1628 |
||
1629 |
"/ the SETTER |
|
1630 |
readersOnly ifFalse:[ |
|
14118
e05c6b42870d
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
13811
diff
changeset
|
1631 |
(aClass includesSelector:(methodName asMutator)) ifFalse:[ |
9706 | 1632 |
((methodName size > 2) and:[ (methodName startsWith:'is') and:[ (methodName at:3) isUppercase ]]) |
1633 |
ifTrue:[ |
|
1634 |
argName := 'aBoolean' |
|
1635 |
]. |
|
1636 |
asValueHolder ifTrue:[ |
|
1637 |
source := methodName , ':%3\'. "/ argName |
|
1638 |
generateComments ifTrue:[ |
|
1639 |
source := source , ' "set the ''%2'' value holder' , ' (automatically generated)"\\'. |
|
1640 |
]. |
|
1641 |
withChange ifTrue:[ |
|
1642 |
source := source |
|
1643 |
, ' |oldValue newValue|\\' |
|
1644 |
, ' %2 notNil ifTrue:[\' |
|
1645 |
, ' oldValue := %2 value.\' |
|
1646 |
, ' %2 removeDependent:self.\' |
|
1647 |
, ' ].\' |
|
1648 |
, ' %2 := %3.\' "/ argName |
|
1649 |
, ' %2 notNil ifTrue:[\' |
|
1650 |
, ' %2 addDependent:self.\' |
|
1651 |
, ' ].\' |
|
1652 |
, ' newValue := %2 value.\' |
|
1653 |
, ' oldValue ~~ newValue ifTrue:[\' |
|
1654 |
, ' self update:#value with:newValue from:%2.\' |
|
1655 |
, ' ].\' |
|
1656 |
] ifFalse:[ |
|
1657 |
source := source |
|
1658 |
, ' %2 := %3.'. "/ argName |
|
1659 |
]. |
|
1660 |
] ifFalse:[ |
|
1661 |
source := methodName , ':%3\'. "/ argName |
|
1662 |
withChange ifTrue:[ |
|
1663 |
generateComments ifTrue:[ |
|
1664 |
source := source , ' "set the value of the %1 variable ''%2'''. |
|
1665 |
source := source , ' and send a change notification (automatically generated)"\\'. |
|
1666 |
]. |
|
1667 |
source := source |
|
1668 |
, ' (%2 ~~ %3) ifTrue:[\' |
|
1669 |
, ' %2 := %3.\' "/ argName |
|
1670 |
, ' self changed:#%2.\' |
|
1671 |
, ' ].\'. |
|
1672 |
] ifFalse:[ |
|
1673 |
generateCommentsForSetters ifTrue:[ |
|
1674 |
source := source , ' "set the value of the %1 variable ''%2'''. |
|
1675 |
source := source , ' (automatically generated)"\\'. |
|
1676 |
]. |
|
1677 |
source := source |
|
1678 |
, ' %2 := %3.'. "/ argName |
|
1679 |
]. |
|
1680 |
]. |
|
1681 |
source := (source bindWith:varType with:name with:argName) withCRs. |
|
1682 |
self |
|
1683 |
compile:source |
|
1684 |
forClass:aClass |
|
11523 | 1685 |
inCategory:(asValueHolder ifTrue:[#aspects] ifFalse:[#accessing]). |
9706 | 1686 |
] ifTrue:[ |
1687 |
Transcript showCR:'method ''', methodName , ':'' already present' |
|
1688 |
]. |
|
1689 |
]. |
|
1690 |
]. |
|
1691 |
||
1692 |
self executeCollectedChangesNamed:('Add Accessors'). |
|
10371
c35aa5f6abf6
changed: #createAccessMethodsFor:in:withChange:asValueHolder:readersOnly:writersOnly:lazyInitialization:
Claus Gittinger <cg@exept.de>
parents:
9960
diff
changeset
|
1693 |
|
c35aa5f6abf6
changed: #createAccessMethodsFor:in:withChange:asValueHolder:readersOnly:writersOnly:lazyInitialization:
Claus Gittinger <cg@exept.de>
parents:
9960
diff
changeset
|
1694 |
"Modified: / 21-07-2011 / 15:19:25 / cg" |
9706 | 1695 |
! |
1696 |
||
1697 |
createCollectionAccessMethodsFor:aCollectionOfVarNames in:aClass withChange:withChange |
|
1698 |
|classesClassVars| |
|
1699 |
||
1700 |
self startCollectChanges. |
|
1701 |
||
1702 |
classesClassVars := aClass theNonMetaclass allClassVarNames. |
|
1703 |
||
1704 |
aCollectionOfVarNames do:[:name | |
|
1705 |
|source varType methodNameBase methodName defaultMethodName| |
|
1706 |
||
1707 |
varType := (classesClassVars includes:name) |
|
1708 |
ifTrue:['static'] |
|
1709 |
ifFalse:[ |
|
1710 |
(aClass isMeta ifTrue:['classInstVar'] ifFalse:['instance'])]. |
|
1711 |
||
1712 |
methodNameBase := name asUppercaseFirst. |
|
1713 |
(methodNameBase endsWith:'s') ifTrue:[ |
|
12712
b412eb74759b
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
12458
diff
changeset
|
1714 |
methodNameBase := methodNameBase copyButLast:1. |
9706 | 1715 |
]. |
1716 |
methodName := 'add' , methodNameBase, ':'. |
|
1717 |
||
1718 |
"check, if method is not already present" |
|
1719 |
(aClass includesSelector:(methodName asSymbol)) ifFalse:[ |
|
1720 |
source := methodName , 'a%1\'. |
|
1721 |
generateComments ifTrue:[ |
|
1722 |
source := source , ' "add a ',methodNameBase,'"\\'. |
|
1723 |
]. |
|
1724 |
source := source , ' %2 isNil ifTrue:[\'. |
|
1725 |
source := source |
|
1726 |
, ' %2 := OrderedCollection new.\'. |
|
1727 |
source := source |
|
1728 |
, ' ].\' |
|
1729 |
, ' %2 add: a%1'. |
|
1730 |
source := (source bindWith:methodNameBase with:name) withCRs. |
|
11523 | 1731 |
self compile:source forClass:aClass inCategory:#accessing. |
9706 | 1732 |
] ifTrue:[ |
1733 |
Transcript showCR:'method ''', methodName , ''' already present' |
|
1734 |
]. |
|
1735 |
||
1736 |
methodName := 'remove' , methodNameBase, ':'. |
|
1737 |
||
1738 |
"check, if method is not already present" |
|
1739 |
(aClass includesSelector:(methodName asSymbol)) ifFalse:[ |
|
1740 |
source := methodName , 'a%1\'. |
|
1741 |
generateComments ifTrue:[ |
|
1742 |
source := source , ' "remove a ',methodNameBase,'"\\'. |
|
1743 |
]. |
|
1744 |
source := source |
|
1745 |
, ' %2 remove: a%1'. |
|
1746 |
source := (source bindWith:methodNameBase with:name) withCRs. |
|
11523 | 1747 |
self compile:source forClass:aClass inCategory:#accessing. |
9706 | 1748 |
] ifTrue:[ |
1749 |
Transcript showCR:'method ''', methodName , ''' already present' |
|
1750 |
]. |
|
1751 |
]. |
|
1752 |
||
1753 |
self |
|
1754 |
createAccessMethodsFor:aCollectionOfVarNames |
|
1755 |
in:aClass |
|
1756 |
withChange:withChange |
|
1757 |
asValueHolder:false |
|
1758 |
readersOnly:true |
|
1759 |
writersOnly:false |
|
1760 |
lazyInitialization:false. |
|
1761 |
||
1762 |
self executeCollectedChangesNamed:('Add Collection Access'). |
|
1763 |
||
1764 |
"Created: / 04-02-2007 / 15:52:31 / cg" |
|
1765 |
! |
|
1766 |
||
1767 |
createValueHoldersFor:aCollectionOfVarNames in:aClass lazyInitialization:lazyInitialization |
|
1768 |
"workhorse for creating access methods for instvars." |
|
1769 |
||
1770 |
|nonMetaClass metaClass classesClassVars generateCommentsForSetters generateCommentsForGetters| |
|
1771 |
||
1772 |
nonMetaClass := aClass theNonMetaclass. |
|
1773 |
metaClass := aClass theMetaclass. |
|
1774 |
||
1775 |
self startCollectChanges. |
|
1776 |
||
1777 |
generateCommentsForSetters := userPreferences generateCommentsForSetters. |
|
1778 |
generateCommentsForGetters := userPreferences generateCommentsForGetters. |
|
1779 |
||
1780 |
classesClassVars := nonMetaClass allClassVarNames. |
|
1781 |
||
1782 |
aCollectionOfVarNames do:[:name | |
|
1783 |
|source varType methodName holderMethodName defaultMethodName| |
|
1784 |
||
1785 |
holderMethodName := name. |
|
1786 |
name isUppercaseFirst ifTrue:[ |
|
1787 |
holderMethodName := holderMethodName asLowercaseFirst. |
|
1788 |
]. |
|
1789 |
(holderMethodName endsWith:'Holder') ifTrue:[ |
|
12712
b412eb74759b
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
12458
diff
changeset
|
1790 |
methodName := holderMethodName copyButLast:6. |
9706 | 1791 |
] ifFalse:[ |
1792 |
methodName := holderMethodName. |
|
1793 |
holderMethodName := methodName , 'Holder'. |
|
1794 |
]. |
|
1795 |
||
1796 |
methodName notNil ifTrue:[ |
|
1797 |
(metaClass includesSelector:(methodName asSymbol)) ifFalse:[ |
|
1798 |
source := '%1\'. |
|
1799 |
generateComments ifTrue:[ |
|
1800 |
source := source , ' "return the value in ''%2''"\\'. |
|
1801 |
]. |
|
1802 |
source := source , ' ^ self %2 value'. |
|
1803 |
source := (source bindWith:methodName with:holderMethodName) withCRs. |
|
11523 | 1804 |
self compile:source forClass:nonMetaClass inCategory:#accessing. |
9706 | 1805 |
] ifTrue:[ |
1806 |
Transcript showCR:'method ''', methodName , ''' already present' |
|
1807 |
]. |
|
1808 |
||
14118
e05c6b42870d
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
13811
diff
changeset
|
1809 |
(metaClass includesSelector:(methodName asMutator)) ifFalse:[ |
9706 | 1810 |
source := '%1: newValue\'. |
1811 |
generateComments ifTrue:[ |
|
1812 |
source := source , ' "set the value in ''%2''"\\'. |
|
1813 |
]. |
|
1814 |
source := source , ' self %2 value: newValue'. |
|
1815 |
source := (source bindWith:methodName with:holderMethodName) withCRs. |
|
11523 | 1816 |
self compile:source forClass:nonMetaClass inCategory:#accessing. |
9706 | 1817 |
] ifTrue:[ |
1818 |
Transcript showCR:'method ''', methodName , ':'' already present' |
|
1819 |
]. |
|
1820 |
]. |
|
1821 |
(metaClass includesSelector:(holderMethodName asSymbol)) ifFalse:[ |
|
1822 |
source := '%1\'. |
|
1823 |
generateComments ifTrue:[ |
|
1824 |
source := source , ' "return/create the valueHolder ''%1''"\\'. |
|
1825 |
]. |
|
1826 |
source := source , ' %1 isNil ifTrue:[\'. |
|
1827 |
source := source , ' %1 := ValueHolder with:nil "defaultValue here".\'. |
|
1828 |
source := source , ' ].\'. |
|
1829 |
source := source , ' ^ %1\'. |
|
1830 |
source := (source bindWith:holderMethodName) withCRs. |
|
11523 | 1831 |
self compile:source forClass:nonMetaClass inCategory:#accessing. |
9706 | 1832 |
] ifTrue:[ |
1833 |
Transcript showCR:'method ''', methodName , ''' already present' |
|
1834 |
]. |
|
1835 |
]. |
|
1836 |
||
1837 |
self executeCollectedChangesNamed:('Add ValueHolder'). |
|
1838 |
! ! |
|
1839 |
||
1840 |
!SmalltalkCodeGeneratorTool methodsFor:'code generation-individual methods'! |
|
1841 |
||
1842 |
createAcceptVisitorMethod:selector in:aClass |
|
13811 | 1843 |
^ self createAcceptVisitorMethod:selector in:aClass withParameter: false. |
1844 |
||
1845 |
"Modified: / 05-08-2013 / 13:34:44 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
1846 |
! |
|
1847 |
||
1848 |
createAcceptVisitorMethod:selector in:aClass withParameter: withParameter |
|
9706 | 1849 |
"create an acceptVisitor: method |
1850 |
(I'm tired of typing)" |
|
1851 |
||
1852 |
self assert:( aClass isMeta not ). |
|
1853 |
||
1854 |
(aClass includesSelector:#'acceptVisitor:') ifFalse:[ |
|
16779 | 1855 |
self |
9706 | 1856 |
compile: |
13811 | 1857 |
(('acceptVisitor:visitor %2 |
9706 | 1858 |
"Double dispatch back to the visitor, passing my type encoded in |
1859 |
the selector (visitor pattern)" |
|
1860 |
||
1861 |
"stub code automatically generated - please change if required" |
|
1862 |
||
13811 | 1863 |
^ visitor %1self %2 |
1864 |
') bindWith:selector with:(withParameter ifTrue:[' with: parameter'] ifFalse:[''])) |
|
16779 | 1865 |
forClass:aClass |
11523 | 1866 |
inCategory:#visiting. |
9706 | 1867 |
] |
13811 | 1868 |
|
1869 |
"Created: / 05-08-2013 / 13:34:25 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
9706 | 1870 |
! |
1871 |
||
1872 |
createAcceptVisitorMethodIn:aClass |
|
1873 |
"create an acceptVisitor: method |
|
1874 |
(I'm tired of typing)" |
|
1875 |
||
1876 |
self |
|
14118
e05c6b42870d
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
13811
diff
changeset
|
1877 |
createAcceptVisitorMethod:('visit' , aClass nameWithoutPrefix) asMutator |
9706 | 1878 |
in:aClass |
1879 |
! |
|
1880 |
||
12879
a544ab64abc0
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12712
diff
changeset
|
1881 |
createAspectMethodFor:anAspectSymbol in:aClass |
a544ab64abc0
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12712
diff
changeset
|
1882 |
"create an aspect method." |
a544ab64abc0
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12712
diff
changeset
|
1883 |
|
a544ab64abc0
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12712
diff
changeset
|
1884 |
|source aspect| |
a544ab64abc0
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12712
diff
changeset
|
1885 |
|
a544ab64abc0
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12712
diff
changeset
|
1886 |
aspect := anAspectSymbol asSymbol. |
a544ab64abc0
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12712
diff
changeset
|
1887 |
source := String |
a544ab64abc0
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12712
diff
changeset
|
1888 |
streamContents:[:s | |
a544ab64abc0
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12712
diff
changeset
|
1889 |
s nextPutAll:aspect asString. |
a544ab64abc0
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12712
diff
changeset
|
1890 |
s nextPutAll:' |
a544ab64abc0
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12712
diff
changeset
|
1891 |
|holder| |
a544ab64abc0
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12712
diff
changeset
|
1892 |
|
a544ab64abc0
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12712
diff
changeset
|
1893 |
(holder := builder bindingAt:',aspect storeString,') isNil ifTrue:[ |
a544ab64abc0
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12712
diff
changeset
|
1894 |
builder aspectAt:',aspect storeString,' put:(holder := nil asValue). |
a544ab64abc0
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12712
diff
changeset
|
1895 |
]. |
a544ab64abc0
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12712
diff
changeset
|
1896 |
^ holder. |
a544ab64abc0
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12712
diff
changeset
|
1897 |
'. |
a544ab64abc0
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12712
diff
changeset
|
1898 |
]. |
a544ab64abc0
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12712
diff
changeset
|
1899 |
self compile:source forClass:aClass inCategory:#aspects. |
a544ab64abc0
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12712
diff
changeset
|
1900 |
! |
a544ab64abc0
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12712
diff
changeset
|
1901 |
|
9706 | 1902 |
createCopyrightMethodFor:copyRightText for:aClass |
1903 |
"add copyright method containing text, |
|
1904 |
but only if not already present." |
|
1905 |
||
14426
98245d2f01a2
class: SmalltalkCodeGeneratorTool
Stefan Vogel <sv@exept.de>
parents:
14118
diff
changeset
|
1906 |
|txt log thisYear initialYear scm firstRev firstRevDate| |
9706 | 1907 |
|
1908 |
(aClass includesSelector:#copyright) ifFalse:[ |
|
1909 |
copyRightText notNil ifTrue:[ |
|
14426
98245d2f01a2
class: SmalltalkCodeGeneratorTool
Stefan Vogel <sv@exept.de>
parents:
14118
diff
changeset
|
1910 |
initialYear := thisYear := Date today year. |
12440 | 1911 |
(copyRightText includes:$%) ifTrue:[ |
1912 |
scm := aClass theNonMetaclass sourceCodeManager. |
|
1913 |
scm == CVSSourceCodeManager ifTrue:[ |
|
1914 |
log := CVSSourceCodeManager revisionLogOf:aClass fromRevision:'1.1' toRevision:'1.1'. |
|
15708
1b35e4573788
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15515
diff
changeset
|
1915 |
log isNil ifTrue:[ |
1b35e4573788
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15515
diff
changeset
|
1916 |
initialYear := thisYear |
1b35e4573788
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15515
diff
changeset
|
1917 |
] ifFalse:[ |
1b35e4573788
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15515
diff
changeset
|
1918 |
(firstRev := (log at:#revisions) firstIfEmpty:nil) notNil ifTrue:[ |
1b35e4573788
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15515
diff
changeset
|
1919 |
firstRevDate := firstRev at:#date ifAbsent:nil. |
1b35e4573788
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15515
diff
changeset
|
1920 |
firstRevDate notNil ifTrue:[ |
1b35e4573788
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15515
diff
changeset
|
1921 |
firstRevDate := Date readFrom:firstRevDate onError:nil. |
1b35e4573788
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15515
diff
changeset
|
1922 |
firstRevDate notNil ifTrue:[ |
1b35e4573788
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15515
diff
changeset
|
1923 |
initialYear := firstRevDate year. |
1b35e4573788
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15515
diff
changeset
|
1924 |
] |
12440 | 1925 |
] |
1926 |
] |
|
15708
1b35e4573788
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15515
diff
changeset
|
1927 |
]. |
12440 | 1928 |
]. |
1929 |
]. |
|
14426
98245d2f01a2
class: SmalltalkCodeGeneratorTool
Stefan Vogel <sv@exept.de>
parents:
14118
diff
changeset
|
1930 |
thisYear ~= initialYear ifTrue:[ |
98245d2f01a2
class: SmalltalkCodeGeneratorTool
Stefan Vogel <sv@exept.de>
parents:
14118
diff
changeset
|
1931 |
txt := copyRightText bindWith:(initialYear printString, '-', thisYear printString). |
98245d2f01a2
class: SmalltalkCodeGeneratorTool
Stefan Vogel <sv@exept.de>
parents:
14118
diff
changeset
|
1932 |
] ifFalse:[ |
98245d2f01a2
class: SmalltalkCodeGeneratorTool
Stefan Vogel <sv@exept.de>
parents:
14118
diff
changeset
|
1933 |
txt := copyRightText bindWith:initialYear. |
98245d2f01a2
class: SmalltalkCodeGeneratorTool
Stefan Vogel <sv@exept.de>
parents:
14118
diff
changeset
|
1934 |
]. |
98245d2f01a2
class: SmalltalkCodeGeneratorTool
Stefan Vogel <sv@exept.de>
parents:
14118
diff
changeset
|
1935 |
|
9706 | 1936 |
self compile: |
1937 |
'copyright |
|
1938 |
" |
|
1939 |
' , txt , ' |
|
1940 |
" |
|
1941 |
' forClass:aClass |
|
11523 | 1942 |
inCategory:#documentation. |
9706 | 1943 |
] |
1944 |
]. |
|
1945 |
! |
|
1946 |
||
1947 |
createDocumentationMethodFor:aClass |
|
1948 |
"add documentation method containing doc template |
|
1949 |
but only if not already present." |
|
1950 |
||
1951 |
|metaClass nonMetaClass userName loginName hostName emailAddress code existingComment| |
|
1952 |
||
1953 |
metaClass := aClass theMetaclass. |
|
1954 |
nonMetaClass := aClass theNonMetaclass. |
|
1955 |
||
1956 |
(metaClass includesSelector:#documentation) ifFalse:[ |
|
1957 |
existingComment := nonMetaClass comment. |
|
1958 |
existingComment isEmptyOrNil ifTrue:[ |
|
1959 |
(nonMetaClass isSubclassOf:HTTPService) ifTrue:[ |
|
1960 |
existingComment := ' [start Server with:] |
|
1961 |
HTTPServer startServerOnPort:8080 |
|
1962 |
||
1963 |
[start with:] |
|
1964 |
(self new) |
|
1965 |
registerServiceOn:(HTTPServer runningServerOnPort:8080)'. |
|
1966 |
]. |
|
1967 |
]. |
|
1968 |
||
1969 |
userName := OperatingSystem getFullUserName. |
|
1970 |
loginName := OperatingSystem getLoginName. |
|
1971 |
hostName := OperatingSystem getHostName. |
|
1972 |
emailAddress := loginName , '@' , hostName. |
|
1973 |
||
9960 | 1974 |
|
1975 |
||
9706 | 1976 |
"/ ugly; should ask the class for that |
1977 |
metaClass isJavaScriptMetaclass ifTrue:[ |
|
1978 |
code := |
|
1979 |
'function documentation() { |
|
1980 |
/* |
|
1981 |
' , (existingComment ? ' documentation to be added.') , ' |
|
1982 |
||
1983 |
[author:] |
|
9960 | 1984 |
' ", userName |
1985 |
, ' (' , emailAddress , ')' " |
|
1986 |
, UserPreferences current historyManagerSignature , ' |
|
9706 | 1987 |
|
1988 |
[instance variables:] |
|
1989 |
||
1990 |
[class variables:] |
|
1991 |
||
1992 |
[see also:] |
|
1993 |
||
1994 |
*/ |
|
1995 |
} |
|
1996 |
' |
|
1997 |
] ifFalse:[ |
|
1998 |
code:= |
|
1999 |
'documentation |
|
2000 |
" |
|
2001 |
' , (existingComment ? ' documentation to be added.') , ' |
|
2002 |
||
2003 |
[author:] |
|
9960 | 2004 |
' ", userName |
2005 |
, ' (' , emailAddress , ')' " |
|
17557 | 2006 |
, ((UserPreferences current historyManagerSignature)?'') , ' |
9706 | 2007 |
|
2008 |
[instance variables:] |
|
2009 |
||
2010 |
[class variables:] |
|
2011 |
||
2012 |
[see also:] |
|
2013 |
||
2014 |
" |
|
2015 |
' |
|
2016 |
]. |
|
2017 |
||
2018 |
self |
|
2019 |
compile:code |
|
2020 |
forClass:metaClass |
|
11523 | 2021 |
inCategory:#documentation. |
9706 | 2022 |
]. |
2023 |
||
2024 |
"Modified: / 24-11-2006 / 15:54:27 / cg" |
|
9960 | 2025 |
"Modified: / 04-02-2011 / 08:24:31 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
17557 | 2026 |
"Modified: / 23-06-2017 / 15:56:15 / Maren" |
9706 | 2027 |
! |
2028 |
||
2029 |
createExamplesMethodFor:aClass |
|
2030 |
"add examples method containing examples template |
|
2031 |
but only if not already present." |
|
2032 |
||
2033 |
|nonMetaclass fragment| |
|
2034 |
||
2035 |
nonMetaclass := aClass theNonMetaclass. |
|
2036 |
||
2037 |
(nonMetaclass isSubclassOf:View) ifTrue:[ |
|
2038 |
self createExamplesMethodForViewClass:aClass. |
|
2039 |
^ self |
|
2040 |
]. |
|
2041 |
||
2042 |
(nonMetaclass isSubclassOf:ApplicationModel) ifFalse:[ |
|
2043 |
^ self |
|
2044 |
]. |
|
2045 |
||
2046 |
(aClass includesSelector:#examples) ifFalse:[ |
|
2047 |
(nonMetaclass isSubclassOf:ApplicationModel) ifTrue:[ |
|
2048 |
fragment := ' Starting the application: |
|
2049 |
[exBegin] |
|
2050 |
' , nonMetaclass name , ' open |
|
2051 |
||
2052 |
[exEnd] |
|
2053 |
' |
|
2054 |
] ifFalse:[ |
|
2055 |
fragment := '' |
|
2056 |
]. |
|
2057 |
||
2058 |
self |
|
2059 |
compile: |
|
2060 |
'examples |
|
2061 |
" |
|
2062 |
' , fragment , ' |
|
2063 |
more examples to be added: |
|
2064 |
[exBegin] |
|
2065 |
... add code fragment for |
|
2066 |
... executable example here ... |
|
2067 |
[exEnd] |
|
2068 |
" |
|
2069 |
' |
|
2070 |
forClass:aClass |
|
11523 | 2071 |
inCategory:#documentation. |
9706 | 2072 |
]. |
2073 |
! |
|
2074 |
||
11841
8df2e8a1dc3e
+generate false return method
Claus Gittinger <cg@exept.de>
parents:
11759
diff
changeset
|
2075 |
createFalseReturnMethodFor:aSelector category:cat in:aClass |
8df2e8a1dc3e
+generate false return method
Claus Gittinger <cg@exept.de>
parents:
11759
diff
changeset
|
2076 |
"add a ^ false method; |
8df2e8a1dc3e
+generate false return method
Claus Gittinger <cg@exept.de>
parents:
11759
diff
changeset
|
2077 |
but only if not already present." |
8df2e8a1dc3e
+generate false return method
Claus Gittinger <cg@exept.de>
parents:
11759
diff
changeset
|
2078 |
|
8df2e8a1dc3e
+generate false return method
Claus Gittinger <cg@exept.de>
parents:
11759
diff
changeset
|
2079 |
(aClass includesSelector:aSelector) ifFalse:[ |
8df2e8a1dc3e
+generate false return method
Claus Gittinger <cg@exept.de>
parents:
11759
diff
changeset
|
2080 |
|
8df2e8a1dc3e
+generate false return method
Claus Gittinger <cg@exept.de>
parents:
11759
diff
changeset
|
2081 |
self compile: |
8df2e8a1dc3e
+generate false return method
Claus Gittinger <cg@exept.de>
parents:
11759
diff
changeset
|
2082 |
(Method methodDefinitionTemplateForSelector:aSelector) , |
8df2e8a1dc3e
+generate false return method
Claus Gittinger <cg@exept.de>
parents:
11759
diff
changeset
|
2083 |
' |
8df2e8a1dc3e
+generate false return method
Claus Gittinger <cg@exept.de>
parents:
11759
diff
changeset
|
2084 |
"return false here; to be redefined in subclass(es)" |
8df2e8a1dc3e
+generate false return method
Claus Gittinger <cg@exept.de>
parents:
11759
diff
changeset
|
2085 |
|
8df2e8a1dc3e
+generate false return method
Claus Gittinger <cg@exept.de>
parents:
11759
diff
changeset
|
2086 |
^ false |
16779 | 2087 |
' |
11841
8df2e8a1dc3e
+generate false return method
Claus Gittinger <cg@exept.de>
parents:
11759
diff
changeset
|
2088 |
forClass:aClass |
8df2e8a1dc3e
+generate false return method
Claus Gittinger <cg@exept.de>
parents:
11759
diff
changeset
|
2089 |
inCategory:cat. |
8df2e8a1dc3e
+generate false return method
Claus Gittinger <cg@exept.de>
parents:
11759
diff
changeset
|
2090 |
]. |
8df2e8a1dc3e
+generate false return method
Claus Gittinger <cg@exept.de>
parents:
11759
diff
changeset
|
2091 |
! |
8df2e8a1dc3e
+generate false return method
Claus Gittinger <cg@exept.de>
parents:
11759
diff
changeset
|
2092 |
|
9706 | 2093 |
createImageSpecMethodFor:anImage comment:comment in:aClass selector:sel |
2094 |
|imageStoreStream mthd imageKey category| |
|
2095 |
||
2096 |
anImage storeOn: (imageStoreStream := WriteStream on: ''). |
|
2097 |
||
2098 |
"/ if that method already exists, do not overwrite the category |
|
11523 | 2099 |
category := #'image specs'. |
9706 | 2100 |
(mthd := aClass compiledMethodAt:sel) notNil ifTrue:[ |
2101 |
category := mthd category. |
|
2102 |
]. |
|
2103 |
||
16553 | 2104 |
imageKey := aClass "theNonMetaclass" name, ' ', sel. |
9706 | 2105 |
Icon constantNamed: imageKey put:nil. |
9960 | 2106 |
self |
9706 | 2107 |
compile: ((sel, |
2108 |
'\', comment, |
|
16779 | 2109 |
'\\' , |
9706 | 2110 |
' "\', |
2111 |
' self ' , sel , ' inspect\', |
|
2112 |
' ImageEditor openOnClass:self andSelector:#', sel, '\', |
|
16779 | 2113 |
' Icon flushCachedIcons', |
9706 | 2114 |
'\ "', |
2115 |
'\\', |
|
2116 |
' <resource: #image>', |
|
2117 |
'\\', |
|
16779 | 2118 |
' ^Icon\') withCRs, |
9706 | 2119 |
' constantNamed:''', imageKey, '''\' withCRs, |
2120 |
' ifAbsentPut:[', imageStoreStream contents, ']') |
|
9960 | 2121 |
forClass:aClass |
2122 |
inCategory: category. |
|
2123 |
||
2124 |
"Modified: / 01-07-2011 / 12:31:12 / cg" |
|
9706 | 2125 |
! |
2126 |
||
2127 |
createInitialHistoryMethodFor:aClass |
|
2128 |
"add history method containing created-entry |
|
2129 |
but only if not already present." |
|
2130 |
||
2131 |
|code| |
|
2132 |
||
2133 |
(aClass includesSelector:#history) ifFalse:[ |
|
2134 |
HistoryManager notNil ifTrue:[ |
|
2135 |
code := HistoryManager codeForInitialHistoryMethodIn:aClass. |
|
2136 |
self |
|
2137 |
compile:code |
|
2138 |
forClass:aClass |
|
11523 | 2139 |
inCategory:#documentation. |
9706 | 2140 |
]. |
2141 |
]. |
|
2142 |
! |
|
2143 |
||
2144 |
createInstanceCreationMethodWithSetupFor:selector category:category in:aMetaClass |
|
2145 |
"add an inst-creation method" |
|
2146 |
||
2147 |
|template instMthd argNames| |
|
2148 |
||
2149 |
(aMetaClass includesSelector:selector) ifFalse:[ |
|
2150 |
instMthd := aMetaClass theNonMetaclass compiledMethodAt:selector. |
|
2151 |
(instMthd notNil |
|
2152 |
and:[ (argNames := instMthd methodArgNames) notEmptyOrNil ]) |
|
2153 |
ifTrue:[ |
|
2154 |
template := Parser methodSpecificationForSelector:selector argNames:argNames. |
|
2155 |
] ifFalse:[ |
|
2156 |
template := Parser methodSpecificationForSelector:selector. |
|
2157 |
]. |
|
2158 |
||
2159 |
self |
|
2160 |
compile: |
|
2161 |
template , ' |
|
2162 |
^ self new ' , template , ' |
|
2163 |
' |
|
2164 |
forClass:aMetaClass |
|
2165 |
inCategory:category. |
|
2166 |
]. |
|
2167 |
! |
|
2168 |
||
16241 | 2169 |
createMultiSetterInstanceCreationMethodFor:aCollectionOfVarNames in:aClass |
2170 |
"create a multi-setter instance creator method for instvars." |
|
2171 |
||
2172 |
|argPart source| |
|
2173 |
||
2174 |
self createMultiSetterMethodFor:aCollectionOfVarNames in:aClass. |
|
2175 |
||
2176 |
source := ''. |
|
2177 |
aCollectionOfVarNames do:[:eachVar | |
|
2178 |
source := source , (eachVar , ':' , eachVar , 'Arg '). |
|
2179 |
]. |
|
2180 |
argPart := source. |
|
2181 |
||
2182 |
source := source , Character cr. |
|
2183 |
(userPreferences generateCommentsForSetters) ifTrue:[ |
|
2184 |
source := source , (' "return a new instance with multiple instance variables initialized"' , Character cr , Character cr). |
|
2185 |
]. |
|
2186 |
source := source , (' ^ self new ' , argPart). |
|
2187 |
self compile:source forClass:aClass theMetaclass inCategory:#'instance creation'. |
|
2188 |
! |
|
2189 |
||
9706 | 2190 |
createMultiSetterMethodFor:aCollectionOfVarNames in:aClass |
2191 |
"create a multi-setter method for instvars." |
|
2192 |
||
2193 |
|source| |
|
2194 |
||
2195 |
source := ''. |
|
2196 |
aCollectionOfVarNames do:[:eachVar | |
|
2197 |
source := source , (eachVar , ':' , eachVar , 'Arg '). |
|
2198 |
]. |
|
2199 |
source := source , Character cr. |
|
2200 |
(userPreferences generateCommentsForSetters) ifTrue:[ |
|
2201 |
source := source , (' "set instance variables"' , Character cr , Character cr). |
|
2202 |
]. |
|
2203 |
aCollectionOfVarNames do:[:eachVar | |
|
2204 |
source := source , (' ' , eachVar , ' := ' , eachVar , 'Arg.' , Character cr). |
|
2205 |
]. |
|
11523 | 2206 |
self compile:source forClass:aClass inCategory:#accessing. |
9706 | 2207 |
! |
2208 |
||
13591
36a35e7a4710
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12879
diff
changeset
|
2209 |
createShouldImplementMethodFor:aSelector category:cat in:aClass |
36a35e7a4710
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12879
diff
changeset
|
2210 |
"add a subclassResponsibility method; |
36a35e7a4710
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12879
diff
changeset
|
2211 |
but only if not already present." |
36a35e7a4710
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12879
diff
changeset
|
2212 |
|
36a35e7a4710
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12879
diff
changeset
|
2213 |
(aClass includesSelector:aSelector) ifFalse:[ |
36a35e7a4710
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12879
diff
changeset
|
2214 |
|
36a35e7a4710
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12879
diff
changeset
|
2215 |
self compile: |
36a35e7a4710
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12879
diff
changeset
|
2216 |
(Method methodDefinitionTemplateForSelector:aSelector) , |
36a35e7a4710
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12879
diff
changeset
|
2217 |
' |
36a35e7a4710
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12879
diff
changeset
|
2218 |
"raise an error: this method should be implemented (TODO)" |
36a35e7a4710
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12879
diff
changeset
|
2219 |
|
36a35e7a4710
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12879
diff
changeset
|
2220 |
^ self shouldImplement |
36a35e7a4710
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12879
diff
changeset
|
2221 |
' |
36a35e7a4710
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12879
diff
changeset
|
2222 |
forClass:aClass |
36a35e7a4710
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12879
diff
changeset
|
2223 |
inCategory:cat. |
36a35e7a4710
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12879
diff
changeset
|
2224 |
]. |
36a35e7a4710
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12879
diff
changeset
|
2225 |
! |
36a35e7a4710
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
12879
diff
changeset
|
2226 |
|
9706 | 2227 |
createSubclassResponsibilityMethodFor:aSelector category:cat in:aClass |
2228 |
"add a subclassResponsibility method; |
|
2229 |
but only if not already present." |
|
2230 |
||
2231 |
(aClass includesSelector:aSelector) ifFalse:[ |
|
2232 |
||
2233 |
self compile: |
|
2234 |
(Method methodDefinitionTemplateForSelector:aSelector) , |
|
2235 |
' |
|
2236 |
"raise an error: must be redefined in concrete subclass(es)" |
|
2237 |
||
2238 |
^ self subclassResponsibility |
|
2239 |
' |
|
2240 |
forClass:aClass |
|
2241 |
inCategory:cat. |
|
2242 |
]. |
|
2243 |
! |
|
2244 |
||
2245 |
createUpdateMethodIn:aClass |
|
2246 |
"create an update:with:from:-method |
|
2247 |
(I'm tired of typing)" |
|
2248 |
||
2249 |
|code| |
|
2250 |
||
11523 | 2251 |
(aClass includesSelector:#update:with:from:) ifFalse:[ |
9706 | 2252 |
generateComments ifFalse:[ |
2253 |
code := |
|
2254 |
'update:something with:aParameter from:changedObject |
|
2255 |
super update:something with:aParameter from:changedObject |
|
2256 |
' |
|
2257 |
] ifTrue:[ |
|
2258 |
code := |
|
2259 |
'update:something with:aParameter from:changedObject |
|
2260 |
"Invoked when an object that I depend upon sends a change notification." |
|
2261 |
||
2262 |
"stub code automatically generated - please change as required" |
|
2263 |
||
2264 |
"/ changedObject == someOfMyValueHolders ifTrue:[ |
|
2265 |
"/ self doSomethingApropriate. |
|
2266 |
"/ ^ self. |
|
2267 |
"/ ]. |
|
2268 |
super update:something with:aParameter from:changedObject |
|
2269 |
' |
|
2270 |
]. |
|
2271 |
||
2272 |
self |
|
2273 |
compile:code |
|
2274 |
forClass:aClass |
|
11523 | 2275 |
inCategory:#'change & update'. |
9706 | 2276 |
] |
2277 |
! |
|
2278 |
||
2279 |
createVersionMethodFor:aClass |
|
2280 |
<resource: #obsolete> |
|
11759
475918fd2d85
changed: #createVersionMethodFor:
Claus Gittinger <cg@exept.de>
parents:
11640
diff
changeset
|
2281 |
"add a version method containing RCS template |
475918fd2d85
changed: #createVersionMethodFor:
Claus Gittinger <cg@exept.de>
parents:
11640
diff
changeset
|
2282 |
but only if not already present and it's not a private class." |
9706 | 2283 |
|
2284 |
|code| |
|
2285 |
||
2286 |
self obsoleteMethodWarning. |
|
2287 |
||
2288 |
aClass isPrivate ifFalse:[ |
|
2289 |
(aClass includesSelector:#version) ifFalse:[ |
|
16355 | 2290 |
code := aClass programmingLanguage methodTemplateForVersionMethodCVS. |
9706 | 2291 |
self |
2292 |
compile:code |
|
2293 |
forClass:aClass |
|
11523 | 2294 |
inCategory:#documentation. |
9706 | 2295 |
] |
2296 |
]. |
|
11759
475918fd2d85
changed: #createVersionMethodFor:
Claus Gittinger <cg@exept.de>
parents:
11640
diff
changeset
|
2297 |
|
475918fd2d85
changed: #createVersionMethodFor:
Claus Gittinger <cg@exept.de>
parents:
11640
diff
changeset
|
2298 |
"Modified (comment): / 21-08-2012 / 11:54:57 / cg" |
9706 | 2299 |
! ! |
2300 |
||
2301 |
!SmalltalkCodeGeneratorTool methodsFor:'code templates'! |
|
2302 |
||
11169 | 2303 |
anyApplicationClassInProjectOf:aClass |
2304 |
|prjDefinition| |
|
2305 |
||
2306 |
prjDefinition := aClass projectDefinitionClass. |
|
2307 |
prjDefinition isNil ifTrue:[ ^ nil ]. |
|
2308 |
^ (prjDefinition allClasses |
|
2309 |
select:[:cls | cls isSubclassOf:ApplicationModel]) |
|
2310 |
firstIfEmpty:nil. |
|
2311 |
||
2312 |
"Created: / 21-01-2012 / 13:24:12 / cg" |
|
2313 |
! |
|
2314 |
||
9706 | 2315 |
codeFor_classInitialize |
2316 |
generateComments ifFalse:[ |
|
2317 |
^ |
|
2318 |
'initialize |
|
2319 |
%(INIT_CLASSINSTVARS) |
|
2320 |
%(INIT_CLASSVARS) |
|
2321 |
'. |
|
2322 |
]. |
|
2323 |
||
2324 |
^ |
|
2325 |
'initialize |
|
2326 |
"Invoked at system start or when the class is dynamically loaded." |
|
2327 |
||
2328 |
"/ please change as required (and remove this comment) |
|
2329 |
%(INIT_CLASSINSTVARS) |
|
2330 |
%(INIT_CLASSVARS) |
|
2331 |
'. |
|
2332 |
! |
|
2333 |
||
2334 |
codeFor_closeAccept |
|
2335 |
generateComments ifFalse:[ |
|
2336 |
^ |
|
2337 |
'closeAccept |
|
2338 |
^ super closeAccept |
|
2339 |
'. |
|
2340 |
]. |
|
2341 |
||
2342 |
^ |
|
2343 |
'closeAccept |
|
2344 |
"This is a hook method generated by the Browser/CodeGeneratorTool. |
|
2345 |
It will be invoked when your dialog-window is closed with OK." |
|
2346 |
||
2347 |
"/ add any actions as required here ... |
|
2348 |
Transcript showCR:''dialog accepted''. |
|
2349 |
||
2350 |
"/ do not remove the one below (otherwise, the dialog will not close itself)... |
|
2351 |
^ super closeAccept |
|
2352 |
'. |
|
2353 |
||
2354 |
"Created: / 27-10-2006 / 10:03:31 / cg" |
|
2355 |
! |
|
2356 |
||
2357 |
codeFor_closeDownViews |
|
2358 |
generateComments ifFalse:[ |
|
2359 |
^ |
|
2360 |
'closeDownViews |
|
2361 |
^ super closeDownViews |
|
2362 |
'. |
|
2363 |
]. |
|
2364 |
||
2365 |
^ |
|
2366 |
'closeDownViews |
|
2367 |
"This is a hook method generated by the Browser/CodeGeneratorTool. |
|
2368 |
It will be invoked when your app/dialog-window is really closed. |
|
2369 |
See also #closeDownViews, which is invoked before and may suppress the close |
|
2370 |
or ask the user for confirmation." |
|
2371 |
||
2372 |
"/ change the code below as required ... |
|
2373 |
"/ This should cleanup any leftover resources |
|
2374 |
"/ (for example, temporary files) |
|
2375 |
"/ super closeRequest will initiate the closeDown |
|
2376 |
||
2377 |
"/ add your code here |
|
2378 |
||
2379 |
"/ do not remove the one below ... |
|
2380 |
^ super closeDownViews |
|
2381 |
'. |
|
2382 |
||
2383 |
"Created: / 27-10-2006 / 10:01:32 / cg" |
|
2384 |
! |
|
2385 |
||
2386 |
codeFor_closeRequest |
|
2387 |
generateComments ifFalse:[ |
|
2388 |
^ |
|
2389 |
'closeRequest |
|
2390 |
self hasUnsavedChanges ifTrue:[ |
|
2391 |
(self confirm:(resources string:''Close without saving ?'')) ifFalse:[ |
|
2392 |
^ self |
|
2393 |
] |
|
2394 |
]. |
|
2395 |
^ super closeRequest |
|
2396 |
'. |
|
2397 |
]. |
|
2398 |
||
2399 |
^ |
|
2400 |
'closeRequest |
|
2401 |
"This is a hook method generated by the Browser/CodeGeneratorTool. |
|
2402 |
It will be invoked when your app/dialog-window is about to be |
|
2403 |
closed (this method has a chance to suppress the close). |
|
2404 |
See also #closeDownViews, which is invoked when the close is really done." |
|
2405 |
||
2406 |
"/ change the code below as required ... |
|
2407 |
"/ Closing can be suppressed, by simply returning. |
|
2408 |
"/ The ''super closeRequest'' at the end will initiate the real closeDown |
|
2409 |
||
2410 |
self hasUnsavedChanges ifTrue:[ |
|
2411 |
(self confirm:(resources string:''Close without saving ?'')) ifFalse:[ |
|
2412 |
^ self |
|
2413 |
] |
|
2414 |
]. |
|
2415 |
||
2416 |
^ super closeRequest |
|
2417 |
'. |
|
2418 |
||
2419 |
"Created: / 27-10-2006 / 10:01:06 / cg" |
|
2420 |
! |
|
2421 |
||
2422 |
codeFor_emptyMenuActionCodeFor:selector menuItem:item |
|
2423 |
generateComments ifFalse:[ |
|
2424 |
^ |
|
2425 |
selector,' |
|
2426 |
self warn:''no action for ''''',item,''''' defined.''. |
|
2427 |
'. |
|
2428 |
]. |
|
2429 |
||
2430 |
^ |
|
2431 |
selector,' |
|
2432 |
"This method was generated by the Browser/CodeGeneratorTool. |
|
2433 |
It will be invoked when the menu-item ''',item,''' is selected." |
|
2434 |
||
2435 |
"/ change below and add any actions as required here ... |
|
2436 |
self warn:''no action for ''''',item,''''' defined.''. |
|
2437 |
'. |
|
2438 |
||
2439 |
"Created: / 27-10-2006 / 10:16:43 / cg" |
|
2440 |
! |
|
2441 |
||
2442 |
codeFor_hasUnsavedChanges |
|
2443 |
generateComments ifFalse:[ |
|
2444 |
^ |
|
2445 |
'hasUnsavedChanges |
|
2446 |
^ false. |
|
2447 |
'. |
|
2448 |
]. |
|
2449 |
||
2450 |
^ |
|
2451 |
'hasUnsavedChanges |
|
16779 | 2452 |
"Return true, if any unsaved changes are present |
9706 | 2453 |
(i.e. the contents needs to be saved or else will be lost)" |
2454 |
||
2455 |
"/ add real code as required (or remove the halt and always return false)... |
|
2456 |
"/ self halt:''check this code''. |
|
2457 |
^ false. |
|
2458 |
'. |
|
2459 |
||
2460 |
"Created: / 27-10-2006 / 10:00:36 / cg" |
|
2461 |
! |
|
2462 |
||
2463 |
codeFor_menuSaveAs |
|
2464 |
^ |
|
2465 |
'menuSaveAs |
|
2466 |
"This method was generated by the Browser/CodeGeneratorTool. |
|
2467 |
It will be invoked when the menu-item ''saveAs'' is selected." |
|
2468 |
||
2469 |
"/ change below as required... (see examples in Dialog class for more options) |
|
2470 |
Dialog |
|
16779 | 2471 |
requestSaveFileName:(resources string:''Save'') |
2472 |
default:''foo.txt'' |
|
2473 |
fromDirectory:nil |
|
2474 |
action:[:fileName | self doSaveAs:fileName] |
|
9706 | 2475 |
appendAction:nil. |
2476 |
'. |
|
2477 |
||
2478 |
"Created: / 27-10-2006 / 10:01:57 / cg" |
|
2479 |
! |
|
2480 |
||
2481 |
codeFor_openAboutThisApplication |
|
2482 |
^ |
|
2483 |
'openAboutThisApplication |
|
2484 |
"This method was generated by the Browser/CodeGeneratorTool. |
|
2485 |
It will be invoked when the menu-item ''help-about'' is selected." |
|
2486 |
||
2487 |
"/ could open a customized aboutBox here ... |
|
2488 |
super openAboutThisApplication |
|
2489 |
'. |
|
2490 |
||
2491 |
"Created: / 27-10-2006 / 10:03:13 / cg" |
|
2492 |
! |
|
2493 |
||
2494 |
codeFor_openDocumentation |
|
2495 |
^ |
|
2496 |
'openDocumentation |
|
2497 |
"This method was generated by the Browser/CodeGeneratorTool. |
|
2498 |
It will be invoked when the menu-item ''help-documentation'' is selected." |
|
2499 |
||
2500 |
"/ change below as required ... |
|
2501 |
||
2502 |
"/ to open an HTML viewer on some document (under ''doc/online/<language>/'' ): |
|
2503 |
self openDocumentationFile:''TOP.html''. |
|
2504 |
||
2505 |
"/ add application-specific help files under the ''doc/online/<language>/help/appName'' |
|
2506 |
"/ directory, and open a viewer with: |
|
2507 |
"/ self openDocumentationFile:''help/<MyApplication>/TOP.html''. |
|
2508 |
'. |
|
2509 |
||
2510 |
"Created: / 27-10-2006 / 10:02:55 / cg" |
|
2511 |
! |
|
2512 |
||
2513 |
codeFor_postBuildWith |
|
2514 |
generateComments ifFalse:[ |
|
2515 |
^ |
|
2516 |
'postBuildWith:aBuilder |
|
2517 |
^ super postBuildWith:aBuilder |
|
2518 |
'. |
|
2519 |
]. |
|
2520 |
||
2521 |
^ |
|
2522 |
'postBuildWith:aBuilder |
|
2523 |
"This is a hook method generated by the Browser/CodeGeneratorTool. |
|
2524 |
It will be invoked during the initialization of your app/dialog, |
|
16779 | 2525 |
after all of the visual components have been built, |
9706 | 2526 |
but BEFORE the top window is made visible. |
2527 |
Add any app-specific actions here (reading files, setting up values etc.) |
|
2528 |
See also #postOpenWith:, which is invoked after opening." |
|
2529 |
||
2530 |
"/ add any code here ... |
|
2531 |
||
2532 |
^ super postBuildWith:aBuilder |
|
2533 |
'. |
|
2534 |
||
2535 |
"Created: / 27-10-2006 / 09:59:33 / cg" |
|
2536 |
! |
|
2537 |
||
2538 |
codeFor_postOpenWith |
|
2539 |
generateComments ifFalse:[ |
|
2540 |
^ |
|
2541 |
'postOpenWith:aBuilder |
|
2542 |
^ super postOpenWith:aBuilder |
|
2543 |
'. |
|
2544 |
]. |
|
2545 |
||
2546 |
^ |
|
2547 |
'postOpenWith:aBuilder |
|
2548 |
"This is a hook method generated by the Browser/CodeGeneratorTool. |
|
2549 |
It will be invoked right after the applications window has been opened. |
|
2550 |
Add any app-specific actions here (starting background processes etc.). |
|
2551 |
See also #postBuildWith:, which is invoked before opening." |
|
2552 |
||
2553 |
"/ add any code here ... |
|
2554 |
||
2555 |
^ super postOpenWith:aBuilder |
|
2556 |
'. |
|
2557 |
||
2558 |
"Created: / 27-10-2006 / 09:59:56 / cg" |
|
2559 |
! |
|
2560 |
||
11169 | 2561 |
codeFor_standAloneApplicationRegistryPathFor:aClassOrNil |
2562 |
|pkg appClass| |
|
2563 |
||
2564 |
pkg := 'someUniqueName'. |
|
2565 |
aClassOrNil notNil ifTrue:[ |
|
2566 |
(appClass := self anyApplicationClassInProjectOf:aClassOrNil) notNil ifTrue:[ |
|
2567 |
pkg := appClass nameWithoutPrefix. |
|
2568 |
] |
|
2569 |
]. |
|
2570 |
||
10591 | 2571 |
generateComments ifFalse:[ |
2572 |
^ |
|
2573 |
'applicationRegistryPath |
|
16779 | 2574 |
^ #(''stx'' ''%1'') |
11169 | 2575 |
' bindWith:pkg. |
10591 | 2576 |
]. |
2577 |
||
2578 |
^ |
|
2579 |
'applicationRegistryPath |
|
16779 | 2580 |
"for windows only: |
15515
895b2312db3e
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15069
diff
changeset
|
2581 |
the key under which this application stores its process ID in the registry |
10591 | 2582 |
as a collection of path-components. |
15515
895b2312db3e
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15069
diff
changeset
|
2583 |
i.e. if #(''foo'' ''bar'' ''baz'') is returned here, the current application''s ID will be stored |
10591 | 2584 |
in HKEY_CURRENT_USER\Software\foo\bar\baz\CurrentID. |
2585 |
(would also be used as a relative path for a temporary lock file under unix). |
|
2586 |
Used to detect if another instance of this application is already running." |
|
2587 |
||
11169 | 2588 |
^ #(''stx'' ''%1'') |
2589 |
' bindWith:pkg. |
|
2590 |
||
2591 |
"Created: / 21-01-2012 / 11:23:40 / cg" |
|
10591 | 2592 |
! |
2593 |
||
2594 |
codeFor_standAloneApplicationUUID |
|
2595 |
generateComments ifFalse:[ |
|
2596 |
^ |
|
2597 |
'applicationUUID |
|
16779 | 2598 |
^ UUID fromString:''', UUID genUUID printString, ''' |
10591 | 2599 |
'. |
2600 |
]. |
|
2601 |
||
2602 |
^ |
|
2603 |
'applicationUUID |
|
2604 |
"answer an application-specific unique uuid. |
|
2605 |
This is used as the name of some exclusive OS-resource, which is used to find out, |
|
2606 |
if another instance of this application is already running. |
|
2607 |
Under win32, a mutex is used; under unix, an exclusive file in the tempDir could be used. |
|
16779 | 2608 |
If redefined, please return a real UUID (i.e. UUID fromString:''.....'') and not a string or |
10591 | 2609 |
similar possibly conflicting identifier. |
16903 | 2610 |
You can paste a fresh worldwide unique id via the editor''s ''more''-''misc''-''paste UUID'' menu function." |
10591 | 2611 |
|
16779 | 2612 |
^ UUID fromString:''', UUID genUUID printString, ''' |
10591 | 2613 |
'. |
2614 |
||
2615 |
"Created: / 19-08-2011 / 02:13:19 / cg" |
|
2616 |
! |
|
2617 |
||
2618 |
codeFor_standAloneMain |
|
2619 |
^ |
|
2620 |
'main:argv |
|
16900 | 2621 |
', |
2622 |
(generateComments |
|
2623 |
ifTrue:[ |
|
2624 |
' "main entry; argv is the array of command arguments |
|
2625 |
(as array of words from space-separated command line). |
|
10591 | 2626 |
Parse arguments, and proceed to the real work function" |
2627 |
||
16900 | 2628 |
"you may want to put those into instvars, in order to be accessible from the realMain: function" |
2629 |
'] |
|
2630 |
ifFalse:[''] |
|
2631 |
), |
|
2632 |
' |aArgs bArgs cOption fileArgs helpOption| |
|
10591 | 2633 |
|
2634 |
aArgs := OrderedCollection new. |
|
2635 |
bArgs := OrderedCollection new. |
|
2636 |
fileArgs := OrderedCollection new. |
|
2637 |
cOption := helpOption := false. |
|
2638 |
||
16900 | 2639 |
', |
2640 |
(generateComments |
|
2641 |
ifTrue:[ |
|
2642 |
' "/ this is a template for getOpt usage: |
|
2643 |
"/ - will collect -a <...> arguments into aArgs, |
|
2644 |
"/ - will collect -b <...> arguments into bArgs, |
|
2645 |
"/ - will set cOption to true if there is a -c argument |
|
2646 |
"/ - will set helpOption to true if there is a -h argument |
|
2647 |
"/ - will show the usage and exit for a -? argument |
|
2648 |
'] |
|
2649 |
ifFalse:[''] |
|
2650 |
), |
|
2651 |
' (GetOpt new) |
|
14572
3ea2ce89adf0
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
14426
diff
changeset
|
2652 |
"/ at:$a |
3ea2ce89adf0
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
14426
diff
changeset
|
2653 |
"/ put:[:opt :arg | aArgs add:arg]; |
3ea2ce89adf0
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
14426
diff
changeset
|
2654 |
"/ at:$b |
3ea2ce89adf0
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
14426
diff
changeset
|
2655 |
"/ put:[:opt :arg | bArgs add:arg]; |
16900 | 2656 |
"/ at:$c |
14572
3ea2ce89adf0
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
14426
diff
changeset
|
2657 |
"/ put:[:opt | cOption := true]; |
10591 | 2658 |
at:$h |
2659 |
put:[:opt | helpOption := true]; |
|
16779 | 2660 |
at:$? |
16900 | 2661 |
put:[:arg | self class usage. ^ Smalltalk exitIfStandalone:0]; |
10591 | 2662 |
default:[:arg | fileArgs add:arg]; |
16914 | 2663 |
onError:[:msg | Stderr nextPutLine:msg. self class usage]; |
10591 | 2664 |
parse:argv. |
11169 | 2665 |
|
16779 | 2666 |
helpOption ifTrue:[ |
16900 | 2667 |
self class usage. |
10591 | 2668 |
] ifFalse:[ |
11169 | 2669 |
self realMain:argv. |
10598
6c40487903e5
changed: #codeFor_standAloneMain
Claus Gittinger <cg@exept.de>
parents:
10593
diff
changeset
|
2670 |
]. |
16900 | 2671 |
', |
2672 |
(generateComments |
|
2673 |
ifTrue:[ |
|
2674 |
' "/ (maybe) do not exit here; |
|
2675 |
"/ caller will go into event loop when a display has been opened and we return here |
|
2676 |
"/ for real non-ui apps, exit with an exitcode of 0 |
|
2677 |
'] |
|
2678 |
ifFalse:[''] |
|
2679 |
), |
|
2680 |
' ^ Smalltalk exitIfStandalone:0 |
|
10591 | 2681 |
'. |
2682 |
||
2683 |
"Created: / 19-08-2011 / 02:18:49 / cg" |
|
16672 | 2684 |
"Modified: / 06-06-2016 / 14:58:15 / cg" |
10591 | 2685 |
! |
2686 |
||
16900 | 2687 |
codeFor_standAloneMainOnClassSide |
2688 |
generateComments ifFalse:[ |
|
2689 |
^ |
|
2690 |
'main:argv |
|
2691 |
^ self new main:argv |
|
2692 |
'. |
|
2693 |
]. |
|
2694 |
||
2695 |
^ |
|
2696 |
'main:argv |
|
2697 |
"main entry; argv is the array of command arguments |
|
2698 |
(as array of words from space-separated command line). |
|
2699 |
Creates an instance of myself and calls the main: method on the instance side." |
|
2700 |
||
16903 | 2701 |
"/ to check if another instance is already running (win32 only for now) |
2702 |
"/ self checkForAndExitIfAnotherApplicationInstanceIsRunning |
|
2703 |
"/ (which tells the running app to open a new window on the passed in document file) |
|
2704 |
"/ see my superclass for how this works. |
|
2705 |
||
16900 | 2706 |
^ self new main:argv |
2707 |
||
2708 |
"to try from within the IDE: |
|
2709 |
self main:#(''-a'' ''someArg'' ''-h'') |
|
2710 |
when deployed, start at the command line with: |
|
2711 |
<nameOfApp> -a someArg -h |
|
2712 |
" |
|
2713 |
'. |
|
2714 |
! |
|
2715 |
||
11169 | 2716 |
codeFor_standAloneRealMainMethodFor:aClass |
2717 |
|appClass| |
|
2718 |
||
2719 |
(appClass := self anyApplicationClassInProjectOf:aClass) notNil ifTrue:[ |
|
2720 |
^ |
|
2721 |
'realMain:args |
|
2722 |
"opens the application" |
|
2723 |
||
2724 |
%1 open |
|
2725 |
' bindWith:appClass name |
|
2726 |
||
2727 |
]. |
|
2728 |
||
2729 |
^ |
|
2730 |
'realMain:argsToEcho |
|
11637 | 2731 |
"just a dummy example - does nothing but echo its arguments" |
11169 | 2732 |
|
11223
52963b06ef66
changed: #codeFor_standAloneRealMainMethodFor:
Claus Gittinger <cg@exept.de>
parents:
11169
diff
changeset
|
2733 |
Stdout nextPutLine:''Hello World''. |
11169 | 2734 |
Stdout nextPutLine:(argsToEcho asStringWith:Character space) |
2735 |
'. |
|
2736 |
||
2737 |
"Created: / 21-01-2012 / 12:28:48 / cg" |
|
2738 |
! |
|
2739 |
||
10591 | 2740 |
codeFor_standAloneUsage |
2741 |
^ |
|
2742 |
'usage |
|
16900 | 2743 |
"output some command-line usage infos on stderr" |
2744 |
||
10591 | 2745 |
Stderr nextPutLine:''usage: '',self applicationName,'' [options...]''. |
16900 | 2746 |
Stderr nextPutLine:'' --noInfoPrint ........ disable diagnostic messages''. |
2747 |
Stderr nextPutLine:'' -h ................... output this message''. |
|
2748 |
Stderr nextPutLine:'' -a <file> ............ a-argument(s)''. |
|
2749 |
Stderr nextPutLine:'' -b <file> ............ b-argument(s)''. |
|
2750 |
Stderr nextPutLine:'' -c ................... c-option''. |
|
10591 | 2751 |
'. |
2752 |
||
2753 |
"Created: / 19-08-2011 / 02:22:46 / cg" |
|
16672 | 2754 |
"Modified: / 06-06-2016 / 14:59:19 / cg" |
10591 | 2755 |
! |
2756 |
||
9706 | 2757 |
code_forWidget_buttonPress |
2758 |
generateComments ifFalse:[ |
|
2759 |
^ |
|
2760 |
'buttonPress:button x:x y:y |
|
2761 |
Transcript show:''button: ''; showCR:button. |
|
2762 |
super buttonPress:button x:x y:y |
|
2763 |
' |
|
2764 |
]. |
|
2765 |
||
2766 |
^ |
|
2767 |
'buttonPress:button x:x y:y |
|
2768 |
"called when a mouse-button is pressed. button is the button-nr (1 for left-button). |
|
2769 |
x/y are the mouse position at the time of the click. |
|
2770 |
There are also corresponding buttonRelease and buttonMotion methods which could be |
|
2771 |
redefined...." |
|
2772 |
||
2773 |
Transcript show:''button: ''; showCR:button. |
|
2774 |
"/ super-code handles middleButtonMenu, if it was assigned (with middleButtonmenu:) |
|
2775 |
super buttonPress:button x:x y:y |
|
2776 |
' |
|
2777 |
! |
|
2778 |
||
2779 |
code_forWidget_initialize |
|
2780 |
^ |
|
2781 |
'initialize |
|
2782 |
super initialize "/ to initialize inherited state |
|
2783 |
||
2784 |
"/ add code to initialize private variables, |
|
2785 |
"/ and sub-components as required. |
|
2786 |
' |
|
2787 |
! |
|
2788 |
||
2789 |
code_forWidget_keyPress |
|
2790 |
generateComments ifFalse:[ |
|
2791 |
^ |
|
2792 |
'keyPress:key x:x y:y |
|
2793 |
"/ key == #Copy ifTrue:[ |
|
2794 |
"/ ]. |
|
2795 |
"/ key == #Cut ifTrue:[ |
|
2796 |
"/ ]. |
|
2797 |
Transcript show:''key: ''; showCR:key. |
|
2798 |
super keyPress:key x:x y:y |
|
2799 |
' |
|
2800 |
]. |
|
2801 |
^ |
|
2802 |
'keyPress:key x:x y:y |
|
2803 |
"called when a keyboard-key was pressed. key is either a character (for ordinary keys) |
|
2804 |
or a symbol, such as #Copy, #Cut or #Paste. |
|
2805 |
x/y are the mouse position at the time of the key-press. |
|
2806 |
There is also a corresponding keyRelease method which could be redefined...." |
|
2807 |
||
2808 |
Transcript show:''key: ''; showCR:key. |
|
2809 |
super keyPress:key x:x y:y |
|
2810 |
' |
|
2811 |
! |
|
2812 |
||
2813 |
code_forWidget_redraw |
|
2814 |
|sel comment code| |
|
2815 |
||
2816 |
sel := 'redrawX:x y:y width:w height:h'. |
|
2817 |
generateComments ifFalse:[ |
|
2818 |
comment := ''. |
|
2819 |
] ifTrue:[ |
|
2820 |
comment := ' |
|
2821 |
"called to redraw a part of the widgets area. x/y define the origin, w/h the size of |
|
2822 |
that area. The clipping region has already been set by the caller, so even if the code |
|
16779 | 2823 |
below draws outside the redraw-area, it will not affect what is on the screen. |
9706 | 2824 |
Therefore, the example below can fill the rectangle in the redraw area, but still draw |
2825 |
the cross in the outside regions." |
|
2826 |
||
2827 |
'. |
|
2828 |
]. |
|
2829 |
||
2830 |
code := ' |
|
2831 |
self paint:Color red. |
|
2832 |
self fillRectangleX:x y:y width:w height:h. |
|
2833 |
||
2834 |
self paint:Color yellow. |
|
2835 |
self displayLineFrom:0@0 to:(width@height). |
|
2836 |
self displayLineFrom:width@0 to:(0@height). |
|
2837 |
'. |
|
2838 |
||
2839 |
^ sel,comment,code |
|
2840 |
! |
|
2841 |
||
2842 |
code_forWidget_sizeChanged |
|
2843 |
generateComments ifFalse:[ |
|
2844 |
^ |
|
2845 |
'sizeChanged:how |
|
2846 |
self invalidate. |
|
2847 |
super sizeChanged:how. |
|
2848 |
' |
|
2849 |
]. |
|
2850 |
||
2851 |
^ |
|
2852 |
'sizeChanged:how |
|
16779 | 2853 |
"Invoked whenever the size of the view changes. |
9706 | 2854 |
Here, we force a full redraw, which might not be needed all the time" |
2855 |
||
2856 |
self invalidate. |
|
2857 |
super sizeChanged:how. |
|
2858 |
' |
|
2859 |
! |
|
2860 |
||
2861 |
code_forWidget_update |
|
2862 |
generateComments ifFalse:[ |
|
2863 |
^ |
|
2864 |
'update:something with:aParameter from:changedObject |
|
2865 |
changedObject == model ifTrue:[ |
|
2866 |
self invalidate. |
|
2867 |
^ self |
|
2868 |
]. |
|
2869 |
super update:something with:aParameter from:changedObject |
|
2870 |
' |
|
2871 |
]. |
|
2872 |
||
2873 |
^ |
|
2874 |
'update:something with:aParameter from:changedObject |
|
2875 |
"Invoked when an object that I depend upon sends a change notification." |
|
2876 |
||
2877 |
"stub code automatically generated - please change as required" |
|
2878 |
||
2879 |
changedObject == model ifTrue:[ |
|
2880 |
self invalidate. |
|
2881 |
^ self |
|
2882 |
]. |
|
2883 |
super update:something with:aParameter from:changedObject |
|
2884 |
' |
|
2885 |
! ! |
|
2886 |
||
14700
37395f593258
Fixes in CodeGeneatorTool for to handle properly mixed-language classes.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14696
diff
changeset
|
2887 |
!SmalltalkCodeGeneratorTool methodsFor:'compilation'! |
37395f593258
Fixes in CodeGeneatorTool for to handle properly mixed-language classes.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14696
diff
changeset
|
2888 |
|
37395f593258
Fixes in CodeGeneatorTool for to handle properly mixed-language classes.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14696
diff
changeset
|
2889 |
compilerClass |
37395f593258
Fixes in CodeGeneatorTool for to handle properly mixed-language classes.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14696
diff
changeset
|
2890 |
"Return a real compiler classto use to compile code" |
37395f593258
Fixes in CodeGeneatorTool for to handle properly mixed-language classes.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14696
diff
changeset
|
2891 |
|
37395f593258
Fixes in CodeGeneatorTool for to handle properly mixed-language classes.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14696
diff
changeset
|
2892 |
^ Compiler |
37395f593258
Fixes in CodeGeneatorTool for to handle properly mixed-language classes.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14696
diff
changeset
|
2893 |
|
37395f593258
Fixes in CodeGeneatorTool for to handle properly mixed-language classes.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14696
diff
changeset
|
2894 |
"Created: / 05-08-2014 / 21:03:08 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
37395f593258
Fixes in CodeGeneatorTool for to handle properly mixed-language classes.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14696
diff
changeset
|
2895 |
! ! |
37395f593258
Fixes in CodeGeneatorTool for to handle properly mixed-language classes.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14696
diff
changeset
|
2896 |
|
9706 | 2897 |
!SmalltalkCodeGeneratorTool methodsFor:'private'! |
2898 |
||
2899 |
codeFor_shouldImplementFor:selector inClass:aClass |
|
2900 |
"used in the 'generate required protocol' to generate a shouldImplement-sending |
|
2901 |
method for each subclassClassresponsibility method above aClass." |
|
2902 |
||
2903 |
|mthd comment implClass methodBodyStream searcher errorMessageString| |
|
2904 |
||
2905 |
(aClass notNil |
|
2906 |
and:[ aClass superclass notNil ]) ifTrue:[ |
|
2907 |
implClass := aClass superclass whichClassImplements:selector. |
|
2908 |
]. |
|
2909 |
implClass isNil ifTrue:[ |
|
2910 |
^ ((Method methodDefinitionTemplateForSelector:selector),'\ ^ self shouldImplement\') withCRs |
|
2911 |
]. |
|
2912 |
||
2913 |
mthd := implClass compiledMethodAt:selector. |
|
2914 |
||
2915 |
methodBodyStream := '' writeStream. |
|
16779 | 2916 |
methodBodyStream |
9706 | 2917 |
nextPutAll:mthd methodDefinitionTemplate; cr; |
2918 |
nextPutAll:' "'. |
|
2919 |
||
2920 |
"/ include the comment of the subclassResponsibility-sending method |
|
2921 |
||
2922 |
comment := mthd methodComment. |
|
2923 |
comment isEmptyOrNil ifTrue:[ |
|
16779 | 2924 |
methodBodyStream |
2925 |
nextPutAll:('superclass <1s> says that I am responsible to implement this method' |
|
9706 | 2926 |
expandMacrosWith:implClass name) |
2927 |
] ifFalse:[ |
|
16779 | 2928 |
comment |
9706 | 2929 |
asStringCollection do:[:eachLine| |
2930 |
methodBodyStream nextPutAll:eachLine. |
|
2931 |
] separatedBy:[ |
|
2932 |
methodBodyStream cr; nextPutAll:' '. |
|
2933 |
]. |
|
2934 |
]. |
|
16779 | 2935 |
methodBodyStream |
9706 | 2936 |
nextPut:$"; cr; cr. |
2937 |
||
2938 |
"/ include the argument of the subclassResponsibility:-sending method |
|
2939 |
self canUseRefactoringSupport ifTrue:[ |
|
17330 | 2940 |
(mthd sendsSelector:#subclassResponsibility:) ifTrue:[ |
9706 | 2941 |
searcher := ParseTreeSearcher new. |
2942 |
searcher |
|
2943 |
matches: 'self subclassResponsibility: `''.*''' |
|
16779 | 2944 |
do:[:node :answer | |
9706 | 2945 |
errorMessageString := node arguments first value. |
2946 |
true. |
|
2947 |
]. |
|
2948 |
searcher executeTree: (mthd parseTree) initialAnswer: false. |
|
2949 |
]. |
|
2950 |
]. |
|
2951 |
errorMessageString notEmptyOrNil ifTrue:[ |
|
16779 | 2952 |
methodBodyStream |
2953 |
nextPutAll:' ^ self shouldImplement:'; |
|
9706 | 2954 |
nextPutLine:(errorMessageString storeString) |
2955 |
] ifFalse:[ |
|
16779 | 2956 |
methodBodyStream |
9706 | 2957 |
nextPutLine:' ^ self shouldImplement'. |
2958 |
]. |
|
2959 |
||
2960 |
^ methodBodyStream contents |
|
17330 | 2961 |
|
2962 |
"Modified: / 05-02-2017 / 01:25:01 / cg" |
|
9706 | 2963 |
! ! |
2964 |
||
2965 |
!SmalltalkCodeGeneratorTool class methodsFor:'documentation'! |
|
2966 |
||
10593
25f79d0bf830
changed: #codeFor_standAloneDoRealWorkMethod
Claus Gittinger <cg@exept.de>
parents:
10591
diff
changeset
|
2967 |
version |
15708
1b35e4573788
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15515
diff
changeset
|
2968 |
^ '$Header$' |
10593
25f79d0bf830
changed: #codeFor_standAloneDoRealWorkMethod
Claus Gittinger <cg@exept.de>
parents:
10591
diff
changeset
|
2969 |
! |
25f79d0bf830
changed: #codeFor_standAloneDoRealWorkMethod
Claus Gittinger <cg@exept.de>
parents:
10591
diff
changeset
|
2970 |
|
25f79d0bf830
changed: #codeFor_standAloneDoRealWorkMethod
Claus Gittinger <cg@exept.de>
parents:
10591
diff
changeset
|
2971 |
version_CVS |
15708
1b35e4573788
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15515
diff
changeset
|
2972 |
^ '$Header$' |
10593
25f79d0bf830
changed: #codeFor_standAloneDoRealWorkMethod
Claus Gittinger <cg@exept.de>
parents:
10591
diff
changeset
|
2973 |
! |
25f79d0bf830
changed: #codeFor_standAloneDoRealWorkMethod
Claus Gittinger <cg@exept.de>
parents:
10591
diff
changeset
|
2974 |
|
9960 | 2975 |
version_SVN |
15708
1b35e4573788
class: SmalltalkCodeGeneratorTool
Claus Gittinger <cg@exept.de>
parents:
15515
diff
changeset
|
2976 |
^ '$Id$' |
9706 | 2977 |
! ! |
12440 | 2978 |