Пример #1
0
        public override void OnDragDrop(DiagramDragEventArgs diagramDragEventArgs)
        {
            base.OnDragDrop(diagramDragEventArgs);

            if (IsDropping)
            {
                string[] missingFiles = null;

                if (diagramDragEventArgs.Data.GetData("Text") is string filename)
                {
                    if (!File.Exists(filename))
                    {
                        missingFiles = new[] { filename }
                    }
                    ;
                    else
                    {
                        FileDropHelper.HandleDrop(Store, filename);
                    }
                }
                else if (diagramDragEventArgs.Data.GetData("FileDrop") is string[] filenames)
                {
                    string[] existingFiles = filenames.Where(File.Exists).ToArray();
                    FileDropHelper.HandleMultiDrop(Store, existingFiles);
                    missingFiles = filenames.Except(existingFiles).ToArray();
                }
                else
                {
                    base.OnDragDrop(diagramDragEventArgs);
                }

                if (missingFiles != null && missingFiles.Any())
                {
                    if (missingFiles.Length > 1)
                    {
                        missingFiles[missingFiles.Length - 1] = "and " + missingFiles[missingFiles.Length - 1];
                    }
                    ErrorDisplay.Show($"Can't find files {string.Join(", ", missingFiles)}");
                }
            }

            IsDropping = false;
        }
