PageRenderTime 29ms CodeModel.GetById 2ms app.highlight 19ms RepoModel.GetById 5ms app.codeStats 0ms

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