PageRenderTime 52ms CodeModel.GetById 20ms app.highlight 4ms RepoModel.GetById 17ms app.codeStats 0ms

/src/away3d/library/naming/ConflictStrategyBase.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 71 lines | 32 code | 9 blank | 30 comment | 2 complexity | 15a5803a4a2c9fc140476de0425ff878 MD5 | raw file
 1package away3d.library.naming
 2{
 3	import away3d.errors.AbstractMethodError;
 4	import away3d.events.AssetEvent;
 5	import away3d.library.assets.IAsset;
 6	
 7	/**
 8	 * Abstract base class for naming conflict resolution classes. Extend this to create a
 9	 * strategy class which the asset library can use to resolve asset naming conflicts, or
10	 * use one of the bundled concrete strategy classes:
11	 *
12	 * <ul>
13	 *   <li>IgnoreConflictStrategy (ConflictStrategy.IGNORE)</li>
14	 *   <li>ErrorConflictStrategy (ConflictStrategy.THROW_ERROR)</li>
15	 *   <li>NumSuffixConflictStrategy (ConflictStrategy.APPEND_NUM_SUFFIX)</li>
16	 * </ul>
17	 *
18	 * @see away3d.library.AssetLibrary.conflictStrategy
19	 * @see away3d.library.naming.ConflictStrategy
20	 * @see away3d.library.naming.IgnoreConflictStrategy
21	 * @see away3d.library.naming.ErrorConflictStrategy
22	 * @see away3d.library.naming.NumSuffixConflictStrategy
23	 */
24	public class ConflictStrategyBase
25	{
26		
27		public function ConflictStrategyBase()
28		{
29		}
30		
31		/**
32		 * Resolve a naming conflict between two assets. Must be implemented by concrete strategy
33		 * classes.
34		 */
35		public function resolveConflict(changedAsset:IAsset, oldAsset:IAsset, assetsDictionary:Object, precedence:String):void
36		{
37			throw new AbstractMethodError();
38		}
39		
40		/**
41		 * Create instance of this conflict strategy. Used internally by the AssetLibrary to
42		 * make sure the same strategy instance is not used in all AssetLibrary instances, which
43		 * would break any state caching that happens inside the strategy class.
44		 */
45		public function create():ConflictStrategyBase
46		{
47			throw new AbstractMethodError();
48		}
49		
50		/**
51		 * Provided as a convenience method for all conflict strategy classes, as a way to finalize
52		 * the conflict resolution by applying the new names and dispatching the correct events.
53		 */
54		protected function updateNames(ns:String, nonConflictingName:String, oldAsset:IAsset, newAsset:IAsset, assetsDictionary:Object, precedence:String):void
55		{
56			var loser_prev_name:String;
57			var winner:IAsset, loser:IAsset;
58			
59			winner = (precedence == ConflictPrecedence.FAVOR_NEW)? newAsset : oldAsset;
60			loser = (precedence == ConflictPrecedence.FAVOR_NEW)? oldAsset : newAsset;
61			
62			loser_prev_name = loser.name;
63			
64			assetsDictionary[winner.name] = winner;
65			assetsDictionary[nonConflictingName] = loser;
66			loser.resetAssetPath(nonConflictingName, ns, false);
67			
68			loser.dispatchEvent(new AssetEvent(AssetEvent.ASSET_CONFLICT_RESOLVED, loser, loser_prev_name));
69		}
70	}
71}