1////////////////////////////////////////////////////////////////////////////////
2//
3//  ADOBE SYSTEMS INCORPORATED
4//  Copyright 2009 Adobe Systems Incorporated
5//  All Rights Reserved.
6//
7//  NOTICE: Adobe permits you to use, modify, and distribute this file
8//  in accordance with the terms of the license agreement accompanying it.
9//
10////////////////////////////////////////////////////////////////////////////////
11
12package mx.automation.delegates.controls
13{
14	import flash.display.DisplayObject;
15	import flash.events.Event;
16	import flash.events.EventDispatcher;
17	import flash.events.MouseEvent;
18
19	import mx.automation.Automation;
20	import mx.automation.IAutomationObjectHelper;
21	import mx.automation.delegates.core.UIComponentAutomationImpl;
22	import mx.controls.scrollClasses.ScrollBar;
23	import mx.core.mx_internal;
24	import mx.events.ScrollEvent;
25	import mx.events.ScrollEventDetail;
26
27	use namespace mx_internal;
28
29	[Mixin]
30	/**
31	 *
32	 *  Defines methods and properties required to perform instrumentation for the
33	 *  ScrollBar class.
34	 *
35	 *  @see mx.controls.scrollClasses.ScrollBar
36	 *
37	 *
38	 *  @langversion 3.0
39	 *  @playerversion Flash 9
40	 *  @playerversion AIR 1.1
41	 *  @productversion Flex 3
42	 */
43	public class ScrollBarAutomationImpl extends UIComponentAutomationImpl
44	{
45		include "../../../core/Version.as";
46
47		//--------------------------------------------------------------------------
48		//
49		//  Class methods
50		//
51		//--------------------------------------------------------------------------
52
53
54		/**
55		 *  Registers the delegate class for a component class with automation manager.
56		 *
57		 *  @param root The SystemManger of the application.
58		 *
59		 *  @langversion 3.0
60		 *  @playerversion Flash 9
61		 *  @playerversion AIR 1.1
62		 *  @productversion Flex 3
63		 */
64		public static function init(root:DisplayObject):void
65		{
66			Automation.registerDelegateClass(ScrollBar, ScrollBarAutomationImpl);
67		}
68
69		//--------------------------------------------------------------------------
70		//
71		//  Constructor
72		//
73		//--------------------------------------------------------------------------
74
75		/**
76		 *  Constructor.
77		 * @param obj ScrollBar object to be automated.
78		 *
79		 *  @langversion 3.0
80		 *  @playerversion Flash 9
81		 *  @playerversion AIR 1.1
82		 *  @productversion Flex 3
83		 */
84		public function ScrollBarAutomationImpl(obj:ScrollBar)
85		{
86			super(obj);
87
88			obj.addEventListener(ScrollEvent.SCROLL, scrollHandler, false, -1, true);
89		}
90
91		/**
92		 *  @private
93		 *  storage for the owner component
94		 */
95		protected function get scroll():ScrollBar
96		{
97			return uiComponent as ScrollBar;
98		}
99
100		/**
101		 *  @private
102		 */
103		private var previousEvent:ScrollEvent;
104
105		//----------------------------------
106		//  automationValue
107		//----------------------------------
108
109		/**
110		 *  @private
111		 */
112		override public function get automationValue():Array
113		{
114			return [ scroll.scrollPosition.toString() ];
115		}
116
117		/**
118		 *  @private
119		 *  Replays ScrollEvents.
120		 *  ScrollEvents are replayed by simply setting the
121		 *  <code>verticalScrollPosition</code> or
122		 *  <code>horizontalScrollPosition</code> properties of the instance.
123		 */
124		override public function replayAutomatableEvent(interaction:Event):Boolean
125		{
126			if (interaction is ScrollEvent)
127			{
128				var scrollEvent:ScrollEvent = ScrollEvent(interaction);
129				var targetObject:EventDispatcher = null;
130				var mouseEvent:MouseEvent = new MouseEvent(MouseEvent.CLICK);
131				if (scrollEvent.detail == scroll.lineMinusDetail)
132					targetObject = scroll.upArrow;
133				else if (scrollEvent.detail == scroll.linePlusDetail)
134					targetObject = scroll.downArrow;
135				else if (scrollEvent.detail == scroll.pageMinusDetail)
136				{
137					targetObject = uiComponent;
138					mouseEvent.localX = 0;
139					mouseEvent.localY = 0;
140				}
141				else if (scrollEvent.detail == scroll.pagePlusDetail)
142				{
143					targetObject = uiComponent;
144					mouseEvent.localX = scroll.width;
145					mouseEvent.localY = scroll.height;
146				}
147				else if (scrollEvent.detail == ScrollEventDetail.THUMB_POSITION)
148				{
149					targetObject = scroll.scrollThumb;
150					scroll.scrollPosition = scrollEvent.position;
151				}
152				else if (scrollEvent.detail == ScrollEventDetail.AT_TOP ||
153					scrollEvent.detail == ScrollEventDetail.AT_LEFT ||
154					scrollEvent.detail == ScrollEventDetail.AT_RIGHT ||
155					scrollEvent.detail == ScrollEventDetail.AT_BOTTOM)
156				{
157					targetObject = scroll.scrollThumb;
158					scroll.scrollPosition = scrollEvent.position;
159				}
160				if (targetObject)
161				{
162					var help:IAutomationObjectHelper = Automation.automationObjectHelper;
163					help.replayClick(targetObject, mouseEvent);
164				}
165				scroll.scrollPosition = scrollEvent.position;
166				return true;
167			}
168			else
169			{
170				return super.replayAutomatableEvent(interaction);
171			}
172		}
173
174		//--------------------------------------------------------------------------
175		//
176		//  Event handlers
177		//
178		//--------------------------------------------------------------------------
179
180		/**
181		 *  @private
182		 */
183		private function scrollHandler(event:ScrollEvent):void
184		{
185			if (!previousEvent || previousEvent.delta != event.delta ||
186				previousEvent.detail != event.detail ||
187				previousEvent.direction != event.direction ||
188				previousEvent.position != event.position ||
189				previousEvent.type != event.type)
190			{
191				recordAutomatableEvent(event);
192				previousEvent = event.clone() as ScrollEvent;
193			}
194		}
195
196	}
197}