Exemple #1
0
        private void _objects_SelectedIndexChanged(object sender, EventArgs e)
        {
            _annotationLayoutControlItem.Visible = false;
            ChangeCustomActionStatus?.Invoke("RemoveReviewNote", false);
            RemoveButtons();

            if (_objects.SelectedItems.Count == 1 && _objects.SelectedItems[0].Tag is IPropertiesContainer container)
            {
                _selected        = container;
                _properties.Item = _selected;
                ChangeCustomActionStatus?.Invoke("AddReviewNote", true);

                _right.SuspendLayout();
                var reviewNotes = _schemaManager.GetAnnotations(container)?
                                  .Where(x => x is ReviewNote).ToArray();
                if (reviewNotes?.Any() ?? false)
                {
                    foreach (var reviewNote in reviewNotes)
                    {
                        AddButton(reviewNote);
                    }
                }

                _right.ResumeLayout();
            }
            else
            {
                _selected        = null;
                _properties.Item = null;
                ChangeCustomActionStatus?.Invoke("AddReviewNote", false);
            }
        }
Exemple #2
0
        private void AnnotationButtonClick(object sender, EventArgs e)
        {
            if (sender is Button button && button.Tag is Annotation annotation)
            {
                if (_currentButton != null)
                {
                    _currentButton.FlatAppearance.BorderColor = Color.White;
                }
                _currentButton = button;
                button.FlatAppearance.BorderColor = ThreatsManager.Utilities.ThreatModelManager.StandardColor;
                button.FlatAppearance.BorderSize  = 2;

                _annotation.SetObject(_model, _selected);
                _annotation.Annotation = annotation;
                _annotationLayoutControlItem.Visible = true;

                if (annotation is ReviewNote)
                {
                    ChangeCustomActionStatus?.Invoke("RemoveReviewNote", true);
                }
                else
                {
                    ChangeCustomActionStatus?.Invoke("RemoveReviewNote", false);
                }
            }
        }
Exemple #3
0
 private void ShowCurrentRow()
 {
     if (_promoted)
     {
         ChangeCustomActionStatus?.Invoke("RemoveSeverity", _currentRow.Tag is ISeverity);
     }
 }
Exemple #4
0
 private void _grid_CellActivated(object sender, GridCellActivatedEventArgs e)
 {
     if (!_loading)
     {
         _currentRow = e.NewActiveCell.GridRow;
         ChangeCustomActionStatus?.Invoke("RemoveQuestion", true);
     }
 }
Exemple #5
0
        private void ShowCurrentRow()
        {
            _properties.Item = _currentRow?.Tag;

            ChangeCustomActionStatus?.Invoke("RemoveThreatType", _currentRow.Tag is IThreatType);
            ChangeCustomActionStatus?.Invoke("AddMitigation", _currentRow.Tag is IThreatType);
            ChangeCustomActionStatus?.Invoke("RemoveMitigation", _currentRow.Tag is IThreatTypeMitigation);
        }
        private void ShowCurrentRow()
        {
            _properties.Item = _currentRow?.Tag;

            ChangeCustomActionStatus?.Invoke("RemoveWeakness", _currentRow.Tag is IWeakness);
            ChangeCustomActionStatus?.Invoke("AddMitigation", _currentRow.Tag is IWeakness);
            ChangeCustomActionStatus?.Invoke("RemoveMitigation", _currentRow.Tag is IWeaknessMitigation);
        }
Exemple #7
0
        private void ShowCurrentRow()
        {
            _properties.Item = _currentRow?.Tag;

            bool isMitigation  = _currentRow?.Tag is IThreatEventMitigation;
            bool isThreatEvent = _currentRow?.Tag is IThreatEvent;

            ChangeCustomActionStatus?.Invoke("AddMitigation", isThreatEvent);
            ChangeCustomActionStatus?.Invoke("RemoveMitigation", isMitigation);
        }
