As we done before with controls, we make a createEvents() method and we put it at the end of the EditableLabel's constructor function. This method is reproduced here:

private function createEvents():void {
 _eventCollector.addEvent(_labelCtrl, UIMouseEvent.CLICK, swapHandler);
 _eventCollector.addEvent(_input, TextEvent.TEXT_FOCUS_OUT, swapHandler);
 _eventCollector.addEvent(_input, UIOEvent.DISPLAYED, onDisplayHandler);
}

We use the UIObject internal EventCollector instance,  _eventCollector, to register events. By this way, we ensure that all registred event will be automatically removed by the UIObject when calling the UIObject finalize() method.

When the user clicks on the label, (or when the edited text input loses the focus), we need to interchange both components. So, we create the swapHandler event that will detect the event caller and will call the swapControls() method.

private function swapHandler(e:Event):void {
 e.target == _labelCtrl ?
 swapControls(_labelCtrl, _input) : swapControls(_input, _labelCtrl);
}

The swapControls will be employed to switch the controls. By the same time, we will pass "label" and "width" parameters from the removed control to the control to be displayed.

private function swapControls(ctrl1:*, ctrl2:*):void {
 ctrl2.label = ctrl1.label;
 ctrl2.width = ctrl1.width;
 ctrl1.remove();
 ctrl2.display();
}


To improve the ergonomy of our EditableLabel object, we can set the focus to the text input each time it is displayed. This is the task of the onDisplayHandler event. The  onDisplayHandler method simply sets the TextImput focus property to true and add an event that switches both controls when the text input loses the focus. The EventCollector addOneShotEvent() method allows to register an event that will automatically be removed when it is fired.

private function onDisplayHandler(e:UIOEvent):void {
 _input.focus = true;
 _eventCollector.addOneShotEvent(_input, TextEvent.TEXT_FOCUS_OUT, swapHandler);
}

(Note that we use the org.flashapi.swing.event.TextEvent object instead of a flash.events. TextEvent object.)