1 /**
  2  * Object that is used to undo actions when figures/groups/connectors are deleted
  3  * @this {DeleteCommand} 
  4  * @constructor
  5  * @param property {Null}
  6  * @param previousValue {Figure}/{Connector}/{Group} the item being deleted
  7  * @param currentValue {Event} the event that caused the delete
  8  */
  9 function DeleteCommand(objectId, typeOfObject, property, previousValue, currentValue){
 10     this.objectId = objectId;
 11     this.typeOfObject = typeOfObject;
 12     this.property = property;
 13     this.previousValue = previousValue;
 14     this.currentValue = currentValue;
 15     this.oType = "Delete Action";
 16 }
 17 
 18 DeleteCommand.prototype = {
 19     /**This method got called every time the Command must execute*/
 20     redo : function(){
 21         this._doAction(this.currentValue);
 22     },
 23     
 24     
 25     /**This method should be called every time the Command should be undone*/
 26     undo : function(){
 27         this._doAction(this.previousValue);
 28     },
 29     
 30     _doAction:function(value){
 31         if(this.typeOfObject == History.OBJECT_FIGURE){
 32             if(stack.figureGetById(this.objectId)){//we are re-deleting
 33                 selectedFigureId = this.objectId;
 34                 state = STATE_FIGURE_SELECTED;
 35                 value.noAddUndo = true;
 36                 value.KEY = KEY.DELETE;
 37                 onKeyDown(value);
 38                 selectedFigureId = -1;
 39             }
 40             else{
 41                 stack.figureAdd(this.previousValue);
 42                 selectedFigureId = this.previousValue.figureId;
 43             }
 44         }
 45         else if(this.typeOfObject == History.OBJECT_CONNECTOR){
 46             if(value instanceof Connector){//we are undoing
 47                 state = STATE_CONNECTOR_PICK_FIRST;
 48                 connectorType = this.property;
 49 
 50                 //create the connector
 51                 connectorPickFirst(value.turningPoints[0].x,value.turningPoints[0].y,null);
 52                 var con = CONNECTOR_MANAGER.connectorGetById(selectedConnectorId);
 53                 con.type = value.type;
 54                 state = STATE_CONNECTOR_PICK_SECOND;
 55                 connectorPickSecond(value.turningPoints[value.turningPoints.length - 1].x,value.turningPoints[value.turningPoints.length - 1].y,null);
 56 
 57                 con.id = this.objectId;
 58                 var conPoints = CONNECTOR_MANAGER.connectionPointGetAllByParent(selectedConnectorId);
 59 
 60                 //re-id the connection points
 61                 for(var i = 0; i < conPoints.length; i++){
 62                     conPoints[i].parentId = this.objectId;
 63                 }
 64 
 65                 //
 66                 for(var i = 0; i < History.COMMANDS.length; i++){
 67                     if(History.COMMANDS[i].typeOfObject == History.OBJECT_CONNECTION_POINT){
 68                         if(History.COMMANDS[i].property.equals(con.turningPoints[0])){
 69                             var cps = CONNECTOR_MANAGER.connectionPointGetAllByParent(this.objectId);
 70                             History.COMMANDS[i].objectId = cps[0].id;
 71                         }
 72                         else if(History.COMMANDS[i].property.equals(con.turningPoints[con.turningPoints.length - 1])){
 73                             var cps = CONNECTOR_MANAGER.connectionPointGetAllByParent(this.objectId);
 74                             History.COMMANDS[i].objectId = cps[cps.length - 1].id;
 75                         }
 76                     }
 77                 }
 78                 state = STATE_CONNECTOR_SELECTED;
 79                 selectedConnectorId = this.objectId;
 80             }
 81             else
 82             {
 83                 CONNECTOR_MANAGER.connectionPointRemoveAllByParent(this.objectId)
 84                 CONNECTOR_MANAGER.connectorRemoveById(this.objectId);
 85                 selectedConnectorId = -1;
 86                 state = STATE_NONE;
 87             }
 88         }
 89         else if(this.typeOfObject == History.OBJECT_GROUP){
 90             if(value instanceof Array){
 91                 state = STATE_GROUP_SELECTED;
 92                 var figureIds = [];
 93                 for(var i = 0; i < value.length; i++){
 94                     stack.figureAdd(value[i]);
 95                     figureIds.push(value[i].id);
 96                 }
 97                 this.objectId = stack.groupCreate(figureIds);
 98                 stack.groupGetById(this.objectId).permanent = this.currentValue;
 99                 selectedGroupId = this.objectId;
100                 state = STATE_GROUP_SELECTED;
101             }
102             else{
103                 var figures = stack.figureGetByGroupId(this.objectId);
104                 stack.groupDestroy(this.objectId);
105                 for(var i = 0; i < figures.length; i++){
106                     stack.figureRemoveById(figures[i].id);
107                 }
108                 selectedGroupId = -1;
109                 state = STATE_NONE;
110             }
111         }
112     }
113 }
114 
115