Skip to content
Permalink
Browse files
Modification to ArrayList and VectorList to correctly find the index …
…when itemUpdated() is invoked. Modifications to the unit tests to demonstrate and test this new behavior

git-svn-id: https://svn.apache.org/repos/asf/incubator/flex/whiteboard@1311914 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
Michael Labriola committed Apr 10, 2012
1 parent c445d3b commit 13fb9deef9eb51b8d62d7642f9b2b937a4a6d0e0
Showing 4 changed files with 75 additions and 24 deletions.
@@ -568,8 +568,17 @@ public class ArrayList extends EventDispatcher
event.property = property;
event.oldValue = oldValue;
event.newValue = newValue;

itemUpdateHandler(event);

//This handler was intended to handle events from child objects, not to be called directly
//itemUpdateHandler(event);

internalDispatchEvent(CollectionEventKind.UPDATE, event);

// need to dispatch object event now
if (_dispatchEvents == 0 && hasEventListener(PropertyChangeEvent.PROPERTY_CHANGE))
{
dispatchPropertyChangeEventClone( event, item );
}
}

/**
@@ -634,6 +643,26 @@ public class ArrayList extends EventDispatcher
//
//--------------------------------------------------------------------------

/**
* Dispatches a PropertyChangeEvent clone either from a child object whose event needs to be redispatched
* or when a PropertyChangeEvent is faked inside of this class for the purposes of informing the view
* of an update to underlying data.
*
* @param event The PropertyChangeEvent to be cloned and dispatched
* @param item The item within the view that was updated.
*
* @see mx.core.IPropertyChangeNotifier
* @see mx.events.PropertyChangeEvent
*/

private function dispatchPropertyChangeEventClone( event:PropertyChangeEvent, item:Object ):void {
var objEvent:PropertyChangeEvent = PropertyChangeEvent(event.clone());

var index:int = getItemIndex( item );
objEvent.property = index.toString() + "." + event.property;
dispatchEvent(objEvent);
}

/**
* Enables event dispatch for this list.
*
@@ -721,17 +750,12 @@ public class ArrayList extends EventDispatcher
protected function itemUpdateHandler(event:PropertyChangeEvent):void
{
internalDispatchEvent(CollectionEventKind.UPDATE, event);
// need to dispatch object event now
if (_dispatchEvents == 0 && hasEventListener(PropertyChangeEvent.PROPERTY_CHANGE))
{
var objEvent:PropertyChangeEvent = PropertyChangeEvent(event.clone());
//When itemUpdated is called, there is no event target. This means getItemIndex returns a -1
//Since this was originally cast as a uint, this caused many strange results. Changing it to
//an int to be consistent throughout other uses of this event
var index:int = getItemIndex(event.target);
objEvent.property = index.toString() + "." + event.property;
dispatchEvent(objEvent);
}

// need to dispatch object event now
if (_dispatchEvents == 0 && hasEventListener(PropertyChangeEvent.PROPERTY_CHANGE))
{
dispatchPropertyChangeEventClone( event, event.target );
}
}

/**
@@ -470,9 +470,15 @@ public class VectorList extends EventDispatcher
event.oldValue = oldValue;
event.newValue = newValue;

itemUpdateHandler(event);
internalDispatchEvent(CollectionEventKind.UPDATE, event);

// need to dispatch object event now
if (_dispatchEvents == 0 && hasEventListener(PropertyChangeEvent.PROPERTY_CHANGE))
{
dispatchPropertyChangeEventClone( event, item );
}
}

/**
* Return an Vector that is populated in the same order as the IList
* implementation.
@@ -505,6 +511,26 @@ public class VectorList extends EventDispatcher
//
//--------------------------------------------------------------------------

/**
* Dispatches a PropertyChangeEvent clone either from a child object whose event needs to be redispatched
* or when a PropertyChangeEvent is faked inside of this class for the purposes of informing the view
* of an update to underlying data.
*
* @param event The PropertyChangeEvent to be cloned and dispatched
* @param item The item within the view that was updated.
*
* @see mx.core.IPropertyChangeNotifier
* @see mx.events.PropertyChangeEvent
*/

