[squeak-dev] Execute code when a Morph is destroyed

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

[squeak-dev] Execute code when a Morph is destroyed

Séverin Lemaignan
Hello,

I would like to execute some code (closing a serial port) when a morph
is destroyed by an user (either by clicking on the cross or by
drag&dropping the morph to the bin). Do you know how to do that?

As a bonus question, is it possible to detect, on the contrary, when a
morph is restored from the bin (to reestablish the communication) ?

Thank you!
Séverin


Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Execute code when a Morph is destroyed

Igor Stasenko
2009/1/6 Séverin Lemaignan <[hidden email]>:
> Hello,
>
> I would like to execute some code (closing a serial port) when a morph
> is destroyed by an user (either by clicking on the cross or by
> drag&dropping the morph to the bin). Do you know how to do that?
>
Use finalization for closing any external resources being used by
squeak-based objects.
Example:

WeakRegistry default add: myObject.

MyClass>>finalize
   self closeAllPorts.

> As a bonus question, is it possible to detect, on the contrary, when a
> morph is restored from the bin (to reestablish the communication) ?
>
Don't know about morph-specifics too much.

> Thank you!
> Séverin
>


--
Best regards,
Igor Stasenko AKA sig.


Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Execute code when a Morph is destroyed

Séverin Lemaignan
Thanks for your very quick reply!

As I understand it, the "finalize" method is called only upon object
destruction. But if the object is only dropped to the bin, it's not
destroyed, is it?

Anyway, finalization is already helpful!

Bye,
Séverin


On Tue, Jan 6, 2009 at 20:48, Igor Stasenko <[hidden email]> wrote:

> 2009/1/6 Séverin Lemaignan <[hidden email]>:
>> Hello,
>>
>> I would like to execute some code (closing a serial port) when a morph
>> is destroyed by an user (either by clicking on the cross or by
>> drag&dropping the morph to the bin). Do you know how to do that?
>>
> Use finalization for closing any external resources being used by
> squeak-based objects.
> Example:
>
> WeakRegistry default add: myObject.
>
> MyClass>>finalize
>   self closeAllPorts.
>
>> As a bonus question, is it possible to detect, on the contrary, when a
>> morph is restored from the bin (to reestablish the communication) ?
>>
> Don't know about morph-specifics too much.
>
>> Thank you!
>> Séverin
>>
>
>
> --
> Best regards,
> Igor Stasenko AKA sig.
>
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Execute code when a Morph is destroyed

Igor Stasenko
2009/1/6 Séverin Lemaignan <[hidden email]>:
> Thanks for your very quick reply!
>
> As I understand it, the "finalize" method is called only upon object
> destruction. But if the object is only dropped to the bin, it's not
> destroyed, is it?

I never dealt with bin in Morphic.
But something tells me that you employing wrong model :)
In a right way, you should use a separate class which operates with
com port, and morph should be a view of an instance of such object.
Then your question will lose context, since you have a model - which
acts by itself and closing external resources when it becomes garbage.
And you have a morph, which is a view of your model object, and you
can have as many views as you want to, and it is irrelevant if any of
them alive or not, putted in bin or somewhere else :)

>
> Anyway, finalization is already helpful!
>
> Bye,
> Séverin
>
>
> On Tue, Jan 6, 2009 at 20:48, Igor Stasenko <[hidden email]> wrote:
>> 2009/1/6 Séverin Lemaignan <[hidden email]>:
>>> Hello,
>>>
>>> I would like to execute some code (closing a serial port) when a morph
>>> is destroyed by an user (either by clicking on the cross or by
>>> drag&dropping the morph to the bin). Do you know how to do that?
>>>
>> Use finalization for closing any external resources being used by
>> squeak-based objects.
>> Example:
>>
>> WeakRegistry default add: myObject.
>>
>> MyClass>>finalize
>>   self closeAllPorts.
>>
>>> As a bonus question, is it possible to detect, on the contrary, when a
>>> morph is restored from the bin (to reestablish the communication) ?
>>>
>> Don't know about morph-specifics too much.
>>
>>> Thank you!
>>> Séverin
>>>
>>
>>
>> --
>> Best regards,
>> Igor Stasenko AKA sig.
>>
>>
>>
>>
>
>
>
>


--
Best regards,
Igor Stasenko AKA sig.


Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Execute code when a Morph is destroyed

Séverin Lemaignan
> I never dealt with bin in Morphic.
> But something tells me that you employing wrong model :)
> In a right way, you should use a separate class which operates with
> com port, and morph should be a view of an instance of such object.
> Then your question will lose context, since you have a model - which
> acts by itself and closing external resources when it becomes garbage.
> And you have a morph, which is a view of your model object, and you
> can have as many views as you want to, and it is irrelevant if any of
> them alive or not, putted in bin or somewhere else :)


It's definitely the way I do :-)
I've basically a SerialHandler, a Module and a ModuleMorph. The Module
class (the "C" of the MVC model) holds all the logic and lives outside
of the "morphic" world. But, to be "user friendly", I would like to
close the serial port when the ModuleMorph (the "V" :-) ) is put in
the garbage. So I need to call smthg in Module from ModuleMorph when
the morph is destroyed.
In parallel, your tip concerning finalization is useful at all events
for the Module class.

Bye,
Séverin


Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Execute code when a Morph is destroyed

Bert Freudenberg
In reply to this post by Igor Stasenko
On 06.01.2009, at 20:48, Igor Stasenko wrote:

> 2009/1/6 Séverin Lemaignan <[hidden email]>:
>> Hello,
>>
>> I would like to execute some code (closing a serial port) when a  
>> morph
>> is destroyed by an user (either by clicking on the cross or by
>> drag&dropping the morph to the bin). Do you know how to do that?

You could override #delete.

> Use finalization for closing any external resources being used by
> squeak-based objects.

Finalization should be used only as a last measure to avoid leaking  
system resources. It can take a very long time from the point where an  
object becomes unreachable until it is finalized.

> As a bonus question, is it possible to detect, on the contrary, when a
> morph is restored from the bin (to reestablish the communication) ?

Implementing #intoWorld: and #outOfWorld: should work (instead of  
putting this into #delete).

- Bert -