Exemple #1
0
        /// <summary>
        /// Populate the NodeTreeView with the attack map, starting from the root
        /// </summary>
        public void refreshList()
        {
            nodeListView.Items.Clear();
            //NodeTreeItem item = new NodeTreeItem(root);
            NodeTreeItem item = root.treeItem;

            item.Header = "Root";
            addChildTreeItems(item);
            nodeListView.Items.Add(item);
        }
Exemple #2
0
 public NodeAssociation(Node node, NodeTreeItem treeItem)
 {
     this.node     = node;
     this.treeItem = treeItem;
     this.treeItem.setAssociation(this);
     this.childAssociations = new Dictionary <Direction, NodeAssociation>();
     this.childAssociations.Add(Direction.Up, null);
     this.childAssociations.Add(Direction.Right, null);
     this.childAssociations.Add(Direction.Down, null);
     this.childAssociations.Add(Direction.Left, null);
 }
Exemple #3
0
        /// <summary>
        /// Handle clicking on the main UI buttons that represent nodes
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void NodeButton_Click(object sender, MouseButtonEventArgs e)
        {
            Canvas     canvas = (Canvas)sender;
            MainWindow main   = (MainWindow)Application.Current.MainWindow;

            //Reset all selections
            foreach (KeyValuePair <Direction, NodeButton> nodeButton in nodeButtons)
            {
                nodeButton.Value.select = false;
            }

            //If it's a single click, just select the item
            if (e.ClickCount == 1)
            {
                foreach (KeyValuePair <Direction, NodeButton> nodeButton in nodeButtons)
                {
                    if (nodeButton.Value.canvas == canvas)
                    {
                        nodeButton.Value.select = true;
                        main.loadNodeDetails(nodeButton.Value.association.node);
                    }
                }
            }
            //Otherwise, for double clicks, navigate to that node
            else if (e.ClickCount == 2)
            {
                NodeTreeItem item = (NodeTreeItem)nodeListView.SelectedItem;
                if (item != null)
                {
                    foreach (KeyValuePair <Direction, NodeButton> nodeButton in nodeButtons)
                    {
                        if (nodeButton.Value.canvas == canvas)
                        {
                            if (!item.HasItems)
                            {
                                continue;
                            }
                            //Found the clicked canvas' button
                            foreach (NodeTreeItem i in item.Items)
                            {
                                if (i.association.node.direction == nodeButton.Key)
                                {
                                    //Found the associated node item in the tree, select it
                                    //triggering the OnSelected for the NodeTreeItem event
                                    i.IsSelected = true;
                                    return;
                                }
                            }
                        }
                    }
                }
            }
        }
Exemple #4
0
        private void Load()
        {
            openFileStreams(false);

            string          fileString      = attackMapReader.ReadToEnd();
            Node            rootNode        = JsonConvert.DeserializeObject <Node>(fileString);
            NodeTreeItem    rootItem        = new NodeTreeItem();
            NodeAssociation rootAssociation = new NodeAssociation(rootNode, rootItem);

            root = rootAssociation;

            closeFileStreams();
            refreshList();
            NodeTreeItem rootListItem = (NodeTreeItem)nodeListView.Items[0];

            rootListItem.IsSelected = true; //This should trigger a refresh
        }
        protected override void OnSelected(RoutedEventArgs e)
        {
            //base.OnSelected(e);

            MainWindow main = (MainWindow)System.Windows.Application.Current.MainWindow;

            NodeTreeItem item = (NodeTreeItem)e.Source;

            item.IsExpanded               = true;
            main.centerButton.select      = true;
            main.centerButton.association = item.association;

            main.updateNodeButtons(item.association);

            main.loadNodeDetails(item.association.node);

            item.Focus(); //Make sure it's visible
        }
Exemple #6
0
 /// <summary>
 /// Recursively add all child NodeTreeItems to the propvided NodeTreeItem
 /// </summary>
 /// <param name="item"></param>
 private void addChildTreeItems(NodeTreeItem item)
 {
     foreach (Direction dir in (Direction[])Enum.GetValues(typeof(Direction)))
     {
         //Ignore "Middle" as it should only ever be used in this context for the root, which should never be a child of anything
         if (dir == Direction.Middle)
         {
             continue;
         }
         NodeAssociation childNode = item.association.childAssociations[dir];
         if (childNode != null && childNode.node != null)
         {
             //NodeTreeItem childItem = new NodeTreeItem(childNode);
             addChildTreeItems(childNode.treeItem);
             //addChildTreeItems(childItem);
             //childItem.parent = item;
             item.Items.Add(childNode.treeItem);
         }
     }
 }