Пример #2
0
        public override void OnDragDrop(DiagramDragEventArgs diagramDragEventArgs)
        {
            // came from model explorer?
            ModelElement element = (diagramDragEventArgs.Data.GetData("Sawczyn.EFDesigner.EFModel.ModelClass") as ModelElement)
                                   ?? (diagramDragEventArgs.Data.GetData("Sawczyn.EFDesigner.EFModel.ModelEnum") as ModelElement);

            if (element != null)
            {
                ShapeElement newShape = AddExistingModelElement(this, element);

                if (newShape != null)
                {
                    using (Transaction t = element.Store.TransactionManager.BeginTransaction("Moving pasted shapes"))
                    {
                        if (newShape is NodeShape nodeShape)
                        {
                            nodeShape.Location = diagramDragEventArgs.MousePosition;
                        }

                        t.Commit();
                    }
                }
            }
            else
            {
                if (IsDroppingExternal)
                {
                    DisableDiagramRules();
                    Cursor prev = Cursor.Current;
                    Cursor.Current = Cursors.WaitCursor;

                    List <ModelElement> newElements = null;
                    try
                    {
                        try
                        {
                            // add to the model
                            string[] filenames;

                            if (diagramDragEventArgs.Data.GetData("Text") is string concatenatedFilenames)
                            {
                                filenames = concatenatedFilenames.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                            }
                            else if (diagramDragEventArgs.Data.GetData("FileDrop") is string[] droppedFilenames)
                            {
                                filenames = droppedFilenames;
                            }
                            else
                            {
                                ErrorDisplay.Show(Store, "Unexpected error dropping files. Please create an issue in Github.");
                                return;
                            }

                            string[] existingFiles = filenames.Where(File.Exists).ToArray();
                            newElements = FileDropHelper.HandleMultiDrop(Store, existingFiles).ToList();

                            string[] missingFiles = filenames.Except(existingFiles).ToArray();

                            if (missingFiles.Any())
                            {
                                if (missingFiles.Length > 1)
                                {
                                    missingFiles[missingFiles.Length - 1] = "and " + missingFiles[missingFiles.Length - 1];
                                }

                                ErrorDisplay.Show(Store, $"Can't find files {string.Join(", ", missingFiles)}");
                            }
                        }
                        finally
                        {
                            // TODO: how to add shapes to the current diagram without taking forever? The trick is disabling line routing, but how?
                            #region Come back to this later

                            //if (newElements != null)
                            //{
                            // add to the active diagram
                            //int elementCount = newElements.Count;
                            //List<ShapeElement> newShapes = new List<ShapeElement>();

                            //using (Transaction t = Store.TransactionManager.BeginTransaction("adding diagram elements"))
                            //{
                            //   for (int index = 0; index < elementCount; index++)
                            //   {
                            //      ModelElement newElement = newElements[index];
                            //      StatusDisplay.Show($"Adding element {index + 1} of {elementCount}");

                            //      ForceAddShape = true;
                            //      FixUpAllDiagrams.FixUp(this, newElement);
                            //      newShapes.Add(newElement.GetFirstShapeElement());
                            //      ForceAddShape = false;
                            //   }

                            //   t.Commit();
                            //}

                            //using (Transaction t = Store.TransactionManager.BeginTransaction("adding diagram links"))
                            //{
                            //   for (int index = 0; index < elementCount; index++)
                            //   {
                            //      ModelElement newElement = newElements[index];
                            //      StatusDisplay.Show($"Linking {index + 1} of {elementCount}");

                            //      // find all element links that are attached to our element where the ends are in the diagram but the link isn't already in the diagram
                            //      List<ElementLink> elementLinks = Store.GetAll<ElementLink>()
                            //                                            .Where(link => link.LinkedElements.Contains(newElement)
                            //                                                        && link.LinkedElements.All(linkedElement => DisplayedElements.Contains(linkedElement))
                            //                                                        && !DisplayedElements.Contains(link))
                            //                                            .ToList();

                            //      foreach (ElementLink elementLink in elementLinks)
                            //      {
                            //         BinaryLinkShape linkShape = CreateChildShape(elementLink) as BinaryLinkShape;
                            //         newShapes.Add(linkShape);
                            //         NestedChildShapes.Add(linkShape);

                            //         switch (elementLink)
                            //         {
                            //            case Association a:
                            //               linkShape.FromShape = a.Source.GetFirstShapeElement() as NodeShape;
                            //               linkShape.ToShape = a.Target.GetFirstShapeElement() as NodeShape;
                            //               break;
                            //            case Generalization g:
                            //               linkShape.FromShape = g.Subclass.GetFirstShapeElement() as NodeShape;
                            //               linkShape.ToShape = g.Superclass.GetFirstShapeElement() as NodeShape;
                            //               break;
                            //         }
                            //      }
                            //   }

                            //   AutoLayoutShapeElements(newShapes);
                            //   t.Commit();
                            //}
                            //}
                            #endregion

                            IsDroppingExternal = false;
                        }
                    }
                    finally
                    {
                        EnableDiagramRules();
                        Cursor.Current = prev;

                        MessageDisplay.Show(newElements == null || !newElements.Any()
                                         ? "Import dropped files: no new elements added"
                                         : BuildMessage(newElements));

                        StatusDisplay.Show("");
                    }
                }
                else
                {
                    try
                    {
                        base.OnDragDrop(diagramDragEventArgs);
                    }
                    catch (ArgumentException)
                    {
                        // ignore. byproduct of multiple diagrams
                    }
                }
            }

            StatusDisplay.Show("");
            Invalidate();

            string BuildMessage(List <ModelElement> newElements)
            {
                int           classCount    = newElements.OfType <ModelClass>().Count();
                int           propertyCount = newElements.OfType <ModelClass>().SelectMany(c => c.Attributes).Count();
                int           enumCount     = newElements.OfType <ModelEnum>().Count();
                List <string> messageParts  = new List <string>();

                if (classCount > 0)
                {
                    messageParts.Add($"{classCount} classes");
                }

                if (propertyCount > 0)
                {
                    messageParts.Add($"{propertyCount} properties");
                }

                if (enumCount > 0)
                {
                    messageParts.Add($"{enumCount} enums");
                }

                return($"Import dropped files: added {(messageParts.Count > 1 ? string.Join(", ", messageParts.Take(messageParts.Count - 1)) + " and " + messageParts.Last() : messageParts.First())}");
            }
        }
