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

/client/src/entity/Scheme.as

http://reliability-circuit-generator.googlecode.com/
ActionScript | 880 lines | 691 code | 52 blank | 137 comment | 62 complexity | 293acf279ce4af36c0ffe155349bb351 MD5 | raw file
  1. package src.entity {
  2. /**
  3. *
  4. * Reliability project
  5. * -----------------------
  6. * @author Dmitrij Boycov
  7. * @mail dmitry.boycov@gmail.com
  8. * @date April 27, 2010
  9. * @version 1
  10. *
  11. */
  12. import fl.data.DataProvider;
  13. import flash.display.Sprite;
  14. import flash.display.Stage;
  15. import flash.events.Event;
  16. import flash.events.MouseEvent;
  17. import flash.geom.Point;
  18. import flash.ui.Mouse;
  19. import flash.utils.Timer;
  20. import flash.events.TimerEvent;
  21. import flash.filters.GlowFilter;
  22. import flash.filters.BitmapFilterQuality;
  23. import flash.text.TextField;
  24. import flash.text.TextFormat;
  25. import flash.text.TextFieldType;
  26. import flash.text.TextFieldAutoSize
  27. import flash.utils.Dictionary;
  28. import flash.display.GradientType;
  29. import flash.geom.Matrix;
  30. import flash.events.KeyboardEvent;
  31. import flash.filters.BlurFilter;
  32. //
  33. import gs.TweenLite;
  34. // src
  35. import src.components.Confirm;
  36. import src.entity.Element;
  37. import src.util.Container;
  38. import src.common.OwnerEvent;
  39. import src.components.BForm;
  40. import src.components.ElementForm;
  41. import src.components.CalcForm;
  42. import src.util.Utils;
  43. import src.common.MsgEvent;
  44. import src.computation.LogicalProbabilisticMethod;
  45. import src.computation.Method;
  46. import src.components.ResForm;
  47. import src.components.HeaderBarButton;
  48. import src.components.StockForm;
  49. // astra
  50. import com.yahoo.astra.fl.managers.AlertManager;
  51. public class Scheme extends Sprite {
  52. private var sWidth:Number;
  53. private var sHeight:Number;
  54. private var basis:Sprite;
  55. private var navigation:Sprite;
  56. private var sColour = 0xFFFFAA;
  57. private var scale = 1;
  58. private var timer:Timer;
  59. private var currentDir:String;
  60. private var stageRef:Stage;
  61. private var ownerEvent:OwnerEvent;
  62. /**
  63. * stock is - stockForm
  64. */
  65. private var stock;
  66. // for header
  67. private var hHeight:Number = 25;
  68. private var hColour:int = 0x424242;// 0x939393;//0x737373;
  69. private var hColour2:int = 0x595959;//0x939393;
  70. private var glowColour = 0x3d4146;
  71. private var nameScheme:String = 'no name';
  72. private var descScheme:String = '';
  73. private var tf:TextField;
  74. // containers
  75. private var elements:Container = new Container();
  76. private var nodes:Container = new Container();
  77. private var links:Container = new Container();
  78. //
  79. private var idBD:Number;
  80. private var cfRotation;
  81. /**
  82. * constructor
  83. * @param stageRef - stage of root
  84. */
  85. public function Scheme(stageRef:Stage) {
  86. super();
  87. this.stageRef = stageRef;
  88. this.sWidth = stageRef.stageWidth;
  89. this.sHeight = stageRef.stageHeight;
  90. this.basis = this.createRectangle();
  91. this.basis.name = "BASIS";
  92. addChild(this.basis);
  93. this.navigation = this.createNavigation();
  94. this.navigation.x = this.sWidth - this.navigation.width - 10;
  95. this.navigation.y = this.sHeight - this.navigation.height - 10;
  96. addChild(this.navigation);
  97. var header = this.createHeader();
  98. header.y = 0;
  99. header.x = this.sWidth - header.width;
  100. this.addChild(header);
  101. // create begin node and end node
  102. var inNode = new Node();
  103. inNode.setName("in");
  104. inNode.x = this.stageRef.stageWidth / 2;
  105. inNode.y = header.height +10;
  106. inNode.addEventListener(Entity.ENTITY_ON_DOWN, onDownEntity);
  107. this.basis.addChild(inNode);
  108. this.nodes.add(inNode);
  109. var outNode = new Node();
  110. outNode.setName("out");
  111. outNode.x = this.stageRef.stageWidth / 2;
  112. outNode.y = this.stageRef.stageHeight - outNode.height - 10;
  113. outNode.addEventListener(Entity.ENTITY_ON_DOWN, onDownEntity);
  114. this.basis.addChild(outNode);
  115. this.nodes.add(outNode);
  116. /////
  117. this.ownerEvent = OwnerEvent.getInstance();
  118. this.ownerEvent.addEventListener(OwnerEvent.NEED_RESET_OWNER_EVENT, onUpEntity);
  119. this.ownerEvent.addEventListener(OwnerEvent.NEED_DELETE_ENTITY, onDeleteEntity);
  120. // this is made to be able to listen keyboard
  121. this.stageRef.focus = this;
  122. this.stageRef.addEventListener(KeyboardEvent.KEY_DOWN,this.ownerEvent.onKeyDown,true);
  123. }
  124. public function getIdBD() {
  125. return this.idBD;
  126. }
  127. public function setIdBD(id) {
  128. this.idBD = id;
  129. }
  130. /**
  131. * setMask
  132. * THis function set mask rectangle, reset active element
  133. */
  134. public function setMask() {
  135. var maskRect = Utils.getMask(parent.width, parent.height);
  136. var blur = new BlurFilter();
  137. blur.quality = BitmapFilterQuality.HIGH;
  138. blur.blurX = 8;
  139. blur.blurY = 8;
  140. this.filters = [blur];
  141. parent.addChild(maskRect);
  142. }
  143. /**
  144. * unmask
  145. * This function remove mask rectangle and form window, set stage focus.
  146. */
  147. public function unmask() {
  148. try{
  149. this.filters = [];
  150. parent.removeChildAt(parent.numChildren - 1);
  151. parent.removeChildAt(parent.numChildren - 1);
  152. // this is made to be able to listen keyboard
  153. this.stageRef.stageFocusRect = false;// To remove the crappy yellow rect on all focused InteractiveObject
  154. this.stageRef.focus = this;
  155. }catch(errObject:Error) {
  156. trace(errObject.message);
  157. }
  158. }
  159. public function removeEntity() {
  160. var ent = this.ownerEvent.getActiveEntity();
  161. if (ent is Element) {
  162. this.elements.remove(ent.getId());
  163. }else if (ent is Node) {
  164. this.nodes.remove(ent.getId());
  165. }else if (ent is Link) {
  166. ent.unlink();
  167. this.links.remove(ent.getId());
  168. }
  169. this.basis.removeChild(ent);
  170. ent = null;
  171. }
  172. private function onDeleteEntity(event) {
  173. var ent = this.ownerEvent.getActiveEntity();
  174. if (this.basis.contains(ent)) {
  175. this.setMask();
  176. var form = null;
  177. form = new Confirm("??????? ???? ????????");
  178. form.x = parent.width / 2 - form.width/2;
  179. form.y = parent.height / 2 - form.height / 2;
  180. var scheme = this;
  181. form.addEventListener(BForm.FORM_CLOSE, function(e:Event) {
  182. scheme.unmask();
  183. });
  184. form.addEventListener(BForm.FORM_OK, function(e:Event) {
  185. scheme.removeEntity();
  186. scheme.unmask();
  187. });
  188. parent.addChild(form);
  189. }
  190. }
  191. /**
  192. * getDropEntity
  193. * This function define entity, that under points.
  194. * @param points
  195. */
  196. public function getDropEntity(points:Array) {
  197. if(points!=null){
  198. for (var i = 0; i < points.length;i++ ) {
  199. var ar:Array = stageRef.getObjectsUnderPoint(points[i]);
  200. for (var j = 0; j < ar.length;j++ ){
  201. if (ar[j] is IObjectOfScheme) {
  202. return ar[j];
  203. }
  204. }
  205. }
  206. }
  207. return null;
  208. }
  209. //
  210. private function createRectangle() {
  211. var rect:Sprite = new Sprite();
  212. var ellipseSize:int = 0;
  213. rect.graphics.lineStyle(0.2);
  214. rect.graphics.beginFill(sColour);
  215. rect.graphics.drawRoundRect(0, 0, sWidth, sHeight, ellipseSize, ellipseSize);
  216. rect.graphics.endFill();
  217. rect.width = sWidth;
  218. rect.height = sHeight;
  219. rect.name = "BASIS";
  220. return rect;
  221. }
  222. private function createTF(text) {
  223. var FORMAT:TextFormat = new TextFormat();
  224. FORMAT.font = "Georgia";
  225. FORMAT.align = "left";
  226. FORMAT.bold = true;
  227. //FORMAT.color = 0x154672;
  228. FORMAT.color = 0xffffff;
  229. FORMAT.size = 14;
  230. var tf = new TextField();
  231. tf.autoSize = TextFieldAutoSize.LEFT;
  232. tf.defaultTextFormat = FORMAT;
  233. tf.type = TextFieldType.DYNAMIC;
  234. tf.text = text;
  235. tf.height = 20;
  236. tf.border = false;
  237. tf.background = false;
  238. tf.sharpness = 4;
  239. tf.selectable = false;
  240. return tf;
  241. }
  242. private function changeHeader(event:MouseEvent) {
  243. this.setMask();
  244. var form = new BForm("?????");
  245. var dict:Dictionary = new Dictionary();
  246. dict["name"] = this.nameScheme
  247. dict["description"] = this.descScheme;
  248. form.setData(dict);
  249. form.x = parent.width / 2 - form.width/2;
  250. form.y = parent.height / 2 - form.height / 2;
  251. var scheme = this;
  252. form.addEventListener(BForm.FORM_CLOSE, function(e:Event) {
  253. scheme.unmask();
  254. });
  255. form.addEventListener(BForm.FORM_OK, function(e:Event) {
  256. var dict:Dictionary = form.getData();
  257. nameScheme = dict["name"];
  258. tf.text = dict["name"] as String;
  259. descScheme = dict["description"];
  260. scheme.unmask();
  261. });
  262. parent.addChild(form);
  263. }
  264. private function createHeader() {
  265. var dist:Number = 5;
  266. var centerPart:Sprite = new Sprite();
  267. // close button
  268. var closeButton = new HeaderBarButton(new IcoClose(), '???????');
  269. closeButton.x = this.basis.width - closeButton.width -dist;
  270. closeButton.y = (hHeight - closeButton.height) / 2;
  271. closeButton.addEventListener(MouseEvent.CLICK, close);
  272. // save Button
  273. var saveButton = new HeaderBarButton(new IcoSave(), '?????????');
  274. saveButton.x = closeButton.x - saveButton.width - dist;
  275. saveButton.y = (hHeight - saveButton.height) / 2;
  276. saveButton.addEventListener(MouseEvent.CLICK, save);
  277. // calc button
  278. var calcButton = new HeaderBarButton(new IcoCalc(), '?????????');
  279. calcButton.x = saveButton.x - calcButton.width - dist;
  280. calcButton.y = (hHeight - calcButton.height)/2;
  281. calcButton.addEventListener(MouseEvent.CLICK, showCalcForm);
  282. // stock button
  283. var stockButton = new HeaderBarButton(new IcoStock(), '?????');
  284. stockButton.x = calcButton.x - stockButton.width - dist;
  285. stockButton.y = (hHeight - stockButton.height)/2;
  286. stockButton.addEventListener(MouseEvent.CLICK, showStockForm);
  287. tf = this.createTF(this.nameScheme);
  288. tf.doubleClickEnabled = true;
  289. tf.addEventListener(MouseEvent.DOUBLE_CLICK, changeHeader);
  290. var rotateMatrix:Matrix = new Matrix();
  291. rotateMatrix.createGradientBox(this.basis.width, hHeight, -Math.PI / 2, 0, 0);
  292. centerPart.graphics.beginGradientFill(GradientType.LINEAR,[hColour,hColour2], [1, 1],[ 0, 255],rotateMatrix);
  293. centerPart.graphics.drawRect(0, 0, this.basis.width, hHeight);
  294. centerPart.graphics.endFill();
  295. centerPart.filters = [Utils.getDropShadowFilter(glowColour)];
  296. tf.x = hHeight/2;
  297. tf.y = 0;
  298. centerPart.addChild(tf);
  299. //
  300. var actualPrompt:Sprite = new Sprite();
  301. actualPrompt.addChild(centerPart);
  302. actualPrompt.addChild(closeButton);
  303. actualPrompt.addChild(saveButton);
  304. actualPrompt.addChild(calcButton);
  305. actualPrompt.addChild(stockButton);
  306. return actualPrompt;
  307. }
  308. private function showStockForm(event) {
  309. this.setMask();
  310. if (this.stock == null) {
  311. this.stock = new StockForm();
  312. this.stock.addEventListener(BForm.FORM_CLOSE, function(e:Event) {
  313. scheme.unmask();
  314. });
  315. }
  316. this.stock.x = parent.width / 2 - this.stock.width/2;
  317. this.stock.y = parent.height / 2 - this.stock.height / 2;
  318. var scheme = this;
  319. parent.addChild(this.stock);
  320. }
  321. private function save(event) {
  322. this.setMask();
  323. var form = null;
  324. form = new Confirm("????????? ??????");
  325. form.x = parent.width / 2 - form.width/2;
  326. form.y = parent.height / 2 - form.height / 2;
  327. var scheme = this;
  328. form.addEventListener(BForm.FORM_CLOSE, function(e:Event) {
  329. scheme.unmask();
  330. });
  331. form.addEventListener(BForm.FORM_OK, function(e:Event) {
  332. var str = "<scheme>";
  333. if(this.idBD != null){
  334. str += "<id>" + this.idBD + "</id>";
  335. }
  336. str += scheme.links.toXML("links");
  337. str += scheme.elements.toXML("elements");
  338. str += scheme.nodes.toXML("nodes");
  339. str += "</scheme>";
  340. trace(str);
  341. scheme.unmask();
  342. });
  343. parent.addChild(form);
  344. }
  345. private function close(event) {
  346. this.setMask();
  347. var form = null;
  348. form = new Confirm("??????? ??????");
  349. form.x = parent.width / 2 - form.width/2;
  350. form.y = parent.height / 2 - form.height / 2;
  351. var scheme = this;
  352. form.addEventListener(BForm.FORM_CLOSE, function(e:Event) {
  353. scheme.unmask();
  354. });
  355. form.addEventListener(BForm.FORM_OK, function(e:Event) {
  356. scheme.unmask();
  357. parent.removeChild(scheme);
  358. scheme = null;
  359. });
  360. parent.addChild(form);
  361. }
  362. /**
  363. * showCalcForm
  364. * It show calc form. And it start computation.
  365. * @param event
  366. */
  367. private function showCalcForm(event:MouseEvent) {
  368. this.setMask();
  369. var form = null;
  370. form = new CalcForm();
  371. form.x = parent.width / 2 - form.width/2;
  372. form.y = parent.height / 2 - form.height / 2;
  373. var scheme = this;
  374. form.addEventListener(BForm.FORM_CLOSE, function(e:Event) {
  375. scheme.unmask();
  376. });
  377. form.addEventListener(BForm.FORM_OK,function(e:Event) {
  378. var dict:Dictionary = form.getData();
  379. var method = dict["method"];
  380. if(method!=null){
  381. method.addEventListener(Method.ERROR, function(event:MsgEvent) {
  382. AlertManager.createAlert(parent, event.msg, "??????");
  383. method = null;
  384. });
  385. //
  386. method.addEventListener(Method.STOP_COMPUTATION, function(event:Event) {
  387. var res = method.getResult();
  388. scheme.unmask();
  389. scheme.setMask();
  390. var form2 = null;
  391. form2 = new ResForm();
  392. var dict2 = new Dictionary();
  393. dict2["result"] = res;
  394. form2.setData(dict2);
  395. form2.x = parent.width / 2 - form.width/2;
  396. form2.y = parent.height / 2 - form.height / 2;
  397. form2.addEventListener(BForm.FORM_OK, function(e:Event) {
  398. scheme.unmask();
  399. });
  400. var dopForm = method.getResForm();
  401. if (dopForm != null) {
  402. form2.add(dopForm);
  403. }
  404. parent.addChild(form2);
  405. method = null;
  406. });
  407. method.setTime(new Number(dict["time"]));
  408. if(scheme.stock!=null){
  409. var stDict = scheme.stock.getData();
  410. if(stDict!=null && stDict["data"]!=null){
  411. method.setStock(stDict["data"].clone());
  412. }
  413. }
  414. method.setContainers(scheme.elements.containerToDict(),
  415. scheme.nodes.containerToDict(),
  416. scheme.links.containerToDict());
  417. method.startComputation();
  418. }
  419. });
  420. parent.addChild(form);
  421. }
  422. /* private function cleareScheme() {
  423. this.cleareContainer(this.elements);
  424. this.cleareContainer(this.links);
  425. this.cleareContainer(this.nodes);
  426. }
  427. private function cleareContainer(container) {
  428. container.first();
  429. var a = container.getNext();
  430. while (a != null) {
  431. container.remove(a.getId());
  432. this.basis.removeChild(a);
  433. a = container.getNext();
  434. }
  435. }
  436. private function refresh() {
  437. this.elements.first();
  438. var a = this.elements.getNext();
  439. while (a != null) {
  440. this.basis.addChild(a);
  441. a = this.elements.getNext();
  442. }
  443. this.nodes.first();
  444. a = this.nodes.getNext();
  445. while (a != null) {
  446. this.basis.addChild(a);
  447. a = this.nodes.getNext();
  448. }
  449. }
  450. */
  451. /**
  452. * createNavigation
  453. * Create navigation panel, which allows zoom in,zoom out, move up,
  454. * move down, move left and move right.
  455. */
  456. private function createNavigation() {
  457. // init
  458. var border:Number = 3;
  459. var nColour = 0xDDDDDD;
  460. var nAlpha = 0.5;
  461. var nav = new Sprite();
  462. var zoomin = new ZoomIn(); // +
  463. zoomin.addEventListener(MouseEvent.MOUSE_DOWN, this.zoomIn);
  464. this.addListenersToButton(zoomin);
  465. var zoomout = new ZoomOut(); // -
  466. zoomout.addEventListener(MouseEvent.MOUSE_DOWN, this.zoomOut);
  467. this.addListenersToButton(zoomout);
  468. var dirUp = new Course(); // up
  469. dirUp.addEventListener(MouseEvent.MOUSE_DOWN, moveUp);
  470. this.addListenersToButton(dirUp);
  471. var dirDown = new Course(); // down
  472. dirDown.addEventListener(MouseEvent.MOUSE_DOWN, moveDown);
  473. this.addListenersToButton(dirDown);
  474. var dirLeft = new Course(); // left
  475. dirLeft.addEventListener(MouseEvent.MOUSE_DOWN, moveLeft);
  476. this.addListenersToButton(dirLeft);
  477. var dirRight = new Course(); // right
  478. dirRight.addEventListener(MouseEvent.MOUSE_DOWN, moveRight);
  479. this.addListenersToButton(dirRight);
  480. var nWidth = 2 * (dirUp.height + border) + zoomin.width + 2 * border;
  481. var nHeight = 2 * (dirUp.height + border) + 2 * zoomin.height + 3 * border;
  482. // draw basis
  483. var ellipseSize:int = 4*border;
  484. //nav.graphics.lineStyle(0);
  485. nav.graphics.beginFill(nColour);
  486. nav.graphics.drawRoundRect(0, 0, nWidth, nHeight, ellipseSize, ellipseSize);
  487. nav.alpha = nAlpha;
  488. nav.graphics.endFill();
  489. // add buttons
  490. dirUp.x = dirUp.height + 2 * border;
  491. dirUp.y = border;
  492. nav.addChild(dirUp);
  493. //--
  494. dirLeft.rotation = -90;
  495. dirLeft.x = border;
  496. dirLeft.y = dirUp.y + dirUp.height + border + dirLeft.height;
  497. nav.addChild(dirLeft);
  498. //--
  499. zoomin.x = dirUp.x
  500. zoomin.y = dirUp.y + dirUp.height + border;
  501. nav.addChild(zoomin);
  502. //--
  503. zoomout.x = dirUp.x;
  504. zoomout.y = zoomin.y+zoomin.height+border;
  505. nav.addChild(zoomout);
  506. //--
  507. dirDown.rotation = 180;
  508. dirDown.x = dirUp.x + dirDown.width;
  509. dirDown.y = zoomout.y+ zoomout.height+ border + dirDown.height;
  510. nav.addChild(dirDown);
  511. //--
  512. dirRight.rotation = 90;
  513. dirRight.y = zoomin.y;
  514. dirRight.x = zoomin.x + zoomin.width + border + dirRight.width;
  515. nav.addChild(dirRight);
  516. nav.addEventListener(MouseEvent.MOUSE_DOWN, doStartDrag);
  517. nav.addEventListener(MouseEvent.MOUSE_UP, doStopDrag);
  518. nav.buttonMode = true;
  519. nav.useHandCursor = true;
  520. return nav;
  521. }
  522. /**
  523. * actions for button of navigation panel
  524. *////////////////////////////////////////////////////////////////////////
  525. private function endMoveNav(event:MouseEvent) {
  526. event.target.removeEventListener(MouseEvent.MOUSE_UP, endMoveNav);
  527. this.timer.removeEventListener(TimerEvent.TIMER, moveNav);
  528. this.timer.stop();
  529. this.timer = null;
  530. this.currentDir = null;
  531. }
  532. private function startMoveNav(event:MouseEvent){
  533. event.target.addEventListener(MouseEvent.MOUSE_UP, endMoveNav);
  534. this.timer = new Timer(50,0);
  535. this.timer.addEventListener(TimerEvent.TIMER, moveNav);
  536. this.timer.start();
  537. }
  538. private function moveNav(event:TimerEvent) {
  539. if (this.currentDir != null) {
  540. var obj = new Object(); //
  541. var x1 = this.basis.x; // +-------------+b
  542. var y1 = this.basis.y + this.basis.height; // | |
  543. var a:Point = new Point(x1, y1); // | |
  544. a = parent.localToGlobal(a); // | |
  545. var x2 = this.basis.x + this.basis.width; // | |
  546. var y2 = this.basis.y; // | |
  547. var b:Point = new Point(x2, y2); // a+-------------+
  548. b = parent.localToGlobal(b); //
  549. switch(this.currentDir) {
  550. case "Right":
  551. if ((a.x < 0) || (b.x < this.stageRef.stageWidth)) {
  552. obj.x = this.basis.x + 10;
  553. }
  554. break;
  555. case "Left" :
  556. if ((a.x >= 0) || (b.x > this.stageRef.stageWidth)) {
  557. obj.x = this.basis.x - 10;
  558. }
  559. break;
  560. case "Up":
  561. if((b.y >= 0) || (a.y > this.stageRef.stageHeight)){
  562. obj.y = this.basis.y - 10;
  563. }
  564. break;
  565. case "Down" :
  566. if((b.y < 0) || (a.y < this.stageRef.stageHeight)){
  567. obj.y = this.basis.y + 10;
  568. }
  569. break;
  570. }
  571. TweenLite.to(this.basis, 1, obj);
  572. }
  573. }
  574. private function zoomIn(event:MouseEvent):void {
  575. this.scale += 1;
  576. TweenLite.to(this.basis, 1, {scaleX:this.scale, scaleY:this.scale});
  577. }
  578. private function zoomOut(event:MouseEvent):void {
  579. if(this.scale>1){
  580. this.scale -= 1;
  581. TweenLite.to(this.basis, 1, { scaleX:this.scale, scaleY:this.scale } );
  582. }
  583. }
  584. private function moveLeft(event:MouseEvent) {
  585. this.currentDir = "Left";
  586. this.startMoveNav(event);
  587. }
  588. private function moveRight(event:MouseEvent) {
  589. this.currentDir = "Right";
  590. this.startMoveNav(event);
  591. }
  592. private function moveUp(event:MouseEvent) {
  593. this.currentDir = "Up";
  594. this.startMoveNav(event);
  595. }
  596. private function moveDown(event:MouseEvent) {
  597. this.currentDir = "Down";
  598. this.startMoveNav(event);
  599. }
  600. //////////////////////////////////////////////////////////////////////////
  601. /**
  602. * Event of Mouse for Button
  603. */////////////////////////////////////////////////////////////////////////
  604. private function addListenersToButton(button) {
  605. button.gotoAndStop(1)
  606. button.addEventListener(MouseEvent.MOUSE_OVER, onOverButton);
  607. button.addEventListener(MouseEvent.MOUSE_OUT, onOutButton);
  608. button.addEventListener(MouseEvent.MOUSE_UP, onUpButton);
  609. button.addEventListener(MouseEvent.MOUSE_DOWN, onDownButton);
  610. }
  611. private function onOverButton(e:MouseEvent) {
  612. e.target.gotoAndStop(2);
  613. }
  614. private function onOutButton(e:MouseEvent) {
  615. e.target.gotoAndStop(1);
  616. }
  617. private function onUpButton(e:MouseEvent) {
  618. e.target.gotoAndStop(2);
  619. }
  620. private function onDownButton(e:MouseEvent) {
  621. e.target.gotoAndStop(3);
  622. }
  623. ////////////////////////////////////////////////////////////////////////////
  624. /*
  625. * Drag and drop for navigation panel
  626. *////////////////////////////////////////////////////////////////////////////
  627. private function doStartDrag(event:MouseEvent):void {
  628. if (this.ownerEvent.getOwnerEvent() == null) {
  629. this.ownerEvent.setOwnerEvent(this.navigation);
  630. this.navigation.startDrag();
  631. }
  632. }
  633. private function doStopDrag(event:MouseEvent):void {
  634. if(this.ownerEvent.getOwnerEvent() == this.navigation){
  635. this.navigation.stopDrag();
  636. this.ownerEvent.resetOwnerEvent();
  637. }
  638. }
  639. ////////////////////////////////////////////////////////////////////////////////
  640. /**
  641. * Handlers for control activity of entity
  642. * @param e
  643. */
  644. private function onDownEntity(e:Event) {
  645. if (this.ownerEvent.getOwnerEvent() == null) {
  646. this.ownerEvent.setOwnerEvent(e.target);
  647. this.basis.setChildIndex(e.target as Sprite, (this.basis.numChildren - 1));
  648. e.target.doActionOnDown();
  649. }
  650. }
  651. private function onUpEntity(e:Event) {
  652. if (this.ownerEvent.getOwnerEvent() != null) {
  653. this.ownerEvent.getOwnerEvent()
  654. if(this.ownerEvent.getOwnerEvent() is IEntity){
  655. this.ownerEvent.getOwnerEvent().doActionOnUp();
  656. }
  657. this.ownerEvent.resetOwnerEvent();
  658. }
  659. }
  660. private function onDoubleClickEntity(e:Event) {
  661. if(e.target is IEntity){
  662. this.setMask();
  663. var form = null;
  664. var ent = e.target;
  665. var scheme = this;
  666. var dict:Dictionary = new Dictionary();
  667. if(ent.getType() == Node.TYPE){
  668. form = new BForm("????");
  669. dict["name"] = ent.getName();
  670. dict["description"] = ent.getDescription();
  671. form.setData(dict);
  672. }
  673. if(ent.getType() == Link.TYPE){
  674. form = new BForm("?????");
  675. dict["name"] = ent.getName();
  676. dict["description"] = ent.getDescription();
  677. form.setData(dict);
  678. }
  679. if(ent.getType() == Element.TYPE){
  680. form = new ElementForm("???????");
  681. dict["name"] = ent.getName();
  682. dict["description"] = ent.getDescription();
  683. dict["law"] = ent.getLaw();
  684. dict["recovery"] = ent.getRecovery();
  685. dict["stockId"] = ent.getStockId();
  686. if(this.stock!=null){
  687. var d = this.stock.getData();
  688. if(d["data"])
  689. dict["stock"] = d["data"];
  690. }
  691. form.setData(dict);
  692. }
  693. form.x = parent.width / 2 - form.width/2;
  694. form.y = parent.height / 2 - form.height / 2;
  695. form.addEventListener(BForm.FORM_CLOSE, function(e:Event) {
  696. scheme.unmask();
  697. });
  698. if(e.target.getType() == Link.TYPE || e.target.getType() == Node.TYPE){
  699. form.addEventListener(BForm.FORM_OK, function(e:Event) {
  700. var dict:Dictionary = form.getData();
  701. ent.setName(dict["name"]);
  702. ent.setDescription(dict["description"]);
  703. scheme.unmask();
  704. });
  705. }
  706. if(e.target.getType() == Element.TYPE){
  707. form.addEventListener(BForm.FORM_OK, function(e:Event) {
  708. var dict:Dictionary = form.getData();
  709. ent.setName(dict["name"]);
  710. ent.setDescription(dict["description"]);
  711. ent.setLaw(dict["law"]);
  712. ent.setRecovery(dict["recovery"]);
  713. ent.setStockId(dict["stockId"]);
  714. scheme.unmask();
  715. });
  716. }
  717. parent.addChild(form);
  718. }
  719. }
  720. private function changeCursorOnRotation(e:Event) {
  721. if (this.ownerEvent.getOwnerEvent() == null) {
  722. var m:Point = new Point(mouseX, mouseY)
  723. m = localToGlobal(m);
  724. if (!this.cfRotation) {
  725. e.target.rotatedNow = 1; // init
  726. var el = e.target as Element;
  727. var point = new Point(el.x+el.width/2, el.y+el.height/2);
  728. point = localToGlobal(point);
  729. Mouse.hide();
  730. this.cfRotation = new CursorForRotation();
  731. this.cfRotation.mouseEnabled = false;
  732. this.cfRotation.mouseChildren = false;
  733. this.cfRotation.gotoAndStop(1);
  734. this.cfRotation.x = m.x;
  735. this.cfRotation.y = m.y;
  736. parent.addChild(this.cfRotation);
  737. this.cfRotation.startDrag();
  738. }
  739. m = null;
  740. }
  741. }
  742. private function resetCursor(e:Event) {
  743. if (this.ownerEvent.getOwnerEvent() == null || this.ownerEvent.getOwnerEvent() == e.target) {
  744. if(this.cfRotation != null){
  745. parent.removeChild(this.cfRotation);
  746. }
  747. this.cfRotation = null;
  748. Mouse.show();
  749. }
  750. }
  751. /**
  752. * removeListeners
  753. * Delete listeners,
  754. */
  755. private function removeListeners() {
  756. this.removeEventListener(MouseEvent.MOUSE_DOWN, onAddElement);
  757. this.removeEventListener(MouseEvent.MOUSE_DOWN, onAddNode);
  758. this.removeEventListener(MouseEvent.MOUSE_DOWN, onAddLink);
  759. }
  760. /**
  761. * onAddElement
  762. * handlers for create new entity
  763. * @param event
  764. */
  765. private function onAddElement(event:MouseEvent) {
  766. this.removeListeners();
  767. var el = new Element();
  768. el.addEventListener(Entity.ENTITY_ON_DOWN, onDownEntity);
  769. el.addEventListener(Element.CHANGE_CURSOR_ON_ROTATION, changeCursorOnRotation);
  770. el.addEventListener(Element.RESET_CURSOR, resetCursor);
  771. el.addEventListener(Entity.DOUBLE_CLICK_ON_ELEMENT, onDoubleClickEntity);
  772. el.x = mouseX;
  773. el.y = mouseY;
  774. this.basis.addChild(el);
  775. this.elements.add(el);
  776. }
  777. /**
  778. * onAddNode
  779. * handlers for create new node
  780. * @param event
  781. */
  782. private function onAddNode(event:MouseEvent) {
  783. this.removeListeners();
  784. var node = new Node();
  785. node.x = mouseX;
  786. node.y = mouseY;
  787. node.addEventListener(Entity.ENTITY_ON_DOWN, onDownEntity);
  788. node.addEventListener(Entity.DOUBLE_CLICK_ON_ELEMENT, onDoubleClickEntity);
  789. this.basis.addChild(node);
  790. this.nodes.add(node);
  791. }
  792. /**
  793. * onAddLink
  794. * handlers for create new link
  795. * @param event
  796. */
  797. private function onAddLink(event:MouseEvent) {
  798. this.removeListeners();
  799. var link = new Link();
  800. link.setParent(this);
  801. link.x = mouseX;
  802. link.y = mouseY;
  803. link.addEventListener(Entity.ENTITY_ON_DOWN, onDownEntity);
  804. link.addEventListener(Entity.DOUBLE_CLICK_ON_ELEMENT, onDoubleClickEntity);
  805. this.basis.addChild(link);
  806. link.startDraw(event);
  807. this.links.add(link);
  808. }
  809. /**
  810. * This functions add listeners for mouse doun event.
  811. */
  812. public function addElement() {
  813. this.removeListeners();
  814. this.addEventListener(MouseEvent.MOUSE_DOWN, onAddElement);
  815. }
  816. public function addNode() {
  817. this.removeListeners();
  818. this.addEventListener(MouseEvent.MOUSE_DOWN, onAddNode);
  819. }
  820. public function addLink() {
  821. this.removeListeners();
  822. this.addEventListener(MouseEvent.MOUSE_DOWN, onAddLink);
  823. }
  824. ////////
  825. }
  826. }