Exemple #8
0
 private void ShowCurrentRow()
 {
     if (_currentRow?.Tag is IItemTemplate template)
     {
         _properties.Item = template;
         ChangeCustomActionStatus?.Invoke("RemoveItemTemplates", true);
     }
     else
     {
         ChangeCustomActionStatus?.Invoke("RemoveItemTemplates", false);
     }
 }
 private void ShowCurrentRow()
 {
     if (_currentRow?.Tag is IDataFlow dataFlow)
     {
         _properties.Item = dataFlow;
         ChangeCustomActionStatus?.Invoke("RemoveDataFlow", true);
         ChangeCustomActionStatus?.Invoke("FindDataFlow", true);
     }
     else
     {
         ChangeCustomActionStatus?.Invoke("RemoveDataFlow", false);
         ChangeCustomActionStatus?.Invoke("FindDataFlow", false);
     }
 }
Exemple #10
0
 private void ModelChildRemoved(IIdentity identity)
 {
     if (identity is IExternalInteractor externalInteractor)
     {
         var row = GetRow(externalInteractor);
         if (row != null)
         {
             RemoveEventSubscriptions(row);
             _grid.PrimaryGrid.Rows.Remove(row);
             ChangeCustomActionStatus?.Invoke("RemoveExternal", false);
             ChangeCustomActionStatus?.Invoke("FindExternal", false);
         }
     }
 }
Exemple #11
0
 private void ShowCurrentRow()
 {
     if (_currentRow?.Tag is IExternalInteractor entity)
     {
         _properties.Item = entity;
         ChangeCustomActionStatus?.Invoke("RemoveExternal", true);
         ChangeCustomActionStatus?.Invoke("FindExternal", true);
     }
     else
     {
         ChangeCustomActionStatus?.Invoke("RemoveExternal", false);
         ChangeCustomActionStatus?.Invoke("FindExternal", false);
     }
 }
Exemple #12
0
 private void ModelChildRemoved(IIdentity identity)
 {
     if (identity is ITrustBoundary tb)
     {
         var row = GetRow(tb);
         if (row != null)
         {
             RemoveEventSubscriptions(row);
             _grid.PrimaryGrid.Rows.Remove(row);
             ChangeCustomActionStatus?.Invoke("RemoveTrustBoundary", false);
             ChangeCustomActionStatus?.Invoke("FindTrustBoundary", false);
         }
     }
 }
 private void ModelChildRemoved(IIdentity identity)
 {
     if (identity is IDataStore dataStore)
     {
         var row = GetRow(dataStore);
         if (row != null)
         {
             RemoveEventSubscriptions(row);
             _grid.PrimaryGrid.Rows.Remove(row);
             ChangeCustomActionStatus?.Invoke("RemoveDataStore", false);
             ChangeCustomActionStatus?.Invoke("FindDataStore", false);
         }
     }
 }
 private void ShowCurrentRow()
 {
     if (_currentRow?.Tag is IDataStore entity)
     {
         _properties.Item = entity;
         ChangeCustomActionStatus?.Invoke("RemoveDataStore", true);
         ChangeCustomActionStatus?.Invoke("FindDataStore", true);
     }
     else
     {
         ChangeCustomActionStatus?.Invoke("RemoveDataStore", false);
         ChangeCustomActionStatus?.Invoke("FindDataStore", false);
     }
 }
Exemple #15
0
 private void ShowCurrentRow()
 {
     if (_currentRow?.Tag is ITrustBoundary trustBoundary)
     {
         _properties.Item = trustBoundary;
         ChangeCustomActionStatus?.Invoke("RemoveTrustBoundary", true);
         ChangeCustomActionStatus?.Invoke("FindTrustBoundary", true);
     }
     else
     {
         ChangeCustomActionStatus?.Invoke("RemoveTrustBoundary", false);
         ChangeCustomActionStatus?.Invoke("FindTrustBoundary", false);
     }
 }
 private void ModelChildRemoved(IIdentity identity)
 {
     if (identity is IProcess process)
     {
         var row = GetRow(process);
         if (row != null)
         {
             RemoveEventSubscriptions(row);
             _grid.PrimaryGrid.Rows.Remove(row);
             ChangeCustomActionStatus?.Invoke("RemoveProcess", false);
             ChangeCustomActionStatus?.Invoke("FindProcess", false);
         }
     }
 }