Пример #3
0
        public override void OnDragDrop(DiagramDragEventArgs diagramDragEventArgs)
        {
            // came from model explorer?
            ModelElement element = (diagramDragEventArgs.Data.GetData("Sawczyn.EFDesigner.EFModel.ModelClass") as ModelElement)
                                   ?? (diagramDragEventArgs.Data.GetData("Sawczyn.EFDesigner.EFModel.ModelEnum") as ModelElement);

            if (element != null)
            {
                ShapeElement newShape = AddExistingModelElement(this, element);

                if (newShape != null)
                {
                    using (Transaction t = element.Store.TransactionManager.BeginTransaction("Moving pasted shapes"))
                    {
                        if (newShape is NodeShape nodeShape)
                        {
                            nodeShape.Location = diagramDragEventArgs.MousePosition;
                        }

                        t.Commit();
                    }
                }
            }
            else
            {
                if (IsDroppingExternal)
                {
                    DisableDiagramRules();
                    Cursor prev = Cursor.Current;
                    Cursor.Current = Cursors.WaitCursor;

                    List <ModelElement> newElements = null;

                    try
                    {
                        try
                        {
                            // add to the model
                            string[] filenames;

                            if (diagramDragEventArgs.Data.GetData("Text") is string concatenatedFilenames)
                            {
                                filenames = concatenatedFilenames.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                            }
                            else if (diagramDragEventArgs.Data.GetData("FileDrop") is string[] droppedFilenames)
                            {
                                filenames = droppedFilenames;
                            }
                            else
                            {
                                ErrorDisplay.Show(Store, "Unexpected error dropping files. Please create an issue in Github.");

                                return;
                            }

                            string[] existingFiles = filenames.Where(File.Exists).ToArray();
                            newElements = FileDropHelper.HandleMultiDrop(Store, existingFiles).ToList();

                            string[] missingFiles = filenames.Except(existingFiles).ToArray();

                            if (missingFiles.Any())
                            {
                                if (missingFiles.Length > 1)
                                {
                                    missingFiles[missingFiles.Length - 1] = "and " + missingFiles[missingFiles.Length - 1];
                                }

                                ErrorDisplay.Show(Store, $"Can't find files {string.Join(", ", missingFiles)}");
                            }
                        }
                        finally
                        {
                            if (newElements?.Count > 0)
                            {
                                string message = $"Created {newElements.Count} new elements that have been added to the Model Explorer. "
                                                 + $"Do you want these added to the current diagram as well? It could take a while.";

                                if (BooleanQuestionDisplay.Show(Store, message) == true)
                                {
                                    AddElementsToActiveDiagram(newElements);
                                }
                            }

                            //string message = $"{newElements.Count} have been added to the Model Explorer. You can add them to this or any other diagram by dragging them from the Model Explorer and dropping them onto the design surface.";
                            //MessageDisplay.Show(message);

                            IsDroppingExternal = false;
                        }
                    }
                    finally
                    {
                        EnableDiagramRules();
                        Cursor.Current = prev;

                        MessageDisplay.Show(newElements == null || !newElements.Any()
                                         ? "Import dropped files: no new elements added"
                                         : BuildMessage(newElements));

                        StatusDisplay.Show("Ready");
                    }
                }
                else
                {
                    try
                    {
                        base.OnDragDrop(diagramDragEventArgs);
                    }
                    catch (ArgumentException)
                    {
                        // ignore. byproduct of multiple diagrams
                    }
                }
            }

            StatusDisplay.Show("Ready");
            Invalidate();

            string BuildMessage(List <ModelElement> newElements)
            {
                int           classCount    = newElements.OfType <ModelClass>().Count();
                int           propertyCount = newElements.OfType <ModelClass>().SelectMany(c => c.Attributes).Count();
                int           enumCount     = newElements.OfType <ModelEnum>().Count();
                List <string> messageParts  = new List <string>();

                if (classCount > 0)
                {
                    messageParts.Add($"{classCount} classes");
                }

                if (propertyCount > 0)
                {
                    messageParts.Add($"{propertyCount} properties");
                }

                if (enumCount > 0)
                {
                    messageParts.Add($"{enumCount} enums");
                }

                return($"Import dropped files: added {(messageParts.Count > 1 ? string.Join(", ", messageParts.Take(messageParts.Count - 1)) + " and " + messageParts.Last() : messageParts.First())}");
            }
        }