PageRenderTime 50ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/DiegoRay/actionscripts/org/asapframework/management/focus/FocusManager.as

https://github.com/joemaffia/flash-junk
ActionScript | 269 lines | 101 code | 62 blank | 106 comment | 27 complexity | 95e9e62fa9e47b813e96ee364b1bb3a9 MD5 | raw file
  1. /*
  2. Copyright 2005-2006 by the authors of asapframework, http://asapframework.org
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // ASAP classes
  14. import org.asapframework.events.Dispatcher;
  15. import org.asapframework.management.focus.FocusEvent;
  16. import org.asapframework.management.focus.IFocus;
  17. import org.asapframework.util.ArrayUtils;
  18. import org.asapframework.util.debug.Log;
  19. import org.asapframework.util.FrameDelay;
  20. /**
  21. @author Martijn de Visser
  22. @description Class to manage focus between various UI elements, without the need of MM's focus manager (which requires elements to be derived from UIComponent class). Listens to TAB key for forward navigation and SHIFT-TAB for backward navigation.
  23. @sends org.asapframework.management.focus.FocusEvent#ON_CHANGE_FOCUS Triggered when the focus of one of the registered elements changes.
  24. @usage You may need to add the 'SeamlessTabbing' parameter to the {@code <object>} tag. From Macromedia: (http://www.macromedia.com/support/documentation/en/flashplayer/7/releasenotes.html#Fixes) The default value is true; set this parameter to false to disable "seamless tabbing", which allows users to use the Tab key to move keyboard focus out of a Flash movie and into the surrounding HTML (or the browser, if there is nothing focusable in the HTML following the Flash movie). ActiveX Flash Player has supported seamless tabbing since version 7.0.14.0.
  25. */
  26. class org.asapframework.management.focus.FocusManager extends Dispatcher {
  27. private var mFocusIndex:Number;
  28. private var mFocusList:Array;
  29. /**
  30. Constructor. IMPORTANT: when using FocusManager in a MovieClip, the manager will remain in memory (and active) when the clip is removed (due to listening to Key object). Use {@link #kill} to remove the listener.
  31. @param listener object (optional) which will receive events.
  32. @usage
  33. <code>
  34. var myFocus:FocusManager = new FocusManager();
  35. myFocus.addEventListener(FocusEvent.ON_CHANGE_FOCUS, EventDelegate.create(this, onChangeFocus));
  36. </code>
  37. The following code makes the object that instantiates the FocusManager the default listener. It will receive the onChangeFocus event.
  38. @see org.asapframework.management.focus.FocusEvent
  39. for the actual name of that event.
  40. <code>
  41. var myFocus:FocusManager = new FocusManager(this);
  42. </code>
  43. */
  44. public function FocusManager ( inListener:Object ) {
  45. super();
  46. clear();
  47. // listen to TAB key
  48. Key.addListener(this);
  49. // if a listener was specified, add it
  50. if (inListener != undefined) {
  51. addEventListener(FocusEvent.ON_CHANGE_FOCUS, inListener);
  52. }
  53. }
  54. /**
  55. * Clears list of focus elements
  56. */
  57. public function clear () : Void {
  58. mFocusIndex = -1;
  59. mFocusList = new Array();
  60. }
  61. /**
  62. Sets the focus to a specific element.
  63. @param inElement: Must implement the {@link org.asapframework.management.focus.IFocus} interface.
  64. @usage
  65. <code>
  66. var formFocus:FocusManager = new FocusManager();
  67. formFocus.addElement(to_name, 0);
  68. formFocus.addElement(to_email, 1);
  69. formFocus.setFocus(to_name);
  70. </code>
  71. */
  72. public function setFocus ( inElement:IFocus ) : Void {
  73. // does element exist?
  74. var idx:Number = ArrayUtils.findElement(mFocusList,inElement);
  75. // element ok
  76. if (idx != -1) {
  77. var f:FrameDelay = new FrameDelay(this, changeFocus, 1, [mFocusIndex,idx]);
  78. }
  79. }
  80. /**
  81. Set the TAB index for an interface element.
  82. @param inElement: Must implement the {@link org.asapframework.management.focus.IFocus} interface.
  83. @param inPosition: zero-based, optional. If ommitted, it will be added to the end of the list. If an element was already found at the position specifed, it will be inserted.
  84. @return Boolean indicating if addition was successfull.
  85. @usage
  86. <code>
  87. formFocus.addElement(to_name, 0);
  88. formFocus.addElement(to_email, 1);
  89. formFocus.addElement(to_city, 2);
  90. </code>
  91. */
  92. public function addElement ( inElement:IFocus, inPosition:Number ) : Boolean {
  93. // element defined?
  94. if (inElement != undefined) {
  95. // not already present in list?
  96. if (ArrayUtils.findElement(mFocusList, inElement) == -1) {
  97. // was position specified?
  98. if (inPosition != undefined) {
  99. // yes, insert element
  100. ArrayUtils.insertElementAt(mFocusList, inPosition, inElement);
  101. } else {
  102. // no, just add element
  103. mFocusList.push(inElement);
  104. }
  105. return true;
  106. } else {
  107. return false;
  108. }
  109. } else {
  110. Log.warn("addElement: No element specified for addition", toString());
  111. return false;
  112. }
  113. }
  114. /**
  115. Listens to SHIFT / TAB key.
  116. */
  117. public function onKeyUp () : Void {
  118. if (Key.getCode() == Key.TAB) {
  119. if (Key.isDown(Key.SHIFT)) {
  120. prevFocus();
  121. } else {
  122. nextFocus();
  123. }
  124. }
  125. }
  126. /**
  127. Sets focus to next item in list.
  128. */
  129. private function nextFocus () : Void {
  130. // has one of our elements focus?
  131. var idx:Number = checkFocus();
  132. if (idx != -1) {
  133. // store previous focus
  134. var prev:Number = idx;
  135. // update
  136. idx++;
  137. // constrain to array bounds
  138. if (idx > mFocusList.length-1) { idx = 0; };
  139. changeFocus(prev,idx);
  140. } else {
  141. mFocusIndex = -1;
  142. }
  143. }
  144. /**
  145. Sets focus to previous item in list.
  146. */
  147. private function prevFocus () : Void {
  148. // has one of our elements focus?
  149. var idx:Number = checkFocus();
  150. if (idx != -1) {
  151. // store previous focus
  152. var prev:Number = idx;
  153. // update
  154. idx--;
  155. // constrain to array bounds
  156. if (idx < 0) { idx = mFocusList.length-1; };
  157. changeFocus(prev,idx);
  158. } else {
  159. mFocusIndex = -1;
  160. }
  161. }
  162. /**
  163. Changes the focus the 'index' passed
  164. @sends FocusEvent#ON_CHANGE_FOCUS
  165. */
  166. private function changeFocus ( inPrevFocus:Number, inNewFocus:Number ) : Void {
  167. // store new focus
  168. mFocusIndex = inNewFocus;
  169. // yes, change focus
  170. IFocus(mFocusList[mFocusIndex]).setFocus();
  171. // dispatch onChangeFocus event
  172. dispatchEvent(new FocusEvent(FocusEvent.ON_CHANGE_FOCUS, this, mFocusList[inPrevFocus], mFocusList[mFocusIndex]));
  173. }
  174. /**
  175. Checks if any of our elements has focus and returns its index
  176. */
  177. private function checkFocus () : Number {
  178. var idx:Number = -1;
  179. var len:Number = mFocusList.length;
  180. for (var i:Number=0; i<len; ++i) {
  181. if (IFocus(mFocusList[i]).hasFocus()) {
  182. idx = i;
  183. break;
  184. }
  185. }
  186. return idx;
  187. }
  188. /**
  189. Kills the link to the Key object so the FocusManager can be removed by garbage collection. Best used in onUnload event of MC where FocusManager is instantiated in.
  190. */
  191. public function kill () : Void {
  192. Key.removeListener(this);
  193. }
  194. /**
  195. * @return List of all elements managed by this FocusManager
  196. */
  197. public function getElements () : Array {
  198. return mFocusList;
  199. }
  200. public function toString() : String {
  201. return ";org.asapframework.management.focus.FocusManager";
  202. }
  203. }