Beispiel #1
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);
        }