author | Claus Gittinger <cg@exept.de> |
Tue, 09 Jul 2019 20:55:17 +0200 | |
changeset 24417 | 03b083548da2 |
parent 24021 | d0173ca4be64 |
child 24732 | 26eebb19b868 |
permissions | -rw-r--r-- |
22888 | 1 |
"{ Encoding: utf8 }" |
2 |
||
4466
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
3 |
" |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
4 |
COPYRIGHT (c) 1999 by eXept Software AG |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
5 |
All Rights Reserved |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
6 |
|
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
7 |
This software is furnished under a license and may be used |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
8 |
only in accordance with the terms of that license and with the |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
9 |
inclusion of the above copyright notice. This software may not |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
10 |
be provided or otherwise made available to, or used by, any |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
11 |
other person. No title to or ownership of the software is |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
12 |
hereby transferred. |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
13 |
" |
6206 | 14 |
"{ Package: 'stx:libbasic' }" |
15 |
||
17598 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
8817
387971ee2b30
Notification is not an exception.
Claus Gittinger <cg@exept.de>
parents:
8778
diff
changeset
|
18 |
GenericException subclass:#Notification |
13674 | 19 |
instanceVariableNames:'tag' |
4443 | 20 |
classVariableNames:'' |
21 |
poolDictionaries:'' |
|
22 |
category:'Kernel-Exceptions' |
|
23 |
! |
|
24 |
||
25 |
!Notification class methodsFor:'documentation'! |
|
26 |
||
4466
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
27 |
copyright |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
28 |
" |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
29 |
COPYRIGHT (c) 1999 by eXept Software AG |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
30 |
All Rights Reserved |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
31 |
|
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
32 |
This software is furnished under a license and may be used |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
33 |
only in accordance with the terms of that license and with the |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
34 |
inclusion of the above copyright notice. This software may not |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
35 |
be provided or otherwise made available to, or used by, any |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
36 |
other person. No title to or ownership of the software is |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
37 |
hereby transferred. |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
38 |
" |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
39 |
|
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
40 |
! |
9cba6f4ecec4
copyrights added; typo in docu
Claus Gittinger <cg@exept.de>
parents:
4443
diff
changeset
|
41 |
|
4443 | 42 |
documentation |
43 |
" |
|
12277 | 44 |
Notification is the superclass of all notification signals in the system. |
11200 | 45 |
contrast to regular exceptions, Notifications are ignored, if no handler is present |
46 |
and a default value is returned from the raise. |
|
15680 | 47 |
When a handler finishes, the do-block is proceeded with the exception handler's value |
8701
bd5034470a47
Optional code to make the default handler action to proceed.
Stefan Vogel <sv@exept.de>
parents:
8698
diff
changeset
|
48 |
(an Exception does a return in this case.). |
4443 | 49 |
|
11200 | 50 |
Thanks to proceedable exceptions, Notifications allow for non-GUI model code to provide |
51 |
user notifications which are optionally shown. |
|
52 |
Very useful, for example to provide progress information from a method which can be |
|
53 |
invoked both from a GUI-Tool (where notifications are wanted) and also from the system, |
|
54 |
where such notifications are not desired. |
|
55 |
||
4443 | 56 |
[author:] |
57 |
Stefan Vogel |
|
58 |
||
59 |
[see also:] |
|
15668 | 60 |
Signal QuerySignal Query |
4443 | 61 |
" |
62 |
! |
|
63 |
||
4550 | 64 |
examples |
4443 | 65 |
" |
6431 | 66 |
an up-notification from a deeply nested operation to a higher level: |
4550 | 67 |
[exBegin] |
68 |
|zero| |
|
69 |
||
70 |
zero := 0. |
|
6431 | 71 |
Notification handle:[:n | |
72 |
Transcript showCR:'Please note that: ' , n description. |
|
73 |
n proceedWith:true |
|
4550 | 74 |
] do:[ |
75 |
'nesting'. |
|
76 |
[ |
|
77 |
[ |
|
8516 | 78 |
Error handle:[:ex | |
4550 | 79 |
Transcript showCR:'some error: ' , ex errorString. |
80 |
ex proceed |
|
81 |
] do:[ |
|
82 |
[ |
|
83 |
1 // zero. 'an error which is caught in the handler'. |
|
6431 | 84 |
Notification notify:'hello world' |
4550 | 85 |
] value |
86 |
] |
|
87 |
] value |
|
88 |
] value |
|
89 |
] |
|
90 |
[exEnd] |
|
4443 | 91 |
" |
92 |
! ! |
|
93 |
||
94 |
!Notification class methodsFor:'initialization'! |
|
95 |
||
96 |
initialize |
|
97 |
||
4550 | 98 |
NotifierString := 'Notification:' |
99 |
! ! |
|
100 |
||
101 |
!Notification class methodsFor:'answering queries'! |
|
102 |
||
103 |
answer:someAnswer do:aBlock |
|
104 |
"evaluate the argument, aBlock. |
|
12060 | 105 |
If the receiver is queried during evaluation, answer and proceed with someAnswer. |
4550 | 106 |
This is a wrapper for #handle:do: for lazy typists; no new functionality." |
107 |
||
8698
948da171e6e5
Make #handle - contexts also #return-able
Stefan Vogel <sv@exept.de>
parents:
8692
diff
changeset
|
108 |
<context: #return> |
4550 | 109 |
<exception: #handle> |
110 |
||
111 |
"/ thisContext markForHandle. -- same as above pragma |
|
5227
73a2dc34cb42
#answer:do: now returns the evaluated blocks value
Claus Gittinger <cg@exept.de>
parents:
4605
diff
changeset
|
112 |
^ aBlock value. "the real logic is in Exception>>doRaise" |
4550 | 113 |
|
114 |
" |
|
115 |
Notification answer:true do:[ |
|
116 |
Transcript showCR:'query answers: ' , (Query query printString). |
|
117 |
] |
|
118 |
" |
|
4443 | 119 |
|
120 |
" |
|
4550 | 121 |
Notification answer:false do:[ |
122 |
Transcript showCR:'first query answers: ' , (Query query printString). |
|
123 |
Query answer:true do:[ |
|
124 |
Transcript showCR:'second query answers: ' , (Query query printString). |
|
125 |
] |
|
126 |
] |
|
4443 | 127 |
" |
128 |
||
4550 | 129 |
"Created: / 10.7.1996 / 15:08:20 / cg" |
130 |
"Modified: / 14.10.1996 / 16:59:18 / cg" |
|
131 |
"Modified: / 25.7.1999 / 23:12:19 / stefan" |
|
132 |
! ! |
|
133 |
||
12084 | 134 |
|
4550 | 135 |
!Notification class methodsFor:'queries'! |
136 |
||
23304 | 137 |
accepts:aSignalOrExceptionClass |
4550 | 138 |
"return true, if the receiver accepts the argument, aSignal. |
139 |
(i.e. the receiver is aSignal or a parent of it). False otherwise." |
|
140 |
||
141 |
|s| |
|
142 |
||
23304 | 143 |
self == aSignalOrExceptionClass ifTrue:[^ true]. |
144 |
aSignalOrExceptionClass isQuerySignal ifFalse:[^ false]. |
|
4550 | 145 |
|
23304 | 146 |
s := aSignalOrExceptionClass. |
147 |
[(s := s parent) notNil] whileTrue:[ |
|
4550 | 148 |
self == s ifTrue:[^ true]. |
149 |
]. |
|
150 |
^ false |
|
151 |
||
23304 | 152 |
"Modified: / 22-03-1999 / 12:45:32 / stefan" |
153 |
"Created: / 23-07-1999 / 15:18:00 / stefan" |
|
154 |
"Modified (format): / 28-08-2018 / 11:15:50 / Claus Gittinger" |
|
4550 | 155 |
! |
156 |
||
157 |
defaultAnswer |
|
158 |
"Return the default answer to the Query. This method is called, |
|
159 |
if nobody catches the signal. |
|
160 |
||
161 |
Subclasses may redefine this method." |
|
162 |
||
19778 | 163 |
"Calling raiseRequest here will execute the exception's action method |
164 |
which returns the defaultResumeValue." |
|
4550 | 165 |
|
19778 | 166 |
^ super raiseRequest |
4550 | 167 |
|
168 |
"Created: / 23.7.1999 / 15:16:03 / stefan" |
|
169 |
! |
|
170 |
||
171 |
handlerForSignal:signal context:theContext originator:originator |
|
172 |
"answer the handler block for the signal from originator. |
|
173 |
The block is retrieved from aContext. |
|
174 |
Answer nil if the signal is not handled" |
|
175 |
||
176 |
|arg| |
|
177 |
||
178 |
theContext selector == #'answer:do:' ifTrue:[ |
|
179 |
(self == signal or:[self accepts:signal]) ifTrue:[ |
|
180 |
arg := theContext argAt:1. |
|
181 |
^ [:ex| ex proceedWith:arg]. |
|
182 |
] |
|
183 |
] ifFalse:[ |
|
184 |
^ super handlerForSignal:signal context:theContext originator:originator. |
|
185 |
]. |
|
186 |
||
187 |
^ nil |
|
188 |
||
189 |
"Created: / 25.7.1999 / 23:11:55 / stefan" |
|
190 |
! |
|
191 |
||
8681
38aa963476a5
Support for Block>>#on:do:on:do:
Stefan Vogel <sv@exept.de>
parents:
8516
diff
changeset
|
192 |
handles:anException |
38aa963476a5
Support for Block>>#on:do:on:do:
Stefan Vogel <sv@exept.de>
parents:
8516
diff
changeset
|
193 |
"return true, if the receiver handles the argument, anException. |
38aa963476a5
Support for Block>>#on:do:on:do:
Stefan Vogel <sv@exept.de>
parents:
8516
diff
changeset
|
194 |
(i.e. the receiver is anExceptions signal or a parent of it)" |
38aa963476a5
Support for Block>>#on:do:on:do:
Stefan Vogel <sv@exept.de>
parents:
8516
diff
changeset
|
195 |
|
23304 | 196 |
^ self accepts:(anException creator). |
8681
38aa963476a5
Support for Block>>#on:do:on:do:
Stefan Vogel <sv@exept.de>
parents:
8516
diff
changeset
|
197 |
|
23304 | 198 |
"/ |signal| |
199 |
"/ |
|
200 |
"/ signal := anException creator. |
|
201 |
"/ |
|
202 |
"/ self == signal ifTrue:[^ true]. "quick check" |
|
203 |
"/ anException isNotification ifFalse:[^ false]. "speed up non-queries by not traversing the parent chain" |
|
204 |
"/ |
|
205 |
"/ [(signal := signal parent) notNil] whileTrue:[ |
|
206 |
"/ self == signal ifTrue:[^ true]. |
|
207 |
"/ ]. |
|
208 |
"/ ^ false |
|
8681
38aa963476a5
Support for Block>>#on:do:on:do:
Stefan Vogel <sv@exept.de>
parents:
8516
diff
changeset
|
209 |
|
23304 | 210 |
"Modified: / 28-08-2018 / 11:26:39 / Claus Gittinger" |
8681
38aa963476a5
Support for Block>>#on:do:on:do:
Stefan Vogel <sv@exept.de>
parents:
8516
diff
changeset
|
211 |
! |
38aa963476a5
Support for Block>>#on:do:on:do:
Stefan Vogel <sv@exept.de>
parents:
8516
diff
changeset
|
212 |
|
4550 | 213 |
isQuerySignal |
4605 | 214 |
"return true, if this is a querySignal - always return true here" |
4550 | 215 |
|
216 |
^ true |
|
217 |
||
218 |
"Created: / 23.7.1999 / 14:59:50 / stefan" |
|
219 |
! |
|
220 |
||
6431 | 221 |
notify:aMessageString |
15680 | 222 |
"raise the query - return the handler's value, or the default |
6431 | 223 |
value, if there is no handler." |
224 |
||
16283 | 225 |
"/ cg: must be redefined, so we get a reasonable originator |
22092 | 226 |
"/ some apps depend on an originator being the non-exception context here. |
16283 | 227 |
"/ (do not do a self raiseRequestErrorString: here) |
228 |
^ self basicNew |
|
229 |
raiseRequestErrorString:aMessageString in:thisContext sender |
|
22092 | 230 |
|
231 |
"Modified (comment): / 25-07-2017 / 09:53:25 / stefan" |
|
6431 | 232 |
! |
233 |
||
19796 | 234 |
notify:aMessageString with:aParameter |
235 |
"raise the query - return the handler's value, or the default |
|
236 |
value, if there is no handler." |
|
237 |
||
238 |
"/ cg: must be redefined, so we get a reasonable originator |
|
239 |
"/ some apps depend on an originator being the non-exception context here. |
|
240 |
"/ (do not do a self raiseRequestErrorString: here) |
|
241 |
^ self basicNew |
|
242 |
parameter:aParameter; |
|
243 |
raiseRequestErrorString:aMessageString in:thisContext sender |
|
244 |
! |
|
245 |
||
4550 | 246 |
query |
15680 | 247 |
"raise the query - return the handler's value, or the default |
4550 | 248 |
value, if there is no handler. |
249 |
Invoking the handler is exactly the functionality of Signal>>raiseRequest, |
|
8778 | 250 |
but we can do it faster here (avoiding the construction of an exception instance)." |
4550 | 251 |
|
19777 | 252 |
^ self raiseAsQuery |
4550 | 253 |
! |
254 |
||
22888 | 255 |
queryWith:aParameter |
256 |
"raise the query - return the handler's value, or the default |
|
257 |
value, if there is no handler. |
|
258 |
Invoking the handler is exactly the functionality of Signal>>raiseRequest, |
|
259 |
but we can do it faster here (avoiding the construction of an exception instance)." |
|
260 |
||
261 |
^ self raiseRequestWith:aParameter |
|
262 |
! |
|
263 |
||
4550 | 264 |
raise |
15849 | 265 |
"Notifications are proceedable by definition, |
4550 | 266 |
so they should be raised with #query or #raiseRequest" |
267 |
||
268 |
^ self shouldNotImplement |
|
269 |
||
270 |
"Created: / 23.7.1999 / 15:19:17 / stefan" |
|
271 |
! |
|
272 |
||
273 |
raiseRequest |
|
274 |
"redefined to use #query" |
|
275 |
||
19777 | 276 |
^ self raiseAsQuery |
4550 | 277 |
|
278 |
"Created: / 25.7.1999 / 23:25:59 / stefan" |
|
4443 | 279 |
! ! |
280 |
||
15849 | 281 |
!Notification class methodsFor:'utilities'! |
282 |
||
283 |
deprecated |
|
284 |
"invoked by some open source packages (SOAP and YAXO, for example)" |
|
285 |
||
286 |
self notify:'called deprecated interface' |
|
287 |
! ! |
|
288 |
||
13674 | 289 |
!Notification methodsFor:'accessing'! |
290 |
||
291 |
tag |
|
292 |
"for squeak compatibility" |
|
293 |
||
294 |
^ tag |
|
295 |
||
296 |
"Modified (comment): / 11-09-2011 / 16:40:54 / cg" |
|
297 |
! |
|
298 |
||
299 |
tag:aSzmbol |
|
300 |
"for squeak compatibility" |
|
301 |
||
302 |
tag := aSzmbol. |
|
303 |
||
304 |
"Modified (format): / 11-09-2011 / 16:41:01 / cg" |
|
305 |
! ! |
|
306 |
||
4443 | 307 |
!Notification methodsFor:'default actions'! |
308 |
||
4525 | 309 |
defaultAction |
4550 | 310 |
"the default action is to return the default value. |
311 |
Subclasses may redefine this" |
|
4443 | 312 |
|
8683
a162510a0408
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
8681
diff
changeset
|
313 |
|handlerBlock| |
a162510a0408
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
8681
diff
changeset
|
314 |
|
a162510a0408
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
8681
diff
changeset
|
315 |
"try per signal handler. |
a162510a0408
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
8681
diff
changeset
|
316 |
I may have been created from a QuerySignal" |
a162510a0408
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
8681
diff
changeset
|
317 |
|
15285 | 318 |
(handlerBlock := self creator handlerBlock) notNil ifTrue:[ |
8683
a162510a0408
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
8681
diff
changeset
|
319 |
"... and call it" |
a162510a0408
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
8681
diff
changeset
|
320 |
^ handlerBlock value:self. |
a162510a0408
QuerySignal generates Notifications instead of Exceptions.
Stefan Vogel <sv@exept.de>
parents:
8681
diff
changeset
|
321 |
]. |
6451
f22b12c0349a
renamed #defaultValue -> #defaultResumeValue (vw54 compat.)
Claus Gittinger <cg@exept.de>
parents:
6431
diff
changeset
|
322 |
^ self defaultResumeValue |
4443 | 323 |
|
4550 | 324 |
"Modified: / 23.7.1999 / 15:13:34 / stefan" |
4443 | 325 |
! ! |
326 |
||
14948 | 327 |
!Notification methodsFor:'helpers'! |
328 |
||
329 |
hasDialog |
|
330 |
"answer true, if we can use a Dialog window" |
|
331 |
||
332 |
(Smalltalk isInitialized |
|
333 |
and:[Dialog notNil |
|
334 |
and:[Screen notNil |
|
335 |
and:[Screen current notNil |
|
336 |
and:[Screen current isOpenAndDispatching |
|
337 |
]]]]) ifTrue:[ |
|
338 |
Dialog autoload. "in case its autoloaded" |
|
339 |
^ true. |
|
340 |
]. |
|
341 |
^ false |
|
342 |
! ! |
|
343 |
||
12042 | 344 |
!Notification methodsFor:'queries'! |
345 |
||
19796 | 346 |
notify |
347 |
"notice the implementation on the class-side: if no additional parameters are to be passed, |
|
348 |
we do not even arrive here, because query has inlined the raiseRequest code" |
|
349 |
||
350 |
^ self raiseRequestIn:thisContext sender |
|
351 |
! |
|
352 |
||
353 |
notify:aMessageString |
|
354 |
"notice the implementation on the class-side: if no additional parameters are to be passed, |
|
355 |
we do not even arrive here, because query has inlined the raiseRequest code" |
|
356 |
||
357 |
^ self raiseRequestErrorString:aMessageString in:thisContext sender |
|
358 |
! |
|
359 |
||
12042 | 360 |
query |
361 |
"notice the implementation on the class-side: if no additional parameters are to be passed, |
|
362 |
we do not even arrive here, because query has inlined the raiseRequest code" |
|
363 |
||
19796 | 364 |
^ self raiseRequestIn:thisContext sender |
12042 | 365 |
! ! |
366 |
||
8681
38aa963476a5
Support for Block>>#on:do:on:do:
Stefan Vogel <sv@exept.de>
parents:
8516
diff
changeset
|
367 |
!Notification methodsFor:'testing'! |
38aa963476a5
Support for Block>>#on:do:on:do:
Stefan Vogel <sv@exept.de>
parents:
8516
diff
changeset
|
368 |
|
38aa963476a5
Support for Block>>#on:do:on:do:
Stefan Vogel <sv@exept.de>
parents:
8516
diff
changeset
|
369 |
isNotification |
38aa963476a5
Support for Block>>#on:do:on:do:
Stefan Vogel <sv@exept.de>
parents:
8516
diff
changeset
|
370 |
^ true |
24021 | 371 |
! |
372 |
||
373 |
suppressDialogIfUnhandled |
|
374 |
"controls if a dialog is to be shown |
|
375 |
in the default action when unhandled, or not. |
|
376 |
Can be redefined by subclasses" |
|
377 |
||
378 |
^ false |
|
379 |
||
380 |
"Created: / 29-03-2019 / 10:55:19 / Claus Gittinger" |
|
8681
38aa963476a5
Support for Block>>#on:do:on:do:
Stefan Vogel <sv@exept.de>
parents:
8516
diff
changeset
|
381 |
! ! |
38aa963476a5
Support for Block>>#on:do:on:do:
Stefan Vogel <sv@exept.de>
parents:
8516
diff
changeset
|
382 |
|
4443 | 383 |
!Notification class methodsFor:'documentation'! |
384 |
||
385 |
version |
|
18675 | 386 |
^ '$Header$' |
12042 | 387 |
! |
388 |
||
389 |
version_CVS |
|
18675 | 390 |
^ '$Header$' |
4443 | 391 |
! ! |
8516 | 392 |
|
14948 | 393 |
|
4443 | 394 |
Notification initialize! |