Exemple #7
0
        public MainWindow()
        {
            InitializeComponent();

            //Find items and pre-fill if applicable
            #region Find UI Elements
            nodeListView   = (TreeView)this.FindName("NodeTreeView");
            propertiesGrid = (Grid)this.FindName("PropertiesGrid");

            classificationBox = (ComboBox)propertiesGrid.FindName("ClassificationBox");
            foreach (Mode mode in (Mode[])Enum.GetValues(typeof(Mode)))
            {
                classificationBox.Items.Add(mode.ToString());
            }

            topButton    = new NodeButton((Canvas)this.FindName("TopButton"));
            rightButton  = new NodeButton((Canvas)this.FindName("RightButton"));
            bottomButton = new NodeButton((Canvas)this.FindName("BottomButton"));
            leftButton   = new NodeButton((Canvas)this.FindName("LeftButton"));
            centerButton = new NodeButton((Canvas)this.FindName("CenterButton"));
            upButton     = (Button)this.FindName("UpButton");

            nodeButtons = new Dictionary <Direction, NodeButton>();
            nodeButtons[Direction.Up]     = topButton;
            nodeButtons[Direction.Right]  = rightButton;
            nodeButtons[Direction.Down]   = bottomButton;
            nodeButtons[Direction.Left]   = leftButton;
            nodeButtons[Direction.Middle] = centerButton;

            foreach (KeyValuePair <Direction, NodeButton> nodeButton in nodeButtons)
            {
                Classification[] classificationArr = new Classification[] { Classification.getNormal(), Classification.getIntermediary(), Classification.getFinal() };
                foreach (Classification temp in classificationArr)
                {
                    if (temp.shape == Shape.Circle)
                    {
                        nodeButton.Value.circle.Fill = new SolidColorBrush(temp.color);
                    }
                    if (temp.shape == Shape.Square)
                    {
                        nodeButton.Value.square.Fill = new SolidColorBrush(temp.color);
                    }
                    if (temp.shape == Shape.Diamond)
                    {
                        nodeButton.Value.diamond.Fill = new SolidColorBrush(temp.color);
                    }
                }

                nodeButton.Value.Visibility = Visibility.Hidden;
                nodeButton.Value.select     = false;

                nodeButton.Value.canvas.MouseLeftButtonDown += new MouseButtonEventHandler(NodeButton_Click);
            }
            #endregion

            upButton.Click += (s, e) => { NavigateUp(); };

            Load();
            if (root == null)
            {
                Node         rootNode = new Node(Classification.getNormal(), Direction.Middle);
                NodeTreeItem rootItem = new NodeTreeItem();
                root = new NodeAssociation(rootNode, rootItem);
            }

            propertiesGrid.IsEnabled = false;

            refreshList();
            NodeTreeItem rootListItem = (NodeTreeItem)nodeListView.Items[0];
            rootListItem.IsSelected = true; //This should trigger a refresh
        }
        public NodeTreeItem()
        {
            this.Header = "ERROR: NOT YET INITIALIZED";
            //this.Header = node.direction.ToString() + ": " + node.classification.mode.ToString();

            ContextMenu cm = new ContextMenu();

            this.ContextMenu = cm;
            MenuItem cmi = new MenuItem();

            cmi.Header = "Add child action";
            cmi.Click += (s, e) =>
            {
                //Prepare list of all direction to be filtered for possible options to be created
                List <Direction> directions = new List <Direction>();
                directions.AddRange((Direction[])Enum.GetValues(typeof(Direction)));

                //Get the NodeTreeItem (and thus the NodeAssociation) to find what options are available to be added
                MenuItem     menuItem = s as MenuItem;
                NodeTreeItem toAddTo  = (NodeTreeItem)(((ContextMenu)menuItem.Parent).PlacementTarget);

                //Filter out existing directions
                foreach (NodeTreeItem child in toAddTo.Items)
                {
                    directions.Remove(child.association.node.direction);
                }
                //Remove "Middle", as it should only ever be used in this context for the root node
                directions.Remove(Direction.Middle);

                //Get all modes a node can be
                List <Mode> modes = new List <Mode>();
                modes.AddRange((Mode[])Enum.GetValues(typeof(Mode)));

                //Display add dialog
                AddDialog dialog = new AddDialog(directions, modes);
                dialog.ShowDialog(); //ShowDialog will prevent interacting with the main window until the dialog is clsed
                if (dialog.save)
                {
                    //Get data back for use
                    Direction dir;
                    bool      success = Enum.TryParse(dialog.directionBox.SelectedItem.ToString(), out dir);
                    if (!success)
                    {
                        MessageBox.Show($"Could not parse direction {dialog.directionBox.SelectedItem.ToString()}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                    Mode mode;
                    success = Enum.TryParse(dialog.classifcationBox.SelectedItem.ToString(), out mode);
                    if (!success)
                    {
                        MessageBox.Show($"Could not parse mode {dialog.classifcationBox.SelectedItem.ToString()}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                    Classification classification = Classification.GetClassificationByMode(mode);

                    Node         newNode = new Node(classification, dir);
                    NodeTreeItem newItem = new NodeTreeItem();
                    newItem.parent = this;
                    NodeAssociation newAssociation = new NodeAssociation(newNode, newItem);
                    toAddTo.association.addChild(dir, newAssociation);

                    newItem.BringIntoView();                                //Show element in tree without selecting it
                    toAddTo.OnSelected(new RoutedEventArgs(null, toAddTo)); //Create a fake click event on the parent so that the details (NodeButtons and the like) update
                }
            };
            cm.Items.Add(cmi);
        }