/src/name/carter/mark/flex/util/Utils.as

http://transcriptstudio4isha.googlecode.com/ · ActionScript · 300 lines · 222 code · 33 blank · 45 comment · 55 complexity · a94a660d6ba2ff547288688fdd878c10 MD5 · raw file

  1. /*
  2. Transcript Studio for Isha Foundation: An XML based application that allows users to define
  3. and store contextual metadata for contiguous sections within a text document.
  4. Copyright 2008 Mark Carter, Swami Kevala
  5. This file is part of Transcript Studio for Isha Foundation.
  6. Transcript Studio for Isha Foundation is free software: you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the Free Software
  8. Foundation, either version 3 of the License, or (at your option) any later version.
  9. Transcript Studio for Isha Foundation is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License along with
  13. Transcript Studio for Isha Foundation. If not, see http://www.gnu.org/licenses/.
  14. */
  15. package name.carter.mark.flex.util
  16. {
  17. import flash.display.DisplayObject;
  18. import flash.display.DisplayObjectContainer;
  19. import flash.text.StyleSheet;
  20. import flash.text.TextField;
  21. import flash.utils.ByteArray;
  22. import flash.utils.describeType;
  23. import mx.controls.TextArea;
  24. import mx.controls.Tree;
  25. import mx.core.Application;
  26. import mx.formatters.DateFormatter;
  27. import mx.utils.StringUtil;
  28. public class Utils
  29. {
  30. [Embed(source="/../assets/default.png")]
  31. public static const DEFAULT_ICON_CLASS:Class;
  32. [Embed(source="/../assets/pencil.png")]
  33. public static const PENCIL_ICON_CLASS:Class;
  34. [Embed(source="/../assets/search.png")]
  35. public static const SEARCH_ICON_CLASS:Class;
  36. private static const ASSETS_PATH:String = "./assets";
  37. private static const ICON_EXT:String = ".png";
  38. public static const DEFAULT_ICON_PATH:String = ASSETS_PATH + "/default" + ICON_EXT;
  39. public static const DIVIDER_SIZE:int = 5;
  40. //[Embed("./TranscriptMarkupsEditor-app.xml", mimeType="application/octet-stream")]
  41. //private static const descriptorXMLClass:Class;
  42. //[Embed("./TranscriptMarkupsEditorDemo-app.xml", mimeType="application/octet-stream")]
  43. //private static const demoDescriptorXMLClass:Class;
  44. private static var descriptorXML:XML = null;
  45. private static function isDemo():Boolean {
  46. return Application.application.className.indexOf("Demo") >= 0;
  47. }
  48. /*private static function getDescriptorXML():XML {
  49. if (descriptorXML == null) {
  50. var cls:Class = isDemo() ? demoDescriptorXMLClass : descriptorXMLClass;
  51. descriptorXML = getXML(cls);
  52. }
  53. return descriptorXML;
  54. }*/
  55. public static function getClassName(obj:Object):String {
  56. var describeXML:XML = describeType(obj);
  57. var qName:String = describeXML.@name;
  58. if (qName == null) {
  59. return null;
  60. }
  61. var index:int = qName.lastIndexOf(".");
  62. if (index < 0) {
  63. return qName;
  64. }
  65. else {
  66. return qName.substring(index + 1);
  67. }
  68. }
  69. public static function expandToItem(item:XML, tree:Tree, showChildren:Boolean = false):void {
  70. if (item == null) {
  71. throw new Error("Passed a null item");
  72. }
  73. var tempKList:Array = new Array();
  74. tempKList.push(item);
  75. while (tempKList[0].parent() != null) {
  76. tempKList.unshift(tempKList[0].parent() as XML);
  77. if (tree.isItemVisible(tempKList[0])) {
  78. // this item (we just added) is already visible so no need to expand parent
  79. break;
  80. }
  81. }
  82. for each (var tempK:XML in tempKList) {
  83. if (showChildren || tempK != item) {
  84. tree.expandItem(tempK, true);
  85. }
  86. }
  87. }
  88. public static function getIconPath(iconName:String):String {
  89. return ASSETS_PATH + "/" + iconName + ICON_EXT;
  90. }
  91. /**
  92. * Removes leading/trailing whitespace and condenses consecutive (one or more) whitespace to a single space
  93. */
  94. public static function normalizeSpace(text:String):String {
  95. var result:String = StringUtil.trim(text);
  96. var consecutiveWhitespacePattern:RegExp = /\s+/g;
  97. result = result.replace(consecutiveWhitespacePattern, " ");
  98. return result;
  99. }
  100. public static function isBlank(text:String):Boolean {
  101. return text == null || normalizeSpace(text) == "";
  102. }
  103. /**
  104. * For each item, removes leading/trailing whitespace and condenses consecutive (one or more) whitespace to a single space
  105. * If the item is reduced to an empty string then that item is removed from the array
  106. *
  107. * Does not modify the given array.
  108. */
  109. public static function condenseWhitespaceForArray(texts:Array):Array {
  110. var result:Array = new Array();
  111. for each (var text:String in texts) {
  112. if (text == null) {
  113. continue;
  114. }
  115. text = normalizeSpace(text);
  116. if (text != "") {
  117. result.push(text);
  118. }
  119. }
  120. return result;
  121. }
  122. public static function getStyleSheet(StyleSheetClass:Class):StyleSheet {
  123. var ba:ByteArray = new StyleSheetClass();
  124. var cssText:String = ba.readUTFBytes(ba.length);
  125. var styleSheet:StyleSheet = new StyleSheet();
  126. styleSheet.parseCSS(cssText);
  127. return styleSheet;
  128. }
  129. public static function getTextField(container:DisplayObjectContainer):TextField {
  130. return getChildByClass(container, TextField) as TextField;
  131. }
  132. public static function getChildByClass(container:DisplayObjectContainer, cls:Class):Object {
  133. var len:int = container.numChildren;
  134. for (var i:int=0; i < len; i++){
  135. var thisChild:DisplayObject = container.getChildAt(i);
  136. if (thisChild is cls){
  137. return thisChild;
  138. }
  139. }
  140. return null;
  141. }
  142. public static function getFirstVisibleLineIndex(ta:TextArea):int {
  143. var tf:TextField = getTextField(ta);
  144. return tf.getLineIndexAtPoint(5, 5);
  145. }
  146. public static function getLastVisibleLineIndex(ta:TextArea):int {
  147. var tf:TextField = getTextField(ta);
  148. return tf.getLineIndexAtPoint(5, ta.height - 5);
  149. }
  150. public static function getFirstVisibleCharIndex(ta:TextArea):int {
  151. var tf:TextField = getTextField(ta);
  152. return tf.getLineOffset(getFirstVisibleLineIndex(ta));
  153. }
  154. public static function getNowDateString(includeTime:Boolean):String {
  155. return getDateString(new Date(), includeTime);
  156. }
  157. public static function getDateString(date:Date = null, includeTime:Boolean = false):String {
  158. if (date == null) {
  159. return null;
  160. }
  161. var df:DateFormatter = new DateFormatter();
  162. df.formatString = includeTime ? "YYYY-MM-DDTJJ:NN:SS" : "YYYY-MM-DD";
  163. return df.format(date);
  164. }
  165. public static function getTimeString(date:Date = null):String {
  166. if (date == null) {
  167. return null;
  168. }
  169. var df:DateFormatter = new DateFormatter();
  170. df.formatString = "JJ:NN:SS";
  171. return df.format(date);
  172. }
  173. public static function copyArray(arr:Array):Array {
  174. return arr.slice();
  175. }
  176. public static function arrayEquals(arr1:Array, arr2:Array):Boolean {
  177. if (arr1 == arr2) {
  178. return true;
  179. }
  180. if (arr1 == null || arr2 == null) {
  181. return false;
  182. }
  183. if (arr1.length != arr2.length) {
  184. return false;
  185. }
  186. for (var i:int; i < arr1.length; i++) {
  187. if (arr1[i] != arr2[i]) {
  188. return false;
  189. }
  190. }
  191. return true;
  192. }
  193. public static function arrayToLowerCase(arr:Array):Array {
  194. var result:Array = new Array();
  195. for each (var s:String in arr) {
  196. result.push(s.toLowerCase());
  197. }
  198. return result;
  199. }
  200. public static function trimFromFront(arr1:Array, arr2:Array):void {
  201. while (arr1.length > 0 && arr2.length > 0) {
  202. if (arr1[0] == arr2[0]) {
  203. arr1.splice(0, 1);
  204. arr2.splice(0, 1);
  205. }
  206. else {
  207. break;
  208. }
  209. }
  210. }
  211. public static function trimFromBack(arr1:Array, arr2:Array):void {
  212. while (arr1.length > 0 && arr2.length > 0) {
  213. if (arr1[arr1.length - 1] == arr2[arr2.length - 1]) {
  214. arr1.splice(arr1.length - 1, 1);
  215. arr2.splice(arr2.length - 1, 1);
  216. }
  217. else {
  218. break;
  219. }
  220. }
  221. }
  222. public static function switchItems(arr:Array, index1:int, index2:int):void {
  223. var item1:Object = arr[index1];
  224. var item2:Object = arr[index2];
  225. arr[index1] = item2;
  226. arr[index2] = item1;
  227. }
  228. /**
  229. * Returns a comparator function by comparing strings returned by the specified toString function.
  230. *
  231. * toString(element:XML):String
  232. */
  233. public static function getStringCompareFunction(toString:Function):Function {
  234. return function compare(element1:XML, element2:XML):int {
  235. var str1:String = toString(element1);
  236. var str2:String = toString(element2);
  237. if (str1 < str2) {
  238. return -1;
  239. }
  240. else if (str1 > str2) {
  241. return +1;
  242. }
  243. else {
  244. return 0;
  245. }
  246. };
  247. }
  248. public static function getFunction(host:Object, funcName:String):Function {
  249. if (host.hasOwnProperty(funcName)) {
  250. return host[funcName];
  251. }
  252. else {
  253. return null;
  254. }
  255. }
  256. public static function encodePath(path:String):String {
  257. var index:int = path.lastIndexOf("/");
  258. var result:String = path.substring(0, index + 1) + encodeURIComponent(path.substring(index + 1));
  259. return result;
  260. }
  261. }
  262. }