private function dispatchPropertyChangeEventClone( event:PropertyChangeEvent, item:Object ):void {
var objEvent:PropertyChangeEvent = PropertyChangeEvent(event.clone());

var index:int = getItemIndex( item );
objEvent.property = index.toString() + "." + event.property;
dispatchEvent(objEvent);
}

/**
* Enables event dispatch for this list.
*/
@@ -573,10 +599,7 @@ public class VectorList extends EventDispatcher
// need to dispatch object event now
if (_dispatchEvents == 0 && hasEventListener(PropertyChangeEvent.PROPERTY_CHANGE))
{
var objEvent:PropertyChangeEvent = PropertyChangeEvent(event.clone());
var index:int = getItemIndex(event.target);
objEvent.property = index.toString() + "." + event.property;
dispatchEvent(objEvent);
dispatchPropertyChangeEventClone( event, event.target );
}
}

@@ -233,6 +233,7 @@ package mx.collections.tests.arrayList {

[Test(description="This test relies upon the patched version of ArrayList in my whiteboard")]
public function itemUpdatedShouldDispatchPropertyChangeEvent():void {
const index:int = 1;
const propertyName:String = "dummy";
const propertyOldVal:String = "dummyOldVal";
const propertyNewVal:String = "dummyNewVal";
@@ -241,15 +242,16 @@ package mx.collections.tests.arrayList {
var eventDispatcher:EventDispatcher = new EventDispatcher();
var arrayList:ArrayList = new ArrayList( array );

arrayList.addItem( eventDispatcher );
arrayList.addItem( new EventDispatcher() );
arrayList.addItemAt( eventDispatcher, index );

expectEvent.from( arrayList ).
hasType( PropertyChangeEvent.PROPERTY_CHANGE ).
instanceOf( PropertyChangeEvent ).
hasPropertyWithValue( "kind", PropertyChangeEventKind.UPDATE ).
hasPropertyWithValue( "newValue", propertyNewVal ).
hasPropertyWithValue( "oldValue", propertyOldVal ).
hasPropertyWithValue( "property", "-1" + '.' + propertyName ).
hasPropertyWithValue( "property", index + '.' + propertyName ).
hasPropertyWithValue( "source", eventDispatcher ).
once();

@@ -231,8 +231,9 @@ package mx.collections.tests.vectorList {
eventDispatcher.dispatchEvent( PropertyChangeEvent.createUpdateEvent( eventDispatcher, propertyName , propertyOldVal, propertyNewVal ) );
}

[Test(description="We should really consider if this is the behavior we want here. Right now mimics fixed ArrayList")]
[Test]
public function itemUpdatedShouldDispatchPropertyChangeEvent():void {
const index:int = 1;
const propertyName:String = "dummy";
const propertyOldVal:String = "dummyOldVal";
const propertyNewVal:String = "dummyNewVal";
@@ -241,15 +242,16 @@ package mx.collections.tests.vectorList {
var eventDispatcher:EventDispatcher = new EventDispatcher();
var vectorList:VectorList = new VectorList( vector );

vectorList.addItem( eventDispatcher );
vectorList.addItem( new EventDispatcher() );
vectorList.addItemAt( eventDispatcher, index );

expectEvent.from( vectorList ).
hasType( PropertyChangeEvent.PROPERTY_CHANGE ).
instanceOf( PropertyChangeEvent ).
hasPropertyWithValue( "kind", PropertyChangeEventKind.UPDATE ).
hasPropertyWithValue( "newValue", propertyNewVal ).
hasPropertyWithValue( "oldValue", propertyOldVal ).
hasPropertyWithValue( "property", "-1" + '.' + propertyName ).
hasPropertyWithValue( "property", index + '.' + propertyName ).
hasPropertyWithValue( "source", eventDispatcher ).
once();

0 comments on commit 13fb9de

Please sign in to comment.