Exemple #17
0
        private void OnObjectSelected(bool selected = true)
        {
            ChangeCustomActionStatus?.Invoke("AlignH", selected);
            ChangeCustomActionStatus?.Invoke("AlignV", selected);
            ChangeCustomActionStatus?.Invoke("AlignT", selected);
            ChangeCustomActionStatus?.Invoke("AlignB", selected);
            ChangeCustomActionStatus?.Invoke("AlignL", selected);
            ChangeCustomActionStatus?.Invoke("AlignR", selected);

            if (_actions != null)
            {
                foreach (var action in _actions)
                {
                    if (action.Scope.HasFlag(Scope.Entity) ||
                        action.Scope.HasFlag(Scope.Group) ||
                        action.Scope.HasFlag(Scope.DataFlow))
                    {
                        ChangeCustomActionStatus?.Invoke(action.Label, selected);
                    }
                }
            }
        }
 private void ShowCurrentRow()
 {
     ChangeCustomActionStatus?.Invoke("RemoveStrength", _currentRow.Tag is IStrength);
 }
Exemple #19
0
        public void ExecuteCustomAction([NotNull] IActionDefinition action)
        {
            string text    = null;
            bool   warning = false;

            try
            {
                switch (action.Name)
                {
                case "AddSeverity":
                    using (var dialog = new SeverityCreationDialog(_model))
                    {
                        if (dialog.ShowDialog(Form.ActiveForm) == DialogResult.OK)
                        {
                            text = "Add Severity";
                        }
                    }
                    break;

                case "RemoveSeverity":
                    var selected = _grid.GetSelectedCells()?.OfType <GridCell>()
                                   .Select(x => x.GridRow)
                                   .Distinct()
                                   .ToArray();

                    if (_currentRow != null)
                    {
                        if ((selected?.Length ?? 0) > 1)
                        {
                            var outcome = MessageBox.Show(Form.ActiveForm,
                                                          $"You have selected {selected.Length} Severities. Do you want to remove them all?\nPlease click 'Yes' to remove all selected Severities,\nNo to remove only the last one you selected, '{_currentRow.Tag?.ToString()}'.\nPress Cancel to abort.",
                                                          "Remove Severities", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning,
                                                          MessageBoxDefaultButton.Button3);
                            switch (outcome)
                            {
                            case DialogResult.Yes:
                                bool removed = true;
                                foreach (var row in selected)
                                {
                                    bool r = false;
                                    if (row.Tag is ISeverity severity)
                                    {
                                        r = _model.RemoveSeverity(severity.Id);
                                    }

                                    removed &= r;

                                    if (r && row == _currentRow)
                                    {
                                        _currentRow = null;
                                    }
                                }

                                if (removed)
                                {
                                    text = "Remove Severities";
                                }
                                else
                                {
                                    warning = true;
                                    text    = "One or more Severities cannot be removed.";
                                }

                                break;

                            case DialogResult.No:
                                if (_currentRow?.Tag is ISeverity severity2)
                                {
                                    if (_model.RemoveSeverity(severity2.Id))
                                    {
                                        _currentRow = null;
                                        text        = "Remove Severity";
                                    }
                                    else
                                    {
                                        warning = true;
                                        text    = "The Severity cannot be removed.";
                                    }
                                }

                                break;
                            }
                        }
                        else if (_currentRow?.Tag is ISeverity severity &&
                                 MessageBox.Show(Form.ActiveForm,
                                                 $"You are about to remove Severity '{severity.Name}'. Are you sure?",
                                                 "Remove Severity", MessageBoxButtons.YesNo, MessageBoxIcon.Warning,
                                                 MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                        {
                            if (_model.RemoveSeverity(severity.Id))
                            {
                                text = "Remove Severity";
                            }
                            else
                            {
                                warning = true;
                                text    = "The Severity cannot be removed.";
                            }
                        }
                    }
                    break;

                case "Restore":
                    var initializer = new SeveritiesInitializer();
                    initializer.Initialize(_model);
                    text = "Severities restore";
                    break;

                case "Promote":
                    if (MessageBox.Show(Form.ActiveForm, Properties.Resources.MessagePromoteSeverities,
                                        "ATTENTION", MessageBoxButtons.YesNo, MessageBoxIcon.Warning,
                                        MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                    {
                        _promoted = true;
                        _grid.PrimaryGrid.Columns["Id"].AllowEdit = true;
                        ChangeCustomActionStatus?.Invoke("AddSeverity", true);
                        ChangeCustomActionStatus?.Invoke("RemoveSeverity", _currentRow?.Tag is ISeverity);
                        ChangeCustomActionStatus?.Invoke("Restore", true);
                    }
                    break;

                case "Refresh":
                    LoadModel();
                    break;
                }

                if (warning)
                {
                    ShowWarning?.Invoke(text);
                }
                else if (text != null)
                {
                    ShowMessage?.Invoke($"{text} has been executed successfully.");
                }
            }
            catch
            {
                ShowWarning?.Invoke($"An error occurred during the execution of the action.");
                throw;
            }
        }
Exemple #20
0
        public void ExecuteCustomAction([NotNull] IActionDefinition action)
        {
            var schemaManager = new AnnotationsPropertySchemaManager(_model);

            _right.SuspendLayout();

            switch (action.Name)
            {
            case "AddNotes":
                if (_selected != null)
                {
                    var notes = new Annotation();
                    schemaManager.AddAnnotation(_selected, notes);
                    AddButton(notes);
                }
                break;

            case "AddTopic":
                if (_selected != null)
                {
                    var topic = new TopicToBeClarified();
                    schemaManager.AddAnnotation(_selected, topic);
                    AddButton(topic);
                }
                break;

            case "AddHighlight":
                if (_selected != null)
                {
                    var high = new Highlight();
                    schemaManager.AddAnnotation(_selected, high);
                    AddButton(high);
                }
                break;

            case "RemoveNotes":
                if (_selected != null && _annotation.Annotation is Annotation annotation &&
                    MessageBox.Show("You are about to remove the currently selected Note. Are you sure?",
                                    "Remove Notes", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning,
                                    MessageBoxDefaultButton.Button2) == DialogResult.OK)
                {
                    schemaManager.RemoveAnnotation(_selected, annotation);
                    _annotation.Annotation = null;
                    RemoveButton(annotation);
                }
                break;

            case "RemoveTopic":
                if (_selected != null && _annotation.Annotation is TopicToBeClarified toBeClarified &&
                    MessageBox.Show("You are about to remove the currently selected Topic. Are you sure?",
                                    "Remove Topic", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning,
                                    MessageBoxDefaultButton.Button2) == DialogResult.OK)
                {
                    schemaManager.RemoveAnnotation(_selected, toBeClarified);
                    _annotation.Annotation = null;
                    RemoveButton(toBeClarified);
                }
                break;

            case "RemoveHighlight":
                if (_selected != null && _annotation.Annotation is Highlight highlight &&
                    MessageBox.Show("You are about to remove the currently selected Highlight. Are you sure?",
                                    "Remove Highlight", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning,
                                    MessageBoxDefaultButton.Button2) == DialogResult.OK)
                {
                    schemaManager.RemoveAnnotation(_selected, highlight);
                    _annotation.Annotation = null;
                    RemoveButton(highlight);
                }
                break;

            case "ShowOpenTopics":
                _show = WhatToShow.OpenTopicsOnly;
                LoadModel();
                ChangeCustomActionStatus?.Invoke("ShowOpenTopics", false);
                ChangeCustomActionStatus?.Invoke("ShowHighlights", true);
                ChangeCustomActionStatus?.Invoke("ShowReviewNotes", true);
                ChangeCustomActionStatus?.Invoke("ShowAll", true);
                break;

            case "ShowHighlights":
                _show = WhatToShow.HighlightsOnly;
                LoadModel();
                ChangeCustomActionStatus?.Invoke("ShowOpenTopics", true);
                ChangeCustomActionStatus?.Invoke("ShowHighlights", false);
                ChangeCustomActionStatus?.Invoke("ShowReviewNotes", true);
                ChangeCustomActionStatus?.Invoke("ShowAll", true);
                break;

            case "ShowAll":
                _show = WhatToShow.All;
                LoadModel();
                ChangeCustomActionStatus?.Invoke("ShowOpenTopics", true);
                ChangeCustomActionStatus?.Invoke("ShowHighlights", true);
                ChangeCustomActionStatus?.Invoke("ShowReviewNotes", true);
                ChangeCustomActionStatus?.Invoke("ShowAll", false);
                break;

            case "ExportOpen":
                var saveFileDialog = new SaveFileDialog()
                {
                    AddExtension       = true,
                    AutoUpgradeEnabled = true,
                    CheckFileExists    = false,
                    CheckPathExists    = true,
                    RestoreDirectory   = true,
                    DefaultExt         = "csv",
                    Filter             = "CSV file (*.csv)|*.csv|Excel file (*.xlsx)|*.xlsx",
                    Title         = "Create a file with open Topics",
                    ValidateNames = true
                };
                if (saveFileDialog.ShowDialog(Form.ActiveForm) == DialogResult.OK)
                {
                    ExportCsv(saveFileDialog.FileName, true);
                }
                break;

            case "ExportAll":
                var saveFileDialog2 = new SaveFileDialog()
                {
                    AddExtension       = true,
                    AutoUpgradeEnabled = true,
                    CheckFileExists    = false,
                    CheckPathExists    = true,
                    RestoreDirectory   = true,
                    DefaultExt         = "csv",
                    Filter             = "CSV file (*.csv)|*.csv|Excel file (*.xlsx)|*.xlsx",
                    Title         = "Create a file with all Topics",
                    ValidateNames = true
                };
                if (saveFileDialog2.ShowDialog(Form.ActiveForm) == DialogResult.OK)
                {
                    ExportCsv(saveFileDialog2.FileName, false);
                }
                break;

            case "Refresh":
                LoadModel();
                break;
            }

            _right.ResumeLayout();
        }
Exemple #21
0
 private void ShowCurrentRow()
 {
     _properties.Item = _currentRow?.Tag;
     ChangeCustomActionStatus?.Invoke("RemoveActor", _currentRow?.Tag is IThreatActor);
 }
Exemple #22
0
        private void _objects_SelectedIndexChanged(object sender, EventArgs e)
        {
            _annotationLayoutControlItem.Visible = false;
            ChangeCustomActionStatus?.Invoke("RemoveNotes", false);
            ChangeCustomActionStatus?.Invoke("RemoveTopic", false);
            ChangeCustomActionStatus?.Invoke("RemoveHighlight", false);
            RemoveButtons();

            if (_objects.SelectedItems.Count == 1 && _objects.SelectedItems[0].Tag is IPropertiesContainer container)
            {
                _selected        = container;
                _properties.Item = _selected;
                ChangeCustomActionStatus?.Invoke("AddTopic", true);
                ChangeCustomActionStatus?.Invoke("AddHighlight", true);

                _right.SuspendLayout();
                var annotations = _schemaManager.GetAnnotations(container)?.ToArray();

                var notes = annotations?
                            .Where(x => !(x is TopicToBeClarified || x is Highlight || x is ReviewNote)).ToArray();
                if (notes?.Any() ?? false)
                {
                    foreach (var note in notes)
                    {
                        AddButton(note);
                    }
                    ChangeCustomActionStatus?.Invoke("AddNotes", false);
                }
                else
                {
                    ChangeCustomActionStatus?.Invoke("AddNotes", true);
                }

                var topicsToBeClarified = annotations?.Where(x => x is TopicToBeClarified).ToArray();
                if (topicsToBeClarified?.Any() ?? false)
                {
                    foreach (var topic in topicsToBeClarified)
                    {
                        AddButton(topic);
                    }
                }

                var highlights = annotations?.Where(x => x is Highlight).ToArray();
                if (highlights?.Any() ?? false)
                {
                    foreach (var highlight in highlights)
                    {
                        AddButton(highlight);
                    }
                }

                var reviewNotes = annotations?.Where(x => x is ReviewNote).ToArray();
                if (reviewNotes?.Any() ?? false)
                {
                    foreach (var reviewNote in reviewNotes)
                    {
                        AddButton(reviewNote);
                    }
                }

                _right.ResumeLayout();
            }
            else
            {
                _selected        = null;
                _properties.Item = null;
                ChangeCustomActionStatus?.Invoke("AddNotes", false);
                ChangeCustomActionStatus?.Invoke("AddTopic", false);
                ChangeCustomActionStatus?.Invoke("AddHighlight", false);
            }
        }
Exemple #23
0
        private void _objectTypes_SelectedValueChanged(object sender, EventArgs e)
        {
            if (_objectTypes.SelectedItem is string selected)
            {
                _objects.Items.Clear();
                _imageList.Images.Clear();
                _properties.Item = null;

                ChangeCustomActionStatus?.Invoke("AddNotes", false);
                ChangeCustomActionStatus?.Invoke("AddTopic", false);
                ChangeCustomActionStatus?.Invoke("AddHighlight", false);
                ChangeCustomActionStatus?.Invoke("RemoveNotes", false);
                ChangeCustomActionStatus?.Invoke("RemoveTopic", false);
                ChangeCustomActionStatus?.Invoke("RemoveHighlight", false);

                RemoveButtons();
                _annotation.Annotation = null;

                switch (selected)
                {
                case "External Interactors":
                    AddObjects(_model.GetExternalInteractors(_schemaManager, _propertyType, _filter.Text), true);
                    break;

                case "Processes":
                    AddObjects(_model.GetProcesses(_schemaManager, _propertyType, _filter.Text), true);
                    break;

                case "Data Stores":
                    AddObjects(_model.GetDataStores(_schemaManager, _propertyType, _filter.Text), true);
                    break;

                case "Flows":
                    AddObjects(_model.GetFlows(_schemaManager, _propertyType, _filter.Text));
                    break;

                case "Trust Boundaries":
                    AddObjects(_model.GetTrustBoundaries(_schemaManager, _propertyType, _filter.Text));
                    break;

                case "Threat Events":
                    AddObjects(_model.GetThreatEvents(_schemaManager, _propertyType, _filter.Text));
                    break;

                case "Threat Event Mitigations":
                    var tems = _model.GetThreatEventMitigations(_schemaManager, _propertyType, _filter.Text)?
                               .Where(x => x is IPropertiesContainer container &&
                                      ((_show == WhatToShow.All && (_schemaManager.HasNotes(container) || _schemaManager.HasTopics(container) || _schemaManager.HasHighlights(container))) ||
                                       (_show == WhatToShow.OpenTopicsOnly && _schemaManager.HasOpenTopics(container)) ||
                                       (_show == WhatToShow.HighlightsOnly && _schemaManager.HasHighlights(container))))
                               .ToArray();
                    if (tems?.Any() ?? false)
                    {
                        bool first = true;

                        foreach (var tem in tems)
                        {
                            if (first)
                            {
                                _imageList.Images.Add(Icons.Resources.mitigations_small);
                                first = false;
                            }

                            _objects.Items.Add(new ListViewItem(tem.Mitigation.Name, 0)
                            {
                                ToolTipText = $"'{tem.Mitigation.Name}' for '{tem.ThreatEvent.Name}' on '{tem.ThreatEvent.Parent.Name}'",
                                Tag         = tem
                            });
                        }
                    }
                    break;

                case "Threat Types":
                    AddObjects(_model.GetThreatTypes(_schemaManager, _propertyType, _filter.Text));
                    break;

                case "Known Mitigations":
                    AddObjects(_model.GetKnownMitigations(_schemaManager, _propertyType, _filter.Text));
                    break;

                case "Standard Mitigations":
                    var sms = _model.GetStandardMitigations(_schemaManager, _propertyType, _filter.Text)?
                              .Where(x => x is IPropertiesContainer container &&
                                     ((_show == WhatToShow.All && (_schemaManager.HasNotes(container) || _schemaManager.HasTopics(container) || _schemaManager.HasHighlights(container))) ||
                                      (_show == WhatToShow.OpenTopicsOnly && _schemaManager.HasOpenTopics(container)) ||
                                      (_show == WhatToShow.HighlightsOnly && _schemaManager.HasHighlights(container))))
                              .ToArray();
                    if (sms?.Any() ?? false)
                    {
                        bool first = true;

                        foreach (var sm in sms)
                        {
                            if (first)
                            {
                                _imageList.Images.Add(Icons.Resources.standard_mitigations_small);
                                first = false;
                            }

                            _objects.Items.Add(new ListViewItem(sm.Mitigation.Name, 0)
                            {
                                ToolTipText = $"'{sm.Mitigation.Name}' for '{sm.ThreatType.Name}'",
                                Tag         = sm
                            });
                        }
                    }
                    break;

                case "Entity Templates":
                    AddObjects(_model.GetEntityTemplates(_schemaManager, _propertyType, _filter.Text), true);
                    break;

                case "Flow Templates":
                    AddObjects(_model.GetFlowTemplates(_schemaManager, _propertyType, _filter.Text));
                    break;

                case "Trust Boundary Templates":
                    AddObjects(_model.GetTrustBoundaryTemplates(_schemaManager, _propertyType, _filter.Text));
                    break;

                case "Diagrams":
                    AddObjects(_model.GetDiagrams(_schemaManager, _propertyType, _filter.Text));
                    break;

                case "Threat Model":
                    AddObject(_model);
                    break;
                }

                _objects.AutoResizeColumn(0, ColumnHeaderAutoResizeStyle.ColumnContent);
            }
        }
Exemple #24
0
        public void ExecuteCustomAction([NotNull] IActionDefinition action)
        {
            switch (action.Name)
            {
            case "AddQuestion":
                var question = new Question();
                _schemaManager.AddQuestion(question);
                AddGridRow(question, _grid.PrimaryGrid);
                break;

            case "RemoveQuestion":
                if (_currentRow?.Tag is Question question2)
                {
                    var text = question2.Text?.Trim().TrimEnd('?');
                    if ((text?.Length ?? 0) > 15)
                    {
                        text = $"{text.Substring(0, 15)}...";
                    }
                    if (MessageBox.Show($"Are you sure you want to delete question '{text}'?",
                                        "Remove Question", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.OK)
                    {
                        _schemaManager.RemoveQuestion(question2);
                        _currentRow.Cells[0].PropertyChanged -= OnQuestionCellChanged;
                        _grid.PrimaryGrid.Rows.Remove(_currentRow);
                        _currentRow = null;
                        ChangeCustomActionStatus?.Invoke("RemoveQuestion", false);
                    }
                }
                break;

            case "Save":
                if (_saveFile.ShowDialog(Form.ActiveForm) == DialogResult.OK)
                {
                    if (File.Exists(_saveFile.FileName))
                    {
                        File.Delete(_saveFile.FileName);
                    }

                    using (var file = File.OpenWrite(_saveFile.FileName))
                    {
                        using (var writer = new BinaryWriter(file))
                        {
                            var questions = _schemaManager.GetQuestions();

                            StringBuilder sb = new StringBuilder();
                            StringWriter  sw = new StringWriter(sb);

                            using (JsonWriter jtw = new JsonTextWriter(sw))
                            {
                                var serializer = new JsonSerializer {
                                    TypeNameHandling = TypeNameHandling.All, Formatting = Formatting.Indented
                                };
                                serializer.Serialize(jtw, questions);
                            }

                            var serialization = Encoding.Unicode.GetBytes(sb.ToString());

                            writer.Write((byte)0xFF);
                            writer.Write((byte)0xFE);
                            writer.Write(serialization);
                        }
                    }
                }
                break;

            case "Refresh":
                LoadModel();
                break;
            }
        }