public void Drop(IDropInfo dropInfo)
        {
            if (dropInfo == null || dropInfo.DragInfo == null)
            {
                return;
            }
            int insertIndex = dropInfo.InsertIndex != dropInfo.UnfilteredInsertIndex
                ? dropInfo.UnfilteredInsertIndex : dropInfo.InsertIndex;
            var destinationList          = dropInfo.TargetCollection.TryGetList();
            AssignationModel data        = dropInfo.Data as AssignationModel;
            AssignationModel currentItem = dropInfo.VisualTargetItem != null ?
                                           ((FrameworkElement)dropInfo.VisualTargetItem)
                                           .DataContext as AssignationModel : null;

            // If the sourcve and destination are in the same control, delete at the current index
            if (Equals(dropInfo.DragInfo.VisualSource, dropInfo.VisualTarget))
            {
                var sourceList = dropInfo.DragInfo.SourceCollection.TryGetList();
                if (currentItem == null || currentItem.CanHaveChild)
                {
                    var index = sourceList.IndexOf(data);
                    if (index != -1)
                    {
                        sourceList.RemoveAt(index);
                        if (Equals(sourceList, destinationList) && index < insertIndex)
                        {
                            --insertIndex;
                        }
                    }
                }
            }
            TreeView treeViewItem = dropInfo.VisualTarget as TreeView;

            // Add data at the new index
            if (currentItem == null || currentItem.CanHaveChild)
            {
                var obj2Insert = data;
                if (!Equals(dropInfo.DragInfo.VisualSource, dropInfo.VisualTarget))
                {
                    var cloneable = data as ICloneable;
                    if (cloneable != null)
                    {
                        obj2Insert = (AssignationModel)cloneable.Clone();
                        obj2Insert.DeleteNodeCommand = new RelayCommand <object>(DeleteNode);
                    }
                }
                destinationList.Insert(insertIndex++, obj2Insert);
            }
        }
        public void TestAssignationModel()
        {
            //Arrange
            AssignationModel model = new AssignationModel();
            AssignationModel child = new AssignationModel();

            //Act
            model.Childrens.Add(child);
            AssignationModel clone = model.Clone() as AssignationModel;

            //Assert
            Assert.IsNotNull(clone);
            Assert.IsTrue(model.Childrens.Any());
            Assert.IsTrue(clone.Childrens.Any());
            Assert.IsTrue(clone.IsCloned);
        }
 public void DeleteInChilds(AssignationModel model, AssignationModel sender)
 {
     //If there are childs in the model
     if (model.Childrens.Any())
     {
         //Loop in all the childs
         foreach (AssignationModel child in model.Childrens.ToList())
         {
             //If it is the same as the searched one
             if (child == sender)
             {
                 //Remove
                 model.Childrens.Remove(child);
             }
             else
             {
                 //loop inside
                 DeleteInChilds(child, sender);
             }
         }
     }
 }
