GraphNode.as 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package iTop
  2. {
  3. import flash.display.*;
  4. import flash.geom.*;
  5. import flash.net.*;
  6. import flash.events.*;
  7. import flash.text.*;
  8. import flash.ui.ContextMenu;
  9. import flash.ui.ContextMenuItem;
  10. import iTop.ToolTip;
  11. import iTop.Navigator;
  12. // Items to load on the main chart
  13. public class GraphNode extends Sprite
  14. {
  15. private var m_oIcon:Loader;
  16. private var m_sClass;String;
  17. private var m_iId:Number;
  18. private var m_sParentKey:String;
  19. private var m_oToolTip:ToolTip;
  20. private var m_fZoom:Number;
  21. private var m_oParent:Navigator;
  22. public function GraphNode(oParent:Navigator, oPt:Point, sClass:String, iId:Number, sLabel:String, sIconPath:String, sParentKey:String, fZoom:Number)
  23. {
  24. x = oPt.x;
  25. y = oPt.y;
  26. m_fZoom = fZoom;
  27. m_sClass = sClass;
  28. m_iId = iId;
  29. m_sLabel.text = sLabel;
  30. m_sLabel.autoSize = TextFieldAutoSize.CENTER;
  31. m_sLabel.width = m_sLabel.textWidth;
  32. m_sLabel.x = -m_sLabel.width/2;
  33. m_sLabel.height = m_sLabel.textHeight;
  34. m_sParentKey = sParentKey;
  35. m_oToolTip = new ToolTip( "<p><b>"+m_sClass+"</b></p><p>"+sLabel+"</p>");
  36. m_oToolTip.scaleX = 1 / m_fZoom;
  37. m_oToolTip.scaleY = 1 / m_fZoom;
  38. m_oParent = oParent;
  39. var myURL:URLRequest = new URLRequest(sIconPath);
  40. m_oIcon = new Loader();
  41. m_oIcon.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);
  42. m_oIcon.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onLoadError);
  43. m_oIcon.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
  44. m_oIcon.load(myURL);
  45. addChild(m_oIcon);
  46. addEventListener(MouseEvent.MOUSE_DOWN, mouseDown)
  47. addEventListener(MouseEvent.MOUSE_UP, mouseReleased);
  48. addEventListener( MouseEvent.MOUSE_OVER, mouseOver );
  49. var oContextMenu:ContextMenu = new ContextMenu();
  50. oContextMenu.hideBuiltInItems();
  51. var oCMI:ContextMenuItem = new ContextMenuItem('Details...');
  52. oCMI.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, navigateToObjectDetails);
  53. oContextMenu.customItems.push(oCMI);
  54. this.contextMenu = oContextMenu;
  55. }
  56. public function GetKey()
  57. {
  58. return m_sClass+'/'+m_iId;
  59. }
  60. public function GetParentKey()
  61. {
  62. return m_sParentKey;
  63. }
  64. function onLoadError(event:ErrorEvent):void
  65. {
  66. // Display error message to user in case of loading error.
  67. trace ("Sorry that there is an error during the loading of an external image. The error is:" + "\n" + event);
  68. }
  69. function onLoadComplete(event:Event):void
  70. {
  71. // Add the Loader on the Sprite when the loading is completed
  72. m_oIcon.x = -m_oIcon.width / 2;
  73. m_oIcon.y = -m_oIcon.height + 8; // Slightly shifted downward
  74. // Construct a tooltip
  75. addChild(m_oToolTip);
  76. addChild(m_oIcon);
  77. trace('m_sLabel, getChildIndex:'+getChildIndex(m_sLabel));
  78. trace('m_oToolTip, getChildIndex:'+getChildIndex(m_oToolTip));
  79. //swapChildren(m_oToolTip, );
  80. // Start the tooltip
  81. m_oToolTip.start();
  82. }
  83. function mouseDown(event:MouseEvent):void
  84. {
  85. trace("Click in Node");
  86. m_oParent.m_bChildDragging = true;
  87. startDrag();
  88. }
  89. function mouseReleased(event:MouseEvent):void
  90. {
  91. stopDrag();
  92. m_oParent.m_bChildDragging = false;
  93. }
  94. public function mouseOver( e:MouseEvent ):void
  95. {
  96. // Move to the top
  97. parent.setChildIndex( this, this.parent.numChildren-1 );
  98. }
  99. private function navigateToObjectDetails(evt:ContextMenuEvent):void
  100. {
  101. var sUrl:String = ReadParam('drillUrl', 'http://localhost/pages/UI.php?operation=details');
  102. sUrl += '&class='+m_sClass+'&id='+m_iId;
  103. var oReq:URLRequest = new URLRequest(sUrl);
  104. navigateToURL(oReq, '_top');
  105. }
  106. public function ReadParam(sName:String, sDefaultValue:String)
  107. {
  108. var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters;
  109. if (paramObj.hasOwnProperty(sName))
  110. {
  111. return unescape(paramObj[sName]);
  112. }
  113. else
  114. {
  115. return sDefaultValue;
  116. }
  117. }
  118. }
  119. }