You can do this with the least amount of code by creating a class to redirect the events. What you want is a class that accepts a source event dispatcher, source event name, target event name, and target event dispatcher. Say you call this EventRedispatcher. Here's a complete example.
EventRedispatcherTest.mxml:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:local="*"
layout="horizontal"
width="100%"
height="100%">
<mx:Script>
<![CDATA[
import mx.events.ListEvent;
private function doLog(event:Event):void {
var extraInfo:String = "";
var listEvent:ListEvent = event as ListEvent;
if (listEvent != null && listEvent.itemRenderer != null) {
extraInfo = String(listEvent.itemRenderer.data);
}
var mouseEvent:MouseEvent = event as MouseEvent;
if (mouseEvent != null) {
extraInfo = mouseEvent.stageX + "," + mouseEvent.stageY;
}
log.text += event.target.id + "." + event.type + ":" + extraInfo + "\n";
}
]]>
</mx:Script>
<mx:TextArea width="300" height="100%" id="log" />
<local:EventRedispatcherComponent
id="component1"
listboxChange="doLog(event)"
comboboxChange="doLog(event)"
buttonClick="doLog(event)"
/>
<local:EventRedispatcherComponent
id="component2"
listboxChange="doLog(event)"
comboboxChange="doLog(event)"
buttonClick="doLog(event)"
/>
</mx:Application>
EventRedispatcherComponent.mxml:
<?xml version="1.0" encoding="utf-8"?>
<mx:HBox
xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="init()"
borderStyle="solid"
borderColor="#FF0000"
width="300"
height="200">
<mx:Metadata>
[Event(name="comboboxChange", type="mx.events.ListEvent")]
[Event(name="listboxChange", type="mx.events.ListEvent")]
[Event(name="buttonClick", type="flash.events.MouseEvent")]
</mx:Metadata>
<mx:Script>
<![CDATA[
private function init():void
{
// TODO: Create EventRedispatcher class :-)
new EventRedispatcher(combobox, "change", this, "comboboxChange");
new EventRedispatcher(listbox, "change", this, "listboxChange");
new EventRedispatcher(button, "click", this, "buttonClick");
}
]]>
</mx:Script>
<mx:ComboBox id="combobox" dataProvider="{[1, 2, 3]}" />
<mx:List id="listbox" dataProvider="{[1, 2, 3]}" />
<mx:Button id="button" label="Text" />
</mx:HBox>
EventRedispatcher.as
package
{
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.utils.describeType;
import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;
public class EventRedispatcher
{
private var targetDispatcher:EventDispatcher;
private var targetName:String;
private static var propertiesByEventType:Object = new Object();
public function EventRedispatcher(sourceDispatcher:EventDispatcher, sourceName:String, targetDispatcher:EventDispatcher, targetName:String)
{
this.targetDispatcher = targetDispatcher;
this.targetName = targetName;
sourceDispatcher.addEventListener(sourceName, redispatch);
}
private function redispatch(event:Event):void {
var newEvent:Event = copyEvent(event);
targetDispatcher.dispatchEvent(newEvent);
}
private function copyEvent(event:Event):Event {
var className:String = getQualifiedClassName(event);
var newEvent:Event = new (getDefinitionByName(className))(targetName);
var properties:Array = getPropertiesForClass(event, className);
for each(var propertyName:String in properties) {
newEvent[propertyName] = event[propertyName];
}
return newEvent;
}
private function getPropertiesForClass(event:Event, className:String):Array {
var properties:Array = propertiesByEventType[className];
if (properties != null) {
return properties;
}
var description:XML = describeType(event);
properties = new Array();
for each(var accessor:XML in description.accessor.(@access == 'readwrite')) {
properties.push(accessor.@name);
}
for each(var variable:XML in description.variable) {
properties.push(variable.@name);
}
propertiesByEventType[className] = properties;
return properties;
}
}
}