Example #1
0
        private void ExecutePattern(TreeNode node)
        {
            TreeNodeCommand nodeCmd = node as TreeNodeCommand;

            if (nodeCmd == null)
            {
                return; // Nothing to do if the node is not a leaf
            }
            nodeCmd.Command.Do();
        }
Example #2
0
        private void FormMain_Load(object sender, EventArgs e)
        {
            /*** Init Components ***/

            // Initialize each pattern with their details
            m_gof.LoadFiles(PatternCategory.Creational, c_sCreationalPath);
            m_gof.LoadFiles(PatternCategory.Structural, c_sStructuralPath);
            m_gof.LoadFiles(PatternCategory.Behavioral, c_sBehavioralPath);

            // Create two root node
            // 1) Sort pattern by category
            // 2) Sort pattern by frequency level
            m_gofByCategoryNode = new TreeNode(m_gof.Name);
            m_gofByLevelNode    = new TreeNode(m_gof.Name);

            // Create nodes for each category
            TreeNode creationalNode = new TreeNode("Creational Patterns");
            TreeNode structuralNode = new TreeNode("Structural Patterns");
            TreeNode behavioralNode = new TreeNode("Behavioral Patterns");

            // Add each category to the appropriate root
            m_gofByCategoryNode.Nodes.Add(creationalNode);
            m_gofByCategoryNode.Nodes.Add(structuralNode);
            m_gofByCategoryNode.Nodes.Add(behavioralNode);

            // Create nodes for each frequency level
            // Add each fequency level to the appropriate root
            // Load frequency images
            TreeNode node;
            string   sImageFormat = Path.Combine(c_sImagePath, c_sImageFormat);

            m_frequencyLevelImages.Add(Image.FromFile(string.Format(sImageFormat, 0)));
            foreach (int i in Enumerable.Range(1, GangOfFour.FrequencyLevelCount))
            {
                node = new TreeNode("Level " + i);
                m_gofByLevelNode.Nodes.Insert(0, node); // Order by level desc

                m_frequencyLevelImages.Add(Image.FromFile(string.Format(sImageFormat, i)));
            }

            // Create nodes for each pattern
            TreeNodeCommand nodeCmd;

            foreach (Pattern pattern in m_gof.Items)
            {
                // Create a node for a pattern
                nodeCmd = new TreeNodeCommand(pattern.Name);

                // Set the concrete commands to the invoker (see: command design pattern)
                nodeCmd.Command = new CommandPattern(nodeCmd.Text, m_gof);

                // Dispatch it to the appropriate category
                switch (pattern.Category)
                {
                case PatternCategory.Creational:
                    creationalNode.Nodes.Add(nodeCmd);
                    break;

                case PatternCategory.Structural:
                    structuralNode.Nodes.Add(nodeCmd);
                    break;

                case PatternCategory.Behavioral:
                    behavioralNode.Nodes.Add(nodeCmd);
                    break;

                default:
                    break;
                }

                // Do the same for the level frequency
                // A tree node does not support to be added at different places
                // Then, it has to be cloned
                nodeCmd = (TreeNodeCommand)nodeCmd.Clone();
                node    = m_gofByLevelNode.Nodes[GangOfFour.FrequencyLevelCount - pattern.FrequencyLevel];
                node.Nodes.Add(nodeCmd);
            }

            /*** Init UI ***/

            // Select the category sort at first
            m_treeView.Nodes.Add(m_gofByCategoryNode);
            m_treeView.ExpandAll();

            // Select the node during a right click event
            // (By default a node is also selected with a left click event)
            m_treeView.NodeMouseClick += (s, args) => m_treeView.SelectedNode = args.Node;
        }