Ejemplo n.º 1
0
        public void Initialize([NotNull] IThreatModel model)
        {
            var schemaManager = new ModelConfigPropertySchemaManager(model);
            var schema        = schemaManager.GetSchema();

            var horizontalPT = schema.GetPropertyType("Diagram Layout Horizontal Spacing");
            var horizontal   = model.GetProperty(horizontalPT);

            if (horizontal == null)
            {
                model.AddProperty(horizontalPT, "200");
            }
            else
            {
                if (string.IsNullOrWhiteSpace(horizontal.StringValue))
                {
                    horizontal.StringValue = "200";
                }
            }

            var verticalPT = schema.GetPropertyType("Diagram Layout Vertical Spacing");
            var vertical   = model.GetProperty(verticalPT);

            if (vertical == null)
            {
                model.AddProperty(verticalPT, "100");
            }
            else
            {
                if (string.IsNullOrWhiteSpace(vertical.StringValue))
                {
                    vertical.StringValue = "100";
                }
            }
        }
        public void ExecuteCustomAction([NotNull] IActionDefinition action)
        {
            var schemaManager = new ModelConfigPropertySchemaManager(_diagram.Model);
            var schema        = schemaManager.GetSchema();
            var hpt           = schema.GetPropertyType("Diagram Layout Horizontal Spacing");
            var vpt           = schema.GetPropertyType("Diagram Layout Vertical Spacing");
            var h             = _diagram.Model.GetProperty(hpt) as IPropertyInteger;
            var v             = _diagram.Model.GetProperty(vpt) as IPropertyInteger;
            var hFloat        = (float)(h?.Value ?? 150f);
            var vFloat        = (float)(h?.Value ?? 100f);

            switch (action.Name)
            {
            case "CreateExtInteractor":
                var interactor = _diagram.Model?.AddEntity <IExternalInteractor>();
                var p1         = GetFreePoint(new PointF(hFloat, vFloat), new SizeF(100, 100), hFloat, vFloat);
                AddShape(_diagram.AddShape(interactor, p1));
                CheckRefresh();
                break;

            case "CreateProcess":
                var process = _diagram.Model?.AddEntity <IProcess>();
                var p2      = GetFreePoint(new PointF(hFloat, vFloat), new SizeF(100, 100), hFloat, vFloat);
                AddShape(_diagram.AddShape(process, p2));
                CheckRefresh();
                break;

            case "CreateDataStore":
                var dataStore = _diagram.Model?.AddEntity <IDataStore>();
                var p3        = GetFreePoint(new PointF(hFloat, vFloat), new SizeF(100, 100), hFloat, vFloat);
                AddShape(_diagram.AddShape(dataStore, p3));
                CheckRefresh();
                break;

            case "CreateTrustBoundary":
                var trustBoundary = _diagram.Model?.AddGroup <ITrustBoundary>();
                var p4            = GetFreePoint(new PointF(350, 200), new SizeF(600, 300), hFloat, 200 + vFloat);
                AddShape(_diagram.AddShape(trustBoundary, p4, new SizeF(600, 300)));
                CheckRefresh();
                break;

            case "CreateThreatType":
                using (var dialog = new ThreatTypeCreationDialog(_diagram.Model))
                {
                    dialog.ShowDialog(Form.ActiveForm);
                }
                break;

            case "RemoveDiagram":
                if (MessageBox.Show(Form.ActiveForm,
                                    "Are you sure you want to remove the current Diagram from the Model?",
                                    "Delete Diagram", MessageBoxButtons.YesNo, MessageBoxIcon.Warning,
                                    MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                {
                    if (_factory.Delete(this))
                    {
                        ShowMessage("Diagram removed successfully.");
                    }
                    else
                    {
                        ShowWarning?.Invoke("Diagram removal has failed.");
                    }
                }
                break;

            case "FixDiagram":
                var fixDiagram = new FixDiagram();
                if (fixDiagram.ShowDialog(Form.ActiveForm) == DialogResult.OK)
                {
                    switch (fixDiagram.Issue)
                    {
                    case DiagramIssue.TooSmall:
                        HandleTooSmall();
                        break;

                    case DiagramIssue.TooLarge:
                        HandleTooLarge();
                        break;
                    }
                }
                break;

            case "AlignH":
                _graph.AlignHorizontally();
                break;

            case "AlignV":
                _graph.AlignVertically();
                break;

            case "AlignT":
                _graph.AlignTops();
                break;

            case "AlignB":
                _graph.AlignBottoms();
                break;

            case "AlignL":
                _graph.AlignLeftSides();
                break;

            case "AlignR":
                _graph.AlignRightSides();
                break;

            case "Layout":
                if (MessageBox.Show("Are you sure you want to automatically layout the Diagram?",
                                    "Automatic Layout confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Warning,
                                    MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                {
                    _graph.DoLayout(h?.Value ?? 200, v?.Value ?? 100);
                }
                break;

            case "MarkerToggle":
                switch (MarkerStatusTrigger.CurrentStatus)
                {
                case MarkerStatus.Full:
                    MarkerStatusTrigger.RaiseMarkerStatusUpdated(MarkerStatus.Hidden);
                    break;

                case MarkerStatus.Hidden:
                    MarkerStatusTrigger.RaiseMarkerStatusUpdated(MarkerStatus.Full);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
                break;

            case "ZoomIn":
                _graph.ZoomIn();
                break;

            case "ZoomOut":
                _graph.ZoomOut();
                break;

            case "ZoomNormal":
                _graph.ZoomNormal();
                break;

            case "ZoomFit":
                _graph.ZoomToFit();
                break;

            case "Clipboard":
                _loading = true;
                _graph.ToClipboard();
                _loading = false;
                break;

            case "File":
                var dialog2 = new SaveFileDialog();
                dialog2.CreatePrompt                 = false;
                dialog2.OverwritePrompt              = true;
                dialog2.AddExtension                 = true;
                dialog2.AutoUpgradeEnabled           = true;
                dialog2.CheckPathExists              = true;
                dialog2.DefaultExt                   = "png";
                dialog2.Filter                       = "PNG (*.png)|*.png|JPEG (*.jpg)|*.jpg|GIF (*.gif)|Bitmap (*.bmp)";
                dialog2.SupportMultiDottedExtensions = true;
                dialog2.Title            = "Export Diagram as Image";
                dialog2.RestoreDirectory = true;
                if (dialog2.ShowDialog(Form.ActiveForm) == DialogResult.OK)
                {
                    _loading = true;
                    _graph.ToFile(dialog2.FileName);
                    _loading = false;
                }
                break;

            default:
                if (action.Tag is IShapesContextAwareAction shapesAction)
                {
                    var           selection = _graph.Selection.ToArray();
                    List <IShape> shapes    = new List <IShape>();
                    List <ILink>  links     = new List <ILink>();
                    foreach (var shape in selection)
                    {
                        RecursivelyAddShapes(shapes, links, shape);
                    }

                    if (shapes.Any())
                    {
                        shapesAction.Execute(shapes, links);
                    }
                }
                break;
            }
        }