Esempio n. 4
0
        public void DeleteNode(object sender)
        {
            if (!(sender is AssignationModel))
            {
                return;
            }
            AssignationModel model = (AssignationModel)sender;

            foreach (AssignationModel child in CodeBlocks.ToList())
            {
                //If it is the same as the searched one
                if (child == sender)
                {
                    //Remove
                    CodeBlocks.Remove(child);
                }
                else
                {
                    //loop inside
                    DeleteInChilds(child, model);
                }
            }
        }
        private static AssignationModel BlockToModel(ICodeStruct currentBlock, RelayCommand <object> deleteCommand)
        {
            //Should never loop inside something other than an assignation, check type
            Assignation assignation = currentBlock as Assignation;

            if (assignation != null)
            {
                Assignation block = assignation;
                //Check if simply empty, consider an empty block
                if (block.Value == null)
                {
                    //Simple assignation, return corresponding model
                    return(new AssignationModel
                    {
                        BackgroundColor = BLOCKS_COLORS[0],
                        BorderColor = BLOCKS_COLORS[1],
                        Color = BLOCKS_COLORS[2],
                        IsNotEditable = false,
                        CanHaveChild = true,
                        Code = Regex.Replace(block.Assignee, @"\t|\n|\r|\s", "") + " "
                               + block.Operator + " {}",
                        LocalizationKey = getAssignationName(block.Assignee),
                        DeleteNodeCommand = deleteCommand
                    });
                }
                //check if a code value
                CodeValue value = block.Value as CodeValue;
                if (value != null)
                {
                    //Simple assignation, return corresponding model
                    return(new AssignationModel()
                    {
                        BackgroundColor = ASSIGNATIONS_COLORS[0],
                        BorderColor = ASSIGNATIONS_COLORS[1],
                        Color = ASSIGNATIONS_COLORS[2],
                        IsNotEditable = false,
                        CanHaveChild = false,
                        Code = Regex.Replace(block.Assignee, @"\t|\n|\r|\s", "") + " " + block.Operator
                               + " " + Regex.Replace(value.Value, @"\t|\n|\r|\s", ""),
                        LocalizationKey = "Scripter_Assignation_Custom",
                        DeleteNodeCommand = deleteCommand
                    });
                    //Ignore childs even if there is some
                }
                //Otherwise, the child is a codeblock
                CodeBlock codeBlock = block.Value as CodeBlock;
                if (codeBlock != null)
                {
                    AssignationModel newAssignation = new AssignationModel()
                    {
                        BackgroundColor = getColorArray(block.Assignee)[0],
                        BorderColor     = getColorArray(block.Assignee)[1],
                        Color           = getColorArray(block.Assignee)[2],
                        IsNotEditable   = false,
                        CanHaveChild    = true,
                        Code            = Regex.Replace(block.Assignee, @"\t|\n|\r|\s", "") +
                                          " " + block.Operator + " {}",
                        LocalizationKey   = getAssignationName(block.Assignee),
                        DeleteNodeCommand = deleteCommand
                    };
                    foreach (ICodeStruct code in codeBlock.Code)
                    {
                        newAssignation.Childrens.Add(BlockToModel(code, deleteCommand));
                    }
                    return(newAssignation);
                }
                //Error, return nothing.
                return(null);
            }
            //If the current block is a code Value (Corrsponding to block = { Value }
            if (currentBlock is CodeValue)
            {
                //Print the value
                return(new AssignationModel()
                {
                    BackgroundColor = ASSIGNATIONS_COLORS[0],
                    BorderColor = ASSIGNATIONS_COLORS[1],
                    Color = ASSIGNATIONS_COLORS[2],
                    IsNotEditable = false,
                    CanHaveChild = false,
                    Code = Regex.Replace(currentBlock.Parse(), @"\t|\n|\r|\s", ""),
                    LocalizationKey = "Scripter_Assignation_Custom",
                    DeleteNodeCommand = deleteCommand
                });
            }
            return(null);
        }
 /// <summary>
 /// Recursive method that loop inside the given model childrens an create the required block
 /// </summary>
 /// <param name="model">The model to loop into</param>
 /// <param name="level">The current level of the script</param>
 /// <returns>a well formed ICodeStruct made from the model's data.</returns>
 private static ICodeStruct ModelToBlock(AssignationModel model, int level = 0)
 {
     if (model.Childrens.Any())
     {
         Assignation newBlock;
         //If contains equals
         if (model.Code.Contains("="))
         {
             //Empty code block, create as such
             newBlock = new Assignation(level)
             {
                 Assignee = model.Code.Split('=')[0].Trim(),
                 Operator = "=",
                 Value    = new CodeBlock(level + 1)
             };
         }
         //If contains lesser than, but also does not contains a block
         else if (model.Code.Contains("<") && !model.Code.Contains("{") &&
                  !model.Code.Contains("}"))
         {
             //If yes, assignation
             return(new Assignation(level)
             {
                 Assignee = model.Code.Split('<')[0].Trim(),
                 Operator = "<",
                 Value = new CodeValue(model.Code.Split('<')[1].Trim())
             });
         }
         //If contains bigger than, but also does not contains a block
         else if (model.Code.Contains(">") && !model.Code.Contains("{") &&
                  !model.Code.Contains("}"))
         {
             //If yes, assignation
             return(new Assignation(level)
             {
                 Assignee = model.Code.Split('>')[0].Trim(),
                 Operator = ">",
                 Value = new CodeValue(model.Code.Split('>')[1].Trim())
             });
         }
         else
         {
             //Weird block, just return its code
             newBlock = new Assignation(level)
             {
                 Assignee = model.Code,
                 Operator = "=",
                 Value    = new CodeBlock(level + 1)
             };
         }
         foreach (AssignationModel child in model.Childrens)
         {
             ((CodeBlock)newBlock.Value).Code.Add(ModelToBlock(child, level + 1));
         }
         return(newBlock);
     }
     //Should be a code value, since there is no children
     //Check if it contains an equal, but no code block
     if (model.Code.Contains("=") && !model.Code.Contains("{") && !model.Code.Contains("}"))
     {
         //If yes, assignation
         return(new Assignation(level)
         {
             Assignee = model.Code.Split('=')[0].Trim(),
             Operator = "=",
             Value = new CodeValue(model.Code.Split('=')[1].Trim())
         });
     }
     //If contains lesser than, but also does not contains a block
     if (model.Code.Contains("<") && !model.Code.Contains("{") && !model.Code.Contains("}"))
     {
         //If yes, assignation
         return(new Assignation(level)
         {
             Assignee = model.Code.Split('<')[0].Trim(),
             Operator = "<",
             Value = new CodeValue(model.Code.Split('<')[1].Trim())
         });
     }
     //If contains bigger than, but also does not contains a block
     if (model.Code.Contains(">") && !model.Code.Contains("{") && !model.Code.Contains("}"))
     {
         //If yes, assignation
         return(new Assignation(level)
         {
             Assignee = model.Code.Split('>')[0].Trim(),
             Operator = ">",
             Value = new CodeValue(model.Code.Split('>')[1].Trim())
         });
     }
     //If contains equals, but also contains a block
     if (model.Code.Contains("="))
     {
         //Empty code block, create as such
         return(new Assignation(level)
         {
             Assignee = model.Code.Split('=')[0].Trim(),
             Operator = "="
         });
     }
     //Code value in block
     return(new CodeValue(model.Code));
 }
 private static AssignationModel BlockToModel(ICodeStruct currentBlock, RelayCommand <object> deleteCommand)
 {
     //Should never loop inside something other than an assignation, check type
     if (currentBlock is Assignation)
     {
         Assignation block = currentBlock as Assignation;
         //Check if simply empty, consider an empty block
         if (block.Value == null)
         {
             ResourceDictionary resourceLocalization = new ResourceDictionary();
             resourceLocalization.Source = new Uri(Configurator.getLanguageFile(), UriKind.Relative);
             //Simple assignation, return corresponding model
             return(new AssignationModel()
             {
                 BackgroundColor = BLOCKS_COLORS[0],
                 BorderColor = BLOCKS_COLORS[1],
                 Color = BLOCKS_COLORS[2],
                 IsNotEditable = false,
                 CanHaveChild = true,
                 Code = Regex.Replace(block.Assignee, @"\t|\n|\r|\s", "") + " " + block.Operator + " {}",
                 Text = getAssignationName(block.Assignee),
                 DeleteNodeCommand = deleteCommand
             });
         }
         //check if a code value
         if (block.Value is CodeValue)
         {
             ResourceDictionary resourceLocalization = new ResourceDictionary();
             resourceLocalization.Source = new Uri(Configurator.getLanguageFile(), UriKind.Relative);
             //Simple assignation, return corresponding model
             return(new AssignationModel()
             {
                 BackgroundColor = ASSIGNATIONS_COLORS[0],
                 BorderColor = ASSIGNATIONS_COLORS[1],
                 Color = ASSIGNATIONS_COLORS[2],
                 IsNotEditable = false,
                 CanHaveChild = false,
                 Code = Regex.Replace(block.Assignee, @"\t|\n|\r|\s", "") + " " + block.Operator
                        + " " + Regex.Replace(((CodeValue)block.Value).Value, @"\t|\n|\r|\s", ""),
                 Text = resourceLocalization["Scripter_Assignation_Custom"] as string,
                 DeleteNodeCommand = deleteCommand
             });
             //Ignore childs even if there is some
         }
         //Otherwise, the child is a codeblock
         else if (block.Value is CodeBlock)
         {
             AssignationModel newAssignation = new AssignationModel()
             {
                 BackgroundColor = getColorArray(block.Assignee)[0],
                 BorderColor     = getColorArray(block.Assignee)[1],
                 Color           = getColorArray(block.Assignee)[2],
                 IsNotEditable   = false,
                 CanHaveChild    = true,
                 Code            = Regex.Replace(block.Assignee, @"\t|\n|\r|\s", "") +
                                   " " + block.Operator + " {}",
                 Text = getAssignationName(block.Assignee),
                 DeleteNodeCommand = deleteCommand
             };
             foreach (ICodeStruct code in ((CodeBlock)block.Value).Code)
             {
                 newAssignation.Childrens.Add(BlockToModel(code, deleteCommand));
             }
             return(newAssignation);
         }
         else
         {
             //An error in the code, log it
             RecursiveCodeException e = new RecursiveCodeException();
             throw e.AddToRecursiveChain("Invalid assignation", block.Assignee, block.Line.ToString());
         }
     }
     return(null);
 }