/src/name/carter/mark/flex/util/icon/IconUtils.as

http://transcriptstudio4isha.googlecode.com/ · ActionScript · 171 lines · 126 code · 10 blank · 35 comment · 37 complexity · c528b5e2ae8635a9385d20836c0059a7 MD5 · raw file

  1. package name.carter.mark.flex.util.icon
  2. {
  3. import flash.display.DisplayObjectContainer;
  4. import mx.core.IDataRenderer;
  5. /**
  6. * Provides functionality to display icons loaded at runtime in components designed to display
  7. * embedded (compile-time) icons.
  8. *
  9. */
  10. public class IconUtils
  11. {
  12. public static function getIconClass(ancestorComponent:DisplayObjectContainer, iconPath:String, iconWidth:int = -1, iconHeight:int = -1, defaultIconClass:Class = null):Class {
  13. if (ancestorComponent == null) {
  14. // this can often happen while components are being initialised
  15. return null;
  16. }
  17. if (iconPath == null) {
  18. throw new Error("Passed a null iconPath");
  19. }
  20. var iconDescriptor:IconDescriptor = new IconDescriptor(iconPath, iconWidth, iconHeight, defaultIconClass);
  21. var iconDescriptorFunction:Function = function(parent:DisplayObjectContainer):IconDescriptor {
  22. return iconDescriptor;
  23. };
  24. IconClass.registerIconDescriptorFunction(ancestorComponent, iconDescriptorFunction);
  25. return IconClass;
  26. }
  27. public static function getIconClassUsingEmbeddedClass(ancestorComponent:DisplayObjectContainer, embeddedIconClass:Class, iconWidth:int = -1, iconHeight:int = -1):Class {
  28. if (ancestorComponent == null) {
  29. // this can often happen while components are being initialised
  30. return null;
  31. }
  32. if (embeddedIconClass == null) {
  33. throw new Error("Passed a null embeddedIconClass");
  34. }
  35. var iconDescriptor:IconDescriptor = new IconDescriptor(null, iconWidth, iconHeight, embeddedIconClass);
  36. var iconDescriptorFunction:Function = function(parent:DisplayObjectContainer):IconDescriptor {
  37. return iconDescriptor;
  38. };
  39. IconClass.registerIconDescriptorFunction(ancestorComponent, iconDescriptorFunction);
  40. return IconClass;
  41. }
  42. public static function getIconFunctionForDataRenderer(ancestorComponent:DisplayObjectContainer, iconPathFunction:Function, iconWidth:int = -1, iconHeight:int = -1, defaultIconClass:Class = null):Function {
  43. if (ancestorComponent == null) {
  44. // this can often happen while components are being initialised
  45. return null;
  46. }
  47. if (iconPathFunction == null) {
  48. throw new Error("Passed a null iconPathFunction");
  49. }
  50. var iconDescriptorFunction:Function = function(parent:DisplayObjectContainer):IconDescriptor {
  51. if (!(parent is IDataRenderer)) {
  52. return null;
  53. }
  54. var dr:IDataRenderer = parent as IDataRenderer;
  55. var iconPath:String = iconPathFunction(dr.data);
  56. var result:IconDescriptor;
  57. if (iconPath == null) {
  58. result = null;
  59. }
  60. else {
  61. result = new IconDescriptor(iconPath, iconWidth, iconHeight, defaultIconClass);
  62. }
  63. return result;
  64. };
  65. IconClass.registerIconDescriptorFunction(ancestorComponent, iconDescriptorFunction);
  66. return function(item:Object):Class {
  67. return IconClass;
  68. };
  69. }
  70. public static function getIconFunctionForDataRendererUsingSimpleIconFunction(ancestorComponent:DisplayObjectContainer, iconFunction:Function, iconWidth:int = -1, iconHeight:int = -1):Function {
  71. if (ancestorComponent == null) {
  72. // this can often happen while components are being initialised
  73. return null;
  74. }
  75. if (iconFunction == null) {
  76. throw new Error("Passed a null iconPathFunction");
  77. }
  78. var iconDescriptorFunction:Function = function(parent:DisplayObjectContainer):IconDescriptor {
  79. if (!(parent is IDataRenderer)) {
  80. return null;
  81. }
  82. var dr:IDataRenderer = parent as IDataRenderer;
  83. var embeddedIconClass:Class = iconFunction(dr.data);
  84. var result:IconDescriptor;
  85. if (embeddedIconClass == null) {
  86. result = null;
  87. }
  88. else {
  89. result = new IconDescriptor(null, iconWidth, iconHeight, embeddedIconClass);
  90. }
  91. return result;
  92. };
  93. IconClass.registerIconDescriptorFunction(ancestorComponent, iconDescriptorFunction);
  94. return function(item:Object):Class {
  95. return IconClass;
  96. };
  97. }
  98. /**
  99. * Preload the icon so it is more readily displayable.
  100. *
  101. * Both specified functions take a single String parameter - which is the icon path.
  102. *
  103. * Note - one of the specified functions may be called (in the same thread) before this function returns
  104. *
  105. * @param iconPath The URL for the icon
  106. * @param successFunction The function to be called when the icon is successfully loaded. If it is already loaded, then it is called immediately (before this function returns).
  107. * @param failureFunction The function to be called when the icon has failed to load. If it is already loaded, then it is called immediately (before this function returns)
  108. */
  109. public static function preloadIcon(iconPath:String, successFunction:Function = null, failureFunction:Function = null):void {
  110. var iconSource:IconSource = IconSource.getInstance(iconPath);
  111. if (successFunction != null || failureFunction != null) {
  112. iconSource.callRelevantFunctionOnCompletion(successFunction, failureFunction);
  113. }
  114. }
  115. /**
  116. * Specify the embedded icon to override runtime icon loading for this iconPath
  117. *
  118. * Note - an attempt will still be made to load so that we can accurately report the availability of the icon.
  119. * This is useful for icon paths used in TextArea html.
  120. */
  121. public static function overrideIcon(iconPath:String, embeddedIconClass:Class):void {
  122. var iconSource:IconSource = IconSource.getInstance(iconPath);
  123. if (embeddedIconClass != null) {
  124. iconSource.embeddedIcon = new embeddedIconClass();
  125. }
  126. }
  127. public static function isKnownToBeUnavailable(iconPath:String):Boolean {
  128. return IconSource.getInstance(iconPath).unavailable;
  129. }
  130. private static function isKnownToBeAvailable(iconPath:String):Boolean {
  131. return IconSource.getInstance(iconPath).available;
  132. }
  133. /**
  134. * Returns the corresponding html <img/> element.
  135. *
  136. * If the specified iconPath is known to be available then its path is used.
  137. * Otherwise, if the defaultIconPath is known to be available, then its path is used
  138. * Otherwise, null is returned.
  139. */
  140. public static function createImgElement(iconPath:String, iconWidth:int = -1, iconHeight:int = -1, defaultIconPath:String = null):XML {
  141. if (!isKnownToBeAvailable(iconPath)) {
  142. if (defaultIconPath == null || !isKnownToBeAvailable(defaultIconPath)) {
  143. // couldnt even load the default - probably security problems
  144. // so don't show icon at all
  145. return null;
  146. }
  147. else {
  148. iconPath = defaultIconPath;
  149. }
  150. }
  151. var result:XML = <img src={iconPath}/>;
  152. if (iconWidth >= 0) {
  153. result.@width = iconWidth;
  154. }
  155. if (iconHeight >= 0) {
  156. result.@height = iconHeight;
  157. }
  158. return result;
  159. }
  160. }
  161. }