My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
Copyright (c) 2009 Tink Ltd - http://www.tink.ws

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions
of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package ws.tink.flex.collections
{
import flash.events.EventDispatcher;

import mx.collections.ArrayCollection;
import mx.collections.CursorBookmark;
import mx.collections.ICollectionView;
import mx.collections.IList;
import mx.collections.IViewCursor;
import mx.collections.ListCollectionView;
import mx.collections.XMLListCollection;
import mx.events.CollectionEvent;
import mx.events.CollectionEventKind;
import mx.events.PropertyChangeEvent;

public class CollectionClone extends EventDispatcher
{

private var _iterator : IViewCursor;
private var _collection : ICollectionView;
private var _outputCollection : ArrayCollection;

public function CollectionClone()
{
initialize();
}

public function set dataProvider( value:Object ):void
{
if( value is Array )
{
_collection = new ArrayCollection( value as Array );
}
else if( value is ICollectionView )
{
_collection = ICollectionView( value );
}
else if( value is IList )
{
_collection = new ListCollectionView( IList( value ) );
}
else if( value is XMLList )
{
_collection = new XMLListCollection( value as XMLList );
}
else
{
// convert it to an array containing this one item
var tmp:Array = [ value ];
_collection = new ArrayCollection( tmp );
}

_iterator = _collection.createCursor();

_collection.addEventListener( CollectionEvent.COLLECTION_CHANGE, onCollectionChange, false, 0, true );

applyOutput( CollectionEventKind.RESET );
}

[Bindable(event="collectionChange")]
public function get output():ArrayCollection
{
return _outputCollection;
}

private function onCollectionChange( event:CollectionEvent ):void
{
var i:int;
var numItems:int;
var item:Object;
var propertyChangeEvent:PropertyChangeEvent;

switch( event.kind )
{
case CollectionEventKind.ADD :
{
numItems = event.items.length;
for( i = 0; i < numItems; i++ )
{
_outputCollection.addItemAt( event.items[ i ], event.location + i );
}
break;
}
case CollectionEventKind.MOVE :
{
numItems = event.items.length;
for( i = 0; i < numItems; i++ )
{
item = _outputCollection.removeItemAt( event.oldLocation + i );
_outputCollection.addItemAt( item, event.location + i );
}
break;
}
case CollectionEventKind.REFRESH :
case CollectionEventKind.RESET :
{
applyOutput( event.kind );
break;
}
case CollectionEventKind.REMOVE :
{
numItems = event.items.length;
for( i = 0; i < numItems; i++ )
{
_outputCollection.removeItemAt( event.location + i );
}
break;
}
case CollectionEventKind.REPLACE :
{
numItems = event.items.length;
for( i = 0; i < numItems; i++ )
{
propertyChangeEvent = PropertyChangeEvent( event.items[ i ] );
_outputCollection.setItemAt( propertyChangeEvent.source, event.location + i );
}
break;
}
case CollectionEventKind.UPDATE :
{
numItems = event.items.length;
for( i = 0; i < numItems; i++ )
{
propertyChangeEvent = PropertyChangeEvent( event.items[ i ] );
_outputCollection.itemUpdated( propertyChangeEvent.source, propertyChangeEvent.property, propertyChangeEvent.oldValue, propertyChangeEvent.newValue );
}
break;
}
}
}

private function applyOutput( kind:String ):void
{
_outputCollection = new ArrayCollection();
var numItems:int = _collection.length;
for( var i:int = 0; i < numItems; i++ )
{
_iterator.seek( CursorBookmark.FIRST, i, 0 );
_outputCollection.addItem( _iterator.current );
}

dispatchEvent( new CollectionEvent( CollectionEvent.COLLECTION_CHANGE, false, false, kind ) );
}


private function initialize():void
{
_outputCollection = new ArrayCollection();
}
}
}

Change log

r62 by sdowns on Nov 13, 2009   Diff
Tag for version of source that uses
ws.tink.flex package naming convention.
Changed to ws.tink.mx getting ready for
Flex 4 and spark.
Go to: 
Project members, sign in to write a code review

Older revisions

r58 by sdowns on Aug 25, 2009   Diff
Removed traces and added MIT license.
r57 by sdowns on Aug 25, 2009   Diff
CollectionClone is used when you need
to make a clone of a collection so
that it can be filtered without
affecting the original collection.

...
All revisions of this file

File info

Size: 4989 bytes, 169 lines
Powered by Google Project Hosting